/*
	SLIDESHOW CLASS
	-
	a div of imgs
	an interval
	on interval:
		hide all imgs
		show old, new (old above new)
		fade old
*/

Slideshow$Class =
{
	constructor: function(options)
	{
		jQuery.extend(this, Slideshow$Class);
		jQuery.extend(true, this, options);
	},
	
	DURATION:4000,
	interval:null,
	div:null,
	id:0,
	total:0,
	isBusy:false,
	
	init: function(div)
	{
		this.div = div;
		
		var imgs = div.find('img');
		this.total = imgs.length;
		
		// stack all imgs in div
		div.css({position:'relative',height:$(imgs[0]).height()});
		imgs.each(function(i){
			$(this).css({position:'absolute',zIndex:imgs.length-i});
		})
		
		return this;
	},
	
	stop: function()
	{
		clearInterval(this.interval);
	},
	
	start: function()
	{
		// show first image
		this.div.find('img:eq(0)').show();
		
		var that = this;
		this.interval = setInterval( function(){that.change()} , this.DURATION );
		
		return this;
	},
	
	change: function(dir)
	{
		dir = dir || 1;
		
		if (this.isBusy) return false;
		this.isBusy = true;
		
		var old_img_id = this.id,
			new_img_id = this.id = (this.id+dir>=this.total)? 0 : (this.id+dir<0)? this.total-1 : this.id+dir,
			that = this;
		
		this.div.children('img').each(function(i){
			$(this)
				.hide()
				.css({zIndex:0});
		});
		
		var n = this.div.children('img:eq('+new_img_id+')')
			.show()
			.css({zIndex:1});
		
		//log( 'new', n.css("z-index") );
			
		var o = this.div.children('img:eq('+old_img_id+')')
			.css({zIndex:2})
			.show()
			.fadeOut("slow",function(){ that.isBusy = false });
		
		//log( 'old', o.css("z-index") );
		
		return n;
	}
	
};
Slideshow = Slideshow$Class.constructor;
