/*@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@                                                       @
@   Copyright © 2008 whadiz.com. All rights reserved.   @
@                                                       @ 
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @*/

//Text fade
var bgcolor; 
var fcolor;  
var bgcolor2; 
var fcolor2; 
var heading;
var headingtwo;

//Number of steps to fade
var steps; 

var colors;
var colors2;
var color = 0;
var color2 = 0;

var step = 1;

var interval1;
var interval2;


//fade: fader function
// Fade from backcolor to forecolor in specified number of steps
function fade(headingtext,backcolor,forecolor,numsteps,containerid) {
	if (color == 0) {	
		steps = numsteps;
		heading = "<font color='{COLOR}'>"+headingtext+"</strong></font>";
		bgcolor = backcolor;
		fcolor = forecolor;
		colors = new Array(steps);
		getFadeColors(bgcolor,fcolor,colors);
	}

	// insert fader color into message
	var text_out = heading.replace("{COLOR}", colors[color]);
	
	// write the message to the document
	document.getElementById(containerid).innerHTML = text_out;

	// select next fader color
	color += step;

	if (color >= steps) clearInterval(interval1);
}



function fade2(headingtext,backcolor,forecolor,numsteps,containerid) {
	if (color2 == 0) {	
		steps = numsteps;
		headingtwo = "<font color='{COLOR}'>"+headingtext+"</strong></font>";
		bgcolor2 = backcolor;
		fcolor2 = forecolor;
		colors2 = new Array(steps);
		getFadeColors(bgcolor2,fcolor2,colors2);
	}

	// insert fader color into message
	var text_out = headingtwo.replace("{COLOR}", colors2[color2]);
	
	// write the message to the document
	document.getElementById(containerid).innerHTML = text_out;

	// select next fader color
	color2 += step;

	if (color2 >= steps) clearInterval(interval1);
}


//getFadeColors: fills colors, using predefined Array, with color hex strings fading from ColorA to ColorB
//Note: Colors.length equals the number of steps to fade
function getFadeColors(ColorA, ColorB, Colors) {
	len = Colors.length;

	//Strip '#' from colors if present
	if (ColorA.charAt(0)=='#') ColorA = ColorA.substring(1);
	if (ColorB.charAt(0)=='#') ColorB = ColorB.substring(1);

	//Substract red green and blue components from hex string
	var r = HexToInt(ColorA.substring(0,2));
	var g = HexToInt(ColorA.substring(2,4));
	var b = HexToInt(ColorA.substring(4,6));
	var r2 = HexToInt(ColorB.substring(0,2));
	var g2 = HexToInt(ColorB.substring(2,4));
	var b2 = HexToInt(ColorB.substring(4,6));

	// calculate size of step for each color component
	var rStep = Math.round((r2 - r) / len);
	var gStep = Math.round((g2 - g) / len);
	var bStep = Math.round((b2 - b) / len);

	// fill Colors array with fader colors
	for (i = 0; i < len-1; i++) {
		Colors[i] = "#" + IntToHex(r) + IntToHex(g) + IntToHex(b);
		r += rStep;
		g += gStep;
		b += bStep;
	}
	Colors[len-1] = ColorB; // make sure we finish exactly at ColorB
}

//IntToHex: converts integers between 0 - 255 into a two digit hex string.
function IntToHex(n) {
	var result = n.toString(16);
	if (result.length==1) result = "0"+result;
	return result;
}

//HexToInt: converts two digit hex strings into integer.
function HexToInt(hex) {
	return parseInt(hex, 16);
}

var startwidth = 0;

//scroll: Make the text scroll using the marginLeft element of the div container
function scroll(startw, containerid) {
	if (startwidth == 0) {
		startwidth=startw;
	}
	
	document.getElementById(containerid).style.marginLeft = startwidth + "px";

	if (startwidth > 1) {
		startwidth -= 1;	
	} else {
		clearInterval(interval2);
	}
}

function fadeandscroll(txt,color1,color2,numsteps,fademilli,containerwidth,scrollmilli) {
    interval1 = setInterval("fade('"+txt+"','"+color1+"','"+color2+"',"+numsteps+",'fader1')",fademilli);
    interval2 = setInterval("scroll("+containerwidth+",'fader1')",scrollmilli);
}

function fadeandscroll2(txt,color1,color2,numsteps,fademilli,containerwidth,scrollmilli) {
    interval1 = setInterval("fade2('"+txt+"','"+color1+"','"+color2+"',"+numsteps+",'fader2')",fademilli);
    interval2 = setInterval("scroll("+containerwidth+",'fader2')",scrollmilli);
}
