var isIE;
var isOpera;

$(document).ready(function() {
	isIE = (navigator.appName == "Microsoft Internet Explorer");
	isOpera = (navigator.appName == "Opera");
	
	getData();
	
	/*
	$.ajax({
    url: '/xml/speakers.xml',
    type: 'GET',
    dataType: 'xml',
    timeout: 2000,
    error: function(){
        alert('Error loading XML document');
    },
    success: function(xml){
		isIE = (navigator.appName == "Microsoft Internet Explorer");
		
		var rawData = xml.getElementsByTagName("speaker");
		var speakers = [];
		var l = rawData.length;
		for(var i=0;i<l;i++){
			var obj = new Object();
			obj.img = rawData[i].attributes[0].value;
			obj.url = rawData[i].attributes[1].value;
			
			if(!isIE){
				obj.name = xml.getElementsByTagName("name")[i].textContent;
				obj.credentials = xml.getElementsByTagName("credentials")[i].textContent;
				obj.location = xml.getElementsByTagName("location")[i].textContent;
			}else{
				obj.name = xml.getElementsByTagName("name")[i].text;
				obj.credentials = xml.getElementsByTagName("credentials")[i].text;
				obj.location = xml.getElementsByTagName("location")[i].text;
			}
			obj.divRef = null;
			speakers.push(obj);
		}
		var speakerNav = new SpeakersNav(speakers, true);
    }
	});
	*/

});


function getData(){
$.ajax({
    url: '/xml/speakers.xml',
    type: 'GET',
    dataType: 'xml',
    timeout: 10000,
    error: function(XMLHttpRequest, textStatus, errorThrown){
        //alert('Error loading XML document ::: '+XMLHttpRequest+" :: "+textStatus+" :::: "+errorThrown);
		//console.log("ERROR");
    },
    success: function(xml){
		isIE = (navigator.appName == "Microsoft Internet Explorer");
		
		var rawData = xml.getElementsByTagName("speaker");
		var speakers = [];
		var l = rawData.length;
		for(var i=0;i<l;i++){
			var obj = new Object();
			obj.img = rawData[i].attributes[0].value;
			obj.url = rawData[i].attributes[1].value;
			
			if(!isIE){
				obj.name = xml.getElementsByTagName("name")[i].textContent;
				obj.credentials = xml.getElementsByTagName("credentials")[i].textContent;
				obj.location = xml.getElementsByTagName("location")[i].textContent;
			}else{
				obj.name = xml.getElementsByTagName("name")[i].text;
				obj.credentials = xml.getElementsByTagName("credentials")[i].text;
				obj.location = xml.getElementsByTagName("location")[i].text;
			}
			obj.divRef = null;
			speakers.push(obj);
		}
		var speakerNav = new SpeakersNav(speakers, true);
    }
});
}

function SpeakersNav(p_speakers, p_randomize){
	this.speakerData = p_speakers; //array of speaker objects
	this.speakerClips; //array of div references
	this.holder = "#scrollArea";
	this.startWidth;
	this.startPosition;
	this.actualWidth;
	this.randomize = p_randomize;
	this.firstClick=false;
	this.divWidth = 239;
	this.divMargin = 7;
	this.btnLeft = ("#scrollLeft");
	this.btnRight = ("#scrollRight");
	
	this.init = function(){
		$(this.btnLeft).hide();
		var thisObj = this;
		if(this.randomize){ this.speakerData = randomizeArray(this.speakerData); }
		this.actualWidth = (this.speakerData.length * this.divWidth);
		
		$(this.holder).width(this.actualWidth);
		
		this.build();
		this.startWidth = $(this.holder).width();
		
		if($(this.holder).position().left != null){
			this.startPosition = $(this.holder).position().left;
		}else{
			this.startPosition = 25;
		}
		
		$(this.btnLeft).click(function(){ thisObj.scrollPrev(); });
		$(this.btnRight).click(function(){ thisObj.scrollNext(); });
		
	}
	
	this.build = function(){
		$(this.holder).hide();
		this.speakerClips = [];
		
		var l = this.speakerData.length;
		for(var i=0;i<l;i++){
			//console.log(i+" ::: "+this.speakerData[i]+" ::: "+$(this.holder));
			$(this.holder).append("<div id='speaker"+i+"' class='speaker'><img src='"+this.speakerData[i].img+"' alt=Photo of '"+this.speakerData[i].name+"' /><div class='info'><a href='"+this.speakerData[i].url+"' class='name'>"+this.speakerData[i].name+"</a><p class='creds'>"+this.speakerData[i].credentials+"</p><p class='location'>"+this.speakerData[i].location+"</p></div></div>");
			this.speakerData[i].divRef = $("#speaker"+i);
		}
		
		$(this.holder).fadeIn('slow');
	}
	
	this.scrollPrev = function(){
		var thisObj = this;
		var futurePos = $(this.holder).position().left + this.divWidth;
		if($(this.holder).position().left < this.startPosition){
			if(futurePos >= this.startPosition){ $(this.btnLeft).fadeOut('fast'); }
			$(this.holder).animate({
    				left: '+='+this.divWidth+'px'
  				}, 250,"linear", function() {
    			// Animation complete.
				$(thisObj.btnRight).fadeIn('fast');
 	 		});
		}
	}
	
	this.scrollNext = function(){
		var measuredWidth = (this.speakerData.length * this.divWidth) - this.divMargin;
		var visibleWidth = $("#scrollWindow").width();
		var maxLeft = visibleWidth - measuredWidth + this.startPosition;
		if(isIE){ var maxLeft = visibleWidth - measuredWidth + this.startPosition; } //+ this.divWidth
		var dif = visibleWidth - measuredWidth;
		var thisObj = this;
		
		var amt = this.divWidth;
		if(isOpera && $(this.holder).position().left==this.startPosition && !this.firstClick){ amt=this.divWidth + this.startPosition; }
		
		var futurePos = $(this.holder).position().left - amt;
		
		//alert(futurePos+" ::: "+$(this.holder).position().left+" :::: "+maxLeft);
		
		if($(this.holder).position().left > maxLeft){
			if(futurePos <= maxLeft || futurePos - 10 <= maxLeft){ $(this.btnRight).fadeOut('fast'); }
		
			$(this.holder).animate({
    				left: '-='+amt+'px'
  				}, 250, function() {
    				// Animation complete.
					$(thisObj.btnLeft).fadeIn('fast');

 	 		});
		}
		this.firstClick=true
	}
	this.init();

}
