
var photos = new Array();
var current = -1;
var previous = -1;
var photoTimer;
var slideshowPlaying = false;
var slideshow_width;
var slideshow_height;
var loaded = 0;

var photo_url = url_base+'images/display/';

$(document).ready(function() {
	if (slideshow_width == null) slideshow_width = 725;
	if (slideshow_height == null) slideshow_height = 483;
	
	$('#slideshow').append($('<div id="loading"></div>'));
	
	if (!isDefined('allow_controls')) {
		allow_controls = true;
	}
	if (allow_controls) {
		setupSlideshowControls();
	}
	
	$.get(url_base+slideshow_url+'/'+iid, function(xml) {
		var images;
		$(xml).find('photo').each(function() {
			photos.push($(this).text());
		});
		preload();
	});
});

function preload() {
	var cache = [];
	$('#loading').append('<div id="results"></div>');
	$('#results').html('Loading... 0%');
	
	$('#results').css('position','absolute');
	$('#results').css('width','200px');
	$('#results').css('top','60px');
	$('#results').css('left','-75px');
	$('#results').css('text-align','center');
	
	for (var i=0;i<photos.length;i++) {
		var cacheImage = document.createElement('img');
		cacheImage.onload = imgLoaded;
		cacheImage.src = url_base+photos[i];
		cacheImage.id = 'slideshowPhoto'+i;
      	cache.push(cacheImage);

		img = $(cacheImage);
		img.css('display','none');
		img.css('position','absolute');
		img.css('top','0');
		$('#slideshow').append(img);
		
		if (!isDefined('allow_controls')) {
			allow_controls = true;
		}
		if (allow_controls) {
			img.css('cursor','pointer');
			img.click(function() {
				nextPhoto();
				pausePlayback();
			})
		}
	}
	photos = cache;
}

function imgLoaded() {
	loaded++;
	var percentage = Math.round((loaded/photos.length)*100);
	$('#results').html('Loading... '+percentage+'%');
	if (loaded == photos.length) {
		$('#loading').remove();
		nextPhoto();
		if ($('#slideshow').children().length > 2) {
			togglePlayPhoto();
		}
		else {
			pausePlayback();
		}
	}
}

function nextPhoto() {
	previous = current;
	
	if (current >= (photos.length-1))
		current = 0;
	else
		current++;
		
	setPhoto(current);
}


function prevPhoto() {
	previous = current;
	
	if (current <= 0)
		current = photos.length-1;
	else
		current--;
		
	setPhoto(current);
}

function togglePlayPhoto() {
	if (slideshowPlaying) {
		pausePlayback();
	}
	else {
		slideshowPlaying = true;
		photoTimer = setInterval("nextPhoto()",5000);
		$('#slideshowPlayToggle').html('Pause');
	}
}

function pausePlayback() {
	slideshowPlaying = false;
	clearInterval(photoTimer);
	$('#slideshowPlayToggle').html('Play');
}

function setPhoto(i) {

	if ($('#slideshow').children().length > 0) {
		$('#slideshowPhoto'+previous).fadeOut('slow', function() {
			//$('#slideshowPhoto'+previous).remove();
		});
	}
		
	$('#slideshowPhoto'+i).fadeIn('slow');
}

function setupSlideshowControls() {
	var container = $('<div id="slideshowControls"></div>');
	var prev = $('<a id="slideshowPrev" href="#">Prev</a>');
	prev.click(function(e) {	
		prevPhoto();
		pausePlayback();
		return false;
	});
	container.append(prev);

	var pause = $('<a id="slideshowPlayToggle" href="#">Pause</a>');
	pause.click(function(e) {	
		togglePlayPhoto();
		return false;
	});
	container.append(pause);
	
	var next = $('<a id="slideshowNext" href="#">Next</a>');
	next.click(function(e) {	
		nextPhoto();
		pausePlayback();
		return false;
	});
	container.append(next);
	$('#slideshow').append(container);
}

function isDefined( variable) {
    return (typeof(window[variable]) == "undefined")?  false: true;
}
