/*Start QuoteRotator Class */
var QuoteRotator = new Class ({
	initialize: function (quoteArray, delay) {
	
		/* Array of quotes and the amount of delay between them */
		this.quotes = quoteArray;
		this.delay = delay;
		
		/* HTML elements */
		this.comment_box = $('comment');
		this.name_box = $('name') ;
		this.title_box = $('title');
		this.company_box = $('company');
		
		/* Current Quote */
		this.currentPos = 0;
		
		/* Show first quote and set to continue */
		this.displayNextQuote();
		this.autoForward();
		
		
	},
	fadeInNextQuote: function() { 

		this.fadeOut();
		display = function () {this.displayNextQuote();}.bind(this);
		display.delay(500);
		fadeIn = function() {this.fadeIn();}.bind(this);
		fadeIn.delay(500);
		
	},
	displayNextQuote: function() {
	
		
		//Update HTML with new information
		this.comment_box.set('html', this.quotes[this.currentPos]['comment']);
		this.name_box.set('html', this.quotes[this.currentPos]['name']);
		this.title_box.set('html', this.quotes[this.currentPos]['title']);
		this.company_box.set('html', this.quotes[this.currentPos]['company']);
		
		//If we're at the last quote, restart
		
		if(this.currentPos >= this.quotes.length - 1){
			this.currentPos=0;
		} else {
			this.currentPos++;
		}
		
	
	}, 
	
	fadeOut : function() {
	this.comment_box.fade(0);
	this.name_box.fade(0);
	this.title_box.fade(0);
	this.company_box.fade(0);
	},
	
	fadeIn : function() {
	this.comment_box.fade(1);
	this.name_box.fade(1);
	this.title_box.fade(1);
	this.company_box.fade(1);
	},
	
	autoForward: function() {
		go = function(){this.fadeInNextQuote()}.bind(this);
		go.periodical(this.delay);
	}





}); /*End QuoteRotator Class */


//Loads quotes from xml file
	var xmlhttp;
	function loadQuotes(url)
	{
	xmlhttp=null;
	if (window.XMLHttpRequest)
	  {// code for all new browsers
	  xmlhttp=new XMLHttpRequest();
	  }
	else if (window.ActiveXObject)
	  {// code for IE5 and IE6
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	if (xmlhttp!=null)
	  {
	  xmlhttp.onreadystatechange=state_Change;
	  xmlhttp.open("GET",url,true);
	  xmlhttp.send(null);
	  }
	else
	  {
	  alert("Your browser does not support XMLHTTP.");
	  }
	}
	
	function state_Change()
	{
	if (xmlhttp.readyState==4)
	  {// 4 = "loaded"
	  if (xmlhttp.status==200)
		{// 200 = OK
		var delay = xmlhttp.responseXML.documentElement.getElementsByTagName("timer")[0].childNodes[0].nodeValue;
		var XMLquotes = xmlhttp.responseXML.documentElement.getElementsByTagName("quote");
		var quotes = [];
		for(i=0;i < XMLquotes.length;i++) {
			quotes[i] = [];
			quotes[i]['comment'] = '"'+XMLquotes[i].getElementsByTagName("comment")[0].childNodes[0].nodeValue+'"';
			quotes[i]['name'] = XMLquotes[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
			quotes[i]['title'] = XMLquotes[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
			quotes[i]['company'] = XMLquotes[i].getElementsByTagName("company")[0].childNodes[0].nodeValue;
		}
		var quoteRotator = new QuoteRotator(quotes,delay);
		}
	  else
		{
		alert("Problem retrieving XML data");
		}
	  }
	}