/*
 BINViz, Bidirectional Interactive Network Visualization
 Copyright (C) 2007  Benjamin Weyers, University of Michigan, Ann Arbor, MI, U.S.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @fileoverview Provides the PLUGIN class necessary for X
 * @author Benjamin Weyers, University of Duisburg-Essen, University of Michigan
 * @version 0.1
 */

/**
 * Constructor description
 * 
 * @constructor
 *  
 * @class Class description
 * 
 * @author Benjamin Weyers, University of Duisburg-Essen, University of Michigan
 * @version 0.1
 */
function SubgraphPlugin()
{
	this.htmlElement;
	this.graphView;
}

SubgraphPlugin.prototype = Object();

SubgraphPlugin.prototype.init=function(graphView, initData)
{
	this.graphView = graphView;
	this.htmlElement = this.createHtmlElement();
	this.currentSelectedSubGraph = null;
}

SubgraphPlugin.prototype.createHtmlElement=function()
{
	var container = document.createElement("div");

	this.subgraphing = document.createElement("select");
	$(this.subgraphing).css({'widht':'100px','heihgt':'20px','position':'absolute','top':'0px','left':'0px'})
	//var sub = this.subgraphing;
	
	var t = this;
	this.subgraphing.onchange = function(e){
		if ( t.subgraphing.options[t.subgraphing.selectedIndex].text == "No sub graph selected" ) t.resetGraphView();
		else t.ghostNodeEffect( t.subgraphing.options[t.subgraphing.selectedIndex].text );
	}
	
	var opt = document.createElement("option");
	opt.appendChild(document.createTextNode("No sub graph selected"));
	this.subgraphing.appendChild(opt);
	
	this.subgraphButton = document.createElement("button");
	this.subgraphButton.appendChild(document.createTextNode("Create Subgraph"));
	$(this.subgraphButton).css({'widht':'40px','heihgt':'20px','position':'absolute','top':'20px','left':'30px'});
	
	this.subgraphButton.onclick = function(){
		var subid = t.graphView.currentGraph.addSubGraph(t.graphView.currentGraph.getNodeByActivity(true));
		var opt = document.createElement("option");
		opt.appendChild(document.createTextNode(subid));
		t.currentSeletedSubGraph = subid;
		t.subgraphing.appendChild(opt);
		t.ghostNodeEffect( subid );
		t.subgraphing.selectedIndex = t.subgraphing.childNodes.length - 1;
	}
	
	container.style.width = "100px";
	container.appendChild(this.subgraphing);
	container.appendChild(this.subgraphButton);
	
	return container;
}

SubgraphPlugin.prototype.resetGraphView=function()
{
	for ( var i = 0; i < this.graphView.currentGraph.getEdges().length; i++ ){this.graphView.currentGraph.getEdges()[i].setGhosted(false);}
	for ( var i = 0; i < this.graphView.currentGraph.getNodes().length; i++ ){this.graphView.currentGraph.getNodes()[i].setGhosted(false);}
}

SubgraphPlugin.prototype.ghostNodeEffect=function(subGraphId)
{
	var graph = this.graphView.currentGraph;
	var subGraph = this.graphView.currentGraph.getSubGraph(subGraphId);	
	for ( var i = 0; i < graph.getEdges().length; i++ ){if ( subGraph.getEdgeById(graph.getEdges()[i].getId()) == null ) graph.getEdges()[i].setGhosted(true);}
	for ( var i = 0; i < graph.getNodes().length; i++ ){if ( subGraph.getNodeById(graph.getNodes()[i].getId()) == null ) graph.getNodes()[i].setGhosted(true);}
}