/**
 * 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,move,scale) {
	this.slides       = [];
	this.loc          = loc;
	this.interval     = iv || 4000;
	this.fadeTime     = ft || 2000;
	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 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");
				}
				owner.nextSlide();
				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]);
	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);
	}
}

