/*Start QuoteRotator Class */
var ClientSlider = new Class ({
	initialize: function (logoArray) {
	
		/* Array of logo locations */
		this.logos = logoArray;
		
		/* HTML elements */
		this.logo_list = $('logos');
		this.forward_link = $('client_slider_right') ;
		this.backward_link = $('client_slider_left');
		
		/* Next logo to be added */
		this.currentPos = 0;
		
		/* Add logos to list */
		this.populateList();
		
		/*Add functionality to links*/
		this.forward_link.addEvent('click',function(){
			this.forward();
		}.bind(this));
		
		this.backward_link.addEvent('click',function(){
			this.backward();
		}.bind(this));		
	},
	populateList: function() {
		for(i = 0;i<5; i++) {
			li = this.makeListItem(this.logos[this.currentPos]);
			li.inject(this.logo_list);
			this.advancePos();
		}	
	},
	makeListItem: function(path) {
		li = new Element('li',{
			'styles': {
				'background-image' : 'url('+path+')'
			}
		});
		return li;
	},
	forward: function() {
	for(i = 0;i<5; i++) {
		li = this.makeListItem(this.logos[this.currentPos]);
		this.advancePos();
		li.inject(this.logo_list);
		this.logo_list.getFirst().dispose();
	}

	},
	backward: function() {
	for(i = 0;i<5; i++) {
		reversePos = this.reversePos();
		li = this.makeListItem(this.logos[reversePos]);
		li.inject(this.logo_list, 'top');
		this.logo_list.getLast().dispose();
	}
	},
	
	advancePos: function() {
		if(this.currentPos >= this.logos.length-1) {
			this.currentPos = 0;
		} else { 
			this.currentPos++;
		}
	}, 
	reversePos: function() {
		if(this.currentPos == 0){
			this.currentPos = this.logos.length-1;
		} else {
			this.currentPos--;
		}
		reversePos = this.currentPos-5;
		if(reversePos < 0) { 
			return this.logos.length + reversePos;
		} else { 
			return reversePos;
		}
	},
	debug: function(){
		$('footerContainer').set('html', this.currentPos);
	}
	

}); /*End ClientSlider Class */


//Loads logo paths from xml file
	var xmlhttp2;
	function loadLogos(url)
	{
	xmlhttp2=null;
	if (window.XMLHttpRequest)
	  {// code for all new browsers
	  xmlhttp2=new XMLHttpRequest();
	  }
	else if (window.ActiveXObject)
	  {// code for IE5 and IE6
	  xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	if (xmlhttp2!=null)
	  {
	  xmlhttp2.onreadystatechange=state_Change2;
	  xmlhttp2.open("GET",url,true);
	  xmlhttp2.send(null);
	  }
	else
	  {
	  alert("Your browser does not support XMLHTTP.");
	  }
	}
	
	function state_Change2()	{
		if (xmlhttp2.readyState==4) {// 4 = "loaded"
		    if (xmlhttp2.status==200) {// 200 = OK
			
				var path = xmlhttp2.responseXML.documentElement.getElementsByTagName("defaultPath")[0].childNodes[0].nodeValue;
				var ext = xmlhttp2.responseXML.documentElement.getElementsByTagName("defaultExtension")[0].childNodes[0].nodeValue;
				var XMLLogos = xmlhttp2.responseXML.documentElement.getElementsByTagName("logo");
				var logos = [];
				
				for(i=0;i < XMLLogos.length;i++) {
					logoFileName = XMLLogos[i].childNodes[0].nodeValue;
					logos[i] = path+logoFileName+"."+ext;
					
					//preload images
					var imagePreLoad = new Image();
					imagePreLoad.src = logos[i];
				}
				//create a slider
				var slider = new ClientSlider(logos);
			} else {
				alert("Problem retrieving XML data");
			}
		}
	}