// Fader - (c)2002 Rick Franchuk @ TranSpecT Computer Consulting inc. <rickf@transpect.net>
//
// Simple program to fade in and out a message within a <SPAN> or <DIV>. Looks nice, and
// lets you avoid busting out the java for some simple flash and dash.
//
// Originally based on another program (I don't remember who's), but it's a complete rewrite
// aside from the names of some of the arrays, so I suppose it doesn't much matter :)
//
// Use:
//      Embed this jscript into your page via <SCRIPT SRC=fader.js></SCRIPT>
//      Add your mess,time,fade and link elements in another <SCRIPT>, where
//        mess[] is the message to display
//        time[] is the length of time to display the faded-in message
//        fade[] is the length of time to delay after the faded-out message
//        link[] is the URL that the message will link to
//      Invoke "fadeEvent()" to start the fadey goodness!
//
// License:
//          You can use this program as an element in your webpage for any purpose.
//          Please send updates back to me if you like it and find ways to improve it.


//// *NOTE* All the variables below can be overridden in your webpage
//// along with your mess[], time[] etc settings.

// Total time (in ms) to perform a fade-in or fade-out
var fadetime = 400;

// Total number of color steps in the fade
// Each step will take (fadetime / steps) milliseconds
var steps    = 10;

// Peak and Faded colors, separated into thier R, G, and B elements
// Someday I'll add that in as an array element.
var peakr    = 0xDB; var peakg = 0x94; var peakb = 0x70;
var fader    = 0xff; var fadeg = 0xff; var fadeb = 0xff;
//DB9470
// The name of the <DIV> or <SPAN> DOM object to fade text in and out of
var fadename = "faderquote";

// ... and finally, the target frame name for <A HREF>s.
var target   = "fade";

///////////////////////////////////////////////
// No User-Servicable Parts Below This Point //
///////////////////////////////////////////////

var mess=new Array();
var time=new Array();
var fade=new Array();
var link=new Array();

var stage="FadeIn";
var msg=0;
var step=0;
var fadetimer=0;
var rdelta = (peakr-fader)/steps;
var gdelta = (peakg-fadeg)/steps;
var bdelta = (peakb-fadeb)/steps;

function fadeEvent()
{
	
  var r, g, b, c, message;
  var nmsg  = msg;
  var delay = fadetime / steps;

  switch(stage) {

    case "FadeIn":
			r = Math.floor(fader + rdelta * step);
			g = Math.floor(fadeg + gdelta * step);
			b = Math.floor(fadeb + bdelta * step);
			c = genColor(r,g,b);
			step++;
			if(step>steps) {
			  step  = 0;
			  delay = time[msg];
			  stage = "FadeOut";
			}
			break;

    case "FadeOut":
			r = Math.floor(peakr - rdelta * step);
			g = Math.floor(peakg - gdelta * step);
			b = Math.floor(peakb - bdelta * step);
			c = genColor(r,g,b);
			step++;
			if(step>steps) {
			  step  = 0;
			  delay = fade[msg];
			  stage = "FadeIn";
                          nmsg  = (msg + 1) % mess.length;
			}
  }
  message = mess[msg];
  getFadenameElement(fadename).innerHTML=message;
  thediv = document.getElementById("quoteboxdiv");
 
 thep = document.getElementById("quotep");
 thep.style.color = c;
 
  clearTimeout(fadetimer);
  setTimeout("fadeEvent()", delay);
  msg = nmsg;
}

// Add in functionality that older netscape browsers can understand...
// ... assuming there's a point. NS may not have the balls to handle DOM updates like this.
function getFadenameElement(fn) {
  return document.getElementById(fn);
}

function genColor(red,green,blue)
{
  rr = toHex(red);
  gg = toHex(green);
  bb = toHex(blue);
  return rr+gg+bb;
}

// Unfortunately, this function is necessary cuz some browsers don't honor toString(radix).
// Stupid browsers.

var hexdigits = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");

function toHex(v) {
  var h = Math.floor(v/16);
  var l = v%16;

  return hexdigits[h]+hexdigits[l];
}

// Reverse Compatibility function
function watchit() { fadeEvent(); }
