var BASE_URL="http://pipes.yahoo.com/pipes/pipe.run?_id=xLjkFsCd3BGoDvL7yZ1_DQ&_render=json&_callback=plotData"
var layout;
var plotter;

setStyle = function(chartStyle) {
	layout.style= chartStyle;

	// update
	layout.evaluate();
	plotter.clear();
	plotter.render();
}

getJSONUrl = function(url) {
  var scriptNode = document.createElement("script");
  scriptNode.src = url;
  scriptNode.type = "text/javascript";
  scriptNode.language = "javascript";
  scriptNode.id = "removeMe";
  document.getElementsByTagName("head")[0].appendChild(scriptNode);
  return false;
}

plotData = function(jsonData) {
	var start = Math.min(90, jsonData.value.items.length);
	var options = {
	   "IECanvasHTC": "/blog/wp-content/uploads/2007/12/plotkit-0.9.1/PlotKit/iecanvas.htc",
	   "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[0]),
	   "padding": {left: 0, right: 0, top: 10, bottom: 30},		
	   "yAxis": [250,350],
	   "drawYAxis": true,
	   "xTicks": [{v:0, label:jsonData.value.items[start-0].Date}, 
			  {v:30, label:jsonData.value.items[start-30].Date}, 
			  {v:60, label:jsonData.value.items[start-60].Date},
			  {v:90, label:jsonData.value.items[start-90].Date}]
	};
	layout = new PlotKit.Layout("line", options);
	var plotData = [];

	for (var i = start; i >= 0; i--) {		
		plotData[start-i] = [start-i, jsonData.value.items[i].Close];
		layout.addDataset("SharePrice", plotData);
//					console.log(i + ":" + [jsonData.value.items[i].Date, jsonData.value.items[i].Close]);
	}
	layout.evaluate();

	var canvas = MochiKit.DOM.getElement("graph");
	plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, {});
	plotter.render();
	document.getElementsByTagName('head')[0].removeChild(document.getElementById('removeMe')); // remove the script import
}

getData = function() {
	getJSONUrl(BASE_URL + "&symbol=LON:BT.A");
}