/* Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Color functions from Steve's Cross Browser Gradient Backgrounds v1.0 (steve@slayeroffice.com && http://slayeroffice.com/code/gradient/)
 *
 * $LastChangedDate: 2007-06-26 19:52:18 -0500 (Tue, 26 Jun 2007) $
 * $Rev: 2163 $
 *
 * Version 1.0
 */
(function($) {

/**
 * Adds a gradient to the background of an element.
 *
 * @example $('div').gradient({ from: '000000', to: 'CCCCCC' });
 *
 * @param Map options Settings/options to configure the gradient.
 * @option String from The hex color code to start the gradient with.
 * 		By default the value is "000000".
 * @option String to The hex color code to end the gradient with.
 * 		By default the value is "FFFFFF".
 * @option String direction This tells the gradient to be horizontal
 *      or vertical. By default the value is "horizontal".
 * @option Number length This is used to constrain the gradient to a
 *      particular width or height (depending on the direction). By default
 *      the length is set to null, which will use the width or height
 *      (depending on the direction) of the element.
 * @option String position This tells the gradient to be positioned
 *      at the top, bottom, left and/or right within the element. The
 *      value is just a string that specifices top or bottom and left or right.
 *      By default the value is 'top left'.
 *
 * @name gradient
 * @type jQuery
 * @cat Plugins/gradient
 * @author Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
 */


 
$.fn.gradientLite = function(options) {
	
	
	// MODIFIED TO BE ABLE TO CHANGE WITH CSS
	
	/*options = $.extend({ from: '000000', to: 'ffffff', direction: 'horizontal', position: 'top', length: null }, options || {});*/
	var endColor = $(this).css("background-color");
	var startColor = $(this).css("border-top-color");	

	Number.prototype.clamp = function(min,max)
	{
		var c = this;
		if(c < min)
			c = min;
		if(c > max)
			c = max;
		return c;
	};
	String.prototype.right = function(n)
	{
		return n < this.length ? this.slice(this.length-n) : this;
	};

	RGB = function(r,g,b)
	{
		this.r = 0;
		this.g = 0;
		this.b = 0;
		if(typeof r == "string") {
			if(r.substr(0,1) == "#") {
				if(r.length == 7) {
					this.r = parseInt(r.substr(1,2),16)/255;
					this.g = parseInt(r.substr(3,2),16)/255;
					this.b = parseInt(r.substr(5,2),16)/255;
				} else {
					this.r = parseInt(r.substr(1,1),16)/15;
					this.g = parseInt(r.substr(2,1),16)/15;
					this.b = parseInt(r.substr(3,1),16)/15;
				}
			} else {
				var rgbPattern = /rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/;
				var c = r.match(rgbPattern);
				if(c) {
					this.r = parseInt(c[1],10)/255;
					this.g = parseInt(c[2],10)/255;
					this.b = parseInt(c[3],10)/255;
				}
			}
		} else {
			this.r = r;
			this.g = g;
			this.b = b;
		}
		return this;
		
	}
	RGB.prototype.mix = function(c,f)
	{
		return new RGB(this.r + (c.r-this.r) * f,this.g + (c.g-this.g) * f,this.b + (c.b-this.b) * f);
	};
	
	// Return an rgb colour as a #rrggbb format hex string
	RGB.prototype.toString = function()
	{
		return "#" + ("0" + Math.floor(this.r.clamp(0,1) * 255).toString(16)).right(2) +
					 ("0" + Math.floor(this.g.clamp(0,1) * 255).toString(16)).right(2) +
					 ("0" + Math.floor(this.b.clamp(0,1) * 255).toString(16)).right(2);
	};

	options = $.extend({ hicolors: false, locolors: [new RGB(startColor), new RGB(endColor)], horizontal: false}, options || {});
	// END OF MODIFY
	
	return this.each(function() {
		var $this = $(this), html = [];
		if(!options.hicolors)
			options.hicolors = options.locolors;
		for(var t=0; t<= 100; t+=2) {
			var bar = document.createElement("div");				

			bar.style.position = "absolute";
			bar.style.left = options.horizontal ? t + "%" : 0;
			bar.style.top = options.horizontal ? 0 : t + "%";
			bar.style.width = options.horizontal ? (101-t) + "%" : "100%";
			bar.style.height = options.horizontal ? "100%" : (101-t) + "%";
			bar.style.zIndex = 0;		
			var p = t/100*(options.locolors.length-1);
			bar.style.backgroundColor = options.hicolors[Math.floor(p)].mix(options.locolors[Math.ceil(p)],p-Math.floor(p)).toString();
			html.push(bar);
		}

		//$this.wrapInner('<div class="holder" style="display: block; position: relative; z-index: 2;"></div>');
		//$this.css({position:'relative'});
		//$this.append("<div class='gradient' style='position: absolute; top:0; left:0; width:100%; height:"+$this.outerHeight()+"px'></div>");
		$(".gradient").append(html);
	});
};

})(jQuery);
