/* This file contains code from Dynamic Drive, with small modifications */
/***********************************************
* Fading Scroller- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/


// addition: instead of having a fixed string in the code, we can have an multitude scrolling divs
var scrolldiv = new Array(); 

/*
* addtion to the Dynamic Drive code.  Instead of one fixed delay it is possible to
*/
function ScrollDivDef (sdivnm, sbegintag, sendtag)
{
	this.divnm = sdivnm;
	this.begintag = sbegintag;
	this.endtag = sendtag
	this.fcontent = new Array();
	this.scrollindex = 0;

	this.delay = 3000; //set delay between message change (in miliseconds)
	this.maxsteps=30; // number of steps to take to change from start color to endcolor
	this.stepdelay=40; // time in miliseconds of a single step
//**Note: maxsteps*stepdelay will be total time in miliseconds of fading effect
	this.startcolor= new Array(0,0,0); // start color (red, green, blue)
	this.endcolor=new Array(255,255,255); // end color (red, green, blue)
	this.enddelay = 7000;
	this.fadecounter;

}

/*
* addtion to the Dynamic Drive code.  Instead of one fixed delay it is possible to
* specify delay per string.  The fcontent array is changed from a simple string array
* to array of MessageWithDelay objects
*/
function MessageWithDelay (smsg,sdelay)
{
	this.msg = smsg;
	this.delay = sdelay;
}



var fadelinks=1;  //should links inside scroller content also fade like text? 0 for no, 1 for yes.

///No need to edit below this line/////////////////

var ie4=document.all&&!document.getElementById;
var DOM2=document.getElementById;
var faderdelay=0;


/*Rafael Raposo edited function*/
//function to change content
function changecontent(sdiv){
  if (scrolldiv[sdiv].scrollindex>=scrolldiv[sdiv].fcontent.length)
    scrolldiv[sdiv].scrollindex=0
  if (DOM2){
    document.getElementById(sdiv).style.color="rgb("+scrolldiv[sdiv].startcolor[0]+", "+scrolldiv[sdiv].startcolor[1]+", "+scrolldiv[sdiv].startcolor[2]+")"
    document.getElementById(sdiv).innerHTML=scrolldiv[sdiv].begintag+scrolldiv[sdiv].fcontent[scrolldiv[sdiv].scrollindex].msg+scrolldiv[sdiv].endtag;
    if (fadelinks)
      linkcolorchange(sdiv,1);
    colorfade(sdiv,1, 15);
  }
  else if (ie4)
    document.all[scrolldiv].innerHTML=scrolldiv[sdiv].begintag+scrolldiv[sdiv].fcontent[scrolldiv[sdiv].scrollindex].msg+scrolldiv[sdiv].closetag;
  scrolldiv[sdiv].scrollindex++
}

// colorfade() partially by Marcio Galli for Netscape Communications.  ////////////
// Modified by Dynamicdrive.com

function linkcolorchange(sdiv,step){
  var obj=document.getElementById(sdiv).getElementsByTagName("A");
  if (obj.length>0){
    for (i=0;i<obj.length;i++)
      obj[i].style.color=getstepcolor(sdiv,step);
  }
}

/*Rafael Raposo edited function*/
function colorfade(sdiv,step) {
  if(step<=scrolldiv[sdiv].maxsteps) {	
    document.getElementById(sdiv).style.color=getstepcolor(sdiv,step);
    if (fadelinks)
      linkcolorchange(sdiv,step);
    step++;
    scrolldiv[sdiv].fadecounter=setTimeout("colorfade('"+sdiv+"',"+step+")",scrolldiv[sdiv].stepdelay);
  }else{
    clearTimeout(scrolldiv[sdiv].fadecounter);
    document.getElementById(sdiv).style.color="rgb("+scrolldiv[sdiv].endcolor[0]+", "+scrolldiv[sdiv].endcolor[1]+", "+scrolldiv[sdiv].endcolor[2]+")";
    setTimeout("changecontent('"+sdiv+"')", scrolldiv[sdiv].fcontent[scrolldiv[sdiv].scrollindex-1].delay);
  }   
}

/*Rafael Raposo's new function*/
function getstepcolor(sdiv,step) {
  var diff
  var newcolor=new Array(3);
  for(var i=0;i<3;i++) {
    diff = (scrolldiv[sdiv].startcolor[i]-scrolldiv[sdiv].endcolor[i]);
    if(diff > 0) {
      newcolor[i] = scrolldiv[sdiv].startcolor[i]-(Math.round((diff/scrolldiv[sdiv].maxsteps))*step);
    } else {
      newcolor[i] = scrolldiv[sdiv].startcolor[i]+(Math.round((Math.abs(diff)/scrolldiv[sdiv].maxsteps))*step);
    }
  }
  return ("rgb(" + newcolor[0] + ", " + newcolor[1] + ", " + newcolor[2] + ")");
}

/*
if (ie4||DOM2)
  document.write('<div id="fscroller" style="border:1px solid black;width:'+fwidth+';height:'+fheight+'"></div>');

if (window.addEventListener)
window.addEventListener("load", changecontent, false)
else if (window.attachEvent)
window.attachEvent("onload", changecontent)
else if (document.getElementById)
window.onload=changecontent
*/

/*addition to the code */
function addScrollingDiv(str,begintag,endtag)
{
	scrolldiv[str] = new ScrollDivDef(str,begintag,endtag);
}

function setStartEndColors(str,start,end)
{
	scrolldiv[str].startcolor = start;
	scrolldiv[str].endcolor = end;
}


function addScrollerMessage(sdiv,str,delay)
{
	scrolldiv[sdiv].fcontent[scrolldiv[sdiv].fcontent.length] = new MessageWithDelay(str,delay);
}
