
<!--
// JavaScript Document
// @author Richard Christie
// @version 0.01 pre-release
// Tortor, I've commented most of the code somewhat, so hopefully you should be able to see what is going on...

// global variables
var panic_red = '#bd2910';			// default fade-in-to colour
var panic_bg = document.bgColor;	// grab the bg colour to fade out to
var up = true;						// we assume they are all invisible to start with
var maxSteps = 100;					// maximum number of steps between faded in and out
var step = 0;						// initial step is 0 - leave alone!
var fadeTime = 10;					// time in milliseconds for each step					
var fadeItems;						// our copy of the objects to fade
var curName;						// current fading item
var timer;							// interrupt timer

// this is opptional - pass values as needed to configure, or 0 for default value
// call this BEFORE calling 'initFade'
function configureFade( inSteps, inStepTime )
{
	if ( inSteps != 0 )	 // number of steps
		maxSteps = inSteps;
	if ( inStepTime != 0 ) // time per step in milliseconds
		fadeTime = inStepTime;
}

// html file must call this (usually in the body 'onLoad' section to init fading.
// objects is the array of div id's for objects that are to be faded
function initFade( objects )
{	
	fadeItems = objects;
	startNewFade();
	if ( !panic_bg )	// assume white
		panic_bg = "#FFFFFF";  

	timer = setTimeout("fadeIt();", fadeTime );
}

// randomly choose a new object for the next fade
function startNewFade()
{
	
	var which = randomNumber( fadeItems.length );
	var x;
	if ( curName )	// move the object back down in case of overlap
		{
		x = getFadeObj( curName );
		x.style.zIndex = 0;
		x.style.visibility = "hidden";
		}
	
	// loops here to kill some randomness - we stop the same object fading twice in a row
	// as this looks silly...	
	while ( curName == fadeItems[which] )
		which = randomNumber( fadeItems.length );
		
	curName = fadeItems[which];
	
	x = getFadeObj( curName );
	x.style.zIndex = 1;	// puil current object up in case of overlap
	x.style.visibility = "visible";
}

function fadeIt()
{
	// do out fade for this time
	var x = getFadeObj( curName );
	var col = scaleObjColor();
	// we have to store in a color first or stupid old Netscrap goes wrong
	x.style.color = col;
	
	// and prep variables for next time
	if ( up )
		{
		step++;
		if ( step > maxSteps )
			{
			up = false;
			step = maxSteps;
			}
		}
	else
		{
		step--;
		if ( step < 0 )
			{
			up = true;
			step = 0;
			startNewFade();
			}
		}
	
	// reset the timer
	timer = setTimeout("fadeIt();", fadeTime );
}

//deep 'n' dirty stuff
function scaleObjColor()
{
	// grab hex colour string into segments e.g. string is "#rrggbb"
	var col1 = new Array ( panic_red.substring( 1,3 ), panic_red.substring( 3, 5 ), panic_red.substring( 5 ) );
	var col2 = new Array ( panic_bg.substring( 1,3 ), panic_bg.substring( 3, 5 ), panic_bg.substring( 5 ) );
	var col3 = new Array ( "","","" );
	var i, first, second;
	
	
	// calc the fade between for each bit (r,g,b) keep working in hex as much as posssible
	for ( i = 0 ; i < 3 ; i++ )
		{
		first = parseInt( col1[i], 16 );
		second = parseInt( col2[i], 16 );
		
		if ( isNaN( first ) )
			first = 0;
		if ( isNaN( second ) )
			second = 0;
		
		col3[i] = decToHex( second - ((second-first)*(step/maxSteps)) );
		}
	// reconstruct a valid hex string from it...
	return "#"+col3[0]+col3[1]+col3[2];
}

// Example: obj = findObj("image1");
function getFadeObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}


// generate a random number
function randomNumber(limit)
{
	
	return Math.floor(Math.random()*limit);
}

// convert a decimal digit to a hex string
function decToHex(dec)
{
	var hexStr = "0123456789abcdef";
	var low = dec % 16;
	var high = (dec - low)/16;
	hex = "" + hexStr.charAt(high) + hexStr.charAt(low);
	return hex;
}


//-->
