var photoContainerID = 'photo';
var captionContainerID = 'caption';
var photoOneID = 'photoOne';
var photoTwoID = 'photoTwo';
var captionOneID = 'captionOne';
var captionTwoID = 'captionTwo';
var loaderID = 'loader';

var slideshow;
var name;
var dir;
var ext;
var timer;
var fadetime;
var order;
var looping;
var captioning;
var slide;
var numLoaded = 0;
var slideIndex = 0;
var loaded = true;
var loading = false;
var nextSlideCallback;

var id;
var obj;
var photoContainer;
var captionContainer;
var photoOne;
var photoTwo;
var captionOne;
var captionTwo;
var loader;

function initSlideshow() {
	switch(initSlideshow.arguments.length) {
		case 1: 	this.id = initSlideshow.arguments[0];
					this.obj = document.body;
					break;
		case 2: 	this.id = initSlideshow.arguments[0];
					this.obj = $(initSlideshow.arguments[1]);
					break;
		case 3: 	this.id = initSlideshow.arguments[0];
					this.obj = $(initSlideshow.arguments[1]);
					this.loading = initSlideshow.arguments[2];
					break;
		default:	this.id = 1;
					this.obj = document.body;
	}
	if (loading == true)
		valign($('loader'));
	getSlideshow();
}

function buildLayout() {
	photoContainer = document.createElement('div');
	photoContainer.setAttribute('id', photoContainerID);
	Element.toggle(photoContainer);
	obj.appendChild(photoContainer);
	captionContainer = document.createElement('div');
	captionContainer.setAttribute('id', captionContainerID);
	Element.toggle(captionContainer);
	obj.appendChild(captionContainer);
	photoOne = document.createElement('img');
	photoOne.setAttribute('id', photoOneID);
	Element.toggle(photoOne);
	photoContainer.appendChild(photoOne);
	photoTwo = document.createElement('img');
	photoTwo.setAttribute('id', photoTwoID);
	Element.toggle(photoTwo);
	photoContainer.appendChild(photoTwo);
	
	if (captioning == 'yes') {
		captionOne = document.createElement('div');
		captionOne.setAttribute('id', captionOneID);
		Element.toggle(captionOne);
		captionContainer.appendChild(captionOne);
		captionTwo = document.createElement('div');
		captionTwo.setAttribute('id', captionTwoID);
		Element.toggle(captionTwo);
		captionContainer.appendChild(captionTwo);
	}
	
	new Effect.Appear(photoContainerID, { duration:fadetime });
	if (captioning == 'yes') new Effect.Appear(captionContainerID, { duration:fadetime });
}

function getSlideshow() {
	var params = 'id=' + id;
	new Ajax.Request('xml/slideshow.php', { method:'get', parameters:params, onSuccess:parseXML, onFailure:ajaxError });	
}

function ajaxError(t) {
    alert('Error ' + t.status + ' -- ' + t.statusText);
}

function parseXML(t) {
	var xml = t.responseXML;
	slideshow = xml.getElementsByTagName('slideshow')[0];
	dir = slideshow.getAttribute('dir');
	ext = slideshow.getAttribute('ext');
	timer = slideshow.getAttribute('timer');
	fadetime = slideshow.getAttribute('fadetime');
	order = slideshow.getAttribute('order');
	looping = slideshow.getAttribute('looping');
	captioning = slideshow.getAttribute('captioning');
	loadImages();
}

function loadImages() {
	numLoaded = 0;
	var image = slideshow.getElementsByTagName('image');
	slide = new Array(image.length);
	for (var x = 0; x < slide.length; x++) { slide[x] = new Array(4); }
	for (var x = 0; x < slide.length; x++) {	
		var path = image[x].getAttribute('path');
		slide[x]['file'] = path;
		slide[x]['image'] = new Image();
		try { slide[x]['caption'] = image[x].getAttribute('caption') }
		catch (e) { slide[x]['caption'] = '' }
		slide[x]['image'].src = dir + slide[x]['file'] + ext;
		if (slide[x]['image'].complete == true) {
			loadProgress();
		} else {
			Event.observe(slide[x]['image'], 'load', loadProgress);
			loaded = false;
		}
	}
	if ((loaded == false) && (loading == true))
		Element.toggle($('loader'));
}

function loadProgress() {
	numLoaded++;
	if (numLoaded == slide.length) {
		if (loading == true) {
			if ($('loader').style.display != 'none')
				Element.toggle($('loader'));
		}
		buildLayout();
		setTimeout("changePhoto(" + slideIndex + ")", 50);
	}
}

function nextSlide() {
	slideIndex++;
	if (slideIndex >= slide.length)
		slideIndex = 0;
	changePhoto(slideIndex);
}

function changePhoto(index) {
	if (!Element.visible(photoOne)) {
		photoOne.src = slide[index]['image'].src;
		photoOne.height = slide[index]['image'].height;
		photoOne.width = slide[index]['image'].width;
		valign(photoOne);
		new Effect.Appear(photoOneID, { duration:fadetime });
		if (Element.visible(photoTwo))
			new Effect.Fade(photoTwoID, { duration:fadetime });
	} else if (!Element.visible(photoTwo)) {
		photoTwo.src = slide[index]['image'].src;
		photoTwo.height = slide[index]['image'].height;
		photoTwo.width = slide[index]['image'].width;
		valign(photoTwo);
		new Effect.Appear(photoTwoID, { duration:fadetime });
		if (Element.visible(photoOne))
			new Effect.Fade(photoOneID, { duration:fadetime });
	}
	if (captioning == 'yes') changeCaption(index);
	nextSlideCallback = setTimeout("nextSlide()", timer * 1000);
}

function changeSlideshow(id) {
	this.id = id;
	obj.removeChild(photoContainer);
	if (captioning == "yes") {
		obj.removeChild(captionContainer);
	}
	clearTimeout(nextSlideCallback);
	getSlideshow();
}

function valign(elem) {
	var dimensions = Element.getDimensions(elem);
	var container = false;
	container = Try.these(
		function () { return elem.parentNode },
		function () { return elem.parentElement },
		function () { return false });
	try {
		Element.setStyle(container, { position:'relative' });
	} catch(e) {}
	Element.setStyle(elem, { position:'absolute', top:'50%', left:'50%', marginLeft:((dimensions.width / 2) * -1) + 'px', marginTop:((dimensions.height / 2) * -1) + 'px' });
}