/**
 * mooSlide
 *
 * simple slideshow, based on MooTools, compatible with SlideshowPro xml
 * author Michiel Jelijs
 * (c) Cinnamon Interactive
 * version 1.1 
 * requires mootools.js and xml.js
 */

function mooSlide(id,loc,iv,ft,random) {
	this.slides       = [];
	this.loc          = loc;
	this.interval     = iv || 4000;
	this.fadeTime     = ft || 2000;
	this.id           = id;
	this.random       = random || false;
	this.currentSlide = 0;
}

mooSlide.prototype.init = function() {
	if ($(this.id)) {
		var owner = this;
		var doOnload = function(xmlObj) {
			if (xmlObj.getElementsByTagName("album").length > 0) {
				var album  = xmlObj.getElementsByTagName("album")[0];
				var path   = album.getAttribute("lgPath");
				var images = xmlObj.getElementsByTagName("img");
				owner.currentSlide = Math.floor(Math.random()*images.length);
				for (var i=0; i<images.length; i++) {
					owner.slides[i] = new Element("img");
					owner.slides[i].src = path + images[i].getAttribute("src");
					owner.slides[i].setStyle("position","absolute");
				}
				if (owner.random)
					owner.slides.sort(function() {return 0.5 - Math.random()}) //Array elements now scrambled
				owner.nextSlide();
				$(owner.id).appendChild(owner.slides[0]);
				$(owner.id).removeChild($(owner.id).childNodes[0]);
				setInterval(function(){owner.nextSlide()},owner.interval);
			}
		}
		loadXML(this.loc, doOnload);
	}
}

mooSlide.prototype.nextSlide = function() {
	var owner = this;
	this.slides[this.currentSlide].setStyle("opacity","0");
	$(this.id).appendChild(this.slides[this.currentSlide]);
	// fade in new image
	var fadeIn = this.slides[this.currentSlide].effect("opacity",{
		duration:this.fadeTime,
		onComplete:function(){
			if ($(owner.id).childNodes.length > 1)
				$(owner.id).removeChild($(owner.id).childNodes[0])
			owner.currentSlide++;
			if (owner.currentSlide >= owner.slides.length)
				owner.currentSlide = 0;
		}
	});
	fadeIn.custom(0,1);
}

