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

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

mooSlide.prototype.init = function() {
	if ($(this.id)) {
		var owner = this;
		var xmlCall = new Ajax(this.loc, {
			method : 'get',
			onComplete : function(dataTXT,xmlObj) {
				if (xmlObj.getElementsByTagName("album").length > 0) {
					var album  = xmlObj.getElementsByTagName("album")[0];
					var path   = album.getAttribute("lgPath");
					var images = xmlObj.getElementsByTagName("img");
					if (owner.startSlide == "random")
						owner.currentSlide = Math.floor(Math.random()*images.length);
					else
						owner.currentSlide = owner.startSlide;
					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].title = images[i].getAttribute("title");
						owner.slides[i].setStyle("position","absolute");
					}
					owner.nextSlide();
					setInterval(function(){owner.nextSlide()},owner.interval);
				}
			}
		}).request();
	}
}

mooSlide.prototype.nextSlide = function() {
	var owner = this;
	this.slides[this.currentSlide].setStyle("opacity","0");
	$(this.id).appendChild(this.slides[this.currentSlide]);
	var previous = this.currentSlide - 1;
	if (previous < 0)
		previous = this.slides.length - 1;
	// 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);
	// fade out previous image
	//var fadeOut = this.slides[previous].effect("opacity",{
	//	duration:this.fadeTime
	//});
	//fadeOut.custom(1,0);
	// move image
	if (this.move) {
		var moveImg = this.slides[this.currentSlide].effect("marginLeft",{
			duration:(this.interval)*2
		});
		moveImg.custom(20,-20);
	}
	// scale image
	if (this.scale) {
		var w = this.slides[previous].offsetWidth;
		var scaleOut = this.slides[previous].effect("width",{
			duration:this.fadeTime
		});
		scaleOut.custom(w,w*0.9);
	}
}

