// testimonialLoadScript.js (vin@madlab.tv)

var startcolor= new Array(236,233,216);
var endcolor=new Array(0,0,0);
var testimonialArray=new Array();
var xmlFileName = "../scripts-and-flash/testimonials.xml";

var wait=4000;   // time between message change.
var maxsteps=80; // number of steps from start to end.
var stepwait=40; // time of a single step.

// get instructor name.
var pathName=location.pathname;
var charPositionFile = pathName.lastIndexOf("/")+1;
var baseFileName = pathName.substring(charPositionFile, pathName.length-4);
var charPositionName = baseFileName.lastIndexOf("instructor")+11;
var baseName = baseFileName.substring(charPositionName);
var instructorName = baseName.toUpperCase();
var generalName = "GENERAL";

begintag='<div>';
closetag='</div>';
if (testimonialArray.length == 0) testimonialArray[0] = ''; // initialize array to avoid "undefined" entry.

// load XML file.
if (window.ActiveXObject){ // if IE.
	xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async=false;
	xmlDoc.load(xmlFileName);
	getArray();
}else if (document.implementation && document.implementation.createDocument){ // if NN/Mozilla.
	xmlDoc= document.implementation.createDocument("","",null);
	xmlDoc.load(xmlFileName);
	xmlDoc.onload=getArray;
}else{
	alert('Your browser cannot handle this script');
}

var notWhiteSpace = /\S/;
function getArray() {
	// get instructors testimonials - if none found use general.
	var instructorElement = instructorName;
	if (!xmlDoc.getElementsByTagName(instructorElement)[0]) instructorElement = generalName;
	var testimonialObj = xmlDoc.getElementsByTagName(instructorElement)[0];

	// remove white spaces in XML DOM tree.
	for (i=0; i<testimonialObj.childNodes.length; i++) {
		if ((testimonialObj.childNodes[i].nodeType == 3) && (!notWhiteSpace.test(testimonialObj.childNodes[i].nodeValue))) {
			testimonialObj.removeChild(testimonialObj.childNodes[i]);
			i--;
		}
	}
	
	// feed XML into testimonialArray().
	for (i=0; i<testimonialObj.childNodes.length; i++) {
		testimonialArray[i] = xmlDoc.getElementsByTagName(instructorElement)[0].childNodes[i].firstChild.nodeValue;
	}
}

var ie4=document.all&&!document.getElementById;
var DOM2=document.getElementById;
var marker=0;

function fadeArray(){
	if (marker>=testimonialArray.length) marker=0;
	if (DOM2){
		document.getElementById("testimonials").style.color="rgb("+startcolor[0]+", "+startcolor[1]+", "+startcolor[2]+")"
	    document.getElementById("testimonials").innerHTML=begintag+testimonialArray[marker]+closetag
	    doFade(1, 15);
	}
	else if (ie4) document.all.testimonials.innerHTML=begintag+testimonialArray[marker]+closetag;
	marker++;
}

var counter;
function doFade(step) {
	if (testimonialArray.length != 1) { // only doFade if more than one testimonial element.
		if(step<=maxsteps) {	
    		document.getElementById("testimonials").style.color=retrieveCurrentColor(step);
	    	step++;
		    counter=setTimeout("doFade("+step+")",stepwait);
		}else{
	    	clearTimeout(counter);
	    	document.getElementById("testimonials").style.color="rgb("+endcolor[0]+", "+endcolor[1]+", "+endcolor[2]+")";
		    setTimeout("fadeArray()", wait);
		}
	} else { // if only one testimonial element text is static.
   		document.getElementById("testimonials").style.color="rgb("+endcolor[0]+", "+endcolor[1]+", "+endcolor[2]+")";
	}
}

function retrieveCurrentColor(step) {
	var contrast
	var newcolor=new Array(3);
	for (var i=0;i<3;i++) {
		contrast = (startcolor[i]-endcolor[i]);
		if (contrast > 0) {
			newcolor[i] = startcolor[i]-(Math.round((contrast/maxsteps))*step);
		} else {
			newcolor[i] = startcolor[i]+(Math.round((Math.abs(contrast)/maxsteps))*step);
		}
	}
	return ("rgb(" + newcolor[0] + ", " + newcolor[1] + ", " + newcolor[2] + ")");
}

if (window.addEventListener) window.addEventListener("load", fadeArray, false);
else if (window.attachEvent) window.attachEvent("onload", fadeArray);
else if (document.getElementById) window.onload=fadeArray;
