/*
Copyright (c) 2006 Lukasz Szajkowski <http://www.szajkowski.com/>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/


/** A GraphView class */
Zorya._GraphView =  function (drag, color, graphModel) {
	this.debug = 10;
	this.className = 'GraphView';
	Zorya.debug().alert(this.className + '  ' + graphModel.getName(), this.debug);
	
	/** Default construtor
	 *
	 * drag - ToolMan drag
	 * color - vertex color	 
	 * graphModel - graph data model
	 */
	{	
		this.graphModel = graphModel;
		this.canvas = null;	
		this.color = color;
		this.drag = drag;				
		this.vertexes = new Array();
		this.edges = new Array();
		this.viewGenericDiv = null;
		this.viewVertexesGenericDiv = null;
		this.viewEdgesGenericDiv = null;
		Zorya.debug().alert(this.className + ' created', this.debug);				
	}

	/** The initialize method 
	 * canvas - parent div
	 */
	this.initialize = function(canvas) {	
		this.canvas = canvas;
		Zorya.debug().alert(this.className + ' initialize ' + canvas, this.debug);
		var viewText = '<div id="' 
			+ this.className + this.graphModel.getId() 
			+ '" style="position: absolute; top:' 
			+ canvas.getX() + 'px; left:' 
			+ canvas.getY()  + 'px;">'	
			+ '</div>';
		canvas.appendContent(viewText);

		viewText = '<div id="Edges' 
			+ this.className + this.graphModel.getId() 
			+ '" style="position: absolute; top:' 
			+ canvas.getX() + 'px; left:' 
			+ canvas.getY()  + 'px;">'	
			+ '</div>'
			+'<div id="Vertexes' 
			+ this.className + this.graphModel.getId() 
			+ '" style="position: absolute; top:' 
			+ canvas.getX() + 'px; left:' 
			+ canvas.getY()  + 'px;">'	
			+ '</div>';
			
		this.getInternalCanvas().appendContent(viewText);
		
		var vertexeModelArray = this.graphModel.getVertexes();
		Zorya.debug().alert(this.className + ' initialized ', this.debug);
		for(var vertexModelId in vertexeModelArray) {
			this.vertexes[vertexModelId].initialize(this.getVertexesCanvas()); 
		}

		var edgeModelArray = this.graphModel.getEdges();
		for(var edgeModelId in edgeModelArray) {
			this.edges[edgeModelId].initialize(this.getEdgesCanvas()); 
		}
	} 	
	
			
	/** The method paints the GraphView */
	this.body = function(){
		var bodyText = '<table><td bgcolor="#AAAAAA">&nbsp;</td></table>';
		return bodyText;
	}	

	/** The method populates the GraphView */
	this.populate = function() {
		var vertexeModelArray = this.graphModel.getVertexes();
		for(var vertexModelId in vertexeModelArray){
			var vertexView = Zorya.VertexView(this.drag, this.color, vertexeModelArray[vertexModelId]);
			this.vertexes[vertexModelId] = vertexView; 
		}
		var edgeModelArray = this.graphModel.getEdges();
		for(var edgeModelId in edgeModelArray) {
			var edgeView = Zorya.EdgeView(this.drag, this.color, edgeModelArray[edgeModelId], this.vertexes);
			this.edges[edgeModelId] = edgeView; 
		}		
	}	
	
	/** The method returns viewGenericDiv field */
	this.getInternalCanvas = function() {
		if (this.viewGenericDiv == null) {
			this.viewGenericDiv = new GenericDiv(this.className + this.graphModel.getId());
		}
		return this.viewGenericDiv;
	}
		
	/** The method returns viewVertexesGenericDiv field */
	this.getVertexesCanvas = function() {
		if (this.viewVertexesGenericDiv == null) {
			this.viewVertexesGenericDiv = new GenericDiv('Vertexes' 
				+ this.className + this.graphModel.getId() );
		}
		return this.viewVertexesGenericDiv;
	}

	/** The method returns viewVertexesGenericDiv field */
	this.getEdgesCanvas = function() {
		if (this.viewEdgesGenericDiv == null) {
			this.viewEdgesGenericDiv = new GenericDiv('Edges' 
				+ this.className + this.graphModel.getId() );
		}
		return this.viewEdgesGenericDiv;
	}
		
	/** The method gets vertex */
	this.getVertex = function(vertexId) {
		return this.vertexes[vertexId];
	}

	/** The method gets vertex */
	this.getVertexes = function() {
		return this.vertexes;
	}

	/** The method gets edge */
	this.getEdge = function(edgeId) {
		return this.edges[edgeId];
	}

	/** The method gets edges */
	this.getEdges = function() {
		return this.edges;
	}
	Zorya.debug().alert(this.className + ' end', this.debug);
}
