hexa = new makearray(16);
for(var i = 0; i < 10; i++)
{
	hexa[i] = i;
}
hexa[10]="a";
hexa[11]="b";
hexa[12]="c";
hexa[13]="d";
hexa[14]="e";
hexa[15]="f";
 
/* Easing */
	 function easeIn (t, b, c, d) {
		return c*t/d + b;
	}


// startColor = HexToRGB(startColor.toLowerCase());
// endColor = HexToRGB(endColor.toLowerCase());
//var this.fadeId = new Array();
function HexToRGB(Color)
{
	var colorArr = new makearray(3);
	for (var i=1; i<7; i++)
	{
		for (var j=0; j<16; j++)
		{
			if (Color.charAt(i) == hexa[j])
			{
				if (i%2 !=0)
				{
					colorArr[Math.floor((i-1)/2)]=eval(j)*16;
				}
				else
				{
					colorArr[Math.floor((i-1)/2)]+=eval(j);
				}
			}
		}
	}
	
	return colorArr;
}

function RGBToHex(RGB)
{

	var r=hex(RGB[0]);
	var g=hex(RGB[1]);
	var b=hex(RGB[2]);
	var value="#"+r+g+b;
	return value;
}


function makearray(n)
{
	this.length = n;
	for(var i = 1; i <= n; i++)
	{
		this[i] = 0;
	}
	return this;
}

function hex(i)
{
	i = Math.round(i);
	
	if (i < 0)
	{
		return "00";
	}
  else if (i > 255)
	{
		return "ff";
	}
	else
	{
		return "" + hexa[Math.floor(i/16)] + hexa[i%16];
	}
}


/* Fade colors effect class */
function Fade(el_id,startHex,endHex,d, id)
{
	this.id = id;
	this.startHex = startHex;
	this.endHex = endHex;
	this.startColor=HexToRGB(this.startHex.toLowerCase());
	this.endColor=HexToRGB(this.endHex.toLowerCase());
	this.duration=d;
	this.el_id=el_id;
	this.interval=0;
	this.startTime=null;
	this.lastTime=null;
			 
	//Fade.Instances[$(this.el_id).id] = this;
}

Fade.prototype.start=function()
{
	this.startTime=new Date().getTime();
	this.stopped=false;
	
	this.interval=setInterval('Fade.Instances[\'' + this.id + '\'].tween()',1);
}

Fade.prototype.stop=function()
{
	this.stopped=true;
	clearInterval(this.interval);
	
	this.onDoneAnimating();
}

Fade.prototype.end=function()
{
	this.stopped=true;
	clearInterval(this.interval);

	this.setColor($(this.el_id), this.endHex);	
	this.onDoneAnimating();
	
	this.startColor=HexToRGB(this.startHex.toLowerCase());
	this.endColor=HexToRGB(this.endHex.toLowerCase());
}

Fade.prototype.tween=function()
{
		//alert(this.duration);
		this.lastTime=new Date();
		var timePassed=this.lastTime.getTime()-this.startTime;


	//	alert(timePassed + ":" + this.duration);
		if (timePassed< this.duration)
		{			
			var newR=easeIn (timePassed, this.startColor[0], this.endColor[0]-this.startColor[0], this.duration);
			var newG=easeIn (timePassed, this.startColor[1], this.endColor[1]-this.startColor[1], this.duration);
			var newB=easeIn (timePassed, this.startColor[2], this.endColor[2]-this.startColor[2], this.duration);
			var hr = hex(newR);
			var hg = hex(newG);
			var hb = hex(newB);
			
			this.setColor($(this.el_id),"#"+hr+hg+hb);

		}
		else
		{
			this.end();
		}
}

Fade.prototype.setColor=function(element,color)
{
	element.style.color = color;
}

Fade.prototype.styleTemplate=function()
{
	
}

Fade.prototype.onDoneAnimating=function()
{
	//alert("onDoneAnimating!: " + $(this.el_id).id);
		
}

Fade.Instances=new Object();



