/*
 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 DotParserPlugin()
{
	this.htmlElement;
	this.graphView;
}

DotParserPlugin.prototype = Object();

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

DotParserPlugin.prototype.createHtmlElement=function()
{
	div = document.createElement("div");
	this.textElement = document.createElement("textarea");
	this.textElement.cols = 50;
	this.textElement.rows = 10;
	
	this.parseButton = document.createElement("button");
	this.parseButton.appendChild(document.createTextNode("Get DOT!"));
	$(this.parseButton).css({'widht':'40px','heihgt':'20px','position':'realtive','top':'20px','left':'30px'});
	
	div.appendChild(this.parseButton);
	div.appendChild(this.textElement);
	
	this.initV = true;
	var t = this;
	this.parsingCounter = 1;
	
	this.parseButton.onclick = function()
	{
		if ( !t.initV ) {
			result  = "//////////////////////////\n";
			/*result += "//   generated by the   //\n";
			result += "//  DOT Parsing plugin  //\n";
			result += "//       of BINViz      //\n";
			result += "//        Graph version " + t.parsingCounter + "\n";
			result += "//////////////////////////\n";*/
			result += "digraph{ \n";
			edges = t.graphView.currentGraph.getEdges();
			nodes = t.graphView.currentGraph.getNodes();
			for ( var i = 0; i < nodes.length; i++) {
				result += nodes[i].getLabel() + "[" + 
					"pos=\"" + nodes[i].getPosition().x + "," + nodes[i].getPosition().y + "\"," + 
					"color=\"" + t.graphView.style.getNodeStyle(nodes[i].getType()).getInactiveColor()[1] + "\"" + "];\n";
			}
			for ( var i = 0; i < edges.length; i++ ) {
				result += edges[i].node1.getLabel() + " -> " + edges[i].node2.getLabel() + ";\n";
			}
			result += "}\n \n";
			t.textElement.appendChild(document.createTextNode(result));
			t.parsingCounter++;
		}
	}
	
	this.initV = false;
	
	return div;
}