$.DefaultOptionsGallery = {
	startDelay: 	500,
	interval:	4000,
	tag:		"li"
 }

jQuery.fn.Gallery = function(options) {
	options      = Object.extend($.DefaultOptionsGallery, options || {});
	
	var $news 	= jQuery(this);
	var index	= 0 ;
	var current	= false ;
	var list		= $news.find(options.tag) ;
	
	if(!list.size()) return ;
	
	$news.rotate = function() {
		if(current) current.fadeOut(options.startDelay) ;
		
		current = $(list[index]) ;
		current.fadeIn(options.startDelay) ;
		
		if(list.size() < 2) return ;
		
		setTimeout(function () {
			index++
			index %= list.size() ;
			$news.rotate() ;
		}, options.interval) ;	
	}

	setTimeout(function () {
		$news.rotate() ;
	}, options.startDelay) ;	
} ;

(function($) {
	/**
	 * $ is an alias to jQuery object
	 *
	 */
	// plugin initialization
	$.fn.extend({
		preloadImages: function(call_back) { 
			var img_size = $(this).size() ;
			if(!img_size) call_back() ; 

			 this.each(function(){	
	 			jQuery("<img>").attr("src", $(this).attr("src")).bind("load", function() {
	 			img_size-- ;
	 			if(img_size <= 0) {
	 				call_back() ; 
	 			}
	 			}) ;
			 });
	 	}
	});	
})(jQuery); // Call and execute the function immediately passing the jQuery object


