﻿/* 
IMAGE TRANISTION SCRIPT

Author: Robert McInnes
For Client: Doodlefish
Date: 21/04/2010
*/

(function($) {

jQuery.fn.applyTrans = function(options){

	//Apply default options
	options = mergedefaults(options);
	
	//Create a self replenishing queue 
	$(this).children('img').each(function(){
		var image = $(this);
		var f = function() {
			image.parent().queue('images',f);
			//trigger event
			image.trigger('makevisible');
		}
		image.parent().queue('images',f);
	});
	$(this).children('img').css({'position':'absolute',	'top': options.top,	'left': options.left,'z-index': 0}).bindevents(options);
	$(this).children('img:last').siblings('img').css('display','none'); // Hide all but the last one
	
	$(this).dequeue('images');
	
}
jQuery.fn.bindevents = function(options){
	$(this).bind('makevisible', function(){
		$(this).css('z-index', 10); //push this image to front (still hidden)
		$(this).fadeIn(options.transpeed,function(){
			$(this).siblings('img').css('display', 'none');
			$(this).css('z-index', 0);
			var p = $(this).parent();
			setTimeout(function(){p.dequeue('images')},options.timebetween);
		});
	});
}
})(jQuery);

function mergedefaults(options){
	var defaults = { 
	top:			0, 
	left:			0,
	visibleclass:	'madevisible',
	transpeed:		4000,
	timebetween:	3000
	}; 
	return $.extend({}, defaults, options); 
}
