


if(typeof Graph == 'undefined')
	Graph = {};


function startGraph(graph, xmlpath)
{
	var grph = GraphCreate(graph);
	
	grph.loadXml(xmlpath);
}


function GraphCreate(graph)
{
	if(typeof graph == 'string')
		graph = $(graph);
	Object.extend(graph, new Graph.Graph() );
	
	return graph;
};


Graph.Graph = Class.create();
Object.extend(Graph.Graph.prototype, {
	initialize: function()
	{
//		this.backimg = new Image();
//		this.backimg.src = 'img/graph/indication_back.gif';
//		this.barimg = new Image();
//		this.barimg.src = 'img/graph/indication_bar.gif';
		this.barw = 17;
		this.gx = 79;
		this.gy = 7;
		this.gw = 500;
		this.gh = 300;
		this.Vectors = new Array();
		this.positionsExists = {};
	},
	
	setVector: function(position, logicaly, type, starty, endy, dy, numbervisible)
	{
		var vector = {};
		vector.position = position;
		vector.logicaly = logicaly;
		vector.type = type;
		vector.starty = starty;
		vector.endy = endy;
		vector.dy = dy;
		vector.numbervisible = numbervisible;
		vector.points = new Array();
		
		this.Vectors.push(vector);
		
		//縦座標の描画.
		if(typeof this.positionsExists[position] == 'undefined')
		{
			if(position == "left")
				var x = parseInt(this.gx) - 50 - 8;
			else
				var x = parseInt(this.gx) + parseInt(this.gw) + 8;
			for(var hi=parseInt(vector.starty);hi<=parseInt(vector.endy);hi+=parseInt(vector.dy))
			{
				var h = parseInt(this.gh)*(hi-parseInt(vector.starty))/(parseInt(vector.endy)-parseInt(vector.starty));
				var y = parseInt(this.gy)+parseInt(this.gh)-h - 8;
				var obj = document.createElement('div');
				Object.extend(obj, Element);
				obj.innerHTML = this.numberF(""+hi);
				obj.style.position = "absolute";
				obj.style.fontSize = "12px";
				obj.style.width = "50px";
				if(position == "left")
					obj.style.textAlign = "right";
				else
					obj.style.textAlign = "left";
//			obj.style.border = '1px solid #FF0000';
				obj.style.left = x+"px";
				obj.style.top = y + 4 +"px";
				document.getElementById(this.id).appendChild(obj);
			}
			
			if(position == "left")
				x = x - 12;
			else
				x = x - 35;
			
			//縦座標の表題の描画.
			var h = parseInt(this.gh)*(hi-parseInt(vector.starty))/(parseInt(vector.endy)-parseInt(vector.starty));
			var y = parseInt(this.gy)+parseInt(this.gh)-h - 8;
			var obj = document.createElement('div');
			Object.extend(obj, Element);
			obj.innerHTML = vector.logicaly;
			obj.style.position = "absolute";
			obj.style.fontSize = "12px";
			obj.style.width = "100px";
			obj.style.lineHeight = "15px";
//		obj.style.border = '1px solid #FF0000';
			obj.style.textAlign = "center";
			obj.style.left = x+"px";
			obj.style.top = (parseInt(this.gy) - 26) +"px";
//			obj.style.top = ((parseInt(this.gh)/2) +parseInt(this.gy) -vector.logicaly.length*15/2) +"px";

			document.getElementById(this.id).appendChild(obj);
		}
		
		//横座標の表題の描画.
		
		if(this.Vectors.length-1 == 0)
		{
			var obj = document.createElement('div');
			Object.extend(obj, Element);
			obj.innerHTML = this.logicalx;
			obj.style.position = "absolute";
			obj.style.fontSize = "14px";
			obj.style.width = "100px";
			obj.style.textAlign = "center";
			obj.style.left = ((parseInt(this.gw)/2) +parseInt(this.gx) -100/2) +"px";
			obj.style.top = (parseInt(this.gy) +parseInt(this.gh) + 30 ) +"px";
			document.getElementById(this.id).appendChild(obj);
		}
		
		this.positionsExists[position] = 1;
		
		return this.Vectors.length-1;
	},
	
	draw: function()
	{
		var ctx = document.getElementById(this.id+"_canvas").getContext('2d');
		
		//背景の描画.
		ctx.drawImage($("kaiji_back"), 0, 0, this.gw, this.gh, this.gx, this.gy, this.gw, this.gh);
//		ctx.drawImage(this.backimg, 0, 0, this.gw, this.gh, this.gx, this.gy, this.gw, this.gh);
		
		var wnum = 0;
		for(var vi=0;vi<this.Vectors.length;vi++)
		{
			if(wnum <= this.Vectors[vi].points.length)
				wnum = this.Vectors[vi].points.length;
		}
		
		var nwidth = this.gw / (wnum);
		
		for(var vi=0;vi<this.Vectors.length;vi++)
		{
			var oldx = null;
			var oldy = null;
			for(var pi=0;pi<this.Vectors[vi].points.length;pi++)
			{
				//位置.
				var x = Math.round(parseInt(this.gx) + (nwidth * (pi+0.5)));
				if(this.Vectors[vi].points[pi].value)
				{
					var h = Math.round(parseInt(this.gh)*(parseInt(this.Vectors[vi].points[pi].value)-parseInt(this.Vectors[vi].starty))/(parseInt(this.Vectors[vi].endy)-parseInt(this.Vectors[vi].starty)));
					var y = Math.round(parseInt(this.gy)+parseInt(this.gh)-h);
					try{
						switch(this.Vectors[vi].type)
						{
							case "bar":
								var t1 = parseInt(this.Vectors[vi].points[pi].value);
	//							ctx.drawImage(this.barimg, 0,0,parseInt(this.barw), 1, x - parseInt(this.barw)/2, y, parseInt(this.barw), h);
								ctx.drawImage($("kaiji_bar"), 0,0,parseInt(this.barw), 1, x - parseInt(this.barw)/2, y, parseInt(this.barw), h);
								break;
								
							case "line":
								ctx.fillStyle = "#FF0000";
								ctx.strokeStyle = "#FF0000";
								
								
								if(oldx != null)
								{
									ctx.beginPath();
									ctx.lineWidth = 2;
									ctx.moveTo(oldx, oldy);
									ctx.lineTo(x,y);
									ctx.stroke();
								}
								
								ctx.beginPath();
								ctx.arc(x, y, 3.5, 0, 1, true);
								ctx.fill();
								
								break;
								
							case "square":
								ctx.fillStyle = "#495F99";
								ctx.strokeStyle = "#495F99";
								
								
								if(oldx != null)
								{
									ctx.beginPath();
									ctx.lineWidth = 2;
									ctx.moveTo(oldx, oldy);
									ctx.lineTo(x,y);
									ctx.stroke();
								}
								
								ctx.beginPath();
								ctx.fillRect(x-4,y-4,8,8);
								ctx.fill();
								
								break;
								
							case "triangle":
								ctx.fillStyle = "#448637";
								ctx.strokeStyle = "#448637";
								
								if(oldx != null)
								{
									ctx.beginPath();
									ctx.lineWidth = 2;
									ctx.moveTo(oldx, oldy);
									ctx.lineTo(x,y);
									ctx.stroke();
								}
								
								ctx.beginPath();
								ctx.moveTo(x+4,y+3);
								ctx.lineTo(x-4,y+3);
								ctx.lineTo(x,y-5);
								ctx.fill();
								
								break;
						}
					} catch(e) {}
					oldx = x;
					oldy = y;
					
					if(this.Vectors[vi].numbervisible == "true")
					{
						//プロット上の値の描画.
						var obj = document.createElement('div');
						Object.extend(obj, Element);
	//					obj.id = "id";
						obj.innerHTML = this.numberF(this.Vectors[vi].points[pi].value);
						obj.style.position = "absolute";
						obj.style.fontSize = "12px";
						obj.style.width = nwidth+"px";
						obj.style.textAlign = "center";
						obj.style.left = (x - nwidth/2)+"px";
						obj.style.top = (y - 20) +"px";
						document.getElementById(this.id).appendChild(obj);
					}
				}
				
				if(vi==0)
				{
					//横座標の描画.
					var obj = document.createElement('div');
					Object.extend(obj, Element);
//					obj.id = "id";
					obj.innerHTML = this.numberF(this.Vectors[vi].points[pi].key);
					obj.style.position = "absolute";
					obj.style.fontSize = "12px";
					obj.style.width = nwidth+"px";
					obj.style.textAlign = "center";
					obj.style.left = (x - nwidth/2)+"px";
					obj.style.top = parseInt(this.gy) + parseInt(this.gh) + 4 +"px";
					document.getElementById(this.id).appendChild(obj);
				}
			}
		}
		this.intervalid = setInterval(this.forceRerendering.bind(this), 1000);
//		this.forceRerendering();
	},
	
	add: function(vno, key, value)
	{
		var point = {};
		point.key = key;
		point.value = value;
		this.Vectors[vno].points.push(point);
	},
	
	numberF: function(str)
	{
	    var temp1 = str.match(/./g).reverse().join("");
	    temp1 = temp1.replace(/(\d{3})/g,"$1,");
	    temp1 = temp1.match(/./g).reverse().join("").replace(/^,/,"");
	    return temp1;
	},

	loadedXml: function(res)
	{
		var xviews = res.responseXML.getElementsByTagName('view');
		
		for(var vi=0;vi<xviews.length;vi++)
		{
			this.gx = xviews[vi].getAttribute('left');
			this.gy = xviews[vi].getAttribute('top');
			this.gw = xviews[vi].getAttribute('width');
			this.gh = xviews[vi].getAttribute('height');
			
		}
		
		var xgraphs = res.responseXML.getElementsByTagName('graph');
		
		for(var gi=0;gi<xgraphs.length;gi++)
		{
			if(xgraphs[gi].getAttribute('logicalx'))
				this.logicalx = xgraphs[gi].getAttribute('logicalx');
			
			var vectorno = this.setVector(xgraphs[gi].getAttribute('position'), xgraphs[gi].getAttribute('logicaly'), xgraphs[gi].getAttribute('type'), xgraphs[gi].getAttribute('starty'), xgraphs[gi].getAttribute('endy'), xgraphs[gi].getAttribute('dy'), xgraphs[gi].getAttribute('numbervisible'));
			
			var xpoints = xgraphs[gi].getElementsByTagName('point');
			
			for(var pi=0;pi<xpoints.length;pi++)
			{
				this.add(vectorno, xpoints[pi].getAttribute('key'), xpoints[pi].getAttribute('value'));
			}
			
		}
		
		
		
		this.draw();
	},
	
	loadXml: function(xmlurl)
	{
		var pars = "";
		
		var ind = xmlurl.indexOf("?");
		
		if(ind > 0)
		{
			pars = xmlurl.substring(ind+1);
			xmlurl = xmlurl.substring(0, ind);
		}
		
		var ajax = new Ajax.Request(xmlurl, { method:'post', onComplete: this.loadedXml.bind(this), parameters: pars});
	},


	forceRerendering: function()
	{
		element = $(this.id+"_canvas");
		element.hide();
		element.show();
		clearInterval(this.intervalid);
	}
});


