/*
 * Bannimate jQuery Plug-in
 * http://armyofnone.com/bannimate
 *
 * Copyright (c) 2009 Samuel Rouse
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 *
 * Date: 2009-10-26
 * Revision: 1
 */
(function($)
{
	$.fn.bannimate = function(options)
	{
		// Set the options.
		options = $.extend({}, $.fn.bannimate.defaults, options);
		$.fn.bannimate.options = options;

		// Go through the matched elements and return the jQuery object.
		return this.each(function()
		{
			$this = $(this);			
			var obj = $(this);
		    var objHeight = obj.height();
		    obj.css("overflow","hidden");
		    
		    var childList = obj.children();
		    var childCt = childList.size();
		    $.fn.bannimate.object = obj;
		    obj.css("overflow","hidden");
		    if (childCt) {
		    	childList.each(function(i){
		    		$(childList[i]).css("margin-bottom",("-" + objHeight + "px")).css("z-index",options.baseZ + childCt - i);	// First on top
		    	});
		    	$.fn.bannimate.timer = setTimeout("$.fn.bannimate.rotate(1)",options.timeout);
		    }
		});
	};
	// Public Values
	$.fn.bannimate.defaults = {
		speed: 1000,
		timeout: 2000,
		baseZ: 2
	};
	$.fn.bannimate.timer = "";
	$.fn.bannimate.object = "";
	$.fn.bannimate.options = "";
	// Public Functions	
	$.fn.bannimate.rotate = function(ct) {
		options = $.fn.bannimate.options;		
		
		if (ct != $.fn.bannimate.object.children().size()){
			$($.fn.bannimate.object.children()[(ct - 1)]).fadeOut(options.speed,function(){
				$.fn.bannimate.timer = setTimeout("$.fn.bannimate.rotate(" + (ct + 1) + ")",options.timeout);		// Fade out the current
			});
		} else {
			$($.fn.bannimate.object.children()[0]).fadeIn(options.speed,function(){
				$.fn.bannimate.timer = setTimeout("$.fn.bannimate.rotate(1)",options.timeout);		// Fade in the topmost
				$.fn.bannimate.object.children().show();								// Show all the underlying banners
			});
		}
	}; 
})(jQuery);
