/**********************************
	Color Shifting DIVs
	Version 1.0
	Last Revision: 06.13.2004
	steve@slayeroffice.com

	PLEASE leave this notice in tact!

	Should you modify/improve this source
	please let me know about it so I can
	update the version hosted at slayeroffice.	

***********************************/

window.onload = init;
var sp = new Array();
var texttohighlight=""
var maxdivs=0
var colorScheme=12;
var colors=new Array();
colors[0] = new Array("F1DE87","869960","FFAA99","800000","D41A23");
colors[1] = new Array("FF0000","FFBF40","FFFF00","00FF00","0000FF","FF00FF");
colors[2] = new Array("FF0000","FFBF40","FFFF00");
colors[3] = new Array("638DA1","F2F6F7","98B4C1","E3EBED");
colors[4] = new Array("434469","CFBE84","16777E","F3E6C8");
colors[5] = new Array("191970","FFFFFF");
colors[6] = new Array("39630C","FFFF80");
colors[7] = new Array("F09E35","0882C6","D03201","5A709A","FFE26E","984A23");
colors[8] = new Array("7EE06F","C4C2BF","C9D1EC");
colors[9] = new Array("DDDDBB","CAAC6C","778855","EEEEDD","99AA77");
colors[10] = new Array("645339","F6F6F6","A38D6F","463928");
colors[11] = new Array("66CC33","0099CC");
colors[12] = new Array("6CB4F0","FFCF60","C60262","E4E4E4");
colors[13] = new Array("F85A3F","F73E78","470100");
colors[14] = new Array("C81624","F6DC31","454E3F","EFF6C6","AAC12B");
colors[15]= new Array("FFDA6F","ED1E24","EEEEEE")

pause = 0;

colorPercent=1.0; 
var colorPath = new Array();
var colorIndex = 0; 
var shiftIndex = new Array();
var isShifting=new Array();
var watchShift=0; 
var SHIFT_STEP=10;
var on=0; 


var d=document;
var zInterval = null;
var numspans=24

function init() {
	if(!document.getElementById) return;
	createFadeObjArray();
	colorPath=createColorPath();
	zInterval = setInterval("shiftColors()",20);
}

function createFadeObjArray() {

   slayercolor = document.getElementById("slayercolortext")

   if (slayercolor){
	cscheme=parseInt(slayercolor.className)
	if (cscheme > -1 && cscheme < 17){
		colorScheme=cscheme
	}
		
	if(texttohighlight.length==0){	

	texttohighlight = slayercolor.innerHTML
	tarray=texttohighlight.split("")
	maxdivs=tarray.length
	var newstring=""
	for(i=0;i<maxdivs;i++) {
		newstring += "<span id=\"fd" + i + "\" title=\"Click to pause or restart the Slayer Office color effect\" onClick=\"pause=pause?0:1\">" + tarray[i] + "</span>"
	}

	slayercolor.innerHTML=newstring

	}

	for(i=0;i<maxdivs;i++) {	// loop over how many divs we want to create
		fdid = "fd" + i;
		sp[i]=d.getElementById(fdid);
		sp[i].style.color = "#"+colors[colorScheme][0];
		shiftIndex[i]=0;
		isShifting[i]=0;
	}

   }


   

	isShifting[0]=1;
}


function createColorPath() {
	nColor = colorIndex+1;

	if(nColor>=colors[colorScheme].length)nColor=0;
	do {
		colorPath[colorPath.length]=setColorHue(longHexToDec(colors[colorScheme][colorIndex]),colorPercent,longHexToDec(colors[colorScheme][nColor]));
		colorPercent-=.01;
	} while(colorPercent>0);

	return colorPath;
}

function shiftColors() {
	if(pause)return;

	for(i=0;i<sp.length;i++) {

		if(isShifting[i]) {
			sp[i].style.color = "rgb("+colorPath[shiftIndex[i]][0]+","+colorPath[shiftIndex[i]][1]+","+colorPath[shiftIndex[i]][2]+")";
			shiftIndex[i]++;
		}	
		if(shiftIndex[i]>=colorPath.length) {
			colorIndex++;
			if(colorIndex>=colors[colorScheme].length)colorIndex=0;
			colorPercent=1.0;
			colorPath=createColorPath();
		}
	}
	watchShift++;
	if(watchShift>=SHIFT_STEP && on<sp.length) {
		on++;
		watchShift=0;
		isShifting[on]=1;
	}
}

// changes the color scheme.
function changeScheme(themeIndex) {
	colorScheme=themeIndex;
	normalize();
	init();
}

// resets vars back to their original states
function normalize() {
	
	clearInterval(zInterval);
	shiftIndex=new Array();
	isShifting=new Array();
	watchShift=0;
	colorIndex=0;
	colorPath=new Array();
	on=0;
	colorPercent=1.0;
}

// calculates a color based on the it being a certain percentage of another color.
function setColorHue(originColor,opacityPercent,maskRGB) {
	returnColor=new Array();
	for(w=0;w<originColor.length;w++) returnColor[w] = Math.round(originColor[w]*opacityPercent) + Math.round(maskRGB[w]*(1.0-opacityPercent));
	return returnColor;
}

// utility method to convert "FFFFFF" to an array of 255,255,255
function longHexToDec(longHex) {
	r=toDec(longHex.substring(0,2));
	g=toDec(longHex.substring(2,4));
	b=toDec(longHex.substring(4,6));
	return new Array(r,g,b);

	function toDec(hex) {
		return parseInt(hex,16);
	}
}

