
function getObject(name) {
	if(document.getElementById) return document.getElementById(name);
	else if(document.all) return document.all[name];
	else if(document.layers) return document.layers[name];
}

function ArivaPushChart(div, quoteStyle, zeitraum) {

	this.data = [];

	this.lastTimeUnix = 0;
	this.lastTimeDraw = 0;
	this.maxDataPoints = 600;
	this.count = 0;
	this.gr = new jsGraphics(div);

	this.colorLine = "#ff0000";
	this.colorLabels = "#000000";
	this.colorBorder = "#eeeeee";
	this.colorVertLines = "#eeeeee";
	this.colorHorzLines = "#eeeeee";

	this.marginTop = 0;
	this.marginBottom = 20;
	this.marginLeft = 60;
	this.marginRight = 0;
	this.lastTime = 0;
	this.lastQuote = 0;


	this.showLastMinutes = zeitraum;
	this.timeTicksEverySeconds = zeitraum*10;

	if ( quoteStyle == "b" ) {
		this.quoteFieldTime = "T";
		this.quoteFieldPrice = "b";
	} else if ( quoteStyle == "a" ) {
		this.quoteFieldTime = "T";
		this.quoteFieldPrice = "a";
	} else if ( quoteStyle == "p" ) {
		this.quoteFieldTime = "t";
		this.quoteFieldPrice = "p";
	}

	this.add = add;
	function add(q, update) {

		if ( q["T"] && q["T"]>this.lastTime ) {
			this.lastTime = q["T"];
		}
		if ( q["t"] && q["t"]>this.lastTime ) {
			this.lastTime = q["t"];
		}

		if ( (this.quoteFieldPrice == "a" || this.quoteFieldPrice == "b") && ! q["T"] ) {
			q["T"] = this.lastTime;
		}

		if ( q[this.quoteFieldTime] &&  q[this.quoteFieldPrice] ) {
			q["tUnix"] = this.timeToUnix(q["t"]);
			q["TUnix"] = this.timeToUnix(q["T"]);
			
			// aktueller Kurs und letzter Kurs
			var currQuote = q[this.quoteFieldPrice];
			if (this.data.length == 0) {
				this.lastQuote = currQuote;
			}
			
			// bis 10 Min => jede Sek zeichnen, 30 Min => alle 3 Sek. usw.
			if (q[this.quoteFieldTime+"Unix"]) {
				this.lastTimeUnix = q[this.quoteFieldTime+"Unix"];
				if (q[this.quoteFieldTime+"Unix"]-this.lastTimeDraw >= zeitraum/10) {
					this.lastTimeDraw = q[this.quoteFieldTime+"Unix"];
					// bei Ausreissern wird Darstellung des Charts zerstoert 
					// Wert muss zwischen 20% und 500% des letzten Wertes liegen 
					if (currQuote > (this.lastQuote / 5) && currQuote < (this.lastQuote * 5)) {
						this.data.push(q);
						this.lastQuote = q[this.quoteFieldPrice];
					}
				}
			}
			
			if ( this.data.length > this.maxDataPoints ) {
				this.data.shift();
			}

			if ( update ) {
				this.updateScales();
				this.paint();
			}
		}

	}

	this.unixToTime = unixToTime;
	function unixToTime(ut) {
		var ss = ut%60;
		ut = Math.floor(ut/60);
		var mm = ut%60;
		var hh= Math.floor(ut/60);
	
		var tUnix = "20070101"+
			(hh>9?hh:"0"+hh)+
			(mm>9?mm:"0"+mm)+
			(ss>9?ss:"0"+ss);
		return tUnix;

	}

	this.timeToUnix = timeToUnix;
	function timeToUnix(t) {
		var time = parseFloat(t);

//		var ye = Math.floor(time%1000000000000 / 10000000000);	
//		var mo = Math.floor(time%10000000000 / 100000000);	
//		var da = Math.floor(time%100000000 / 1000000);	
		var hh = Math.floor(time%1000000 / 10000);	
		var mm = Math.floor(time%10000 / 100);	
		var ss = Math.floor(time%100 / 1);	

		var tUnix = hh*3600+mm*60+ss;
		return tUnix;

	}

	this.paint = paint;
	function paint() {
		var gr = this.gr;
		gr.clear();
		this.drawScaleX();
		this.drawScaleY();
		if ( this.colorBorder ) {
			gr.setColor(this.colorBorder);
			gr.drawRect(this.marginLeft, this.marginTop, this.width, this.height);
		}
		this.drawChart();
 		gr.paint();
	}

	this.drawChart = drawChart;
	function drawChart() {

		var gr = this.gr;

		gr.setColor(this.colorLine);

		var xOld = -1;
		var yOld = -1;
		var tOld = -1;
		for ( idx in this.data ) {
			var v = this.getValue(idx);
			var t = this.getTimeUnix(idx);
			if ( v>0 && t>0 && t > this.tMin && t > tOld) {
				var xPos = this.xScale(t);
				var yPos = this.yScale(v);

				if ( xOld != -1 ) {	
//					gr.drawLine(xOld,yOld,xPos, yPos);
					gr.drawLine(xOld,yOld,xPos, yOld);
					gr.drawLine(xPos,yOld,xPos, yPos);
				} else {
					gr.drawRect(xPos-1, yPos-1,2,2);
				}
				
				xOld = xPos; yOld = yPos; tOld = t;
			}
		}


	}

	this.getValue = getValue;
	function getValue(idx) {
		return parseFloat(this.data[idx][this.quoteFieldPrice]);
	}

	this.getTimeUnix = getTimeUnix;
	function getTimeUnix(idx) {
		return parseFloat(this.data[idx][this.quoteFieldTime+"Unix"])
	}

	this.getTime = getTime;
	function getTime(idx) {
		return parseFloat(this.data[idx][this.quoteFieldTime]);
	}


	this.drawScaleX = drawScaleX;
	function drawScaleX() {
		var gr = this.gr;
		var labelWidth = 60;

		if ( this.timeTicksEverySeconds ) {
			var ix = 0;
			var t = Math.floor(this.tMin/30)*30;
			var oldX = this.marginLeft; 
			while ( t < this.tMax && ix<30 ) {
				var x = this.xScale(t);
				if ( x-oldX > labelWidth && x+(labelWidth/2)<this.marginLeft+this.marginRight+this.width ) {
					gr.setColor(this.colorLabels);
					gr.drawStringRect(this.formatTime(this.unixToTime(t)),x-(labelWidth/2),this.height+this.marginTop+4,labelWidth,"center");			
					if ( this.colorVertLines ) {
						gr.setColor(this.colorVertLines);
						gr.drawLine(x,this.marginTop,x,this.marginTop+this.height);
					}
					xOld = x; 	
				}
				t += this.timeTicksEverySeconds;
				ix++;
			}

		} else {
	
			var oldX = this.marginLeft; 
			for ( idx in this.data ) {
				var t = this.getTimeUnix(idx);
				var tf = this.getTime(idx);
				var v = this.getValue(idx);
				var x = this.xScale(t);

				if ( v>0 && x-oldX > labelWidth && x+(labelWidth/2)<this.marginLeft+this.marginRight+this.width) {

					gr.setColor(this.colorLabels);
					gr.drawStringRect(this.formatTime(tf),x-(labelWidth/2),this.height+this.marginTop+4,labelWidth,"center");			
					if ( this.colorVertLines ) {
						gr.setColor(this.colorVertLines);
						gr.drawLine(x,this.marginTop,x,this.marginTop+this.height);
					}

					oldX = x;
				}
			}
		}

	}

	this.formatTime = formatTimeHHMMSS;
	function formatTimeHHMMSS(t) {
		t = ""+t;
		return t.substring(8,10)+":"+t.substring(10,12)+":"+t.substring(12,14);
	}

	this.formatValueD = formatValueD;
	this.formatValue = formatValue;
	function formatValueD(v) {
		return v>999 ? Math.round(v) : Math.round(v*100)/100;
	}
	function formatValue(v) {
		return v>999 ? Math.round(v) : this.formatNum(v,2);
	}

	this.calcSkipY = calcSkipY;
	function calcSkipY(min,max,count) {
		var st = (max-min) / count;
		if ( st < 0.01 ) {
			st = 0.01;
		} else if ( st < 0.05 ) {
			st = 0.05;
		} else if ( st < 0.1 ) {
			st = 0.1;
		} else if ( st < 0.25 ) {
			st = 0.25;
		} else if ( st < 0.5 ) {
			st = 0.5;
		} else if ( st < 1 ) {
			st = 1;
		} else if ( st < 2 ) {
			st = 2;
		} else if ( st < 5 ) {
			st = 5;
		} else if ( st < 10 ) {
			st = 10;
		} else if ( st < 25 ) {
			st = 25;
		} else if ( st < 50 ) {
			st = 50;
		} else if ( st < 100 ) {
			st = 100;
		} else if ( st < 200 ) {
			st = 200;
		}
		return st;
	}

	this.drawScaleY = drawScaleY;
	function drawScaleY() {
		var gr = this.gr;
		var countTicks = this.height/40;
		var labelHeight = 15;

	  if ( countTicks<1 || this.vMin==this.vMax ) return;

		var st = this.ySkip;

		var yOld = -999;
		var vfOld = -999;
		var idx = 0;

		var xx = function(t,v) {
			v = t.formatValueD(v);
			var y = t.yScale(parseFloat(v));
			if ( Math.floor(y) == Math.floor(yOld) ) return;
			var vf = t.formatValue(v)
			if ( vf != vfOld && y>=0 && y <= t.marginTop + t.height + t.marginBottom) {
				gr.setColor(t.colorLabels);
				gr.drawStringRect(vf,0,y-labelHeight/2,t.marginLeft-4,"right");			
				if ( t.colorHorzLines ) {
					gr.setColor(t.colorHorzLines);
					gr.drawLine(t.marginLeft,y,t.marginLeft+t.width,y);
				}
			}

			yOld = y; 
			vfOld = vf;
			idx++;

		}

		var v = this.vMin;
		while ( v <= this.vMax ) {
			xx(this,v);
			v += st;
		}
//		xx(this,this.vMax);
	}

	this.updateScales = updateScales;
	function updateScales() {


		var minV = 99999;
		var maxV = -99999;

		var minT = 20991231232323;
		var maxT = 0;
		for ( idx in this.data ) {
			var t = this.getTimeUnix(idx);
			var v = this.getValue(idx);
			if ( this.lastTimeUnix-t <= this.showLastMinutes*60 || ! this.showLastMinutes ) {
				if ( v > 0  ) {
					if ( v < minV ) minV = v;
					if ( v > maxV ) maxV = v;
				}
			}
			if ( t > 0 ) {
				if ( t < minT ) minT = t;
				if ( t > maxT ) maxT = t;
			}
		}

		if ( this.showLastMinutes ) {
			minT = maxT - 60 * this.showLastMinutes;
		}

		if ( minV == maxV ) {
			minV *= 0.995;
			maxV *= 1.005;
		}

		var ySkip = this.calcSkipY(minV, maxV, 10);
		if ( maxV > 999 && ySkip < 1 ) {
			ySkip = 1;
		}

		minV = Math.floor(minV/ySkip)*ySkip;
		maxV = Math.ceil(maxV/ySkip)*ySkip;

		this.ySkip = ySkip;

		if ( minT == maxT ) {
			minT *= 0.95;
			maxT *= 1.05;
		}

		this.vMin = minV;
		this.vMax = maxV;
		this.tMin = minT;
		this.tMax = maxT;

		this.width = 0+parseFloat(this.gr.cont.style.width)-this.marginLeft-this.marginRight;
		this.height = 0+parseFloat(this.gr.cont.style.height)-this.marginTop-this.marginBottom;

		this.yScaleFactor = this.height / (maxV-minV);
		this.xScaleFactor = this.width / (maxT-minT);

	}

	this.xScale = xScale;
	function xScale(t) {
		return this.marginLeft + (t - this.tMin) * this.xScaleFactor;
	}

	this.yScale = yScale;
	function yScale(val) {
		return this.marginTop + this.height - (val - this.vMin) * this.yScaleFactor;
	}

	this.formatNum = formatNum;
	function formatNum(value, stellen) {

		stellen = stellen ? stellen : 2;
		value = ""+value;
		
		var pointidx = value.indexOf(".");

		var before = "";
		var after = "";

		if ( pointidx == -1 ) {
			before = value;
			after = "0";
		} else {
			before = value.substr(0,pointidx);
			after = value.substr(pointidx+1);
		}

		after = after.substr(0,stellen);
		while ( after.length < stellen ) {
			after += "0";
		}

		var num = "";
		var tidx = before.length;
		var ct = 0;
		while (tidx > 0 ) {
			ct++;
			num = before.substr(tidx-1,1) + num;
			if ( ct == 3 && tidx>1 ) {
			    num = "."+num;
			    ct=0;
			}
			tidx--;
		}

		if ( stellen == 0 ) {
			return num;
		} else {
			return num+","+after;
		}

	}

	this.quoteUpdate = quoteUpdate;
	function quoteUpdate(q) {
		this.add(q,1);
	}

}

