/*
	Sparklines for jQuery
	Copyright (c) 2006 Franck Marcia
	Licensed under the MIT license


	Part of the code adapted from sparkline of TiddlyWiki (http://tiddlywiki.com):

	TiddlyWiki 2.1.2 by Jeremy Ruston, (jeremy [at] osmosoft [dot] com)
	Copyright (c) Osmosoft Limited 2004-2006

	Redistribution and use in source and binary forms, with or without modification,
	are permitted provided that the following conditions are met:

	Redistributions of source code must retain the above copyright notice, this
	list of conditions and the following disclaimer.

	Redistributions in binary form must reproduce the above copyright notice, this
	list of conditions and the following disclaimer in the documentation and/or other
	materials provided with the distribution.

	Neither the name of the Osmosoft Limited nor the names of its contributors may be
	used to endorse or promote products derived from this software without specific
	prior written permission.


	Part of the code (canvas) adapted from sparkline by John Resig
	(http://ejohn.org/projects/jspark/)
*/

jQuery.fn.sparkline = function(options)
{
	// Options
	options = jQuery.extend({
		color: '#000000',
		width: 2,
		backgroundColor: null,
		useCanvas: false
	}, options || {});

	// Can use a color string or an array
	if (options.color.constructor != Array) {
		options.color = options.color.split(',');
	}

	return this.each(function(){

		// Retrieve et prepare values
		var min = 0, max = 0, data = [], row = this.innerHTML.split(',');
		for (var i = 0; i < row.length; ++i) {
			var v = parseInt(row[i], 10);
			if (v < min) min = v;
			if (v > max) max = v;
			data.push(v);
		}
		if (data.length < 2) return;

		// Retrieve dimensions
		jQuery(this.parentNode).append('<span id="x_x_x">&nbsp;</span>');
		var h = jQuery('#x_x_x')[0].offsetHeight;
		var w = jQuery('#x_x_x')[0].offsetWidth;
		jQuery('#x_x_x').remove();

		// Use a canvas element
		if (options.useCanvas && window.CanvasRenderingContext2D) {

			var id = '__' + (new Date).getTime();
			w = options.width * data.length;

			jQuery(this).html('<canvas id="' + id + '" height="' + h + '" width="' + w + '"></canvas>');

			jQuery('#' + id).each(function(){
				if (this.getContext) {

					var c = this.getContext('2d');

					c.lineWidth = 1.0;
					if (options.backgroundColor) {
						c.fillStyle = options.backgroundColor;
						c.fillRect(0, 0, w, h);
					}
					c.strokeStyle = options.color[0];

					c.beginPath();
					for (var i = 0; i < data.length; ++i) {
						var v = Math.floor(((data[i] - min) / (max - min)) * h);
						c.lineTo(w * i / (data.length - 1), h - v);
					}
					c.stroke();
				}
			});

		// Use images
		} else {

			// Prepare the container
			jQuery(this).css({
				paddingRight:(data.length * options.width - w) + 'px',
				position:'relative',
				border:'0'
			}).html('&nbsp;');
			if (options.backgroundColor) {
				jQuery(this).css('backgroundColor', options.backgroundColor);
			}

			// Create bars
			for (var i=0, c=0; i<data.length; ++i, c=i%options.color.length) {
				var v = Math.floor(((data[i] - min) / (max - min)) * h);
				jQuery(this).append([
					'<img ',
						'border="0" ',
						'style="',
							'position:absolute;',
							'background:', options.color[c], ';',
							'width:' , options.width, 'px;',
							'height:', v, 'px;',
							'left:'  , (i * options.width), 'px;',
							'top:'   , (h - v), 'px" ',
						'src="data:image/gif,GIF89a%01%00%01%00%91%FF%00%FF%FF%FF',
						'%00%00%00%C0%C0%C0%00%00%00!%F9%04%01%00%00%02%00%2C%00',
						'%00%00%00%01%00%01%00%40%02%02T%01%00%3B"/>'].join('')
				);
			}
		}
	});
}
