/*****************************************
/* 	Display Quotes
/*	April 22, 2008
/* 	Ver 1.00
/*		
/* 	Written by Samuel J. Lovetro, III
/* 	Site: lovetro.com
/*
/*	Function:
/*		Will read a series of <blockquote> elements formatted like:
/*			<blockquote>
/*				<p class="testimonial">[some text]</p>
/*				<p class="giver">[some text]</p>
/*			</blockquote>
/*
/*		Will output the following HTML code:
/*			<blockquote>
/*				<h2>Testimonial</h2>
/*				<p.testimonial>[text]</p>
/*				<p.giver>[text]</p>
/*			</blockquote>
*/
function addLoadEvent (func) {
//alert ("addLoadEvent: func="+func);
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			func();
		}
	}
}
function opacity(id, opacStart, opacEnd, millisec) {
//alert(" opacity invoked. id="+id);
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

function displayQuotes (aDiv) {
//Assumptions: 
//	aDiv exists
//	aDiv contains blockquotes
//	aDiv = 	<blockquote>
//				<h2>Testimonial</h2>
//				<p.testimonial>[text]</p>
//				<p.giver />[text]</p>
//			</blockquote>
//alert("displayQuotes invoked. aDiv="+aDiv);
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById(aDiv)) return false;
	
	// Make an array of all of the blockquotes in aDiv
	var quoteDiv = document.getElementById(aDiv);
	var quotesToStore = quoteDiv.getElementsByTagName("blockquote");
	quotes = new Array;
	for ( var i=0; i<quotesToStore.length; i++ ) quotes[i] = quotesToStore[i].cloneNode(true);
	
	// Empty quoteDiv of contents
	while (quoteDiv.hasChildNodes()) quoteDiv.removeChild(quoteDiv.firstChild);
	
	// Add blockquote for replacement
	var dummy = document.createElement("blockquote");
	dummy.setAttribute ("id", "displayQuote");
	var dummyHead = document.createElement("h2");
	dummyHead.appendChild(document.createTextNode("Testimonial"));
	dummy.appendChild( dummyHead );
	var dummyTesti = document.createElement("p");
	dummyTesti.className = "testimonial";
	dummy.appendChild( dummyTesti );
	var dummyGiver = document.createElement("p");
	dummyGiver.className="giver";
	dummy.appendChild( dummyGiver );
	quoteDiv.appendChild( dummy );

	// Add first quote
	replaceQuoteContent();
	
	// Set the quotes to automatically replace
	setInterval( function() { replaceQuotes( aDiv ) }, 10000);
}

function replaceQuotes (aDiv) {
//Assumptions: 
//	aDiv exists: contains ONE blockquote
//	quotes exists: contains an array of blockquotes

//alert("replaceQuotes invoked. aDiv="+aDiv);
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById(aDiv)) return false;
	
	// Make a reference to aDiv, named quoteDiv
	var quoteDiv = document.getElementById(aDiv);
	
	// Fadeout the existing quote
	opacity( "displayQuote", 100, 0, 1000);
	
	// Set timeout for replacing quote contents
	setTimeout(replaceQuoteContent, 1200);
	
	// Set timeout for redisplay of quote
	setTimeout( function (){opacity( "displayQuote", 0, 100, 1000);}, 2000 );
}

function replaceQuoteContent () {
//Assumptions: 
// Quote = 
//	<blockquote>
//		<h2>Testimonials</h2>
//		<p.testimonial>[text]</p>
//		<p.giver>[text]</p>
//	</blockquote>

	// Get the new quote
	numQuote++;
	if (numQuote==quotes.length) numQuote = 0;
	var newQuote = quotes[numQuote].cloneNode(true);
	//newQuote.setAttribute( "id", "newDisplayQuote" );
	var oldQuote = document.getElementById( "displayQuote" );

	// Replace content
	// Get reference to old data
	var oldTesti, oldGiver;
	var newTesti, newGiver;
	var oldP = oldQuote.getElementsByTagName("p");
	var newP = newQuote.getElementsByTagName("p");
	for (var i=0; i < oldP.length; i++) {
		switch (oldP[i].className) {
			case "testimonial": 
				oldTesti = oldP[i]; 
				break;
			case "giver":
				oldGiver = oldP[i]; 
				break;
			//default: result = 'unknown';
		}
	}
	// Get reference to new data
	for (var i=0; i < newP.length; i++) {
		switch (newP[i].className) {
			case "testimonial": 
				newTesti = newP[i]; 
				break;
			case "giver":
				newGiver = newP[i]; 
				break;
		}
	}
	oldQuote.replaceChild( newTesti, oldTesti );
	oldQuote.replaceChild( newGiver, oldGiver );
}

var numQuote = -1; 										// Which quote is being displayed
var quotes; 											// Array to hold the quotes already displayed in the page
var mootools=false; 										// Boolean for mootools use
var aDivSlider;											// Storage for mootools effect

addLoadEvent( function () {
	displayQuotes('testimonialQuotes');
});
