﻿///////////////////Utility
	function formatTime(val) {
		var a = new Number(val.substring(0,val.indexOf(":")));
		if (a > 12) {
			a -= 12;
		}
		var b = val.substring(val.indexOf(":"),val.length);
		return a + b;
	}
	
	function formatDecimal(value, decimals, keepZero) {
		var mul = new String("1");
		var zero = new String("0");
		for (var i = decimals; i > 0; i--) {
			mul += zero;
		}
		value = Math.round(value * mul);
		value = value / mul;
		var strVal = new String(value);
		if (!keepZero) {
			return strVal;	
		}
		
		var nowDecimals = 0;
		var dot = strVal.indexOf(".");
		if (dot == -1) {
			strVal += ".";
		} else {
		 	nowDecimals = strVal.length - dot - 1;
		}
		for (var i = nowDecimals; i < decimals; i++) {
			strVal = strVal + zero;
		}
		return strVal;
	}
	
	
/////////////////ChartTable
	var gBeginTime = getSeconds("08:30:00");
	var gEndTime = getSeconds("11:30:00");
	var indexItem = "item0";
	var showHistory = false;
	var gLine;
	var gTable = new ChartTable(indexItem, "time open", "MERGE");

	gTable.setSnapshotRequired(true);
	gTable.setAreaClass("lsgbox");
	gTable.setAreaWidth(300);
	gTable.setAreaHeight(155);
	gTable.setAreaLeft(40);
	gTable.setAreaTop(10);
	gTable.onItemUpdate = onChartUpdate;
	gTable.setClearOnRemove(true);
	gTable.setClearOnDisconnected(false);
	gTable.setClearOnAdd(false);
	
	gTable.setXAxis(3, false);
	var labelFormatter = {};
	labelFormatter.formatValue = formatXlbl;
	gTable.setXLabels(4,"lslblx",labelFormatter);
	
	lsPage.addTable(gTable,"graph");
	
	//showHistorialValues();

	function showHistorialValues() {
		showHistory = true;

		var values = new Array(2);
		var bt = getSeconds("09:00:00");
		if (gTable && gLine) {
			for (i=0;i<10;i++) {
				values["time"] = bt + 2;
				values["open"] = 700;
				gTable.showValues(indexItem, values);
			}
		}
		
		showHistory = false;
	}

	function getNewChart(item,xStart,yRef) {
		chart = new ChartLine();

		chart.setPointClass("lspxbig");
		chart.setLineClass("lspx");
		xStart = new String(xStart);
		yRef = new String(yRef);
		
		maxX = gEndTime;
		minX = gBeginTime;
		gTable.positionXAxis(minX, maxX)
		
		minY=yRef.replace(",",".")*0.90;
		maxY=yRef.replace(",",".")*1.10;
		chart.setYAxis(item,4,true);
		chart.positionYAxis(minY,maxY);
		
		var lblFrmttr = {};
		lblFrmttr.formatValue = formatYlbl;
		chart.setYLabels(4,"lslbly",lblFrmttr);
		
		return chart;
	}
	
	function onChartUpdate(item, upOb) {
		var aValue = upOb.getOldValue(1);
		if (aValue == null) { //first update for this item
			if (!gLine) {
				var fy = yPosition(upOb.getNewValue(2));
				gLine = getNewChart(item,gBeginTime,fy);
				gTable.addLine(gLine,"t"+item);
			}
		}
		var t=getSeconds(upOb.getNewValue(1).substring(11,19));
		if (showHistory) {
			upOb.addField(3,t,true);
			upOb.addField(4,yPosition(upOb.getNewValue(2)),true);
		} else {
			if ((t < gBeginTime) || (t > gEndTime)) return;
			upOb.addField(3,t,true);
			upOb.addField(4,yPosition(upOb.getNewValue(2)),true);		
		}
	}
	
	function getSeconds(stringDate) {
		stringDate = new String(stringDate);
		i1 = stringDate.indexOf(':');
		i2= stringDate.lastIndexOf(':');
		return(stringDate.substring(0,i1)*3600+stringDate.substring(i1+1,i2)*60+stringDate.substring(i2+1,stringDate.length)*1);
	}
	
	function yPosition(yValue) {	
		var y = new String(yValue);
		if (y.indexOf(",") > -1 ) {
			var y=y.replace(",",".");
		}
		return new Number(y);
	}
	
	function formatYlbl (val) {
		return formatDecimal(val,2,true);
	}
	
	function formatXlbl (val) {
		return formatTime(getTime(val));
	}
	
	function getTime(secondsStr) {
		var hours = Math.floor(secondsStr/(60*60));
		var seconds = secondsStr - (hours * (60*60));
		var minutes = Math.floor(seconds/60);
		var seconds = Math.round(seconds - (minutes * 60));
		
		if (minutes.toString().length < 2) {
			minutes = ":0" + minutes; 
		} else {
			minutes = ":" + minutes; 
		}
		return hours +  minutes;
	}