/*
Title:      jQuery.slideshow
Description:Simple fading slideshow
Developer:  Antenna Praxis (http://theantenna.org)
Date:       June 2009
Version:    0.0.1
Usage:      
Notes:      • Constrains img WH to max dimensions
			• Pauses to load unloaded img
			• Pauses on rollover
			• Diff img dimensions = fade out->in instead of crossfade
To Do:      
License:    Dual licensed under the MIT and GPL licenses:
            http://www.opensource.org/licenses/mit-license.php,
            http://www.gnu.org/licenses/gpl.html
*/

(function($) {
		  
	that = null,
	opts = null,
	interval = null,
	div = null,
	imgs = null,
	id = 0,
	total = 0,
	isBusy = false;
	
	$.fn.slideshow = function(selector, options)
	{
		opts = $.extend(this.slideshow.defaults, options);
		
		div = that = this;
		imgs = $(this).find(selector);
		
		total = imgs.length;
		//log('total',total);
		
		// prepare images
		div.css({position:'relative'});
		
		// set z-index stack
		imgs.each(function(i){
			$(this).css({position:'absolute', zIndex:imgs.length-i })//.attr('id','img_'+i)
				.mouseover(function(e){ stop(); })
				.mouseout(function(e){ start(); }); // pause slideshow on rollover
		})
		
		imgs.eq(0).show(); // show first image
		imgs.slice(1).hide(); // hide others
		
		// constrain first img
		var img = imgs.eq(0).find('img');
		if( !img.height() ) img.one('load', function(){log('delayed'); constrain(img) } );
		else constrain(img);
		
		return that;
	};
	
	stop = $.fn.slideshow.stop = function()
	{
		clearInterval(interval);
		
		return this;
	};
	
	start = $.fn.slideshow.start = function()
	{
		interval = setInterval( function(){ change() }, opts.duration );
		
		return this;
	};
	
	change = $.fn.slideshow.change = function(dir)
	{
		dir = dir || 1;
		
		if (isBusy) return false;
		isBusy = true;
		
		var old_img_id = id,
			new_img_id = id = ( id+dir>=total? 0 : id+dir<0? total-1 : id+dir ),
			that = this;
		
		//reset all
		imgs.hide().css({zIndex:0});
		
		var n = imgs.eq(new_img_id).show().css({zIndex:1}),
			nimg = n.find('img');
		
		if(!nimg.height()) // wait for new img to load
		{
			log(new_img_id,'no height');
			stop();
			nimg.one('load',function(e){
				log(new_img_id,'now loaded');		
				start(); id--; change(1);
			});
			return this;
		}
		
		constrain(nimg);
		
		var o = imgs.eq(old_img_id).css({zIndex:2}).show(),
			oimg = o.find('img');
		
		if(nimg.width() == oimg.width() && nimg.height() == oimg.height() ) // dims same, can crossfade
		{
			var o = imgs.eq(old_img_id).css({zIndex:2}).show().fadeOut("slow",function(){
				that.isBusy = false;
			});
		}
		else // no crossfade
		{
			n.hide();
			o.fadeOut("fast",function(){
				that.isBusy = false;
				n.fadeIn("slow");
			});
		}
		
		//log('-');
		//log( 'old', old_img_id, o );
		//log( 'new', new_img_id, n );
		
		return this;
	};
	
	constrain = function(img)
	{
		if(img.width() > opts.maxW){
			//log('constraining',img, 'to W');
			img.width(opts.maxW);
		}
		if(img.height() > opts.maxH){
			//log('constraining',img, 'to H');
			img.height(opts.maxH);
		}
	};
	
})(jQuery);

// defaults
jQuery.fn.slideshow.defaults = {
	duration:4000,
	maxW:300,
	maxH:300
};

