/*
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 VertexView class */
Zorya._VertexView = function (drag, color, vertexModel){
	this.debug = 10;
	this.className = 'VertexView';	
	Zorya.debug().alert(this.className + ' ' + vertexModel.getName(), this.debug);
	
	/** Default construtor
	 *
	 * drag - ToolMan drag
	 * color - vertex color
	 * vertexModel - vertex model
	 */
	{	
		this.vertexModel = vertexModel;
		this.viewGenericDiv = null;
		this.viewBackgroundGenericDiv = null
		this.canvas = null;		
		this.color = color;
		this.drag = drag;
		this.countOutEdges = 0;
		this.countInEdges = 0;
		this.countAllEdges = 0;
		this.outEdges = new Array();
		this.inEdges = new Array();
		this.allEdges = new Array();		
		Zorya.debug().alert(this.className + ' created', this.debug);		
	}
	
	/** The initialize method 
	 * canvas - parent div
	 */
	this.initialize = function(canvas) {
		this.canvas = canvas;
		var viewText = '<div id="'
			+ this.className + this.vertexModel.getId()
			+ '" style="position: absolute; top:'
			+ this.vertexModel.getX() + 'px; left:'
			+ this.vertexModel.getY()  + 'px;">'
			+ '</div>';
		canvas.appendContent(viewText);
		
		viewText = '<div id="Back'
			+ this.className + this.vertexModel.getId()
			+ '" style="position: absolute; top:'
			+ canvas.getX() + 'px; left:'
			+ canvas.getY()  + 'px;">'
			+ this.background()
			+ '</div>';
		this.getInternalCanvas().appendContent(viewText);
			
		viewText = '<div id="Front' 
			+ this.className + this.vertexModel.getId() 
			+ '" style="position: absolute; top:' 
			+ canvas.getX() + 'px; left:' 
			+ canvas.getY() +'px;">'	
			+ this.body()
			+ '</div>';
			
		this.getInternalCanvas().appendContent(viewText);
				
		this.drag.createSimpleGroup(this.getInternalCanvas().obj);	
		Zorya.debug().alert(this.className + ' initialized ' + canvas, this.debug);
	} 	
		
	/** The method paints the vertex */
	this.body = function(){
		//var bodyText = 'Test';
		var bodyText = '<table width="' + this.getBackgroundCanvas().getWidth() 
			+ '" height="' + this.getBackgroundCanvas().getHeight() 
			+ '"><tr align="center"><td align="center"><A href="'
			+ this.vertexModel.getUrl() + '">'
			+ this.vertexModel.getName() 
			+ '</A></td></table>';
		return bodyText;
	}
	/** The method paints the vertex */
	this.background = function(){
		var bodyText = '<img src="img/vertex.JPG">';
		//var bodyText = '<table><td bgcolor="#AAAAAA">' + this.vertexModel.getName() + '</td></table>';
		return bodyText;
	}
			
	/** The method paints the vertex */
	this.paint = function(canvas){
		document.write('<div id="vertex' + this.className + this.vertexModel.getId() 
			+ '" style="position: absolute; top:' 
			+ this.y + 'px; left:' 
			+ this.x  + 'px;">');	
		document.write(this.body());	
		document.write('</div>');					
	}
	/** The method refreshes the vertex */
	this.refresh = function() {
		this.refreshEdges();
	}
	
	/** The method refreshes the edges */
	this.refreshEdges = function(){
		var i;
		for (i = 0; i < this.allEdges.length; i++) {
    		this.allEdges[i].refresh();
		}
	}
	
 	
	/** The method returns y field */
	this.getX = function() {
		return this.viewGenericDiv.getX();
	}

	/** The method returns y field */
	this.getY = function() {
		return this.viewGenericDiv.getY();
	}
	
	/** The method returns centerX field */
	this.getCenterX = function() {
		//alert(this.getBackgroundCanvas());
		return this.getInternalCanvas().getCenterX() + this.getBackgroundCanvas().getWidth()/2;
	}

	/** The method returns centerY field */
	this.getCenterY = function() {
		//alert(getBackgroundCanvas().getWidth());
		return this.getInternalCanvas().getCenterY() + this.getBackgroundCanvas().getHeight()/2;
	}	 				

	/** The method returns viewGenericDiv field */
	this.getBackgroundCanvas = function() {
		if (this.viewBackgroundGenericDiv == null) {
			this.viewBackgroundGenericDiv = new GenericDiv('Back'
			+ this.className + this.vertexModel.getId());
		}
		return this.viewBackgroundGenericDiv;
	}

	/** The method returns viewGenericDiv field */
	this.getInternalCanvas = function() {
		if (this.viewGenericDiv == null) {
			this.viewGenericDiv = new GenericDiv(this.className + this.vertexModel.getId());
		}
		return this.viewGenericDiv;
	}
	
 	/** The method adds edge to inEdges array */
 	this.addInEdge = function(edgeId, edge) {
 		this.allEdges[this.countAllEdges] = edge;
 		this.countAllEdges++;
 	}

 	/** The method adds edge to outEdges array */
 	this.addOutEdge = function(edgeId, edge) {
 		this.allEdges[this.countAllEdges] = edge;
 		this.countAllEdges++
 	}		
 	
	/** The method returns all edges field */
	this.getAllEdges = function() {
		return this.allEdges;
	} 		
	
	Zorya.debug().alert(this.className + ' end', this.debug);
}