
var slideWidth = 980;
var numberOfSlides = 0;
var currentSlide = 1;
var slideSpeed = 8000;

function loadSlideShowXml(xmlLocation, callback) {
	try
	{
		$.ajax(
		{
			type: "GET",
			url: xmlLocation,
			dataType: "xml",
			success: function(xml)
			{
				if (callback)
				{
					callback(xml);
				}
				//removeLoading('loadXml');
			},
			error: function(x, s, e)
			{
				alert("xml loading error");
			}
		});
	}
	catch (e)
	{
		$.log('ERROR: (loadXml:catch) ' + e);
	}
}

function processSlideShowXml(theXml) {
	// clear out the global array
	statsArray = [];
    $("statData", theXml).each(function(i) 
    {
		var statItem = new Object();
        statItem.id = i;
		statItem.imageBg = $('imageBg', this).text();
		statItem.text = $('text', this).text();
		statItem.size = $('size', this).text();
		statItem.lineheight = $('lineheight', this).text();
		statItem.citation = $('citation', this).text();
        statsArray.push(statItem);
    });

	numberOfSlides = statsArray.length;
	
    appendTable(statsArray);
}

function appendTable(list) {

	var statsWidth = 0;
	var theDiv = $('#stats');
	
	$.each(list, function(i){ 

		statsWidth +=slideWidth;
		$('#stats').css("width",statsWidth+"px");
		
		theDiv.dom('div').attr({id:'statItem'+this.id}).addClass('statItemContainer');
		var theContainer = $('#statItem'+this.id);
		theContainer.dom('div').addClass('statImage').dom('img').attr({src:this.imageBg});
		theContainer.dom('div').addClass('statText').css("font-size",this.size).css("line-height",this.lineheight).append(this.text);
		theContainer.dom('div').addClass('statCitation').append(this.citation);

	});
	

	statsWidth +=slideWidth;
	$('#stats').css("width",statsWidth+"px");
	
	theDiv.dom('div',true).attr({id:'statItemFirst'}).addClass('statItemContainer');
	var theContainer = $('#statItemFirst');
	theContainer.dom('div').addClass('statImage').dom('img').attr({src:list[numberOfSlides-1].imageBg});
	theContainer.dom('div').addClass('statText').css("font-size",list[numberOfSlides-1].size).css("line-height",list[numberOfSlides-1].lineheight).append(list[numberOfSlides-1].text);
	theContainer.dom('div').addClass('statCitation').append(list[numberOfSlides-1].citation);
	
	statsWidth +=slideWidth;
	$('#stats').css("width",statsWidth+"px");
	
	theDiv.dom('div').attr({id:'statItemLast'}).addClass('statItemContainer');
	var theContainer = $('#statItemLast');
	theContainer.dom('div').addClass('statImage').dom('img').attr({src:list[0].imageBg});
	theContainer.dom('div').addClass('statText').css("font-size",list[0].size).css("line-height",list[0].lineheight).append(list[0].text);
	theContainer.dom('div').addClass('statCitation').append(list[0].citation);
	
	delayhide=setTimeout("resetTimer();",slideSpeed);
	
	$('#stats').css("left","-"+slideWidth+"px");
	
}

function resetTimer () {
	delayhide=setTimeout("resetTimer();",slideSpeed);
	moveToRight();
}


var containerPositionLeft = -slideWidth;


function moveToLeft() {
	
	if (containerPositionLeft == 0) {
		containerPositionLeft = -(slideWidth*(numberOfSlides-1));
		$('#stats').css("left","-"+slideWidth*numberOfSlides+"px");
	} else {
		containerPositionLeft = containerPositionLeft + slideWidth;
	}
	
	updateContainerPositionLeft(containerPositionLeft);
	
}


function moveToRight() {


	if (containerPositionLeft == -(slideWidth*(numberOfSlides+1))) {
		containerPositionLeft = -(slideWidth*2);
		$('#stats').css("left","-"+slideWidth+"px");
	} else {
		containerPositionLeft = containerPositionLeft - slideWidth;
	}
	

	updateContainerPositionLeft(containerPositionLeft);
	
}


function moveTo(n) {

	if(n!=currentSlide){
		if (n == 1) {
			containerPositionLeft = -slideWidth;
		} else {
			containerPositionLeft = -(slideWidth*n);
		}
	}
	
	updateContainerPositionLeft(containerPositionLeft,n);
}



function updateContainerPositionLeft(theLeft,n) {

	$('#stats').queue( [ ] ).stop();
	
	$('.btnStatControl').removeClass('active');
	
	
	
	if(n!=null){

		currentSlide = parseFloat(n);
	
	} else {

		currentSlide = currentSlide + 1;
		
		if(currentSlide == (numberOfSlides+1)){
			currentSlide = 1;
		}
	}


	
	$('.btnStatControl'+currentSlide+'').addClass('active');

	$("#stats").animate({ left:theLeft+"px"}, 500, "swing", function(){/*alert('hi');*/} );

}


var playPauseStatus = "playing";

function playPause() {

	if (playPauseStatus == "playing") {
	
		if (window.delayhide){
			clearTimeout(delayhide)
		}
	
		$('a[title="statControlPlayPause"]').html('play');
		playPauseStatus = "paused";

	} else {
	
		resetTimer();
	
		$('a[title="statControlPlayPause"]').html('pause');
		playPauseStatus = "playing";
	}

}

function stopShow() {

	if (playPauseStatus == "playing") {
	
		if (window.delayhide){
			clearTimeout(delayhide)
		}
	
		$('a[title="statControlPlayPause"]').html('play');
		playPauseStatus = "paused";

	}

}







