

/* general variables */

var quotesId = 'quotes'; /* change this to the ID of the quotes list */
var	quotes; /* this will be the object reference to the list later on */
var quotesList; /* array that will hold all child elements of the list */
var currQuote; /* keeps track of which quote should currently be showing */
var prevQuote;
var preInitTimer;
var opacity = 100;
var objCurrQuote;
var qtyQuotes;
preInit();

/* functions */

function fader(obj,opacity) {
	
	if (obj.style) {
		if (obj.style.MozOpacity!=null) {  
			/* Mozilla's pre-CSS3 proprietary rule */
			obj.style.MozOpacity = (opacity/100) - .001;
		}
		if (obj.style.opacity!=null) {
			/* CSS3 compatible */
			obj.style.opacity = (opacity/100) - .001;
		} 
		if (obj.style.filter!=null) {
			/* IE's proprietary filter */
			obj.style.filter = "alpha(opacity="+opacity+")";
		}		
	}
}



function preInit() {
	/* an inspired kludge that - in most cases - manages to initially hide the quote quotes list
	   before even onload is triggered (at which point it's normally too late, and the whole list already
	   appeared to the user before being remolded) */
	if ((document.getElementById)&&(quotes=document.getElementById(quotesId))) {
		quotes.style.visibility = "hidden";
		if (typeof preInitTimer != 'undefined') clearTimeout(preInitTimer); /* thanks to Steve Clay http://mrclay.org/ for this small Opera fix */
	} else {
		preInitTimer = setTimeout("preInit()",2);
	}
}


function buildQuoteList() {
	if (document.getElementById) {
		preInit(); /* shouldn't be necessary, but IE can sometimes get ahead of itself and trigger buildQuoteList first */
		quotesList = new Array;
		var node = quotes.firstChild;

		/* instead of using childNodes (which also gets empty nodes and messes up the script later)
		we do it the old-fashioned way and loop through the first child and its siblings */
		while (node) {
			if (node.nodeType==1) {
				quotesList.push(node);
			}
			node = node.nextSibling;
		}
		
		/* make the list visible again */
		quotes.style.visibility = 'visible';
		currQuote = Math.floor( Math.random()* (quotesList.length));

		/* start showing the quotes process after a second's pause */
		//window.setTimeout("showRandomQuote(-1)", 0);
		window.setTimeout("showNextQuote(-1)", 0);
		
	}
}


function showRandomQuote(fade) {
	// fade = 1 (fade up), fade = -1 (fade down), fade = 0 (none)
	
	var nextQuote;
	
	objCurrQuote=quotesList[currQuote];
	
	if (fade == -1 ) {
		opacity -= 10; 
		fader(objCurrQuote,opacity);
		if (opacity <= 0) {
			// done fading it out
			objCurrQuote.style.display = 'none';
			while( currQuote == (nextQuote=Math.floor( Math.random()* (quotesList.length)))){
				//do nothing
			}
			// document.title = "currQuote=" + currQuote + " opacity=" + opacity + " fade=" + fade;
			currQuote = nextQuote;
			objCurrQuote=quotesList[currQuote];
			fader(objCurrQuote,0);
			objCurrQuote.style.display = 'block';
			fade = 1;
		}
		window.setTimeout("showRandomQuote("+fade+")", 50);
		return;
	}
	if (fade == 1) {
		opacity += 10;
		fader(objCurrQuote,opacity);
		if (opacity < 100) {
			window.setTimeout("showRandomQuote("+fade+")", 50);
			return;
		}
		fade = -1;
		window.setTimeout("showRandomQuote("+fade+")", 5000);
		return;
	}	
}


function showNextQuoteWithFade(fade) {
	// fade = 1 (fade up), fade = -1 (fade down), fade = 0 (none)
	objCurrQuote=quotesList[currQuote];
	// document.title = "currQuote=" + currQuote + " opacity=" + opacity + " fade=" + fade;
	if (fade == -1 ) {
		opacity -= 10; 
		fader(objCurrQuote,opacity);
		if (opacity <= 0) {
			// done fading it out
			objCurrQuote.style.display = 'none';
			currQuote += 1;
			if (currQuote>=quotesList.length) currQuote=0;			
			objCurrQuote=quotesList[currQuote];
			fader(objCurrQuote,0);
			objCurrQuote.style.display = 'block';
			fade = 1;
		}
		window.setTimeout("showNextQuote("+fade+")", 50);
		return;
	}
	if (fade == 1) {
		opacity += 10;
		fader(objCurrQuote,opacity);
		if (opacity < 100) {
			window.setTimeout("showNextQuote("+fade+")", 50);
			return;
		}
		fade = -1;
		window.setTimeout("showNextQuote("+fade+")", 5000);
		return;
	}	
}


function showNextQuote() {
	// fade = 1 (fade up), fade = -1 (fade down), fade = 0 (none)
	objCurrQuote=quotesList[currQuote];
	// document.title = "currQuote=" + currQuote + " opacity=" + opacity + " fade=" + fade;

	objCurrQuote.style.display = 'none';
	currQuote++;
	if (currQuote>=quotesList.length) currQuote=0;			
	objCurrQuote=quotesList[currQuote];
	objCurrQuote.style.display = 'block';
	
	return;		
}

function showPrevQuote() {
	// fade = 1 (fade up), fade = -1 (fade down), fade = 0 (none)
	objCurrQuote=quotesList[currQuote];
	// document.title = "currQuote=" + currQuote + " opacity=" + opacity + " fade=" + fade;

	objCurrQuote.style.display = 'none';
	currQuote--;
	if (currQuote<0) currQuote=quotesList.length-1;
	objCurrQuote=quotesList[currQuote];
	objCurrQuote.style.display = 'block';
	
	return;		
}
		
/* initialise fader by hiding quote object first */
addEvent(window,'load',buildQuoteList)



/* 3rd party helper functions */

/* addEvent handler for IE and other browsers */
function addEvent(elm, evType, fn, useCapture) 
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
 if (elm.addEventListener){
   elm.addEventListener(evType, fn, useCapture);
   return true;
 } else if (elm.attachEvent){
   var r = elm.attachEvent("on"+evType, fn);
   return r;
 }
} 

