
pi = Math.PI/180;

function releaseDrag(){
	dragOK = false;
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}


function Label(id,canvas,orginX,orginY,type,text,font,fontSize,fontColor){
	svgCanvasAdjust();
	this.id = id;
	this.paper = canvas;	
	
	this.orginX = orginX/1;
	this.orginY = orginY/1;

	this.type = type/1;
	this.name = text;
	this.font = font;
	this.fontSize = fontSize/1;
	this.fontColor = fontColor;
	
	this.label;
	this.create = createLabelType;
	this.setFont = setFont;
	this.setLabelFontSize = setLabelFontSize;
	this.setLabel = setLabel;
	this.setType = setType;
	this.setColor = setColor;
	this.move = move;
    this.setPositionX = setPositionX;
    this.setPositionY = setPositionY;
	this.deleteObject = deleteObject;
	this.showTop = showTop;
	this.animThread;
	this.animate = startAnimate;
	this.highlightItem = highlightItem;

	function deleteObject()
	{
		this.label.remove();
		var r1 = document.getElementById(this.id);
		document.getElementById("myform").removeChild(r1);
		if(selectedItem1 != null){
			selectedItem1.attr("path","z");
		}
		if(selectedItem2 != null){
			selectedItem2.attr("path","z");
		}
		if(selectedItem3 != null){
			selectedItem3.attr("path","z");
		}
		if(selectedItem4 != null){
			selectedItem4.attr("path","z");
		}
	}
	
	function showTop(){
		this.label.toFront();
		if(selectedItem1 != null){
				selectedItem1.toFront();
		}
		if(selectedItem2 != null){
				selectedItem2.toFront();
		}
		if(selectedItem3 != null){
				selectedItem2.toFront();
		}
		if(selectedItem4 != null){
				selectedItem2.toFront();
		}
	}
	
	function updatePropValues(o)
	{
		if(!viewMode)
		{
			var str = o.orginX+","+o.orginY+","+o.type+","+o.name+","+o.font+","+o.fontSize+","+o.fontColor;
			document.getElementById(id).value = str;
		} 
	}
	
	function setType(type)
	{
		this.type = type;
		updatePropValues(this);
	}
	
	function setLabel(name)
	{
		this.name = name;
		this.label.attr("text", name);
		updatePropValues(this);
	}

	function setFont(font)
	{
		this.font = font;
		var fontTxt = this.fontSize+"px \""+font+"\"";
		this.label.attr("font", fontTxt);
		this.label.attr("font-weight", "bold");
		updatePropValues(this);
	}

    function setLabelFontSize(size)
    {
        this.fontSize = size;
        var fontTxt = size+"px \""+this.font+"\"";
		this.label.attr("font", fontTxt);
		this.label.attr("font-weight", "bold");
		updatePropValues(this);
    }
    
    function setColor(fontColor)
    {
    	this.fontColor = fontColor;
    	this.label.attr('fill', fontColor);
    	updatePropValues(this);
    }
	
	function setPositionX(currX)
	{
        currentObject.move(currX - currentObject.orginX, 0);
        currentObject.orginX = currX;
        updatePropValues(this);
    }
    
    function setPositionY(currY)
    {
        currentObject.move(0, currY - currentObject.orginY);
        currentObject.orginY = currY;
        updatePropValues(this);
    }
    
    function highlightItem(obj){

		var oulinePath1 = "M"+(obj.orginX - 10)+","+(this.orginY-(obj.label.getBBox().height)-5)+" v"+-10+" h"+10+"z";
		selectedItem1.attr("path",oulinePath1);
		var oulinePath2 = "M"+(obj.orginX + obj.label.getBBox().width+10)+","+(this.orginY-(obj.label.getBBox().height)-5)+" v"+-10+" h"+-10+"z";
		selectedItem2.attr("path",oulinePath2);
		var oulinePath3 = "M"+(obj.orginX - 10)+","+(this.orginY+5)+" v"+10+" h"+10+"z";
		selectedItem3.attr("path",oulinePath3);
		var oulinePath4 = "M"+(obj.orginX + obj.label.getBBox().width+10)+","+(this.orginY+5)+" v"+10+" h"+-10+"z";
		selectedItem4.attr("path",oulinePath4);
	}

	function move(x,y)
	{
		svgCanvasAdjust();
		var newX = this.orginX+x;
		var newY = this.orginY+y;
		if(newX > 0 && newY > 0)
		{
			this.label.translate(x,y);
			if(selectedItem1 != null){
				selectedItem1.translate(x,y);
			}
			if(selectedItem2 != null){
				selectedItem2.translate(x,y);
			}
			if(selectedItem3 != null){
				selectedItem3.translate(x,y);
			}
			if(selectedItem4 != null){
				selectedItem4.translate(x,y);
			}
			this.orginX = this.orginX+x;
			this.orginY = this.orginY+y;
			updatePropValues(this);
		}
	}

	function createLabelType(){
		makeLabelType(this);
		currentObject = this;
		updatePropValues(this);
		if(!viewMode){
			this.highlightItem(this);
			updatePropertiesBox();
		}
		if(this.anim){
			this.animate();
		}
	}

	function makeLabelType(obj){
		var fontTxt = "25"+"px \""+"Times New Roman"+"\"";
		labelMode = true;
		var label = obj.paper.text(obj.orginX, obj.orginY, obj.name).attr({"font": fontTxt, opacity: 1});
		label.attr('fill', fontColor);
		labelMode = false;
		if(!viewMode){
				label.node.onmousedown = function (e) 
				{ 
					dragOK=true;
					obj.showTop();
	      			
					if (!e){
							mouseDownX = window.event.screenX;
							mouseDownY = window.event.screenY;
					}else{
						mouseDownX = e.pageX;
						mouseDownY = e.pageY;
					}
					if(!viewMode && currentObject != obj && !linkCmd){
						obj.highlightItem(obj);
						currentObject = obj;
						manageDiv(currentObject);
						updatePropertiesBox();
					}else{
						currentObject = obj;
					}
					
				};
				label.node.onmouseup = function (e) 
				{ 
						dragOK = false;
						//currentObject = null;
				};
			}
			obj.label = label; 
			
	}
	function startAnimate(){}
}
