
pi = Math.PI/180;


function Button(id,canvas,orginX,orginY,width,height,name,text,cmdName,component,command,componentName,action){
	svgCanvasAdjust();
	this.id = id;
	this.paper = canvas;	
	
	this.actualX = orginX/1;
	this.actualY = orginY/1;
	
	if(viewMode){
		this.orginX = orginX/1;
		this.orginY = orginY/1;
	}else{
		this.orginX = (orginX/1)+pageOrginX;
		this.orginY = (orginY/1)+pageOrginY;
	}

	this.text = text;
	this.cmdName = cmdName;
	
	this.component = component;
	this.componentName = componentName;
	
	this.width = width/1;
	this.height = height/1; 
	
	this.name =  text;
	this.command = command;
	
	this.action = action;
	this.button;
	
	this.setScript = setScript;
	this.setAction = setAction;
	this.setValue = setValue;
	this.setWidth = setWidth;
	this.setHeight = setHeight;
	this.setPositionX = setPositionX;
	this.setPositionY = setPositionY;
	this.move = move;
	this.deleteObject = deleteObject;
	this.create = createButtonType;
	this.getElement = getElement;
	this.setLikedElement = setLikedElement;
	this.runCmd = runCmd;
	this.highlightItem = highlightItem;
	
	function updatePropValues(o)
	{
		if(!viewMode)
		{
		//(id,canvas,orginX,orginY,width,height,name,text,cmdName,component,command,componentName)	
			var comp = (o.component == null) ? comp="null" : o.componentName.replace(" ","");
			var str = (o.orginX-pageOrginX)+","+(o.orginY-pageOrginY)+","+o.width+","+o.height+","+o.name+","+o.text+","+o.cmdName+"%"+comp+"$"+o.command+","+o.componentName+","+o.action;
			document.getElementById(id).value = str;
		} 
	}
	
	function deleteObject(){
		if(document.getElementById("envCmd") != null){
			document.getElementById("envCmd").removeChild(this.button);
		}
		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 getElement(){
		linkCmd = true;
		cmdObject = this;
	}

	function setScript(val)
	{
		this.command = val;
		updatePropValues(this);
	}	
	
	function setAction(val)
	{
		if(val=="true"){
			this.action = true;
		}
		else{
			this.action = false;
		}
		updatePropValues(this);
	}
	
	function setValue(val)
	{
		this.text = val;
		this.button.setAttribute("value", val);
		updatePropValues(this);
	}
	
	function setLikedElement(obj){
		this.component=obj;
		this.componentName = obj.id;
		updatePropValues(this);
	}
	
	function runCmd(obj){
		if(navigator.userAgent.indexOf("Opera") == -1) { 
			cmdDiv = document.createElement("div");
			cmdDiv.setAttribute("id","cmdDIVFF");
			var str = "\<script\>function btnAction(obj){obj.component.";
			str+=obj.command;
			str+="}";
			str+="\<\/script>";
			cmdDiv.innerHTML = str;
			svgCanvas.parentNode.appendChild(cmdDiv);
			btnAction(obj);
			if(cmdDiv != null){
				svgCanvas.parentNode.removeChild(cmdDiv);
			}
		}
		else{
			cmdDiv = document.getElementById("cmdDIV");
			var str = "\<script\>function btnAction(obj){obj.component.";
			str+=obj.command;
			str+="}";
			str+="\<\/script>";
			document.getElementById("cmdDIV").innerHTML = str; 
			document.body.appendChild(cmdDiv);
			btnAction(obj);
			document.getElementById("cmdDIV").innerHTML ="";
	
		}
	}
	
	function setWidth(val)
	{
		this.width = val;
		var style = "position:absolute;left:"+this.orginX+"px;top:"+this.orginY+"px;width:"+val+"px;height:"+this.height+"px;";
		this.button.setAttribute("style", style);
		updatePropValues(this);
	}
	
	function setHeight(val)
	{
		this.height = val;
		var style = "position:absolute;left:"+this.orginX+"px;top:"+this.orginY+"px;width:"+this.width+"px;height:"+val+"px;";
		this.button.setAttribute("style", style);
		updatePropValues(this);
	}
	
	function highlightItem(obj){
		var oulinePath1 = "M"+(obj.actualX - 10)+","+(obj.actualY-5)+" v"+-10+" h"+10+"z";
		selectedItem1.attr("path",oulinePath1);
		var oulinePath2 = "M"+(obj.actualX + obj.width+10)+","+(obj.actualY-5)+" v"+-10+" h"+-10+"z";
		selectedItem2.attr("path",oulinePath2);
		var oulinePath3 = "M"+(obj.actualX - 10)+","+(obj.actualY+obj.height+5)+" v"+10+" h"+10+"z";
		selectedItem3.attr("path",oulinePath3);
		var oulinePath4 = "M"+(obj.actualX + obj.width+10)+","+(obj.actualY+obj.height+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;
		
		
		
		
		var relX = this.actualX+x;
		var relY = this.actualY+y;
		
		if(relX >= 0 && relY >= 0){
			if(relX < 760-(this.width)){
				this.orginX = newX;
				this.orginY = newY;
				
				this.actualX = relX;
				this.actualY = relY;
			
				if(newX < pageOrginX){
					this.orginX = pageOrginX;
				}
				if(newY < pageOrginY){
					this.orginY = pageOrginY;
				}
				if(newX > 760+pageOrginX-this.width){
					this.orginX = 760+pageOrginX-this.width;
				}
				
				var newStyle = "position:absolute;left:"+this.orginX+"px;top:"+this.orginY+"px;width:"+this.width+"px;height:"+this.height+"px;";
				
				this.button.setAttribute("style",newStyle);
				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);
				}
				updatePropValues(this);
			}
		}
	}

	function createButtonType(){
		makeButton(this);
		currentObject = this;
		updatePropValues(this);
		if(!viewMode){
			this.highlightItem(this);
			updatePropertiesBox();
		}
	}
	
	function setPositionX(currX)
	{
		currX += pageOrginX;
        currentObject.move(currX - currentObject.orginX, 0);
        currentObject.orginX = currX;
        updatePropValues(this);
    }
    
    function setPositionY(currY)
    {
    	currY += pageOrginY;
        currentObject.move(0, currY - currentObject.orginY);
        currentObject.orginY = currY;
        updatePropValues(this);
    }
    
	function makeButton(obj){
		var field = document.createElement("input");
		field.setAttribute("type","button");
		field.setAttribute("value",obj.text);
		field.setAttribute("class","envbtn");
		
		field.setAttribute("style","position:absolute;left:"+obj.orginX+"px;top:"+obj.orginY+"px;width:"+obj.width+"px;height:"+obj.height+"px;");
		field.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){
						obj.highlightItem(obj);
						currentObject = obj;
						manageDiv(currentObject);
						updatePropertiesBox();
						manageDiv(currentObject);
					//	updatePropertiesBox();
					}else{						
						currentObject = obj;
					}
	      			
						
				};
				field.onmouseup = function (e) 
				{ 
					dragOK = false;
						//currentObject = null;
				};
				field.onclick = function (e) 
				{ 		
					if(obj.component != null && obj.action ){
						try{
							runCmd(obj);
						}catch(ex){}
					}
				};
		obj.button = field;
		var cmdDiv = document.getElementById("envCmd");
		if(cmdDiv == null){
			cmdDiv = document.createElement("div");
			svgCanvas.parentNode.appendChild(cmdDiv);
			cmdDiv.setAttribute("id","envCmd");
		}
		cmdDiv.appendChild(field);
	}
}
