toolboxObj = function() {
	this.currentTool = "zoom";// Set default tool
	this.tool = [];
	this.tool["trendline"] = [];
	this.maxTrendlines = 6;
	this.AddToolBox();
}

toolboxObj.prototype.AddToolBox = function(){
	var main = document.getElementsByTagName('body')[0];;
	Element.create('div', {id:"mozWin"}, '&nbsp;', main);
	Element.create('div', {id:"infoDiv"}, '&nbsp;', main);
	Element.create('div', {id:"divZoom"}, '&nbsp;', main);
	Element.create('div', {id:"divZoomH"}, '&nbsp;', main);
}

toolboxObj.prototype.getNewData = function() {
	this.init();
	if (chart.exportData) {
		this.chartData = getParsedData();
		this.spacing = (d.yX / d.cDS.length);
	}
}

function changeTool(tool) {

	if (tool == 'trendline') {
		toolbox.currentTool = "trendline";
	} else if (tool == 'zoom') {
		toolbox.currentTool = "zoom";
	} else if (tool == 'crosshair') {
		toolbox.currentTool = "crosshair";
	} else if (tool == 'normal') {
		toolbox.currentTool = "normal";
		toolbox.clearTrendlines();
	}
	toolbox.init();
}

// init Toolbox
toolboxObj.prototype.init = function(){
	// Get Coords of Chart
	var xOffset = 0;var yOffset = 0;
	this.chartArea = document.getElementById("cht");
	this.origin = chart.getPos(this.chartArea,xOffset,yOffset);
	
	this.regionX = {x0:1, x1:d.yX}; // d.yX is the width of the x axis, or x pos of y axis
	this.regionY = [{y0:0, y1:chart.upperHeight}];

	// setup legend
	this.infoDiv = document.getElementById("infoDiv");
	
	with (this.infoDiv.style) { // dynamic position
		top = this.origin.y + 'px';
		left = ( this.origin.x + 5 ) + 'px';
		height = '12px';
		//position = "absolute";
		display = 'block';
	}
  
	// Fix for Safari/Mozilla - Add events to Div instead of the actual chart area
	if (typeof(document.all) == "undefined") {
		this.divMoz = document.getElementById("mozWin");
		
		with (this.divMoz.style) { // dynamic position
			top = this.origin.y + 'px';
			left = this.origin.x + 'px';
			display = 'block';
			//position = "absolute";
		}
		
		addEvent(this.divMoz, "mouseup", userAction);
		addEvent(this.divMoz, "mousedown", userAction);
		addEvent(this.divMoz, "mousemove", userAction); // ie needs this.
		addEvent(this.divMoz, "selectstart", cancelEvent);
	} else {
		// i.e works much better like this
		addEvent(this.chartArea, "mouseup", userAction);
		addEvent(this.chartArea, "mousedown", userAction);
		addEvent(this.chartArea, "mousemove", userAction); // ie needs this.
		addEvent(this.chartArea, "selectstart", cancelEvent);
	}

	// Add Handlers/User Action for Trendlines
	var canvasID;
	for (var x=0; x < this.maxTrendlines; x++){
		canvasID = document.getElementById("canvas" + x);
		//addEvent(canvasID, "mouseup", userAction);
	}
	
	// Some reference vars for the Trendlines
	this.outLeft = function(x0){ return x0 <= this.regionX.x0 + this.origin.x; }
	this.outRight = function(x0){ return x0 >= this.regionX.x1 + this.origin.x; }
	this.outTop = function(y0){ return y0 <= this.regionY[0].y0 + this.origin.y}
	this.outBottom = function(y0){ return y0 >= this.regionY[0].y1 + this.origin.y;}	
  
	// Initialize Zooming
	if (this.currentTool == 'zoom' || this.currentTool == 'normal' || this.currentTool == 'crosshair') {
		this.tool["zoom"] =	new zoomObj();
		this.tool["zoom"].init();
	} else if (this.currentTool == 'trendline') {
		// clear old cross hair
		this.tool["zoom"].divZoom.style.visibility = "hidden";
		this.tool["zoom"].divZoomH.style.visibility = "hidden";
	}
}

// quick util for finding array key
toolboxObj.prototype.getSnapIndex = function(xIn) {
	this.dataKey = Math.floor((xIn - this.spacing) / this.spacing);
	while (xIn > (d.xDS[this.dataKey]+(this.spacing*.75))) {
		this.dataKey++;
	}
	if (typeof(d.cDS[this.dataKey]) == "undefined"){
		return ((xIn < 300 || d.cDS.length == 0)?0:d.cDS.length - 1)
	}
	return this.dataKey;
}

// Update Price Legend
toolboxObj.prototype.updateLegend = function(x0, x1, ixStart, ixStop) {
	if (x1){ // being dragged

	if (chart.issueType == 'MF') {
		var volume1 = '';
		var volume2 = '';
	} else {
		var volume1 = '<div class="inLab" style="width:40px;">Volume</div><div class="inData" style="width:100px;">'+this.chartData[ixStart].v+'</div>';
		var volume2 = '<div class="inLab" style="width:40px;">Volume</div><div class="inData" style="width:100px;">'+this.chartData[ixStop].v+'</div>';
	}
	this.infoDiv.innerHTML = '<div class="inDa">'+this.chartData[ixStart].df+'</div><div class="inLab">Open</div><div class="inData">'+this.chartData[ixStart].o+'</div><div class="inLab">High</div><div class="inData">'+this.chartData[ixStart].h+'</div><div class="inLab">Low</div><div class="inData">'+this.chartData[ixStart].l+'</div><div class="inLab">Close</div><div class="inData">'+this.chartData[ixStart].cF+'</div>'+volume1+'<br class="clear" /><div class="inDa">'+this.chartData[ixStop].df+'</div><div class="inLab">Open</div><div class="inData">'+this.chartData[ixStop].o+'</div><div class="inLab">High</div><div class="inData">'+this.chartData[ixStop].h+'</div><div class="inLab">Low</div><div class="inData">'+this.chartData[ixStop].l+'</div><div class="inLab">Close</div><div class="inData">'+this.chartData[ixStop].cF+'</div>'+volume2;

	} else { // crosshair
		try {
			if (chart.issueType == 'MF') {
				var volume1 = '';
			} else {
				var volume1 = '<div class="inLab" style="width:40px;">Volume</div><div class="inData" style="width:100px;">'+this.chartData[ixStart].v+'</div>';
			}		
		this.infoDiv.innerHTML = '<div class="inDa">'+this.chartData[ixStart].df+'</div><div class="inLab">Open</div><div class="inData">'+this.chartData[ixStart].o+'</div><div class="inLab">High</div><div class="inData">'+this.chartData[ixStart].h+'</div><div class="inLab">Low</div><div class="inData">'+this.chartData[ixStart].l+'</div><div class="inLab">Close</div><div class="inData">'+this.chartData[ixStart].cF+'</div>'+volume1;
		} catch(e) {}
	}
}

// ----------------------------------------------------------
// Trendline Features
// ----------------------------------------------------------
toolboxObj.prototype.clearTrendlines = function() {
	for (var x=toolbox.tool["trendline"].length - 1; x >= 0; x--){
		document.getElementById("canvas" + x).innerHTML = '';
		toolbox.tool["trendline"][x].clearHTM();
		toolbox.tool["trendline"][x].hideHandle(0);
		toolbox.tool["trendline"][x].hideHandle(1);
	}
}

// Get Price
toolboxObj.prototype.getPrice = function(yIn) {
	if (typeof(d.pDS[(parseInt(yIn,10) - this.origin.y)]) != "undefined"){
		return d.pDS[(parseInt(yIn,10) - this.origin.y)];
	}
	return 0;
}
// Create a new Trendline - try to organize better
toolboxObj.prototype.createNewTrendLine = function(x0, y0) {
	if (typeof(this.currLine) == "undefined") {
		this.currLine = 0;
	} else if (this.tool["trendline"][this.currLine].x1 && this.tool["trendline"][this.currLine].y1) {		
		// only increment if dragging did actually occur.
		this.currLine += 1;
	}

	if (this.currLine >= this.maxTrendlines){
		this.currLine = 0;
	}
	if (this.currLine > this.tool["trendline"].length - 1){
		this.tool["trendline"][this.currLine] = new trendlineObj(this.currLine);
	}
	this.tool["trendline"][this.currLine].dragStart(x0, y0);
}

// check boundry
toolboxObj.prototype.checkCoords = function(cX, cY) {
	return (cY > this.regionY[0].y0 &&
	cY < this.regionY[0].y1 &&
	cX > this.regionX.x0 &&
	cX < this.regionX.x1
	);
}
toolboxObj.prototype.checkRollCoords = function(cX, cY) {
	return (cY > this.regionY[0].y0 && 
	cX > this.regionX.x0 && cY < chart.height);
}
toolboxObj.prototype.checkPositiveCoords = function() {
	for (var x=0; x < arguments.length; x++) {
		if (arguments[x] < 0){
			return false;
		}
	}
	return true;
}

// ----------------------------------------------------------
// EVENT HANDLERS
// ----------------------------------------------------------
function userAction(what, who){

	try { // needed for actions before toolbox is fully inited

	// event handling
	if (what == "move") {
	
	} else {
		if (arguments.length == 0){ 
			var what = window.event.type.replace("mouse","");
		} else if (typeof (what) == "object"){
			var what = what.type.replace("mouse","");
		}
	}

	var xOffset = toolbox.origin.x + 2; // because the window layout is stretchy, need to offset by x pos of chart
	var yOffset = toolbox.origin.y; // dito for y

	// call specific handler
	switch(what) {

		case "move": {
			if (toolbox.currentTool == "zoom" || toolbox.currentTool == "normal" || toolbox.currentTool == "crosshair") {
				if (chart.mView == 0 || chart.mView2 == 0 || chart.submenu.style.display == 'block') {
					// don't roll
				} else {
					if (toolbox.checkRollCoords(mouse.x - xOffset, mouse.y - yOffset)) { // remove restriction on lower
						toolbox.tool["zoom"].roll(mouse.x - xOffset);
					}
					
					if (chart.fullscreen == 2) {
							closeHover();
					}
				}
			} else if (toolbox.currentTool == "trendline" && typeof(toolbox.currLine) != "undefined" && typeof(toolbox.tool["trendline"][toolbox.currLine]) != "undefined") {
				//toolbox.tool["zoom"].roll(mouse.x - xOffset);
				toolbox.tool["trendline"][toolbox.currLine].roll(mouse.x, mouse.y);
			}
			break;
		}

		case "down": {
			if (toolbox.currentTool == "crosshair") {
				toolbox.tool["zoom"].setupHorizontalLine(mouse.y - yOffset);
				cancelEvent();
			} else if (toolbox.currentTool == "zoom") {
				toolbox.tool["zoom"].dragStart(mouse.x - xOffset);
				cancelEvent();
			} else if (toolbox.currentTool == "trendline") {
				if (toolbox.checkCoords(mouse.x - xOffset, mouse.y - yOffset)) {
					toolbox.createNewTrendLine(mouse.x, mouse.y);
					cancelEvent();
				}
			}
			break;
		}	

		case "up": {
			if (toolbox.currentTool == "crosshair") {
				toolbox.tool["zoom"].drawHorizontalLine(mouse.y - yOffset);
				cancelEvent();
			} else if (toolbox.currentTool == "zoom") {
				toolbox.tool["zoom"].dragStop(mouse.x - xOffset);
				cancelEvent();
			} else if (toolbox.currentTool == "trendline") {
				toolbox.tool["trendline"][toolbox.currLine].dragStop();
			}
			break;
		}

		case "trendHandleDrag": {
			toolbox.currLine = parseInt(who.substr(0,1));
			toolbox.tool["trendline"][toolbox.currLine].startTrendHandleDrag(who.substr(0,1), who.substr(1,1));
			break;
		}		
		
	}

	} catch(err) {  }
	
	return false;
}

function cancelEvent() {
	if (window.event.preventDefault){window.event.preventDefault()};
	if (window.event.stopPropagation){window.event.stopPropagation()}
	window.event.cancelBubble = true;
	window.event.returnValue = false;
	return false;
}

// ----------------------------------------------------------
// Zoom
// ----------------------------------------------------------
function zoomObj() {
	this.isCrosshair = true;
	this.isBeingDragged = false;
	this.divZoom = document.getElementById("divZoom"); // vertical crosshair
	this.divZoomH = document.getElementById("divZoomH");// horizontal crosshair

	if (chart.lowerManager.length > 1) {
		var chH = chart.upperHeight + 34 + (chart.lowerManager.length * (chart.lowerHeight + 24))
	} else {
		var chH = chart.height;
	}	

	this.init = function() {

		this.indexStart = 0;
		this.indexStop = null;
		this.isBeingDragged = false;

		if (toolbox.currentTool == "normal") {
		
			this.divZoom.style.visibility = "hidden";
			this.divZoomH.style.visibility = "hidden";

		} else {

			this.divZoom.style.top = toolbox.origin.y + "px";
			this.divZoom.style.left = toolbox.origin.x + 'px';
			this.divZoom.style.width = "1px";
			this.divZoom.style.height = chH + "px";
			this.divZoom.style.background = "#999999";
			this.divZoom.style.visibility = "visible";
			
			addEvent(this.divZoom, "mouseup", userAction);
			addEvent(this.divZoom, "mousemove", userAction);
			addEvent(this.divZoom, "mousedown", userAction);		

			this.isCrosshair = true;
			
			if (toolbox.currentTool == "crosshair") { // Add cross hair - support/resistance
			
				this.divZoomH.style.top = toolbox.origin.y + "px";
				this.divZoomH.style.width = chart.width+'px';
				this.divZoomH.style.left = toolbox.origin.x + 'px';
				this.divZoomH.style.visibility = "visible";
				
				addEvent(this.divZoomH, "mouseup", userAction);
				addEvent(this.divZoomH, "mousemove", userAction);
				addEvent(this.divZoomH, "mousedown", userAction);				

			} else {
				this.divZoomH.style.visibility = "hidden";
			}
		}
	}

	var x0, x1;
	var mmXold = 0;

	this.roll = function(xIn) {

		this.indexStop = toolbox.getSnapIndex(xIn);
	
		if (toolbox.currentTool == "crosshair") {
			this.isCrosshair = true;

			try {
				this.draw(toolbox.chartData[this.indexStop].x);
				setTimeout('toolbox.updateLegend(' + toolbox.chartData[this.indexStop].x + ',false,' + this.indexStop + ')', 1);
			} catch(e) {}
			
		} else if (!this.isBeingDragged || toolbox.currentTool == "normal"){
			this.isCrosshair = true;

			try {
				if (!mmXold || mmXold != toolbox.chartData[this.indexStop].x) {

					this.draw(toolbox.chartData[this.indexStop].x);
					setTimeout('toolbox.updateLegend(' + toolbox.chartData[this.indexStop].x + ',false,' + this.indexStop + ')', 1);

					mmXold = toolbox.chartData[this.indexStop].x;
				}
			} catch(e) {}
	
		} else {

			toolbox.dragDirRight = this.indexStop == Math.max(this.indexStart, this.indexStop)

			if (toolbox.dragDirRight){

				x0 = toolbox.chartData[this.indexStart].x;
				x1 = toolbox.chartData[this.indexStop].x;

			} else {
				x0 = toolbox.chartData[this.indexStop].x;
				x1 = toolbox.chartData[this.indexStart].x;
			}

			this.dragWidth = Math.abs(x1 - x0);

			if (this.dragWidth > 0 && this.isCrosshair){

				this.isCrosshair = false;
				this.divZoom.innerHTML = '<table class="dragTable" style="height:'+chH+'px;" cellpadding="0" cellspacing="0"><tr><td><img class="dragRegion" src="/gif/x.gif" style="height:'+chH+'px;"></td></tr></table>';

			} else if (this.dragWidth == 0 && !this.isCrosshair){
				this.isCrosshair = true;
			}

			this.draw(x0,x1);
			toolbox.updateLegend(x0, x1, this.indexStart, this.indexStop);

		}
	}

	this.draw = function(x0, x1){
		
		if (!x0){ return false;}
		
		if (toolbox.currentTool == "normal" || toolbox.currentTool == "trendline") {
		
			this.divZoom.style.visibility = "hidden";
			this.divZoomH.style.visibility = "hidden";
		
		} else {
		
			if (this.divZoom.style.visibility == "hidden") {
				this.divZoom.style.visibility = "visible";
			}

			this.x2 = x0;

			if (x1){ // dragged
			
				this.divZoom.style.left = this.x2 + "px";
				this.divZoom.style.width = (x1 - x0 + 1) + "px";
				this.divZoom.style.background = "none";
				
			} else { // crosshair
			
				this.divZoom.style.left = this.x2 + 1 + "px";
				
			}
			
			if (toolbox.currentTool == "crosshair") {
			
				if (this.divZoomH.style.visibility == "hidden") {
					this.divZoomH.style.visibility = "visible";
				}
				
				this.divZoomH.style.top = mouse.y + "px";
			}
		}
	}

	this.dragStart = function(xStart) {
		toolbox.dragDirRight = false;
		this.isBeingDragged = true;
		this.indexStart = toolbox.getSnapIndex(xStart);
	}

	this.dragStop = function(xStop) {
		this.indexStop = toolbox.getSnapIndex(xStop);
		
		if (toolbox.chartData.length == 0){ return false }
		var dMin = Math.min(toolbox.chartData[this.indexStart].d, toolbox.chartData[this.indexStop].d);
		var dMax = Math.max(toolbox.chartData[this.indexStart].d, toolbox.chartData[this.indexStop].d);
		
		if (dMin != dMax && this.isBeingDragged) { // eh Drag Stop is called to much, need some restraints
			chart.dMin = dMin;
			chart.dMax = dMax;
			chart.exportData = 1;
			toolbox.clearTrendlines();
			setTimeout('chart.load()',1);
		}
		this.isBeingDragged = false;
		this.divZoom.innerHTML = '&nbsp;';
		this.divZoom.style.width = '1px;'
		this.divZoom.style.background = "#999999";
	}

	this.setupHorizontalLine = function(yStop) {
		this.isHorzLine = 1;
	}	
	
	this.drawHorizontalLine = function(yStop) {
		if (this.isHorzLine) {
			var yP = getYPriceByCoord(yStop);
			if (yP) {
				chart.addUpper('HORZLINE','upper',yP);
			}
			this.isHorzLine = 0;
		}
	}
}

// ----------------------------------------------------------
// Trendlines
// ----------------------------------------------------------
function trendlineObj(idx) {
	this.idx = idx;
	this.id = "trendline";
	this.initialized = false;
	this.colorUp = "#2E9F1A";
	this.colorDn = "#890000";
	this.colorUnch = "#999999";
	this.htm = [];
	this.lineRegex = /%(\d+);(\d+);(\d+);(\d+)/g;

	this.init = function() {

		this.initialized = true;
		this.myCanvas = document.getElementById("canvas" + idx);
		this.isBeingDragged = false;
		this.x0 = false; this.x1 = false;
		this.y0 = false; this.y1 = false;

		this.handle = [];
		this.handle[0] = Element.create("DIV");
		this.handle[1] = Element.create("DIV");

		for (var x=0; x < 2; x++) {
		
			this.handle[x].id = "" + idx + x;
			addEvent(this.handle[x], "mouseover", userAction);
			addEvent(this.handle[x], "mouseup", userAction);
			addEvent(this.handle[x], "mousemove", userAction);
			addEvent(this.handle[x], "selectstart", userAction);
			
			this.handle[x].innerHTML = '<div id="'+("" + idx + x + 2 + 'a')+'" onmousedown="trendHandleDrag(this.id);"><img src="trendPt.gif" /></div>';
			this.handle[x].className = "divTrendHandle";
			document.body.appendChild(this.handle[x]);
		}
	}

	this.init();

	this.dragStart = function(newX, newY){

		if (d.cDS.length > 1){
			this.x0 = newX;
			this.y0 = newY;
			this.x1 = false;
			this.y1 = false;
			this.indexStart = toolbox.getSnapIndex(newX);
			this.drawHandle(newX - 2, newY - 2, 0);
			this.isBeingDragged = true;
		} else {
			return false;
		}
	}

	this.dragStop = function(){

		this.isBeingDragged = false;
		if (!this.x1 || !this.y1){
			this.hideHandle(0);
		}
	}

	this.drawHandle = function(x,y, idx) {

		this.handle[idx].style.left = x + "px";
		this.handle[idx].style.top = y + "px";
		this.handle[idx].style.visibility = "visible";

	}

	this.hideHandle = function(idx){
		this.handle[idx].style.visibility = "hidden";
	}

	this.startTrendHandleDrag = function(idx, which) {
		if (which == "0"){ // flip side.
			var tmp0 = this.x1;
			var tmp1 = this.y1;
			var tmp2 = this.indexStop;
			this.x1 = this.x0;
			this.y1 = this.y0;
			this.indexStop = this.indexStart;
			this.x0 = tmp0;
			this.y0 = tmp1;
			this.indexStart = tmp2;
			this.drawHandle(this.x0 - 2, this.y0 - 2, 0)
		}
		this.isBeingDragged = true;
	}	

	this.roll = function(newX, newY){

		if (this.isBeingDragged){
			this.indexStop = toolbox.getSnapIndex(newX);
			this.collideLeft = false;this.collideTop = false;
			this.collideRight = false;this.collideBottom = false;
			this.collideLeftLabel = false;
			if (toolbox.outLeft(newX - 40)){ this.collideLeftLabel = true; }
			if (toolbox.outLeft(newX - 10)){

				newX = toolbox.regionX.x0 + toolbox.origin.x + 10;
				this.collideLeft = true;

			} else if (toolbox.outRight(newX)){
				newX = toolbox.regionX.x1 + toolbox.origin.x - 10;
			}

			if (toolbox.outTop(newY - 10)){
				newY = toolbox.regionY[0].y0 + toolbox.origin.y + 10;
			} else if (toolbox.outBottom(newY + 10)){
				newY = toolbox.regionY[0].y1 + toolbox.origin.y - 10;
			}

			this.x1 = newX;
			this.y1 = newY;

			toolbox.tool["trendline"][toolbox.currLine].drawLine(this.x0,this.y0,this.x1,this.y1)
		}
		return false;
	}

	this.drawLine = function(x0, y0, x1, y1){

		this.dirLeft = x0 > x1;
		this.drawLeft = this.dirLeft && !this.collideLeftLabel;

		this.lineUp = (x0 < x1 && y0 >= y1) || (x0 >= x1 && y0 < y1)
		this.lineColor = (this.lineUp)?this.colorUp:this.colorDn;
		this.clearHTM()

		this.mkLine(x0, y0, x1, y1);
		this.paint()
		this.drawHandle(this.x1 - 2, this.y1 - 2, 1);
	
	}

	this.clearHTM = function() {
		this.htm = [];
	}

	this.paint = function() {
		this.myCanvas.innerHTML = this.htm.join("").replace(this.lineRegex,'<div class=t  style=background:'+this.lineColor+';left:$1px;top:$2px;width:$3px;height:$4px;></div>');
	}

	this.mkLine = function(x1, y1, x2, y2){if(x1>x2){_x2=x2;_y2=y2;x2=x1;y2=y1;x1=_x2;y1=_y2;};dx=x2-x1;dy=Math.abs(y2-y1);x=x1;y=y1;yIncr=(y1>y2)?-1:1;if(dx>=dy){pr=dy<<1;pru=pr-(dx<<1);p=pr-dx;ox=x;while((dx--)>1){++x;if(p>0){this.htm[this.htm.length]='%'+ox+';'+y+';'+(x-ox)+';2';y+=yIncr;p+=pru;ox=x;}else p += pr;};this.htm[this.htm.length]='%'+ox+';'+y+';'+(x2-ox+2)+';2';}else{pr=dx<<1;pru=pr-(dy<<1);p=pr-dy;oy=y;if(y2<=y1){while((dy--)>1){if(p>0){this.htm[this.htm.length]='%'+(x++)+';'+y+';2;'+(oy-y+2);y+=yIncr;p+=pru;oy=y;}else{y+=yIncr;p+=pr;}};this.htm[this.htm.length]='%'+x2+';'+y2+';2;'+(oy-y2+2);}else{while((dy--)>1){y+=yIncr;if(p>0){this.htm[this.htm.length]='%'+(x++)+';'+oy+';2;'+(y-oy);p+=pru;oy=y;}else{p+=pr;}};this.htm[this.htm.length]='%'+x2+';'+oy+';2;'+(y2-oy+2);}}};
}

function trendHandleDrag(id) {
	userAction("trendHandleDrag", id);
}

// common functions
function addEvent(obj, eventType, afunction, isCapture) {
	if (obj.addEventListener) { // W3C DOM
		obj.addEventListener(eventType, afunction, isCapture);
		return true;
	} else if (obj.attachEvent) { // is ie
		return obj.attachEvent("on"+eventType, afunction);
	} else {
		return false;
	}
}

function getYPriceByCoord(yCoord) { // returns price by coordinate
	yCoord = parseInt(yCoord);
	var yValue = d.pDS[yCoord];
	if (yValue && yValue > 0) {
		return yValue;
	}
}

function getParsedData() {
	var data = [];
	for (var x=0; x < d.cDS.length; x++){
		var dF = formatD((d.dDS[x]-25569)*86400000); // check time zone
		data[data.length] = {
			o:formatPrice(d.oDS[x]),
			h:formatPrice(d.hDS[x]),
			l:formatPrice(d.lDS[x]),
			c:d.cDS[x],
			v:formatPrice((d.vDS[x]*100),-1),
			d:d.dDS[x],
			x:d.xDS[x] + toolbox.origin.x,
			y:d.yDS[x],
			df:dF,
			cF:formatPrice(d.cDS[x])
		};
	}
	return data;
}

function formatPrice(value,sig) {
	if (value > 999 || sig == -1) {
		var sig = 0;
	} else if (value < .01 && value > 0) {
		var sig = 4;
	} else {
		var sig = 2;
	}

	if (value) {
		value = String(value.toFixed(sig));
		if (value.length < 7 && value.indexOf(".") > -1 || sig == 4) {
			return value;
		} else {
			x = value.split('.');
			x1 = x[0];
			x2 = x.length > 1 ? '.' + x[1] : '';

			if (x1.length > 3) {
				var rgx = /(\d+)(\d{3})/;
				while (rgx.test(x1)) {
					x1 = x1.replace(rgx, '$1' + ',' + '$2');
				}
			}
			return x1 + x2;
		}
	} else {
		return '--';
	}
}

function formatD(value) {
	var t =  new Date(value);
	var day = t.getUTCDate();
	var mon = t.getUTCMonth();
	var year = t.getUTCFullYear();
	var formatDatemo = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
	if (chart.durations[chart.duration].interval.indexOf('minu') > -1) {
		var h = t.getUTCHours();
		var minute = t.getUTCMinutes() + '';
		var utc = chart.tzO;

		var utcHour = h - utc;
		if (utcHour > 11) {
			if (utcHour > 12) {
				utcHour = (utcHour - 12);
			}
			var ampm = 'PM';
		} else { var ampm = 'AM'; }
		if (minute.length == 1) {minute = '0'+minute;}
		return utcHour+':'+minute+ ' '+ampm;
	} else {
		return formatDatemo[mon]+' '+day+', '+year;
	}
}

// ------------------------------------------------------------
// mouse stuff
// ------------------------------------------------------------
var mouse = [];mouse.x = 0;mouse.y = 0;

var m_xOff, m_yOff, m_ev, m_isStandard;

// some browser stuff
var agent = navigator.appName.toLowerCase();

var isSafari = false;
var isNav6 = false;
if (agent.indexOf('safari') != -1) {
	isSafari = true;
} else if (agent.indexOf('gecko') > -1) {
	if (agent.indexOf('netscape') > -1) {
		isNav6 = true
	}
}

if (window.addEventListener) {
	window.addEventListener("mousemove",watchMouseCoords,true);
} else { // check safari
	document.attachEvent("onmousemove",watchMouseCoords);
}

function watchMouseCoords(e) {
	if (isSafari) {
		m_ev = event;
	} else {
		m_ev = e;
	}

	if (isNav6) { // need to catch the various implementations of scrollTop
		m_yOff = window.pageYOffset;
		m_xOff = window.pageXOffset;
	} else {
		isStandard = (document.compatMode && document.compatMode == "CSS1Compat") ? true : false;
		
		if (isStandard) {
			m_yOff = document.documentElement.scrollTop;
		} else {
			m_yOff = document.body.scrollTop;
		}
		if (isStandard) {
			m_xOff = document.documentElement.scrollLeft;
		} else {
			m_xOff = document.body.scrollLeft;
		}

		m_xOff = document.documentElement.scrollLeft;
		m_yOff = document.documentElement.scrollTop;
	}

	if (isSafari) { // eh check on this, but safari seems to only like doc element scrolltop
		//m_yOff = 0 
		// Latest version likes below better
	}

	mouse.x = m_ev.clientX + m_xOff;
	mouse.y = m_ev.clientY + m_yOff;

	if (mouse.callback) { mouse.callback() }
}

// callback function is executed onmousemove in mouse.js.
mouse.callback = function() {
	userAction("move", "callback");
};