/*
 * zoolutionFader - jQuery Plugin
 * Lightweight Content Fader
 *
 * Examples and documentation at: http://zoolution-labs.com
 * 
 * Copyright (c) 2009 - 2010 zoolution-labs.com 
 *
 * Requires: jQuery v1.3+
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 */

(function($) {

        $.fn.zoolutionFader = function(options) {
		
		// set defaults for html structure, pause, fade speed and preaload
		
          var defaults = {
          
          	zooID: '#'+$(this).attr('id'),
          	zooIndex: 0,
          	zooTimeout: 4000,
         	zooFadeIn: 600,
          	zooFadeOut: 400,
          	zooPreload: false
          
          };
        
        // override the defaults by options
         var options = $.extend(defaults, options);
          
          
       		
       	 zoolution_rotator = function(options){
  		      
  		     
  		     // count the child elements
  		       		
       		 var size = $(options.zooID+" ul").children().size();
	 			
	 		
	 		// if the loop starts 
	 		 if(options.zooIndex == "0"){
	 			
	 			// setting the step index 
	 			var step = 1;
	 			var prev = 0;
	 	
	 			var current = $(options.zooID+" ul li:nth-child("+step+")");
	    		var oldie = $(options.zooID+" ul li:nth-child("+prev+")");
	    		
	    		// show the first element
	    		$(current).fadeIn(options.zooFadeIn);
	
	 		 }
	 		
	 		 else{
	    		
	    		// setting the step index 
	    		var step = options.zooIndex+1;
	    		var prev = options.zooIndex;
	   			
	   			
	   			// if the current element ist the last element
	    		if(size < step){
	    
	    			
	    			// setting the step index 
	    			step = 1;
	    			prev = size;
	    
	    		} 
	 
	 		 
	 		 var current = $(options.zooID+" ul li:nth-child("+step+")");
	 		 var oldie = $(options.zooID+" ul li:nth-child("+prev+")"); 	
	 	
	 	
	 		 }
	 			
	 		 var update = {
	    		zooIndex: step
	    	 };
	    	
	    	 var options = $.extend(options, update);
	 		
	 		 // fade the last element to the current element
	 		 $(oldie).fadeOut(options.zooFadeIn, function() {
        		$(current).fadeIn(options.zooFadeIn);
      		 });
      		 
			
	 		 
	 		 if(size > 1){
	 		 
	 		 timeout = setTimeout(function(){
				
				zoolution_rotator(options);
	
			 },options.zooTimeout); 
       		
			};
			
			};
			
			
			
       		// hiding the images until they are loaded
       	   
       	   	if(options.zooPreload){
       	   		
       	   		$(options.zooID).html('<div class="zoolution_preloader">'+$(options.zooID).html()+'</div>');
       	   		
       	   
       	   		$(window).load( function() {
    		
    				// calling the loop
    				zoolution_rotator(options);
    			
    				// hiding the preload background image
    	    		$('.zoolution_preloader').css('background-image', 'none');
    
    	   		});
    	   	}
    	   	else{
    	   	
    	   		zoolution_rotator(options);
    	   	
    	   	}

       	   
     		

        };
})(jQuery);
