/* ================================================================
This copyright notice must be untouched at all times.

The original version of this short url script
is available at http://www.nuff-respec.com/technology/short-url
Copyright (c) 2005-2007 Daniel Bulli. All rights reserved.
This code may be modified in any way to fit your requirements.
=================================================================== */


	// -------------------------------------------------------------
	// function do_step_style
	// @container = ID of CONTAINER
	// @element_to_style 		= ELEMENT TO STYLE
	// @sub_element_to_style 	= SUB ELEMENT TO STYLE (overrides ELEMENT) - can be null
	// @start_val 				= STARTING VALUE
	// @end_val 				= ENDING VALUE
	// @style_prop 				= property to style (color/fontSize)
	// @reset 					= whether or not to reset (can be empty)
	function do_step_style(container,element_to_style,sub_element_to_style,start_val,end_val,style_prop,reset) 
	{
		//IF WE DON'T SUPPORT getElementById or no "container" by that ID ON PAGE
		if (!document.getElementById || !document.getElementById(container)) return false;
	
		//GET ALL "element_to_style" IN "containter"
		var elements = document.getElementById(container).getElementsByTagName(element_to_style);

		//START at 0
		var cur_step = 0;
		
		//ONLY TWO THINGS SUPPORTED RIGHT NOW
		if(style_prop == 'color')
		{
			//CONVERT STARTING/ENDING HEX -> RGB
			var rgb_start= hex_to_rgb(start_val.toLowerCase());
			var rgb_end  = hex_to_rgb(end_val.toLowerCase());

			var r_s = rgb_start[0];
			var g_s = rgb_start[1];
			var b_s = rgb_start[2];
			
			var r_e = rgb_end[0];
			var g_e = rgb_end[1];
			var b_e = rgb_end[2];			
		}
		else if(style_prop == 'fontSize')
		{
			//STORE STARTING/ENDING FONT SIZE
			var f_s = start_val;
			var f_e = end_val;		
		}
		else
		{
			//BAIL IF UNSUPPORTED		
			return false;
		}
		
		//LOOP OVER "element_to_style"
		var el = elements.length - 1;
		for (var i = 0; i < elements.length; i++)
		{
			var element = elements[i];
			
			//IF WE HAVE A "sub_element_to_style" then use that
			if(sub_element_to_style != null)
			{
				if(!element.getElementsByTagName(sub_element_to_style)[0]) return false;
				
				element = element.getElementsByTagName(sub_element_to_style)[0];
			}

			if(style_prop == 'color')
			{
				//CHANGE COLOR TO NEXT IN STEP
				var r_c  =	Math.floor(r_s*((el-cur_step)/el) + r_e*(cur_step/el));
				var g_c  =	Math.floor(g_s*((el-cur_step)/el) + g_e*(cur_step/el));
				var b_c  =	Math.floor(b_s*((el-cur_step)/el) + b_e*(cur_step/el));		
				
				if(reset)
				{
					element.style.color = '';
				}
				else
				{
					element.style.color = '' + rgb_to_hex(r_c,g_c,b_c);					
				}
			}
			else if(style_prop == 'fontSize')
			{
				//CHANGE FONT TO NEXT IN STEP
				var f_c  = Math.round((f_s*((el-cur_step)/el) + f_e*(cur_step/el))*100)/100;	
				
				if(reset)
				{
					element.style.fontSize = '';
				}
				else
				{
					element.style.fontSize = '' + f_c + 'em';				
				}				
	
			}			
			
			//INCREMENT
			cur_step++;
			
		}
		
		return false;
	}
	
	//hexidecimal array 
	var hex_array = new create_array(0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f');
		
	// -------------------------------------------------------------
	// simple function to create an array
	function create_array() 
	{
		this.length = create_array.arguments.length;
		for (var i = 0; i < this.length; i++)
			this[i] = create_array.arguments[i];
			
		return this;
	}


	// -------------------------------------------------------------	
	// take a hex color (#123456 -> rgb(1,2,3)
	function hex_to_rgb(hex_color)
	{
		var rgb_array = new create_array(0,0,0);
		
		hex_color = hex_color.toLowerCase();
		
		for (var i=1; i<=6; i++)
		{
			for (var j=0; j<16; j++)
			{
				if (hex_color.charAt(i) == hex_array[j])
				{
					if (i%2 !=0)
						rgb_array[Math.floor((i-1)/2)]=eval(j)*16;
					else
						rgb_array[Math.floor((i-1)/2)]+=eval(j);
				}
			}
		}
		
		return rgb_array;	
	}
	
	// -------------------------------------------------------------	
	// rgb -> #12
	function rgb_to_hex_value(rgb_color)
	{	
		if (rgb_color < 0)
			return "00";
		else if (rgb_color > 255)
			return "ff";
		else
			return "" + hex_array[Math.floor(rgb_color/16)] + hex_array[rgb_color%16];
	}
	
	
	// -------------------------------------------------------------	
	// rgb(1,2,3) -> #123456
	function rgb_to_hex(rr,gg,bb) 
	{	
		return "#" + rgb_to_hex_value(rr)+rgb_to_hex_value(gg)+rgb_to_hex_value(bb);
	}	
