var defaultWidth = 25;
var defaultHeight = 100;

function Graph(id,canvas,orginX,orginY,signalName,graphName,backGround1,backGround2,width,height,startValue,endValue,majorticks,type,indexColor,anim){

	this.id = id;
	this.paper = canvas;
	this.orginX = orginX/1;
	this.orginY = orginY/1;
	this.signalName =  signalName;
	this.name = graphName;
	this.type = type/1;
	this.displacementX = 0;
	this.displacementY = 0;
	this.height = height/1;
	this.width = width/1;
	this.indexColor = indexColor;

	this.backGround1 = backGround1;
	this.backGround2 = backGround2;
	this.step = majorticks/1;
	this.setName = setName;
	this.startValue = startValue/1;
	this.endValue = endValue/1;

	this.setStartValue = setStartValue;
	this.setEndValue = setEndValue;
//	this.strokeColor			=	"#036"; //gray
//	this.numericDataColor	=	"#036"; //gray
//	this.tickColor			=	"#036"; //gray
	this.tickCount			=	3;  

	this.zoom				=	0;
//	this.needleColor		=	"#FF0000"

//	this.needleStokeColor	=	"#036"; //gray

	this.setBackgroundColor = setBackgroundColor;
//	this.unitText;
//	this.unit;
	this.setPositionX = setPositionX;
	this.setPositionY = setPositionY;
	
	this.currentNeedlePos = 0;
	this.setSteps = setSteps;
	this.setMinorTicks = setMinorTicks;

	this.currentNeedlePosX = 0;
	this.currentNeedlePosY = 0;
	
	this.newNeedleValue;
	
	this.numArray;
	this.tickArray;

	this.graph;
//	this.mark;
	
	this.characterSize;
	this.characterFont;

	this.needleType = 1;
	this.interval = "1000";

	this.anim = anim;
	this.running = false;
	//this.newNeedleAngle = endAngle;
	this.needleSize;

	this.setIndexColor = setIndexColor;
	this.create = createGraphType;
	this.setZoom = zoom;
	this.move = move;
	this.changeType = changeType;
	this.setWidth = setWidth;
	this.setHeight = setHeight;
	this.setCharacterStyle=setCharacterStyle;
	this.refreshCharacter =refreshCharacter;
	this.animThread;
	this.stopanimate = stopanimate;
	
	this.deleteObject = deleteObject;
	this.setAnimation = setAnimation;
	this.showTop = showTop;

	this.animate = startAnimate;
	this.drawLine = drawLine;
	this.highlightItem = highlightItem;
	
	function setAnimation(val)
	{
		if(val == "true"){
			this.animate();
		}
		else{
			this.anim = false;
		}
		updatePropValues(this);
	}
	
	function stopanimate()
	{
		this.anim = false;
		updatePropValues(this);
	}
	
	function updatePropValues(o)
	{
		if(!viewMode)
		{
			var str = o.orginX+","+o.orginY+","+o.signalName+","+o.name+","+o.backGround1+","+o.backGround2+","+o.width+","+o.height+","+o.startValue+","+o.endValue+","+o.step+","+o.type+","+o.indexColor+","+o.anim;
			document.getElementById(id).value = str;
		} 
	}
	
	function setBackgroundColor(color){
		this.backGround1 = color;
		this.graph.attr("fill", color);
		updatePropValues(this);
	}
	
	function setSteps(val){
		this.step = val;
		createNumericData(this)
		createTicks(this);
		updatePropValues(this);
	}
	
	function setMinorTicks(val){
		this.tickCount = val+1;
		createTicks(this);
		updatePropValues(this);
	}
	
	function setStartValue(val){
		this.startValue = val;
		createNumericData(this);
		createTicks(this);
		updatePropValues(this);
	}
	
	function setEndValue(val){
		this.endValue = val;
		createNumericData(this);
		createTicks(this);
		updatePropValues(this);
	}
	
	function setName(val){
		this.name = val;
		updatePropValues(this);
	}
	
	function setIndexColor(val)
	{
		this.indexColor = val;
		this.needle.attr("stroke",val);
		updatePropValues(this);
	}
	
	function deleteObject(){
		this.graph.remove();
		for (var i = 0; i < this.numArray.length; i++){ 
			 this.numArray[i][0].remove();
		} 
		if(this.tickArray != null){
			for (var i = 0; i < this.tickArray.length; i++){ 
				this.tickArray[i][0].remove();
				for(var j = 0; j < this.tickArray[i][1].length;j++){
					if(this.tickArray[i][1][j] != null){
						this.tickArray[i][1][j].remove();
					}
				}
			}
		}
		if(this.needle != null){
			this.needle.remove();
		}
		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.graph.toFront();
		//if(this.unit != null){
		//	this.unit.translate(x,y);
		//}

		for (var i = 0; i < this.numArray.length; i++){ 
			 this.numArray[i][0].toFront();
	 
		} 
		if(this.tickArray != null){
			for (var i = 0; i < this.tickArray.length; i++){ 
				this.tickArray[i][0].toFront();
				for(var j = 0; j < this.tickArray[i][1].length;j++){
					if(this.tickArray[i][1][j] != null){
						this.tickArray[i][1][j].toFront();
					}
				}
			}
		}
		if(this.needle != null){
			this.needle.toFront();
		}
		if(selectedItem1 != null){
			selectedItem1.toFront();
		}
		if(selectedItem2 != null){
			selectedItem2.toFront();
		}if(selectedItem3 != null){
			selectedItem3.toFront();
		}
		if(selectedItem4 != null){
			selectedItem4.toFront();
		}
	
	}

	function setHeight(value){
	   if(value!=this.height){
            this.height = value;
            if(this.anim){
                this.currentNeedlePosY = this.orginY;
                this.currentNeedlePosX = this.orginX;
                this.needle.attr("path","M"+this.orginX+","+this.orginY);
            }
            
		    createOutLineType1(this);
		    createNumericData(this);
		    createTicks(this);
		    currentObject = this;
		    updatePropValues(this);
		    if(!viewMode){
			 this.highlightItem(this);
			 updatePropertiesBox();
            }
       }
		
	}

	function setWidth(value){
	   if(value!=this.width){
	   
		this.width = value;
		if(this.anim){
		    this.currentNeedlePosY = this.orginY;
            this.currentNeedlePosX = this.orginX;
            this.needle.attr("path","M"+this.orginX+","+this.orginY);
		}
		createOutLineType1(this);
		createNumericData(this);
		createTicks(this);
		currentObject = this;
		updatePropValues(this);
		if(!viewMode){
			this.highlightItem(this);
			updatePropertiesBox();
		}
	  }
	}

	function changeType(type){
		this.type = type;
		updatePropValues(this);
	}

	function setCharacterStyle(size,font){
		this.characterSize = size;
		this.characterFont = font;
		updatePropValues(this);
	}

	function refreshCharacter(){
		createNumericData(this);
		updatePropValues(this);
	}
	
	function highlightItem(obj){
		var heightAdjust = (obj.height/100) * 15;
		var oulinePath1 = "M"+(obj.orginX - 10)+","+(this.orginY-(obj.height+heightAdjust)-5)+" v"+-10+" h"+10+"z";
		selectedItem1.attr("path",oulinePath1);
		var oulinePath2 = "M"+(obj.orginX + obj.width+10)+","+(this.orginY-(obj.height+heightAdjust)-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.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;
		var heightAdjust = (this.height/100) * 15;
			if(newX > 0 && newY > this.height+heightAdjust){
				if(newX < 760-(this.width)){
				this.graph.translate(x,y);
				//if(this.unit != null){
				//	this.unit.translate(x,y);
				//}
		
				for (var i = 0; i < this.numArray.length; i++){ 
					 this.numArray[i][0].translate(x,y);
			 
				} 
				if(this.tickArray != null){
					for (var i = 0; i < this.tickArray.length; i++){ 
						this.tickArray[i][0].translate(x,y);
						for(var j = 0; j < this.tickArray[i][1].length;j++){
							if(this.tickArray[i][1][j] != null){
								this.tickArray[i][1][j].translate(x,y);
							}
							
						}
					}
				}
				if(this.needle != null){
					this.needle.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;
				this.currentNeedlePosX  = this.currentNeedlePosX +x;
				this.currentNeedlePosY  = this.currentNeedlePosY +y;
				
				updatePropValues(this);
			}
		}
		
	}

	function createGraphType(){
		
		createOutLineType1(this);
		createNumericData(this);
		createTicks(this);
		currentObject = this;
		updatePropValues(this);
		if(!viewMode){
			this.highlightItem(this);
			updatePropertiesBox();
		}
		if(this.anim && !viewMode){
			this.animate();
		}
		if(viewMode && this.anim == "true"){
			this.animate();
		}
		
		this.currentNeedlePosX = this.orginX;
		this.currentNeedlePosY = this.orginY-((this.height/100) * 7.5);
	
	//	createNeedle(this);
		//}
		//else{
		//	alert("Unable to Create Meter with following parameters");
		//}
	}

	function createOutLineType1(obj){
	
			var pointX1 = obj.orginX;
			var pointY1 = obj.orginY;
			var heightAdjust = (obj.height/100) * 15;
			var path = "M"+pointX1+","+pointY1+" v"+-(obj.height+heightAdjust)+" h"+obj.width+" v"+(obj.height+heightAdjust)+" z";
			if(obj.graph != null){
				obj.graph.attr("path",path)
			}else{
				var graph =obj.paper.path({stroke: "#036"},path); 
				var boxGradient = {type: "linear", dots: [{color: obj.backGround1, opacity: 1}, {color: obj.backGround2, opacity: .95}], vector: [0, 0, 1, 1]};
				//graph.attr({gradient: boxGradient, "stroke-width": 0, "stroke-opacity": 0});
				graph.attr("fill", obj.backGround1);
				graph.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;
					}
						
				};
				graph.node.onmouseup = function (e) 
				{ 
					dragOK = false;
				};
				obj.graph = graph;
			}
	}


	function createNumericData(obj){
		var count = (obj.endValue-obj.startValue)/obj.step;
		var step = (obj.height/count);
		
		var fontSize;
		if(obj.numArray != null){
			for (var i = 0; i < obj.numArray.length; i++){ 
				obj.numArray[i][0].remove();
			}
			obj.numArray = null;
		}
		if(obj.step == 0)return;
		obj.numArray = new Array(Math.round(count));
		var xValue ,yValue;

		xValue = obj.orginX -20;
		for(var i=0;i<=Math.round(count);i++){
			obj.numArray[i] = new Array(2);
			yValue = obj.orginY - (step*i)-(obj.height/100) * 5;

			if(obj.characterSize != null){
				fontSize = obj.characterSize;
			}else{
				fontSize = 12;
			}
			var fontName = "Arial";
			if(obj.characterFont != null){
				fontName  = obj.characterFont;
			}

			var fontTxt = fontSize+"px \""+fontName+"\"";

			var numValue = ""+((i*obj.step)+obj.startValue);
			var numDecimalPoints = ""+obj.endValue;
			var len = numDecimalPoints.length;
			numDecimalPoints = numDecimalPoints.indexOf(".")
			if(numDecimalPoints > 0){
				numDecimalPoints = len - numDecimalPoints;
			}
			if(numValue.indexOf(".") > 0){
				numValue = roundNumber(numValue,numDecimalPoints);
			}
			
			var number = obj.paper.text(xValue, yValue, ""+numValue).attr({"font": fontTxt, opacity: 1,"font-weight" :'bold'});
			if(obj.type == 1){
				number.attr("fill", "000000");
				//number.rotate(0,true);
			}else if(obj.type == 2){
				number.attr("fill", "000000");
			}
			obj.numArray[i][0] = number;
			obj.numArray[i][1] = 0;
		}
	}

	function createTicks(obj){
		var count = (obj.endValue-obj.startValue)/obj.step;
		var step = (obj.height/count);
		if(obj.tickArray != null){
			for (var i = 0; i < obj.tickArray.length; i++){ 
				obj.tickArray[i][0].remove();
				obj.tickArray[i][0] = null;
				for(var j = 0; j < obj.tickArray[i][1].length;j++){
					if(obj.tickArray[i][1][j] != null){
						obj.tickArray[i][1][j].remove();
						obj.tickArray[i][1][j] = null;
					}
					
				}
			}
			obj.tickArray = null;
		}
		if(obj.step == 0)return;
		var xValue ,yValue;
		var yOldValue;
		xValue = obj.orginX;
		obj.tickArray = new Array(Math.round(count+1));
		for(var i=0;i<=Math.round(count);i++){
			obj.tickArray[i] = new Array(2);
			yOldValue = yValue;
			yValue = obj.orginY - (step*i)-(obj.height/100) * 7;
			tickPos = "M "+(xValue)+" "+yValue+" h" +(obj.width)+"z";
			var tick =  obj.paper.path({stroke: "#ffffff","stroke-width": .25}, tickPos);
			var minorTickStep =(yOldValue-yValue)/(obj.tickCount);
			var minorYValue;
			obj.tickArray[i][0] = tick;
			obj.tickArray[i][1] = new Array(obj.tickCount);
			for(var j = 1; i>0 && j<obj.tickCount;j++){
				minorYValue =  yValue+(minorTickStep*j);
				tickPos = "M "+(xValue)+" "+minorYValue+" h" +(obj.width)+"z";
				var minorTicks =  obj.paper.path({stroke: "#ffffff","stroke-width": .25,"stroke-opacity": .25}, tickPos);
				obj.tickArray[i][1][j-1] = minorTicks;
			}

		}
	}

	function zoom(zoomFactor){
		this.outerCircleRadius = this.defaultSize+((this.defaultSize/100)*zoomFactor);
	}
	
	function drawLine(value){
    	
		var count =  (this.endValue-this.startValue)/this.step;
		var diff = (this.endValue-this.startValue);
		var step = this.height/diff;
		var xValue ,yValue;
		var yOldValue;
		this.currentNeedlePos = value;
		var adjustFactor  = (this.height/100) * 7.5;
		yValue = (this.orginY - adjustFactor)-(step*this.currentNeedlePos);

		var path;
		if(this.needle == null){
			path = "M"+(this.currentNeedlePosX)+" "+(this.currentNeedlePosY)+" L"+(this.currentNeedlePosX+5)+" "+(yValue);
			var needle =  this.paper.path({stroke: this.indexColor,"stroke-width": 1,"stroke-opacity": 1}, path);
			this.needle = needle;
			this.currentNeedlePosY = yValue;
			this.currentNeedlePosX = this.currentNeedlePosX+5;
			
		}else{
			path = this.needle.attr("path");
			if((this.currentNeedlePosX+5) > (this.orginX+this.width)){
				//this.currentNeedlePosX = this.currentNeedlePosX-5;
				var firstPos = path.indexOf("L");
				var newPath = "M"+path.substring(firstPos+1,path.length);
				path=newPath;
				path = path+" L "+(this.currentNeedlePosX+5)+" "+(yValue);
		    	this.needle.attr("path",path);
				this.needle.translate(-5,0);
				this.currentNeedlePosY = yValue;
			} else{
		    	path = path+" L "+(this.currentNeedlePosX+5)+" "+(yValue);
		    	this.needle.attr("path",path);
		    	this.currentNeedlePosY = yValue;
				this.currentNeedlePosX = this.currentNeedlePosX+5;
		    }
		}
	}

	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 startAnimate(){
		if(this.running)return;
		this.running=true;
		var objThis = this;
		this.anim =true;
		var thr = window.setInterval(function(){
			if(objThis.anim){
				var rndPos = Math.random() * (objThis.endValue-objThis.startValue);
				rndPos = rndPos + startValue;
				objThis.drawLine(rndPos);
			}else{
				window.clearInterval(thr);
				objThis.running=false;
			}
			
		},500);
	}
}
