
pi = Math.PI/180;



function Gauge(id,canvas,orginX,orginY,signalName,gaugeName,type,scaleColor1,scaleColor2,outerScale,innerScale,startAngle,endAngle,startValue,endValue,valueStep,characterFont,characterSize,characterColor,unitText,unitTextColor,minorTicks,majorTickColor,minorTickColor,needleType,needleSize,needleColor1,needleColor2,currentNeedleAngle,specialAreaStartAngle,specialAreaEndAngle,sacolor1,sacolor2,anim){

	svgCanvasAdjust();
	this.id = id;
	this.paper = canvas;	
	
	this.orginX = orginX/1;
	this.orginY = orginY/1;

	this.signalName = signalName;
	this.name = gaugeName;
	
	this.type = type/1;

	this.backGround1 = scaleColor1;
	this.backGround2 = scaleColor2;

	this.outerCircleRadius = outerScale/1;
	this.innerCircleRadius = innerScale/1;

	this.startValue = startValue/1;
	this.endValue = endValue/1;
	
	this.startAngle =  startAngle/1;
	this.endAngle = endAngle/1;
	
	this.step = valueStep/1;

	this.characterSize = characterSize/1;
	this.characterFont = characterFont;
	this.textColor = characterColor;

	this.unitText = unitText;

	this.unitTextColor = unitTextColor;
	this.stopanimate = stopanimate;
	this.tickCount			=	(minorTicks/1)+1;  

	this.majorTickColor	=	majorTickColor;
	this.minorTickColor = minorTickColor;
	
	this.specialAreaStartAngle = specialAreaStartAngle/1;
	this.specialAreaEndAngle = specialAreaEndAngle/1;
	this.specialAreaColor1 = sacolor1;
	this.specialAreaColor2 = sacolor2;
	
	this.zoom				=	0;

	this.needleType 		= 	needleType/1;
	this.needleColor1		=	needleColor1;
	this.needleColor2		=	needleColor2;
	this.needleSize 		= 	needleSize/1;


	this.interval = "1000";
	this.debug = false;

	
	this.running = false;
	this.newNeedleAngle = endAngle/1;
	this.currentNeedleAngle = currentNeedleAngle/1;
	
	this.setSAColor = setSAColor;
	this.setSpecialAreaStartAngle = setSpecialAreaStartAngle;
	this.setSpecialAreaEndAngle = setSpecialAreaEndAngle;
	this.numArray;
	this.tickArray;

	this.outLine;

	this.needleOuterCircle;		
	this.needleInnerCircle;		

	this.needle;
	this.mark;
	this.unit;


	this.majorTickColor;
	this.minorTickColor;

	this.create = createSpeedoMeterType1;
	this.setZoom = zoom;
	this.move = move;
	this.changeType = changeType;
	//this.setNeedleStyle = setNeedleType;
	//this.setScale = setCircleRadius;
	
    this.setStartValue = setStartValue;
    this.setEndValue = setEndValue;
    this.setCharacterFont = setCharacterFont;
    this.setCharacterSize = setCharacterSize;
    this.setCharacterColor = setCharacterColor;	
    this.setNeedleType = setNeedleType;
    this.setNeedleSize = setNeedleSize;
    this.setTicksColor = setTicksColor;
    this.setMinorTicks = setMinorTicks;
    this.setPositionX = setPositionX;
    this.setPositionY = setPositionY;
//	this.setCharacterStyle=setCharacterStyle;
	this.setNeedleAngle = setNeedleAngle;
	this.setNeedleColor = setNeedleColor;
	this.setUnitText = setUnitText;
	this.setBackground = setBackground;
	this.setSteps = setSteps;
	this.setAngleRange = setAngleRange;
	this.setInnerScale = setInnerScale;
	this.setOuterScale = setOuterScale;
	this.stopAnimation = stopAnimation;
	this.setSignalName = setSignalName;
	this.setGaugeName = setGaugeName;
	
	this.deleteObject = deleteObject;
	this.showTop = showTop;
	this.highlightItem = highlightItem;
	
//	this.setTicks = setTicks;
	this.animThread;

	this.animate = startAnimate;
	this.anim = anim;
	
	function deleteObject(){
		this.needleOuterCircle.remove();
		if(this.type == 1){
			this.needleInnerCircle.remove();
			this.mark.remove();
		}
		this.needle.remove();
		this.outLine.remove();
		if(this.unit != null){
			this.unit.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(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 stopanimate()
	{
		this.anim = false;
	}
	
	function showTop(){
		this.outLine.toFront();
		if(this.type == 1){
			this.mark.toFront();
		}
		if(this.numArray != null){
			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.type == 1){
			this.needleOuterCircle.toFront();
			this.needle.toFront();
			this.needleInnerCircle.toFront();
		}else{
			this.needle.toFront();
			this.needleOuterCircle.toFront();
		}
		
		this.unit.toFront();
		if(selectedItem1 != null){
				selectedItem1.toFront();
		}
		if(selectedItem2 != null){
				selectedItem2.toFront();
		}
		if(selectedItem3 != null){
				selectedItem2.toFront();
		}
		if(selectedItem4 != null){
				selectedItem2.toFront();
		}
	}
	
	function stopAnimation(){
		this.anim = false;
	}
	function updatePropValues(o){
		if(!viewMode){
			var str = o.orginX+","+o.orginY+","+o.signalName+","+o.name+","+o.type+","+o.backGround1+","+o.backGround2+","+o.outerCircleRadius+","+o.innerCircleRadius+","+o.startAngle+","+o.endAngle+","+o.startValue+","+o.endValue+","+o.step+","+o.characterFont+","+o.characterSize+","+o.textColor+","+o.unitText+","+o.unitTextColor+","+o.tickCount+","+o.majorTickColor+","+o.minorTickColor+","+o.needleType+","+o.needleSize+","+o.needleColor1+","+o.needleColor2+","+o.currentNeedleAngle+","+o.specialAreaStartAngle+","+o.specialAreaEndAngle+","+o.specialAreaColor1+","+o.specialAreaColor2+","+o.anim;
			document.getElementById(id).value = str;
		} 
	}

	function setSignalName(signalName){
		this.signalName = signalName;
	}

	function setGaugeName(name){
		this.name = name;
	}
	
	function setBackground(color1,color2){
		this.backGround1 = color1;
		this.backGround2 = color2;
		var boxGradient = {type: "linear", dots: [{color: color1, opacity: 1}, {color: color2, opacity: .95}], vector: [0, 0, 1, 1]};
		this.outLine.attr({gradient: boxGradient, "stroke-width": 0, "stroke-opacity": 0});

		var radialGradient = {type: "linear", dots: [{color: color1, opacity: 1}, {color: color2, opacity: 1}], vector: [0, 0, 1, 1]};
		if(this.type == 2){
			this.needleOuterCircle.attr({gradient: radialGradient, "stroke-width": 1, "stroke-opacity": .9});
		}else{
			this.needleOuterCircle.attr({gradient: radialGradient, "stroke-width": 0, "stroke-opacity": 0});
		}

		if(this.needleInnerCircle != null){
			this.needleInnerCircle.attr({gradient: radialGradient, "stroke-width": 0, "stroke-opacity": 0});
		}
		updatePropValues(this);
	}

	function setAngleRange(startAngle,endAngle){
		if(startAngle != this.startAngle || endAngle != this.endAngle){
			var oldAngle = this.startAngle;
			this.startAngle =  startAngle;
			this.endAngle = endAngle;
			if(this.type == 2 && ((oldAngle > 180 && startAngle<180) || startAngle  > 180 )){
				if(startAngle<180){
					this.startAngle = 180;
					this.endAngle = 0;
				}
				createOutLineType2(this);
			}
			createNumericData(this);
			createTicks(this);
			this.needle.toFront();
	   		if(this.type == 2){
	   			this.needleOuterCircle.toFront();
	   		}
			if(this.needleInnerCircle != null){
				this.needleInnerCircle.toFront();
			}
			//updatePropValues(this);
		}
	}

//	function setTicks(count,majorTickColor,minorTickColor){
//		this.majorTickColor	=	majorTickColor; 
//		this.minorTickColor = minorTickColor;
//		this.tickCount	=	count+1;  
//		createTicks(this);
//		this.needle.toFront();
//		if(this.needleInnerCircle != null){
//			this.needleInnerCircle.toFront();
//		}
//		if(this.type ==2 && this.needleOuterCircle != null){
//			this.needleOuterCircle.toFront();
//		}
//	}
	
	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 setNeedleType(type){
		this.needleType = type;
		createNeedle(this);
	   	this.needle.toFront();
	    if(this.type == 2){
	   		this.needleOuterCircle.toFront();
	    }
	   	if(this.needleInnerCircle != null){
			this.needleInnerCircle.toFront();
	   	}
	   	updatePropValues(this);
	}
	
	function setNeedleSize(size){
	   this.needleSize = size;
	   createNeedle(this);
	   this.needle.toFront();
	   if(this.type == 2){
	   		this.needleOuterCircle.toFront();
	   }
	   if(this.needleInnerCircle != null){
			this.needleInnerCircle.toFront();
		}
		updatePropValues(this);
	}
	
	function setTicksColor(majorTickColor){
		this.majorTickColor	=	majorTickColor; 
		if(this.tickArray != null){
			for (var i = 0; i < this.tickArray.length; i++){ 
				this.tickArray[i][0].attr("stroke",majorTickColor);
			}
		}
		updatePropValues(this);
	}
	
	function setMinorTicks(count){
		if(count == 0){
			this.tickCount	=	0;  
		}else{
			this.tickCount	=	count+1;
		}  
		createTicks(this);
		this.needle.toFront();
		if(this.needleInnerCircle != null){
			this.needleInnerCircle.toFront();
		}
		if(this.type ==2 && this.needleOuterCircle != null){
			this.needleOuterCircle.toFront();
		}
		updatePropValues(this);
	}

	function setSteps(step){
		this.step = step;
		createNumericData(this);
		createTicks(this);
		this.needle.toFront();
		if(this.type == 2){
	   		this.needleOuterCircle.toFront();
	    }
		if(this.needleInnerCircle != null){
			this.needleInnerCircle.toFront();
		}
		updatePropValues(this);

	}
	
    function setStartValue(startValue){
		this.startValue = startValue;
		createNumericData(this);
		createTicks(this);
		this.needle.toFront();
	   	if(this.type == 2){
	   		this.needleOuterCircle.toFront();
	   	}
		if(this.needleInnerCircle != null){
			this.needleInnerCircle.toFront();
		}
		updatePropValues(this);
	}
	
    function setEndValue(endValue){
		this.endValue = endValue;
		createNumericData(this);
		createTicks(this);
		this.needle.toFront();
	   	if(this.type == 2){
	   		this.needleOuterCircle.toFront();
	   	}
		if(this.needleInnerCircle != null){
			this.needleInnerCircle.toFront();
		}
		updatePropValues(this);
	}
	
	
	function zoom(zoomFactor){
		svgCanvasAdjust();
		this.zoom = zoomFactor;
		this.outerCircleRadius = this.outerCircleRadius+((this.outerCircleRadius/100)*zoomFactor);
		this.innerCircleRadius = this.innerCircleRadius+((this.innerCircleRadius/100)*zoomFactor);
		this.characterSize =  this.characterSize+((this.characterSize/100)*zoomFactor);
		this.needleSize = this.needleSize+((this.needleSize/100)*zoomFactor);
		if(this.type ==1){
			createOutLineType1(this);
			specialArea(this);
		}else if(this.type == 2){
			createOutLineType2(this);
		}
		createNumericData(this);
		createTicks(this);
		createNeedle(this);
		highlightItem(this);
		updatePropValues(this);
	}


	function setUnitText(text){
		this.unitText = text;
		this.unit.attr("text", text);
		updatePropValues(this);
	}
	
	function setUnitTextColor(color){
		this.unitTextColor = color;
		this.unit.attr("fill", color);
		updatePropValues(this);
	}
	
	function setOuterScale(outer){
		svgCanvasAdjust();
		this.outerCircleRadius = outer;
		if(this.type ==1){
			createOutLineType1(this);
			specialArea(this);
		}else if(this.type == 2){
			createOutLineType2(this);
		}
		createNumericData(this);
		createTicks(this);
		createNeedle(this);
		this.highlightItem(this);
		updatePropValues(this);
	}
	
	function setInnerScale(inner){
		this.innerCircleRadius = inner;
		if(this.type ==1){
			createOutLineType1(this);
			specialArea(this);
		}else if(this.type == 2){
			createOutLineType2(this)
		}
		createNumericData(this);
		createTicks(this);
		createNeedle(this);
		this.highlightItem(this);
		updatePropValues(this);
	}


	
	function changeType(type){
		svgCanvasAdjust();
		this.type = type;
		if(this.type ==1){
			createOutLineType1(this);
			specialArea(this);
		}else if(this.type == 2){
			if(this.mark != null){
				this.mark.remove();
				this.mark = null;
			}
			if(this.needleInnerCircle != null){
				this.needleInnerCircle.remove();
				this.needleInnerCircle = null;
			}
			this.needle.toFront();

			createOutLineType2(this);
		}
		createNumericData(this);
		createTicks(this);
		createNeedle(this);
		updatePropValues(this);
	}

	function setNeedleColor(needleColor1,needleColor2){
		this.needleColor1 = needleColor1;
		this.needleColor2 = needleColor2;
		var boxGradient = {type: "linear", dots: [{color: needleColor1, opacity: 1}, {color: needleColor2, opacity: .9}], vector: [0, 0, 1, 1]};
		this.needle.attr({gradient: boxGradient, "stroke-width": 0, "stroke-opacity": 0});
		updatePropValues(this);
	}

    function setCharacterFont(font){
		this.characterFont = font;
		var fontTxt = this.characterSize+"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 setCharacterSize(size){
		this.characterSize = size;
		var fontTxt = size+"px \""+this.characterFont+"\"";
		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",this.textColor);
		}
		updatePropValues(this);
	}
	
	function setCharacterColor(color){
		this.textColor = color;
		var fontTxt = this.characterSize+"px \""+this.characterFont+"\"";
		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 move(x,y){
		svgCanvasAdjust();
		var newX = this.orginX+x;
		var newY = this.orginY+y;
		if(newX > 0 && newY > 0){
			if(newX < 760-(this.outerCircleRadius)){
				this.needleOuterCircle.translate(x,y);
				if(this.type == 1){
					this.needleInnerCircle.translate(x,y);
					this.mark.translate(x,y);
				}
				this.needle.translate(x,y);
				this.outLine.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.type == 1){
						 if(navigator.appName != "Microsoft Internet Explorer"){
							this.numArray[i][0].rotate(90-this.numArray[i][1],true);
						 }
					}else if(this.type == 2){
						
					}
				} 
				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(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 createSpeedoMeterType1(){
		if(this.type ==1){
			createOutLineType1(this);
			specialArea(this);
		}else if(this.type == 2){
			createOutLineType2(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 highlightItem(obj){
		var percent = (obj.outerCircleRadius/100);
		var oulinePath1 = "M"+(obj.orginX-(this.outerCircleRadius)-5)+","+(this.orginY-(obj.outerCircleRadius-(obj.outerCircleRadius/8)))+" v"+-(obj.outerCircleRadius/8)+" h"+((obj.outerCircleRadius/8))+"z";
		var oulinePath2 = "M"+(obj.orginX+(this.outerCircleRadius)+5)+","+(this.orginY-(obj.outerCircleRadius-(obj.outerCircleRadius/8)))+" v"+-(obj.outerCircleRadius/8)+" h"+(-(obj.outerCircleRadius/8))+"z";
		var oulinePath3 = "M"+(obj.orginX-(this.outerCircleRadius)-5)+","+(this.orginY+(percent*15))+" v"+(obj.outerCircleRadius/8)+" h"+((obj.outerCircleRadius/8))+"z";
		var oulinePath4 = "M"+(obj.orginX+(this.outerCircleRadius)+5)+","+(this.orginY+(percent*15))+" v"+(obj.outerCircleRadius/8)+" h"+(-(obj.outerCircleRadius/8))+"z";

		
		selectedItem1.attr("path",oulinePath1);
		selectedItem2.attr("path",oulinePath2);
		selectedItem3.attr("path",oulinePath3);
		selectedItem4.attr("path",oulinePath4);
		
	}

	function createOutLineType1(obj){
	
		if(obj.endAngle > obj.startAngle){
			obj.endAngle = 0;
		}
		if(obj.startAngle > 180){
			obj.startAngle = 180;
		}	
		var pointX1 = obj.orginX-obj.outerCircleRadius;
		var pointY1 = obj.orginY;
		var percent = (obj.outerCircleRadius/100);
		var rPointX2 = percent*20;
		var rPointY2 = percent*20;
		var rPointX3 = obj.orginX-(pointX1+rPointX2)-rPointY2;
		var rPointX4 = rPointY2*2;
		var rPointX5 = rPointX3
		var rPointX6 = percent*20;
		var rPointY7 = -percent*20;
		var curve = percent*18;
		var curveBottom = percent*35;
		var path = "M"+pointX1+","+pointY1+" a"+curve+","+curve+" 0 0,0 "+rPointX2+" "+rPointY2+" h"+rPointX3+" a"+curveBottom+","+curveBottom+" 0 0,0 "+rPointX4+",0 "+" h"+rPointX5+"  a"+curve+","+curve+" 0 0,0 "+rPointX6+" ,"+rPointY7+" a"+obj.outerCircleRadius+","+obj.outerCircleRadius+" 0 0,0 "+(-(obj.outerCircleRadius*2))+",0 z";
		if(obj.outLine != null){
			obj.outLine.attr("path",path)
		}else{
			var outLine =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]};
			outLine.attr({gradient: boxGradient, "stroke-width": 0, "stroke-opacity": 0});
			if(!viewMode){
				outLine.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;
					}
	      			
						
				};
				outLine.node.onmouseup = function (e) 
				{ 
						dragOK = false;
						//currentObject = null;
				};
			}
			obj.outLine = outLine;
		}
		return true;
	}
	
	function specialArea(obj){
		var angle = obj.specialAreaStartAngle;
		var theta = angle * (pi);

		x1Value = obj.orginX+(obj.outerCircleRadius*(Math.cos(theta)));
		y1Value =obj.orginY-(obj.outerCircleRadius*(Math.sin(theta)));
		
		var theta = obj.specialAreaEndAngle * (pi);

	
		x2Value = obj.orginX+(obj.outerCircleRadius*(Math.cos(theta)));
		y2Value =obj.orginY-(obj.outerCircleRadius*(Math.sin(theta)));
		path = "M"+obj.orginX+","+obj.orginY+" L "+x1Value+" "+y1Value+" a"+obj.outerCircleRadius+","+obj.outerCircleRadius+" 0 0,1 "+(x2Value-x1Value)+","+(y2Value-y1Value)+" z";
		if(obj.mark != null){
			obj.mark.attr("path",path)
		}else{
			var boxGradient = {type: "linear", dots: [{color: obj.specialAreaColor1, opacity: .8}, {color: obj.specialAreaColor2, opacity: .8}], vector: [0, 0, 1, 1]};
			
			var mark =obj.paper.path({stoke: "#41A317"},path); 
			mark.attr({gradient: boxGradient, "stroke-width": 0, "stroke-opacity": 0});
			obj.mark = mark;
			if(!viewMode){
				mark.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;
					}
						
				};
				mark.node.onmouseup = function (e) 
				{ 
						dragOK = false;
						//currentObject = null;
				};
			}
		}
	}

	function createOutLineType2(obj){
		//if(obj.orginX >= obj.outerCircleRadius){

			var radius2;

			if(obj.innerCircleRadius != null){
				radius2 = obj.innerCircleRadius;
			}else{
				radius2 = (obj.outerCircleRadius/100)*80;
			}

			var theta = obj.startAngle * (pi);

			xValue = obj.orginX+(obj.outerCircleRadius*(Math.cos(theta)));
			yValue =obj.orginY-(obj.outerCircleRadius*(Math.sin(theta)));

			var pointX1 = xValue;
			var pointY1 = yValue;

			var pointX2 = obj.orginX-obj.outerCircleRadius;
			var pointY2 = obj.orginY+(obj.outerCircleRadius/2);

			var rPointX3 = (obj.orginX-pointX1)*2;
			var rPointY3 = (obj.orginY-pointY1)*2;

			theta = obj.endAngle * (pi);

			xValue = obj.orginX+(radius2*(Math.cos(theta)));
			yValue = obj.orginY-(radius2*(Math.sin(theta)));


			var rPointX4 = -(xValue-obj.orginX)*2;
			var rPointY4 = yValue;

			var rPointY5 = pointY1- rPointY4;

			var path = "M"+pointX1+","+pointY1+" a"+obj.outerCircleRadius+","+obj.outerCircleRadius+" 0 1,1 "+rPointX3+" ,0 L "+xValue+","+yValue+" a"+radius2+","+radius2+" 0 1,0 "+rPointX4+",0 z";

			if(obj.outLine != null){
				obj.outLine.attr("path",path)
			}else{
				var outLine =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]};
				outLine.attr({gradient: boxGradient, "stroke-width": 0, "stroke-opacity": 0});
			//	outLine.attr("fill", obj.backGround);
				if(!viewMode){
					outLine.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;
							updatePropertiesBox();
						}else{
							currentObject = obj;
						}
							
					};
					outLine.node.onmouseup = function (e) 
					{ 
							dragOK = false;
							//currentObject = null;
					};
					obj.outLine = outLine;
				}
			}
			return true;
		//}
		//else{
		//	return false;
		//}
	}

	function createNumericData(obj){
		var count = Math.round((obj.endValue-obj.startValue)/obj.step);
		var angleDiffrence;
		if(obj.endAngle > obj.startAngle){
			angleDiffrence =(obj.startAngle+(360-obj.endAngle));
		}else{
			angleDiffrence = obj.startAngle - obj.endAngle;
		}
		var angleStep = (angleDiffrence/count);
		var angle = obj.startAngle;
		var numericDataRadius;
		if(obj.type == 1){
			numericDataRadius	=	(obj.outerCircleRadius/100)*85;
		}else if(obj.type == 2){
			var radius2;
			if(obj.innerCircleRadius != null){
				radius2 = obj.innerCircleRadius;
				numericDataRadius	=	(radius2/100)*80;

			}else{
				radius2 = obj.outerCircleRadius;
				numericDataRadius	=	(radius2/100)*70;
			}
		}
		
		var fontSize;
		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.numArray = null;
		}
		
		if(count<1 || obj.step < 1) return;
		obj.numArray = new Array(Math.round(count));
		for(var i=0;i<=Math.round(count);i++){
			obj.numArray[i] = new Array(2);
			var theta = angle * (pi);

			xValue = obj.orginX+(numericDataRadius*(Math.cos(theta)));
			yValue =obj.orginY-(numericDataRadius*(Math.sin(theta)));

			if(obj.characterSize != null){
				fontSize = obj.characterSize;
			}else{
				fontSize = Math.round((obj.outerCircleRadius/100)*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'});
			var color = "000000";
			if(obj.textColor != null){
				color = obj.textColor;
			}

			if(obj.type == 1){
				number.attr("fill", color);
				if(navigator.appName != "Microsoft Internet Explorer"){
					number.rotate(90-angle,true);
				}
			}else if(obj.type == 2){
				number.attr("fill", color);
			}

			obj.numArray[i][0] = number;
			obj.numArray[i][1] = angle;
		 
			angle = angle - angleStep;
			if(angle < 0){
				angle = 360 + angle;
			}
			if(numValue != obj.endValue){
				obj.endValue = numValue;
			}
			
		}
	}

	function createTicks(obj){
		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;
					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.unit != null){
			obj.unit.remove();
			obj.unit = null;
		}

		var count = Math.round((obj.endValue-obj.startValue)/obj.step);
		var tickX1,tickY1,tickX2,tickY2,tickAltRadius;

		var tickRadius1;
		var tickRadius2;
		

		var angleDiffrence;
		if(obj.endAngle > obj.startAngle){
			angleDiffrence =(obj.startAngle+(360-obj.endAngle));
		}else{
			angleDiffrence = obj.startAngle - obj.endAngle;
		}
		var angleStep = (angleDiffrence/count);
		var angle = obj.startAngle;

		if(obj.type == 1){
			tickRadius1		=	(obj.outerCircleRadius/100)*80;
			tickRadius2		=	(obj.outerCircleRadius/100)*75;
			tickAltRadius = (obj.outerCircleRadius/100)*78;
		}else if(obj.type == 2){
			tickRadius1		=	(obj.outerCircleRadius/100)*90;
			tickAltRadius = (obj.outerCircleRadius/100)*85;
			if(obj.innerCircleRadius != null){
				tickRadius2		=	(obj.innerCircleRadius);
			}else{
				tickRadius2 = (obj.outerCircleRadius/100)*81;
			}
		}
		var prevAngle;
		if(count<1 || obj.step < 1) return;
		obj.tickArray = new Array(Math.round(count));
		var step = (angleStep)/obj.tickCount;
		for(var i=0;i<=Math.round(count);i++){
			obj.tickArray[i] = new Array(2);
			var theta = angle * (pi);

			var cosValue = Math.cos(theta);
			var sinValue = Math.sin(theta);

			tickX1 = obj.orginX+(tickRadius1*(cosValue));
			tickX2 = obj.orginX+(tickRadius2*(cosValue));
			tickY1 = obj.orginY-(tickRadius1*(sinValue));
			tickY2 = obj.orginY-(tickRadius2*(sinValue));
			var unitTextY = tickY2;

			var tickPos = "M "+tickX1+" "+tickY1+" L" +tickX2+" "+tickY2+"z";
			var tick =  obj.paper.path({stroke: obj.majorTickColor,opacity: .75,"stroke-width": 1.5}, tickPos);
			tick.toFront();
			obj.tickArray[i][0] = tick;
			obj.tickArray[i][1] = new Array(obj.tickCount);
			
			var tickAngle = (prevAngle > obj.startAngle)?prevAngle-360:prevAngle;
			
			for(var j = 1;i > 0 && j<obj.tickCount;j++){
				var tAngle = tickAngle - (step*j);
				var reqRadius;
				//if(obj.type == 2){
					reqRadius = tickAltRadius;
				//}else{
				//	reqRadius = tickRadius1;
				//}
				tAngle = (tAngle < 0)?360+tAngle:tAngle;
				theta = tAngle * (pi);
				cosValue = Math.cos(theta);
				sinValue = Math.sin(theta);

				tickX1 = obj.orginX+(reqRadius*(cosValue));
				tickX2 = obj.orginX+(tickRadius2*(cosValue));
				tickY1 = obj.orginY-(reqRadius*(sinValue));
				tickY2 = obj.orginY-(tickRadius2*(sinValue));
				tickPos = "M "+tickX1+" "+tickY1+" L" +tickX2+" "+tickY2+"z";
				tick =  obj.paper.path({stroke: obj.minorTickColor,opacity: .75,"stroke-width": 1.5}, tickPos);
				obj.tickArray[i][1][j-1] = tick;
				tick.toFront();
			}
			prevAngle = angle;
			angle = angle - angleStep;
			if(angle < 0){
				angle = 360 + angle;
			}
		}

		var xValueUnit = obj.orginX+(obj.outerCircleRadius/100)*50;
		var yValueUnit = unitTextY+15 ;
		//fontSize = Math.round((obj.outerCircleRadius/100)*8);
	
		var fontTxt = obj.characterSize+"px \""+obj.characterFont+"\"";
		var number = obj.paper.text(xValueUnit, yValueUnit, ""+obj.unitText).attr({"font": fontTxt, opacity: 1});
		number.attr("fill", obj.unitTextColor);

		obj.unit = number;
	}

	function setSpecialAreaStartAngle(val)
	{
		this.specialAreaStartAngle = val;
		specialArea(this);
		updatePropValues(this);
//		this.needle.toFront();
	}
	
	function setSpecialAreaEndAngle(val)
	{
		this.specialAreaEndAngle = val;
		specialArea(this);
		updatePropValues(this);
	}
	
	function setSAColor(col1, col2)
	{
		this.specialAreaColor1 = col1;
		this.specialAreaColor2 = col2;
		var boxGradient = {type: "linear", dots: [{color: this.specialAreaColor1, opacity: .8}, {color: this.specialAreaColor2, opacity: .8}], vector: [0, 0, 1, 1]};
		this.mark.attr({gradient: boxGradient, "stroke-width": 0, "stroke-opacity": 0})
		updatePropValues(this);
	}
	
	function createNeedle(obj){
			var percent = (obj.outerCircleRadius/100);
			var outerCircle;
			var innerCircle;
			if(obj.type == 1){
				outerCircle = percent*15;
				innerCircle = percent * 5;
				if(obj.needleInnerCircle != null){
					obj.needleInnerCircle.remove();
				}
			}else if(obj.type == 2){
				outerCircle = percent*30;
			}

			if(obj.needleOuterCircle != null){
				obj.needleOuterCircle.remove();
			}
			
			if(obj.needle != null){
				obj.needle.remove();
				obj.needle = null;
			}
			var circleNeedleOrginCircleOuter = obj.paper.circle(obj.orginX, obj.orginY, outerCircle);
			obj.needleOuterCircle = circleNeedleOrginCircleOuter;
			var radialGradient;
			if(obj.type == 1){
				//circleNeedleOrginCircleOuter.attr("fill", "blue");
				var circleNeedleOrginCircleInner = obj.paper.circle(obj.orginX, obj.orginY, innerCircle);
				obj.needleInnerCircle = circleNeedleOrginCircleInner;
				radialGradient = {type: "radial", dots: [{color: obj.backGround2, opacity: 1}, {color: obj.backGround1, opacity: 1}], vector: [0, 0, 1, 1]};
				circleNeedleOrginCircleOuter.attr({gradient: radialGradient, "stroke-width": 0, "stroke-opacity": 0});
				circleNeedleOrginCircleInner.attr({gradient: radialGradient, "stroke-width": 0, "stroke-opacity": 0});
			}else if(obj.type == 2){
				radialGradient = {type: "linear", dots: [{color: obj.backGround1, opacity: 1}, {color: obj.backGround2, opacity: 1}], vector: [0, 0, 1, 1]};
				circleNeedleOrginCircleOuter.attr({gradient: radialGradient, "stroke-width": 1, "stroke-opacity": .9});
			}
			
			needleAngle = obj.currentNeedleAngle;
			var theta;
			var smallradius;
			smallradius =obj.needleSize;
			if(obj.needleType == 2 ){
				theta =  (needleAngle-smallradius) * pi;		
			}else if(obj.needleType == 1 ){
				theta =  (needleAngle) * pi;
				
			}
			var xValue = Math.cos(theta);
			var yValue = Math.sin(theta);
			var xSecond;
			var ySecond;
			var x2Value;
			var y2Value;
			var radius	=	(obj.outerCircleRadius/100)*85;

			xValue =obj.orginX+((radius*(xValue)));

			yValue =obj.orginY-((radius*(yValue)));
				
			if(obj.needleType == 2 ){
				theta = (needleAngle+(smallradius/4)) * pi;
				x2Value = Math.cos(theta);
				y2Value = Math.sin(theta);

				x2Value =obj.orginX+(radius*(x2Value));
				y2Value =obj.orginY-(radius*(y2Value));

				var xd,yd;
				if(xValue > x2Value){
					xd = xValue-x2Value;
				}else{
					xd = x2Value-xValue;
				}
				if(yValue > y2Value){
					yd = yValue-y2Value;
				}else{
					yd = y2Value-yValue;
				}
				var distance = Math.sqrt(xd*xd + yd*yd);
				smallradius = distance/2;

			}else{
			}
			
			var percent = (obj.outerCircleRadius/100);
			var deflectionAngle = (needleAngle+90) * pi;
			
			var x1 = Math.cos(deflectionAngle);
			var y1 = Math.sin(deflectionAngle);

			if(x1 < 0){
				x1 =obj.orginX-((smallradius*Math.abs(x1)));
				var xDiff = obj.orginX - x1;
				x2=obj.orginX+xDiff;
			}else{
				x1 =obj.orginX+((smallradius*(x1)));
				if(x1 == obj.orginX){
					x2 = x1;
				}else{
					var xDiff = x1 - obj.orginX;
					x2 = obj.orginX-xDiff;
				}
			}

			if(y1 < 0){
				y1 =obj.orginY+((smallradius*Math.abs(y1)));
				var yDiff = y1 - obj.orginY;
				y2=obj.orginY-yDiff;

			}else{
				y1 =obj.orginY-((smallradius*(y1)));
				if(y1 == obj.orginY){
					y2 = y1;
				}else{
					var yDiff = obj.orginY-y1;
					y2=obj.orginY+yDiff;
				}
			}
			var needlePos;
			if(obj.needleType == 2 ){
				needlePos = "M "+x1+" "+y1+" L" +x2+" "+y2+" L"+xValue+" "+ yValue+" L"+x2Value+" "+y2Value+" z";
			}else{
				needlePos = "M "+x1+" "+y1+" L" +x2+" "+y2+" L"+xValue+" "+ yValue+" z";
			}
			
			needle =  obj.paper.path({stroke: "#036"}, needlePos);
			var boxGradient = {type: "linear", dots: [{color: obj.needleColor1, opacity: 1}, {color: obj.needleColor2, opacity: .9}], vector: [0, 0, 1, 1]};
			needle.attr({gradient: boxGradient, "stroke-width": 0, "stroke-opacity": 0});
			obj.needle = needle;
			if(obj.type == 1){
				circleNeedleOrginCircleInner.toFront();
			}else if(obj.type == 2){
				circleNeedleOrginCircleOuter.toFront();
			}
	}


	function setNeedleAngle(angle){
		this.currentNeedleAngle = angle;
		if(angle < 0){
			angle = (360+angle);
		}
		if(this.debug == true){
			var totalAngle;
			if(this.startAngle < this.endAngle){
				totalAngle = this.startAngle + (360-this.endAngle);
			}else{
				totalAngle = this.startAngle - this.endAngle;
			}
			var varDiffValue = this.endValue-this.startValue

			var step = totalAngle/varDiffValue;
			
			
			var reqValue = roundNumber(((this.startAngle-angle)/step)+this.startValue,4);

			this.unit.attr("text",reqValue); 

		}
		var needleAngle = angle;
		var percent = (this.outerCircleRadius/100);
		var theta;
		var smallradius;
		smallradius =this.needleSize;
		if(this.needleType == 2 ){
			theta =  (needleAngle-smallradius) * pi;		
		}else if(this.needleType == 1 ){
			theta =  (needleAngle) * pi;
		}

		var xValue = Math.cos(theta);
		var yValue = Math.sin(theta);
		var xSecond;
		var ySecond;
		var x2Value;
		var y2Value;

		var radius	=	(this.outerCircleRadius/100)*82;

		xValue =this.orginX+(radius*(xValue));
		yValue =this.orginY-(radius*(yValue));

		if(this.needleType == 2 ){
			theta = (needleAngle+(smallradius/4)) * pi;
			x2Value = Math.cos(theta);
			y2Value = Math.sin(theta);

			x2Value =this.orginX+(radius*(x2Value));
			y2Value =this.orginY-(radius*(y2Value));

			var xd,yd;
			if(xValue > x2Value){
				xd = xValue-x2Value;
			}else{
				xd = x2Value-xValue;
			}
			if(yValue > y2Value){
				yd = yValue-y2Value;
			}else{
				yd = y2Value-yValue;
			}
			var distance = Math.sqrt(xd*xd + yd*yd);
			smallradius = distance/2;
		}else{
		}
			
		var percent = (this.outerCircleRadius/100);
		var deflectionAngle = (needleAngle+90) * pi;	

		var x1 = Math.cos(deflectionAngle);
		var y1 = Math.sin(deflectionAngle);
		var xDiff,yDiff;
		if(x1 < 0){
			x1 = this.orginX-((smallradius*Math.abs(x1)));
			xDiff = this.orginX - x1;
			x2=this.orginX+xDiff;
					
		}else{

			x1 =this.orginX+((smallradius*(x1)));
			if(x1 == this.orginX){
				x2 = x1;
			}else{
				xDiff = x1 - this.orginX;
				x2 = this.orginX-xDiff;
			}
		}

		if(y1 < 0){
			y1 =this.orginY+((smallradius*Math.abs(y1)));
			yDiff = y1 - this.orginY;
			y2=this.orginY-yDiff;

		}else{
			y1 =this.orginY-((smallradius*(y1)));
			if(y1 == this.orginY){
				y2 = y1;
			}else{
				yDiff = this.orginY-y1;
				y2=this.orginY+yDiff;
			}
		}

		var needlePos;
		if(this.needleType == 2 ){
			needlePos = "M "+x1+" "+y1+" L" +x2+" "+y2+" L"+xValue+" "+ yValue+" L"+x2Value+" "+y2Value+" z";
		}else{
			needlePos = "M "+x1+" "+y1+" L" +x2+" "+y2+" L"+xValue+" "+ yValue+" z";
		}
		this.needle.attr("path",needlePos);


	}

	function startAnimate(){
		if(this.running)return;
		this.running=true;
		var objThis = this;
		this.anim = true;
		updatePropValues(this);
		objThis.animThread = window.setInterval(function(){
			if(!objThis.anim || this.step == 0){
				window.clearInterval(objThis.animThread);
				objThis.running = false;
				updatePropValues(objThis);
				return;
			}
			if(Math.round(objThis.currentNeedleAngle) != Math.round(objThis.newNeedleAngle)){
				var angleDiffrence;
				if(objThis.endAngle > objThis.startAngle){
					angleDiffrence =(objThis.startAngle+(360-objThis.endAngle));
				}else{
					angleDiffrence = objThis.startAngle - objThis.endAngle;
				}
				var count = (objThis.endValue-objThis.startValue)/objThis.step;
				var stepValue;
				stepValue = (angleDiffrence)/((count*objThis.tickCount));
				
				if(objThis.newNeedleAngle >  objThis.startAngle){
					objThis.newNeedleAngle = objThis.newNeedleAngle-360;
				}
				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.newNeedleAngle > objThis.currentNeedleAngle){
					objThis.currentNeedleAngle = objThis.currentNeedleAngle + (stepValue/divValue);
				}else if(objThis.newNeedleAngle < objThis.currentNeedleAngle){
					objThis.currentNeedleAngle = objThis.currentNeedleAngle - (stepValue/divValue);
					
				}

				objThis.running = true;
				objThis.setNeedleAngle(objThis.currentNeedleAngle);

			}
		},1);

		var thr = window.setInterval(function(){
			if(!objThis.anim){
				window.clearInterval(thr);
				return;
			}
			var value = (Math.random() * (objThis.endValue-objThis.startValue))+objThis.startValue;
			
			//objThis.unit.attr("text", roundNumber(value,4)); 
			var totalAngle;
			if(objThis.startAngle < objThis.endAngle){
				totalAngle = objThis.startAngle + (360-objThis.endAngle);
			}else{
				totalAngle = objThis.startAngle - objThis.endAngle;
			}
			var varDiffValue = objThis.endValue-objThis.startValue;
			
			var nRndNum = varDiffValue-(value-objThis.startValue);
			
//			objThis.unit.attr("text", roundNumber(value,4)); 
			var step = totalAngle/varDiffValue;
			//alert(varDiffValue);
			var reqAngle = objThis.endAngle+((nRndNum) * step);

			objThis.newNeedleAngle = reqAngle;

		//	window.clearInterval(thr);
			
			
				/*var rndNum = (Math.random() * (objThis.endValue-objThis.startValue))+objThis.startValue;
				objThis.unit.attr("text", roundNumber(rndNum,4)); 
				var totalAngle;
				if(objThis.startAngle < objThis.endAngle){
				totalAngle = 180 + (360-objThis.endAngle);
				}else{
				totalAngle = objThis.startAngle - objThis.endAngle;
				}
				var varDiffValue = objThis.endValue-objThis.startValue
				var step = totalAngle/varDiffValue;
				var reqAngle = rndNum * step;
				objThis.newNeedleAngle = reqAngle;*/
		},2000);
	}
	
	
}
