/**
 * jSlide
 *
 * simple slideshow, based on jQuery, compatible with SlideshowPro xml
 * author Michiel Jelijs
 * (c) Cinnamon Interactive
 * version 0.1
 * requires jquery.js
 */

function jSlide(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;
}

jSlide.prototype.init = function() {
	if ($(this.id)) {
		var owner = this;
		var xmlCall = $.ajax({
			url: this.loc,
			dataType : 'xml',
			success : function(res) {
				if (res.getElementsByTagName("album").length > 0) {
					var album  = res.getElementsByTagName("album")[0];
					var path   = album.getAttribute("lgPath");
					var images = res.getElementsByTagName("img");
					if (owner.startSlide == "random")
						owner.currentSlide = Math.floor(Math.random()*images.length);
					else
						owner.currentSlide = owner.startSlide;
					$('#'+owner.id+' img').css("position","absolute");
					for (var i=0; i<images.length; i++) {
						owner.slides[i] = document.createElement("img");
						owner.slides[i].src = path + images[i].getAttribute("src");
						$(owner.slides[i]).css("position","absolute");
						$(owner.slides[i]).css("display","block");
					}
					owner.nextSlide();
					setInterval(function(){owner.nextSlide()},owner.interval);
				}
			}
		});
	}
}

jSlide.prototype.nextSlide = function() {
	var owner = this;
	var previous = owner.currentSlide - 1;
	if (previous < 0)
		previous = owner.slides.length - 1;
	// fade in new image
	$(owner.slides[owner.currentSlide]).fadeIn(owner.fadeTime,function(){
		if (document.getElementById(owner.id).childNodes.length > 1)
			document.getElementById(owner.id).removeChild(document.getElementById(owner.id).childNodes[0])
		owner.currentSlide++;
		if (owner.currentSlide >= owner.slides.length)
			owner.currentSlide = 0;
	});
	document.getElementById(owner.id).appendChild(owner.slides[owner.currentSlide]);
	// Below: needs fixing for jquery!
	// 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);
	}
	*/
}

