// Copyright 2008 ProjectWizards GmbH. All rights reserved.

//console.log("ActivitiesViewController");

var ViewController  = WRLObject.extend({
	constructor: function(context){
		this.base();
		this.moc = context;
	},
	
	rootProject: function() {
		if(!this._rootProject || this._rootProject.isInvalid) {
			var objects = Object.values(this.moc.registeredObjects);
			for(var i=0; i<objects.length; i++) {
				var object = objects[i];
				if(object.className() == "MEProject" && !object.valueForKey("linkedParentActivity")) {
                    this._rootProject = object;
					break;
				}
			}
		}
		return this._rootProject;
	},
	
	workspaceViewName: function() {
		return "";
	},
	
	getMenuContentForPart: function(part) {
		var rootProject = this.rootProject();
		var result = {enums:{}, order:[], pullDown:true, offsetLeft:-20, minWidth:100};
		var separatorIndex = 0;
		function appendContentForWorkspaces(result, workspaces){
			if(workspaces.length) {
				if (result.order.length) {
					var separatorKey = "//separator-"+(separatorIndex++);
					result.order.push(separatorKey);
					result.enums[separatorKey] = separatorKey;
				}
				workspaces.each(function(workspace){
					var uri = workspace.uri();
					var title = workspace.valueForKey("title")				
					result.order.push(uri);
					result.enums[uri] = title;
				});
			}		
		}
        
		appendContentForWorkspaces(result, MEWorkspace.filteredWorkspacesForSingleViewSinglePart(rootProject.valueForKey("bundledWorkspaces"), this.workspaceViewName(), part));
		appendContentForWorkspaces(result, MEWorkspace.filteredWorkspacesForSingleViewSinglePart(rootProject.valueForKey("globalWorkspaces"), this.workspaceViewName(), part));
		appendContentForWorkspaces(result, MEWorkspace.filteredWorkspacesForSingleViewSinglePart(rootProject.valueForKey("sortedWorkspaces"), this.workspaceViewName(), part));

		return result;
	},

	workspaceSelected: function(element, newValue) {
		if(!newValue.startsWith("separator-")) {
			var workspace = this.moc.registeredObjectWithURIString(newValue);
			if(workspace)
				this.applyWorkspaceMethod({workspaceURI:workspace.uri()});
		}
	},
	
	columnSetMenuContent: function() {
		return this.getMenuContentForPart("columnConfiguration");
	},

	styleSetMenuContent: function() {
		return this.getMenuContentForPart("styleList");
	},
	
    updateColumnSetButton: function() {
		if(this.columnSetButton){
			makePopUpMenuForElement(this.columnSetButton.parentNode, 0, this.columnSetMenuContent(), this.workspaceSelected, null, null, this.columnSetMenuContent, this);	    
        }
    },
    
	setColumnSetSelectionButtons: function(columnSetButton, applyWorkspaceMethod) {
		this.columnSetButton = columnSetButton;
		this.applyWorkspaceMethod = applyWorkspaceMethod;
        this.updateColumnSetButton();
	}
		
});
