/**
*
* jquery.sparkline.js
*
* v1.1
* (c) Splunk, Inc
* Contact: Gareth Watts (gareth@splunk.com)
* http://omnipotent.net/jquery.sparkline/

*/

(function($) {

	// Provide a cross-browser interface to a few simple drawing primitives
	$.fn.simpledraw = function(width, height, use_existing) {
		if (use_existing && this[0].vcanvas) {
			return this[0].vcanvas;
		}
		if (width == undefined) {
			width=$(this).innerWidth();
		}

		if (height == undefined) {
			height = $(this).innerHeight();
		}

		if ($.browser.hasCanvas) {
			return new vcanvas_canvas(width, height, this);
		} else {
			if ($.browser.msie) {
				return new vcanvas_vml(width, height, this);
			}
			else {
				return false;
			}
		}
	};

	$.fn.sparkline = function(uservalues, options) {
		var options = $.extend({
			type : 'line',
			lineColor : '#00f',
			fillColor : '#cdf',
			width : 'auto',
			height : 'auto',
			composite : false
		}, options ? options : {});

		return this.each(function() {
			var values = (uservalues=='html' || uservalues==undefined) ? $(this).text().split(',') : uservalues;
			//values = $.map(values, Number);
			var width = options.width=='auto' ? values.length*3 : options.width;
			if (options.height == 'auto') {
				// must be a better way to get the line height
				var tmp = document.createElement('span');
				tmp.innerHTML = 'a';
				$(this).append(tmp);
				height = $(tmp).innerHeight();
				$(tmp).remove();
			} else {
				height = options.height;
			}
			// var height = options.height=='auto' ? $(this).innerHeight() : options.height;

			$.fn.sparkline[options.type].call(this, values, options, width, height);
		});
	};


	$.fn.sparkline.bar = function(values, options, width, height) {

		var options = $.extend({
			type : 'bar',
			barColor : '#00f',
			negBarColor : '#f44',
			zeroAxis : undefined,
			barWidth : 4,
			barSpacing : 1
		}, options ? options : {});

		var width = (values.length * options.barWidth) + ((values.length-1) * options.barSpacing);

		var min = buzzz_min;
		var max = buzzz_max;

		if (options.zeroAxis == undefined) {
			options.zeroAxis = min < 0;
		}

		var range = max-min+1;

		var target = $(this).simpledraw(width, height);
		if (target) {
			var canvas_width = target.pixel_width;
			var canvas_height = target.pixel_height;
			var yzero = min<0 ? Math.round(canvas_height * (Math.abs(min)/range))-1 : canvas_height-1;
			for(var i=0; i<values.length; i++) {

				var x = i*(options.barWidth+options.barSpacing);
				var val = values[i];
				for (var i2 = 0; i2 < val.length; i2++) {

					var color = (val[i2] < 0) ? options.negBarColor : options.barColor;

					if (i == values.length-1) {
						if (val[i2] < 0) {
							color = options.negBarColor;
						} else {
							color = options.barColor;
						}
					} else {
						if (val[i2] < 0) {
							color = options.negBarColorPast;
						} else {
							color = options.barColorPast;
						}
					}
					if (options.zeroAxis) {
						var height = val[i2] == 0 ? 0 : Math.round(canvas_height * ((Math.abs(val[i2]) / range)));
						var y = (val[i2] < 0) ? (canvas_height/2-3) : (canvas_height/2-3)-height;
					}

					if (height >= 0) {
						if ($.browser.msie) { // IE's bars look fuzzy without this :-/
							var mode = (document.documentMode) ? document.documentMode : 0;
							if (height != 0) {
								target.drawRect(x, y + ((mode == 7)?52:2), options.barWidth - 1, height - 1, color, color);
							}
						}
						else {
							target.drawRect(x, y+1, options.barWidth, height, undefined, color);
						}
					}
				}
			}
		} else {
			// Remove the tag contents if sparklines aren't supported
			this.innerHTML = '';
		}
	};

	// IE doesn't provide an indexOf method for arrays :-(
	if (!Array.prototype.indexOf) {
		Array.prototype.indexOf = function(entry) {
			for(var i=0; i<this.length; i++) {
				if (this[i] == entry) {
					return i;
				}
			}
			return -1;
		};
	}

	// Setup a very simple "virtual canvas" to make drawing the few shapes we need easier
	// This is accessible as $(foo).simpledraw()

	if ($.browser.msie && !document.namespaces['v']) {
		document.namespaces.add("v", "urn:schemas-microsoft-com:vml");
		document.createStyleSheet().cssText = "v\\:*{behavior:url(#default#VML); display:inline-block; padding:0px; margin:0px;}";
	}

	if ($.browser.hasCanvas == undefined) {
		var t = document.createElement('canvas');
		$.browser.hasCanvas = t.getContext!=undefined;
	}

	var vcanvas_base = function(width, height, target) {
	};

	vcanvas_base.prototype = {
		init : function(width, height, target) {
			this.width = width;
			this.height = height;
			this.target = target;
			if (target[0]) {
				target = target[0];
			}
			target.vcanvas = this;
		},

		drawShape : function(path, lineColor, fillColor) {
			alert('drawShape not implemented');
		},

		drawLine : function(x1, y1, x2, y2, lineColor) {
			return this.drawShape([ [x1,y1], [x2,y2] ], lineColor);
		},

		drawCircle : function(x, y, radius, lineColor, fillColor) {
			alert('drawCircle not implemented');
		},

		drawPieSlice : function(x, y, radius, startAngle, endAngle, lineColor, fillColor) {
			alert('drawPieSlice not implemented');
		},

		drawRect : function(x, y, width, height, lineColor, fillColor) {
			alert('drawRect not implemented');
		},

		getElement : function() {
			return this.canvas;
		},

		_insert : function(el, target) {
			$(target).html(el);
		}
	};

	var vcanvas_canvas = function(width, height, target) {
		return this.init(width, height, target);
	};

	vcanvas_canvas.prototype = $.extend(new vcanvas_base, {
		_super : vcanvas_base.prototype,

		init : function(width, height, target) {
			this._super.init(width, height, target);
			this.canvas = document.createElement('canvas');
			if (target[0]) {
				target = target[0];
			}
			target.vcanvas = this;
			$(this.canvas).css({ display:'inline', width:width, height:height });
			this._insert(this.canvas, target);
			this.pixel_height = $(this.canvas).height();
			this.pixel_width = $(this.canvas).width();
			this.canvas.width = this.pixel_width;
			this.canvas.height = this.pixel_height;
		},

		_getContext : function(lineColor, fillColor) {
			var context = this.canvas.getContext('2d');
			if (lineColor != undefined) {
				context.strokeStyle = lineColor;
			}
			context.lineWidth = 1;
			if (fillColor != undefined) {
				context.fillStyle = fillColor;
			}
			return context;
		},

		drawShape : function(path, lineColor, fillColor) {
			var context = this._getContext(lineColor, fillColor);
			context.beginPath();
			context.moveTo(path[0][0]+0.5, path[0][1]+0.5);
			for(var i=1; i<path.length; i++) {
				context.lineTo(path[i][0]+0.5, path[i][1]+0.5); // the 0.5 offset gives us crisp pixel-width lines
			}
			if (lineColor != undefined) {
				context.stroke();
			}
			if (fillColor != undefined) {
				context.fill();
			}
		},

		drawCircle : function(x, y, radius, lineColor, fillColor) {
			var context = this._getContext(lineColor, fillColor);
			context.beginPath();
			context.arc(x, y, radius, 0, 2*Math.PI, false);
			if (lineColor != undefined) {
				context.stroke();
			}
			if (fillColor != undefined) {
				context.fill();
			}
		},

		drawPieSlice : function(x, y, radius, startAngle, endAngle, lineColor, fillColor) {
			var context = this._getContext(lineColor, fillColor);
			context.beginPath();
			context.moveTo(x, y);
			context.arc(x, y, radius, startAngle, endAngle, false);
			context.lineTo(x, y);
			context.closePath();
			if (lineColor != undefined) {
				context.stroke();
			}
			if (fillColor) {
				context.fill();
			}
		},

		drawRect : function(x, y, width, height, lineColor, fillColor) {
			var context = this._getContext(lineColor, fillColor);
			if (fillColor != undefined) {
				context.fillRect(x, y, width, height);
			}
			if (lineColor != undefined) {
				context.strokeRect(x, y, width, height);
			}
		}

	});

	var vcanvas_vml = function(width, height, target) {
		return this.init(width, height, target);
	};

	vcanvas_vml.prototype = $.extend(new vcanvas_base, {
		_super : vcanvas_base.prototype,

		init : function(width, height, target) {
			this._super.init(width, height, target);
			if (target[0]) {
				target = target[0];
			}
			target.vcanvas = this;
			this.canvas = document.createElement('span');
			$(this.canvas).css({ display:'inline-block', position: 'relative', overflow:'hidden', width:width, height:height, margin:'0px', padding:'0px' });
			this._insert(this.canvas, target);
			this.pixel_height = $(this.canvas).height();
			this.pixel_width = $(this.canvas).width();
			this.canvas.width = this.pixel_width;
			this.canvas.height = this.pixel_height;;
			var groupel = '<v:group coordorigin="0 0" coordsize="'+this.pixel_width+' '+this.pixel_height+'"'
					+' style="position:absolute;top:0;left:0;width:'+this.pixel_width+'px;height='+this.pixel_height+'px;"></v:group>';
			this.canvas.insertAdjacentHTML('beforeEnd', groupel);
			this.group = $(this.canvas).children()[0];
		},

		drawShape : function(path, lineColor, fillColor) {
			var vpath = [];
			for(var i=0; i<path.length; i++) {
				vpath[i] = ''+(path[i][0]-1)+','+(path[i][1]-1);
			}
			var initial = vpath.splice(0,1);
			var stroke = lineColor == undefined ? ' stroked="false" ' : ' strokeWeight="1" strokeColor="'+lineColor+'" ';
			var fill = fillColor == undefined ? ' filled="false"' : ' fillColor="'+fillColor+'" filled="true" ';
			var closed = vpath[0] == vpath[vpath.length-1] ? 'x ' : '';
			var vel = '<v:shape coordorigin="0 0" coordsize="'+this.pixel_width+' '+this.pixel_height+'" '
				+ stroke
				+ fill
				+' style="position:absolute;left:0px;top:0px;height:'+this.pixel_height+'px;width:'+this.pixel_width+'px;padding:0px;margin:0px;" '
				+' path="m '+initial+' l '+vpath.join(', ')+' '+closed+'e">'
				+' </v:shape>';
			 this.group.insertAdjacentHTML('beforeEnd', vel);
		},

		drawCircle : function(x, y, radius, lineColor, fillColor) {
			x -= radius+1;
			y -= radius+1;
			var stroke = lineColor == undefined ? ' stroked="false" ' : ' strokeWeight="1" strokeColor="'+lineColor+'" ';
			var fill = fillColor == undefined ? ' filled="false"' : ' fillColor="'+fillColor+'" filled="true" ';
			var vel = '<v:oval '
				+ stroke
				+ fill
				+' style="position:absolute;top:'+y+'; left:'+x+'; width:'+(radius*2)+'; height:'+(radius*2)+'"></v:oval>';
			this.group.insertAdjacentHTML('beforeEnd', vel);

		},

		drawPieSlice : function(x, y, radius, startAngle, endAngle, lineColor, fillColor) {
			var startx = x + Math.round(Math.cos(startAngle) * radius);
			var starty = y + Math.round(Math.sin(startAngle) * radius);
			var endx = x + Math.round(Math.cos(endAngle) * radius);
			var endy = y + Math.round(Math.sin(endAngle) * radius);

			var vpath = [  x-radius, y-radius, x+radius, y+radius, startx, starty, endx, endy ];
			var stroke = lineColor == undefined ? ' stroked="false" ' : ' strokeWeight="1" strokeColor="'+lineColor+'" ';
			var fill = fillColor == undefined ? ' filled="false"' : ' fillColor="'+fillColor+'" filled="true" ';
			var vel = '<v:shape coordorigin="0 0" coordsize="'+this.pixel_width+' '+this.pixel_height+'" '
				+ stroke
				+ fill
				+' style="position:absolute;left:0px;top:0px;height:'+this.pixel_height+'px;width:'+this.pixel_width+'px;padding:0px;margin:0px;" '
				+' path="m '+x+','+y+' wa '+vpath.join(', ')+' x e">'
				+' </v:shape>';
			 this.group.insertAdjacentHTML('beforeEnd', vel);
		},

		drawRect : function(x, y, width, height, lineColor, fillColor) {
			return this.drawShape( [ [x, y], [x, y+height], [x+width, y+height], [x+width, y], [x, y] ], lineColor, fillColor);
		}
	});

})(jQuery);
