var defaultWidth = 25;
var defaultHeight = 100;

function Meter(id,canvas,orginX,orginY,signalName,meterName,backGround1,backGround2,width,height,startValue,endValue,textStep,type,needleColor,tickCount,tickColor,fontName,fontSize,fontColor,anim){
	svgCanvasAdjust();
	this.id = id;
	this.paper = canvas;
	this.orginX = orginX/1;
	this.orginY = orginY/1;
	this.signalName =  signalName;
	this.name = meterName;
	this.type = type;
	this.displacementX = 0;
	this.displacementY = 0;
	this.height = height/1;
	this.width = width/1;

	this.setFontColor = setFontColor;
	this.backGround1 = backGround1;
	this.backGround2 = backGround2;
	this.stopanimate = stopanimate;

	this.startValue = startValue/1;
	this.endValue = endValue/1;

	this.step = textStep/1;
	
	this.setName = setName;
	this.strokeColor			=	"#036"; //gray

	this.numericDataColor	=	"#036"; //gray

	this.tickColor			=	tickColor; //gray
	this.tickCount			=	tickCount/1;  

	this.zoom				=	0;
	this.needleColor		=	needleColor;

	this.needleStokeColor	=	"#036"; //gray

	this.unitText;
	this.unit;
	this.currentNeedlePos = 0;
	
	this.setMinorTicks = setMinorTicks;
	this.setBackground 	= setBackground;
	this.setStartValue 	= setStartValue;
	this.setEndValue 	= setEndValue;
	
	this.newNeedleValue;
	this.numArray;
	this.tickArray;
	this.meter;
	this.needle;
	this.mark;
	
	this.fontSize = fontSize;
	this.fontName = fontName;
	this.fontColor = fontColor;

	this.needleType = 1;

	this.interval = "1000";

	this.anim = anim;
	this.running = false;
	//this.newNeedleAngle = endAngle;
	this.needleSize;

	this.setSteps = setSteps;
	this.setPositionX = setPositionX;
	this.setPositionY = setPositionY;
	this.create = createMeterType;
	this.setZoom = zoom;
	this.move = move;
	this.setType = setType;
	this.setNeedleStyle = setNeedleType;
	this.setWidth = setWidth;
	this.setHeight = setHeight;
	this.setFontSize = setFontSize;
	this.setFont = setFont;
//	this.setCharacterStyle = setCharacterStyle;
	this.refreshCharacter = refreshCharacter;
	this.setNeedlePos = setNeedlePos;
	this.setNeedleColor = setNeedleColor;
	this.setTickColor = setTickColor;
	this.deleteObject = deleteObject;
	
	this.showTop = showTop;
	this.animThread;
	this.setAnimation = setAnimation;
	this.animate = startAnimate;
	this.highlightItem = highlightItem;
	
	function setAnimation(val){
		if(val == "true"){
			this.animate();
		}
		else{
			this.anim = false;
		}
	}
	
	function setName(val){
		this.name = val;
		updatePropValues(this);
	}
	
	function stopanimate()
	{
		this.anim = false;
	}
	
	function setBackground(col1, col2){
		this.backGround1 = col1;
		this.backGround2 = col2
		var boxGradient = {type: "linear", dots: [{color: this.backGround1, opacity: 1}, {color: this.backGround2, opacity: .95}], vector: [0, 0, 1, 1]};
		this.meter.attr({gradient: boxGradient, "stroke-width": 0, "stroke-opacity": 0});
		updatePropValues(this);
	}
	
	function setFont(font)
	{
		this.fontName = font;
		var fontTxt = this.fontSize+"px \""+font+"\"";
		for (var i = 0; i < this.numArray.length; i++){ 
			 this.numArray[i][0].attr("font", fontTxt);
			 this.numArray[i][0].attr("font-weight", "bold");
		} 
		updatePropValues(this);
	}

    function setFontSize(size)
    {
        this.fontSize = size;
		var fontTxt = size+"px \""+this.fontName+"\"";
		for (var i = 0; i < this.numArray.length; i++){ 
			 this.numArray[i][0].attr("font", fontTxt);
			 this.numArray[i][0].attr("font-weight", "bold");
		} 
		updatePropValues(this);
    }
	
	function setFontColor(color){
		this.fontColor = color;
		var fontTxt = this.fontSize+"px \""+this.fontName+"\"";
		for (var i = 0; i < this.numArray.length; i++){ 
			 this.numArray[i][0].attr("font", fontTxt);
			 this.numArray[i][0].attr("font-weight", "bold");
			 this.numArray[i][0].attr("fill",color);
		}
		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 setSteps(val){
		if(val == 0){
			this.step = 0;
		}else{
			this.step = val;
		}
		createNumericData(this);
		createTicks(this);
		updatePropValues(this);
		
	}
	
	function setMinorTicks(val){
		this.tickCount = val+1;
		createTicks(this);
		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 deleteObject(){
		this.meter.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();
					}
				}
			}
		}
		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.meter.toFront();
		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();
					}
				}
			}
		}
		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){
		this.height = value;
		constructMeterType(this);
		createNumericData(this);
		createTicks(this);
		createNeedle(this);
		this.highlightItem(this);
		updatePropValues(this);
	}

	function setWidth(value){
		this.width = value;
		constructMeterType(this);
		createNumericData(this);
		createTicks(this);
		createNeedle(this);
		this.highlightItem(this);
		updatePropValues(this);
	}

	function setTickColor(tickColor){
		this.tickColor = tickColor;
		updatePropValues(this);
	}

	function setNeedleType(type,size){
		this.needleType = type;
		this.needleSize = size;
		createNeedle(this);
	}

	function setType(type){
		this.type = type;
		createTicks(this);
		createNeedle(this);
		updatePropValues(this);
	}

	function setNeedleColor(needleColor){
		this.needleColor = needleColor;
		var boxGradient = {type: "linear", dots: [{color: this.needleColor, opacity: 1}, {color: this.needleColor, opacity: .9}], vector: [0, 0, 1, 1]};
		this.needle.attr({gradient: boxGradient, "stroke-width": 0, "stroke-opacity": 0});
		updatePropValues(this);
	}

	function updatePropValues(o)
	{
		if(!viewMode)
		{
		//orginX,orginY,signalName,name,backGround1,backGround2,width,height,startValue,endValue,textStep,type,needleColor,tickCount,tickColor,fontName,fontSize,fontColor,anim){
		
			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.needleColor+","+o.tickCount+","+o.tickColor+","+o.fontName+","+o.fontSize+","+o.fontColor+","+o.anim;
			document.getElementById(id).value = str;
		} 
	}
	
	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 setCharacterStyle(size,font){
		this.fontSize = size;
		this.FontName = font;
	}*/

	function refreshCharacter(){
		createNumericData(this);
	}
	
	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.meter.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);
							}
							
						}
					}
				}
		
				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;
				
				updatePropValues(this);
			}
		}
	}

	function createMeterType(){
		constructMeterType(this);
		createNumericData(this);
		createTicks(this);
		createNeedle(this);
		currentObject = this;
		updatePropValues(this);
		if(!viewMode){
			this.highlightItem(this);
			updatePropertiesBox();
		}
		if(this.anim && !viewMode){
			this.animate();
		}
		if(viewMode && this.anim == "true"){
			this.animate();
		}
	}

	function constructMeterType(obj){
	
			var pointX1 = obj.orginX;
			var pointY1 = obj.orginY;
			var heightAdjust = (obj.height/100) * 15;
			//var heightAdjust =0;
			var path = "M"+pointX1+","+pointY1+" v"+-(obj.height+heightAdjust)+" h"+obj.width+" v"+(obj.height+heightAdjust)+" z";
			//var path = "M"+pointX1+","+pointY1+" v"+-(obj.height)+" h"+obj.width+" v"+(obj.height)+" z";
			
			if(obj.meter != null){
				obj.meter.attr("path",path)
			}else{
				var meter =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]};
				meter.attr({gradient: boxGradient, "stroke-width": 0, "stroke-opacity": 0});
				//meter.attr("fill", obj.backGround1);
				meter.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;
					}
						
				};
				meter.node.onmouseup = function (e) 
				{ 
					dragOK = false;
				};
				obj.meter = meter;
			}
	}


	function createNumericData(obj){
		var count = (obj.endValue-obj.startValue)/obj.step;
		var step = (obj.height/(count));
		if(obj.numArray != null){
			for (var i = 0; i < obj.numArray.length; i++){ 
				if(obj.numArray[i][0] != null){
					obj.numArray[i][0].remove();
					obj.numArray[i][0] = null;
				}
			}
			//obj.unit.remove();
			obj.numArray = null;
		}
		
		if(obj.step == 0)return;
		obj.numArray = new Array(Math.round(count));
		var xValue ,yValue;

		xValue = obj.orginX + (obj.width*.25);
		for(var i=0;i<=Math.round(count);i++){
			obj.numArray[i] = new Array(2);
			var yAdjustValue  = (obj.height/100) * 7.5;
			yValue = obj.orginY - (step*i)-yAdjustValue;
			//yValue = obj.orginY - (step*i);

			var fontTxt = obj.fontSize+"px \""+obj.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;
		}
		
		//var fontTxt = obj.fontSize+"px \""+obj.fontName+"\"";
		//var test = obj.paper.text(xValue, obj.orginY+20, "").attr({"font": fontTxt, opacity: 1,"font-weight" :'bold'});
		//test.attr("fill", "red");
		//obj.unit = test;
	}

	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++){ 
				if(obj.tickArray[i][0] != null){
					obj.tickArray[i][0].remove();
					obj.tickArray[i][0] = null
				}
				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;
		var endX,endY;
		if(obj.type == 1){
			endX = obj.width*.45
		}
		else{
			endX = obj.width*.35
		}
		xValue = obj.orginX + (obj.width*.50);
		obj.tickArray = new Array(Math.round(count+1));
		var yAdjustValue  = (obj.height/100) * 7.5;
		for(var i=0;i<=Math.round(count);i++){
			obj.tickArray[i] = new Array(2);
			yOldValue = yValue;
			yValue = obj.orginY - (step*i)-yAdjustValue;
			//yValue = obj.orginY - (step*i);
			if(obj.type == 1){
				tickPos = "M "+xValue+" "+yValue+" h" +(endX)+"z";
			}
			else{
				tickPos = "M "+(xValue+10)+" "+yValue+" L" +(obj.orginX+obj.width-2)+" "+yValue+"z";
			}
			var tick =  obj.paper.path({stroke: "blue"}, 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);
				if(obj.type == 1){
					tickPos = "M "+(xValue+(obj.width*.2))+" "+minorYValue+" h" +(obj.width*.25)+"z";
				}else{
					tickPos = "M "+(xValue+20)+" "+minorYValue+" L" +(obj.orginX+obj.width-2)+" "+ minorYValue+"z";
				}
				var minorTicks =  obj.paper.path({stroke: "green"}, tickPos);
				obj.tickArray[i][1][j-1] = minorTicks;
			}

		}
	}

	function createNeedle(obj){
		var count = (obj.endValue-obj.startValue);
		var step = obj.height/count;
		var xValue ,yValue;
		var yOldValue;
		obj.currentNeedlePos = 0;
		xValue = obj.orginX + (obj.width*1.05);
		var adjustFactor  = (obj.height/100) * 7.5;
		yValue = (obj.orginY - adjustFactor)-(step*obj.currentNeedlePos);
		if(obj.type == 1){
			var path = "M "+xValue+" "+yValue+" L "+(xValue+10)+" "+(yValue-10)+" v "+20+ "z";
		}else{
			xValue = this.orginX + (this.width*.45);
			var path = "M "+xValue+" "+yValue+" v "+(0)+" h "+10+" v "+0+ "z";
		}
		if(obj.needle == null){
			var needle =  this.paper.path({fill: "red"}, path);
			var boxGradient = {type: "linear", dots: [{color: "#FF0000", opacity: 1}, {color: "#E41B17", opacity: .9}], vector: [0, 0, 1, 1]};
			needle.attr({gradient: boxGradient, "stroke-width": 0, "stroke-opacity": 0});
			obj.needle = needle;
		}else{
			obj.needle.attr("path",path);
		}
	}

	function zoom(zoomFactor){
		this.outerCircleRadius = this.defaultSize+((this.defaultSize/100)*zoomFactor);
	}

	function setNeedlePos(value){
		var count =  (this.endValue-this.startValue)/this.step;
		var diff = (this.endValue-this.startValue);
		//alert(count);
		var step = this.height/diff;
		var xValue ,yValue;
		var yOldValue;
		this.currentNeedlePos = value;
		xValue = this.orginX + (this.width*1.05);
		var adjustFactor  = (this.height/100) * 7.5;
		yValue = (this.orginY - adjustFactor)-(step*this.currentNeedlePos);
		
		
		if(this.type == 1){
			var path = "M "+xValue+" "+yValue+" L "+(xValue+10)+" "+(yValue-10)+" v "+20+ "z";
		}else{
		 	xValue = this.orginX + (this.width*.45);
			var adjustFactor  = (this.height/100) * 7.5;
			var newYValue = this.orginY - adjustFactor;
			yValue = newYValue-yValue;
			var path = "M "+xValue+" "+newYValue+" v "+(-yValue)+" h "+10+" v "+(yValue)+ "z";
		}
		
		if(this.needle == null){
			var needle =  this.paper.path({fill: "red"}, path);
			var boxGradient = {type: "linear", dots: [{color: "#FFFF00", opacity: 1}, {color: "#E41B17", opacity: .9}], vector: [0, 0, 1, 1]};
			needle.attr({gradient: boxGradient, "stroke-width": 1, "stroke-opacity": 1});
			this.needle = needle;
		}else{
			this.needle.attr("path",path);
		}
	}
	
	function startAnimate(){
		if(this.running)return;
		this.running=true;
		var objThis = this;
		this.anim = true;
		this.newNeedleValue = this.currentNeedlePos;
		updatePropValues(this);
			objThis.animThread = window.setInterval(function(){
			if(!objThis.anim){
				window.clearInterval(thr);
				updatePropValues(objThis);
				objThis.running = false;
			}
			var stepValue =  (objThis.endValue-objThis.startValue)/(objThis.step);
		//	var stepValue = objThis.height/(count*objThis.tickCount);
			//objThis.unit.attr("text", Math.round(objThis.currentNeedlePos));
			if((Math.round(objThis.currentNeedlePos)) != (Math.round(objThis.newNeedleValue))){
				stepValue = ""+stepValue;
				
				var stepValueLen = stepValue.length;
				var adjustVal;
				if((adjustVal = stepValue.indexOf("."))>0){
					stepValueLen = adjustVal; 
				}
				var divValue= 1;
				if(stepValueLen > 0){
					for(var j = 0;j<stepValueLen;j++){
						divValue = divValue+"0";
					}
				}
				
				divValue = divValue/1;
				
				if(objThis.newNeedleValue > objThis.currentNeedlePos){
					objThis.currentNeedlePos = objThis.currentNeedlePos + (stepValue/divValue);
					
				}else if(objThis.newNeedleValue < objThis.currentNeedlePos){
					objThis.currentNeedlePos = objThis.currentNeedlePos - (stepValue/divValue);
				}
				objThis.running = true;
				//
				objThis.setNeedlePos(objThis.currentNeedlePos);
				
			}
		},10);

		var thr = window.setInterval(function(){
			if(!objThis.anim){
				window.clearInterval(thr);
				return;
			}
			var rndPos = Math.random() * (objThis.endValue-objThis.startValue);
			rndPos = rndPos + startValue;
			
			
			//objThis.unit.attr("text", (rndPos)); 
			//var rndVal = rndPos-objThis.startValue;
			objThis.newNeedleValue = Math.round(rndPos);
		},1000);
	}
}