/*
 *	Implements functionality for the context sensitive menus that pop up when the user right clicks 
 *  on a node in the ELV. 
 * 
 */
ContextMenu = function() {
	// Define text that will appear on the menus
	this.DISPLAY_INTERACTORS = "Display Interactors";
	this.DISPLAY_COMPONENTS = "Display Particating Molecules";
	this.EXPORT_INTERACTORS = "Export Interactors"
	this.HIDE_INTERACTORS = "Hide Interactors";
	this.OTHER_PATHWAYS = "Other Pathways";
	this.PARTICIPATING_MOLECULES = "Participating Molecules";
	this.GO_TO_PATHWAY = "Go To Pathway";
	this.db = null;
	this.oContextMenu = null;
	this.menuItemClickedEvent = new YAHOO.util.CustomEvent(
			"menuItemClickedEvent", this);
	
	this.init = function(db){
		this.db=db;
	};
	this.destroyMenu = function(){
		if (this.oContextMenu != null) {
			this.oContextMenu
			.clearContent();
			this.oContextMenu
			.destroy();
			this.oContextMenu = null;
		}
	};
	
	this.destroyMenuIfNecessary = function(){
		if (this.oContextMenu != null && this.oContextMenu.element.style.visibility == "hidden"){
			this.destroyMenu();
		}
	};
	
	this.renderMenu = function(contextMenu){
		contextMenu.cfg
		.setProperty(
				"maxheight",
				270);
		contextMenu.initEvents();
		contextMenu
		.render(document
				.getElementById("clipwindow"));

	};
	
	this.createMenu=function(){
		if (this.oContextMenu != null) {
			this.oContextMenu
			.clearContent();
			this.oContextMenu
			.destroy();
		}
	
		this.oContextMenu = new YAHOO.widget.ContextMenu(
			"mycontextmenu",
			{
				trigger : document
						.getElementById("clipwindow"),
				hidedelay : 750,
				autosubmenudisplay : true
			});
		return this.oContextMenu;
	};
		
	/*
	 * Create a menu for display when a protein node is right clicked.
	 */
	this.createEWASMenu = function(node){
		var contextMenu = this.createMenu();
		this.addOtherPathwaysItem(contextMenu, node);
		if(node.interactors && node.interactors == "hide"){
			contextMenu.addItem({ text: this.HIDE_INTERACTORS,
				id: "displayinteractors",
				onclick: { fn: this.onMenuItemDisplayInteractorsClick,obj: node, scope:this }});
			
		}else{
			contextMenu.addItem({ text: this.DISPLAY_INTERACTORS,
				id: "displayinteractors",
				onclick: { fn: this.onMenuItemDisplayInteractorsClick,obj: node, scope:this }});
		}
		contextMenu.addItem({ text: this.EXPORT_INTERACTORS,
				id: "exportinteractors",
					onclick: { fn:this.onMenuItemExportInteractorsClick,obj: node, scope:this }});

		this.renderMenu(contextMenu);
	};
	
	/*
	 * Create a menu for display when a complex is right clicked.
	 */
	this.createComplexMenu = function(node){
		var contextMenu = this.createMenu();
		this.addOtherPathwaysItem(contextMenu, node);
			
			if(node.expressionDisplayed == undefined || node.expressionDisplayed == 0 ){
				contextMenu
				.addItems( [
						{
							text : this.PARTICIPATING_MOLECULES,
							id : "participatingmolssubmenu"
						} ]);

		var items = contextMenu.getItems();
		var itemIndex=0;
		for(var i=0;i<items.length; i++){
			var item = items[i];
			if(item.id == "participatingmolssubmenu"){
				itemIndex = i;
				item.cfg.setProperty(
						"submenu",
						{
							autosubmenudisplay : true,
							id : "participatingmolslist"
						});
				}
		}
		var diagrampaneparticipatingmolscallback = new DiagramPaneCtxMenuCallback(
				contextMenu,
				itemIndex, "participatingmols",
				node);
		diagrampaneparticipatingmolscallback.menuItemClickedEvent
				.subscribe(
						this.onCtxMenuItemClick
								.bind(this),
						this);
		var idstring = "complexidlist=" + node.userObject.id;
		YAHOO.util.Connect.asyncRequest('POST',
				"/ReactomeGWT/entrypoint/elv/ComplexComponentsServlet",
				diagrampaneparticipatingmolscallback, idstring
						+ "&class=EntityWithAccessionedSequence,SimpleEntity");
			}
		if((node.expressionDisplayed && node.expressionDisplayed == 1) || (node.displayComparison && node.displayComparison == 1)){
			contextMenu.addItem({ text: this.DISPLAY_COMPONENTS,
				id: "displaycomponents",
				onclick: { fn: this.onMenuItemDisplayComponentsClick,obj: node, scope:this }});
		}
		
		
		
		this.renderMenu(contextMenu);
	};

	/*
	 * Create a menu for display when a SimpleEntity is right clicked
	 */
	this.createSimpleEntityMenu = function(node){
		var contextMenu = this.createMenu();
		this.addOtherPathwaysItem(contextMenu, node);
		this.renderMenu(contextMenu);
	};
	
	/*
	 * Create a menu for display when a Pathway node is right clicked.
	 */
	this.createPathwayMenu = function(node){
		var contextMenu = this.createMenu();
		contextMenu.addItems( [{text : this.GO_TO_PATHWAY, 
	    	  id : "gotopathway",
	    	  onclick: { fn: this.onMenuItemGoToPathwayClick, obj: node, scope:this }} ]);
		this.addOtherPathwaysItem(contextMenu, node);
		this.renderMenu(contextMenu);
	};
	
	/*
	 * Entry point for menu functionality. This is called by PathwayDiagramPane when the user right clicks
	 * a node in the diagram.
	 */
	this.displayMenu = function(node){
		if(node.userObject.cls == "EntityWithAccessionedSequence"){
			this.createEWASMenu(node);
		}else if(node.userObject.cls == "Complex"){
			this.createComplexMenu(node);
		}else if(node.userObject.cls == "SimpleEntity"){
			this.createSimpleEntityMenu(node);
		}else if(node.userObject.cls == "Pathway"){
			this.createPathwayMenu(node);
		}else if(node.userObject.cls == "DefinedSet"){
			this.createDefinedSetMenu(node);
		}else if(node.userObject.cls == "CandidateSet"){
			this.createCandidateSetMenu(node);
		}else if(node.userObject.cls == "Reaction"){
			this.createReactionMenu(node);
		}
		else{
			this.destroyMenu();
		}
	};
	
	this.createReactionMenu = function(node){
		var contextMenu = this.createMenu();
		this.addOtherPathwaysItem(contextMenu, node);
		this.renderMenu(contextMenu);
	};
	
	this.createDefinedSetMenu = function(node){
		var contextMenu = this.createMenu();
		this.addOtherPathwaysItem(contextMenu, node);
		this.renderMenu(contextMenu);
	};

	this.createCandidateSetMenu = function(node){
		var contextMenu = this.createMenu();
		this.addOtherPathwaysItem(contextMenu, node);
		this.renderMenu(contextMenu);
	};

	this.addOtherPathwaysItem = function(contextMenu, node){
		var otheritem = contextMenu.addItems( [
				{
					text : "Retrieving Other Pathways...",
					id : "otherpathwayssubmenu",
					disabled : true
				} ]);
		var items = contextMenu.getItems();
		var itemIndex=0;
		
		for(var i=0;i<items.length; i++){
			var item = items[i];
			if(item.id == "otherpathwayssubmenu"){
				itemIndex = i;
				item.cfg.setProperty(
						"submenu",
						{
							autosubmenudisplay : true,
							id : "otherpathwayslist"
						});
				}
		}
		
		var diagrampanecallback = new DiagramPaneCtxMenuCallback(
				contextMenu,
				itemIndex, "otherpathways",
				node);
		diagrampanecallback.menuItemClickedEvent
				.subscribe(
						this.onCtxMenuItemClick
								.bind(this),
						this);
		YAHOO.util.Connect.asyncRequest('POST',
				"/ReactomeGWT/entrypoint/elv/ELVUtilsServlet",
				diagrampanecallback, "action=otherpathways&dbid=" 
				+ node.userObject.id
				+ "&FOCUS_SPECIES_ID="
				+ pba.pathwayDiagramPane.focus_species_id
				+ "&FOCUS_PATHWAY_ID="
				+ pba.pathwayDiagramPane.focus_pathway_id);
	};
	
	this.onCtxMenuItemClick = function(type, args, me) {
		if(args[1] == "participatingmols"){
			var refdbid = args[0];
			
			window.open("http://www.reactome.org/cgi-bin/eventbrowser?DB="
				+ this.db + "&ID=" + refdbid);		
		}
		else{
			this.menuItemClickedEvent.fire(args[0], args[1]);
		}
	};
	// Does this need a separate event or can it be bundled with otherpathways and part.mols 
	this.onMenuItemGoToPathwayClick = function(eventType, p_aArgs, node){
        this.menuItemClickedEvent.fire(node.userObject.id, node.userObject.id, "gotopathway");
    };
    
    this.onMenuItemDisplayInteractorsClick = function(eventType, p_aArgs, node){
    	if(node.interactors == "hide"){
    		this.menuItemClickedEvent.fire(node,"hideinteractors");
    	}else{
    		this.menuItemClickedEvent.fire(node,"displayinteractors");
    	}
    };

    
    this.onMenuItemDisplayComponentsClick = function(eventType, p_aArgs, node){
   		this.menuItemClickedEvent.fire(node,"displaycomponents");
    };

	this.onMenuItemExportInteractorsClick = function(eventType, p_aArgs, node){
	    // dblistid is the id of the dropdown containing the list of interaction data sources.
		var id = $("dblistid").value;	
		var selectedIndex = $(id).si; // The index for service(interaction database).
									 // Servlet uses this to retrieve the url of the new
									// service.

		window.open("/ReactomeGWT/entrypoint/elv/PSICQUICProxyServlet?action=exi"
				+ "&refid=" +node.userObject.refid
				+ "&si=" + selectedIndex);
	};

};
