
var Fader = function (instanceName,width, height, delay, maxsteps , stepDelay, startcolorArray ,endcolorArray, contentArray, fadelinks, begintag, closetag)
{
	this.InstanceName  = instanceName;
	this.Width = width                     || '100%';
	this.Height = height                   || '100%';
	this.Delay = delay                     || '8000';
	this.Maxsteps = maxsteps               || '30';
	this.StepDelay = stepDelay             || '40';
	this.StartcolorArray = startcolorArray || new Array(255,255,255);
	this.EndcolorArray = endcolorArray     || new Array(000,000,000);
	this.ContentArray = contentArray 	   || new Array('Defalut','Defalut','Defalut');
	this.Fadelinks = fadelinks			   || '1';
	this.Begintag = begintag			   || '<div>';	
	this.Closetag = closetag			   || '</div>';	
	this.Ie4 = document.all&&!document.getElementById;
	this.DOM2 = document.getElementById;
	this.Index = 0;
	this.Fadecounter ="";
}

Fader.prototype.Create = function(){
	if (this.Ie4||this.DOM2)
		document.write('<div id="'+this.InstanceName+'" style="border:0px solid black;width:'+this.Width+';height:'+this.Height+'"></div>');
		this.changecontent();
}

//function to change content
 Fader.prototype.changecontent = function(){
  if (this.Index>=this.ContentArray.length)
    this.Index=0
  if (this.DOM2){
    document.getElementById(this.InstanceName).style.color="rgb("+this.StartcolorArray[0]+", "+this.StartcolorArray[1]+", "+this.StartcolorArray[2]+")"
    document.getElementById(this.InstanceName).innerHTML=this.Begintag+this.ContentArray[this.Index]+this.Closetag
    
    if (this.Fadelinks){
      this.linkcolorchange(1);
    }
    this.colorfade(1, 15);
  }
  else if (this.Ie4)
    document.all[this.InstanceName].innerHTML=this.Begintag+this.ContentArray[this.Index]+this.Closetag;
  this.Index++
}

 Fader.prototype.linkcolorchange  = function(step){
  var obj=document.getElementById(this.InstanceName).getElementsByTagName("A");
  if (obj.length>0){
    for (i=0;i<obj.length;i++)
      obj[i].style.color=this.getstepcolor(step);
  }
}

/*Rafael Raposo edited function*/
Fader.prototype.colorfade  = function(step) {
 var MySelf = this;
  if(step<=this.Maxsteps) {	
    document.getElementById(this.InstanceName).style.color=this.getstepcolor(step);
    if (this.Fadelinks)
      this.linkcolorchange(step);
    step++;
    //this.Fadecounter=setTimeout("this.colorfade("+step+");",this.StepDelay);
    this.Fadecounter=setTimeout(function(test){MySelf.colorfade(step);},this.StepDelay);
  }else{
    clearTimeout(this.Fadecounter);
    document.getElementById(this.InstanceName).style.color="rgb("+this.EndcolorArray[0]+", "+this.EndcolorArray[1]+", "+this.EndcolorArray[2]+")";
    setTimeout(function(){MySelf.changecontent();}, this.Delay);
  }   
}

/*Rafael Raposo's new function*/
Fader.prototype.getstepcolor  = function(step) {
  var diff
  var newcolor=new Array(3);
  for(var i=0;i<3;i++) {
    diff = (this.StartcolorArray[i]-this.EndcolorArray[i]);
    if(diff > 0) {
      newcolor[i] = this.StartcolorArray[i]-(Math.round((diff/this.Maxsteps))*step);
    } else {
      newcolor[i] = this.StartcolorArray[i]+(Math.round((Math.abs(diff)/this.Maxsteps))*step);
    }
  }
  return ("rgb(" + newcolor[0] + ", " + newcolor[1] + ", " + newcolor[2] + ")");
}
