/*=============================    
jquery.barouselite.js 
v.0.1
Julien Decaudin - 03/2011
www.juliendecaudin.com
=============================*/

(function ($) {
    $.fn.barouselite = function (callerSettings) {
        var settings = $.extend({            
            slideDuration: 4000, //duration of each slide in milliseconds            
            fadeIn: 1, //fade between slides; activated by default
            fadeInSpeed: 600, //fade duration in milliseconds             
            debug: 0
        }, callerSettings || {});
        
        settings.imageList = $(this).find('img'); //list of the items        
        settings.currentIndex = 0; //index of the current displayed item
        settings.totalItem = settings.imageList.length; //total number of items        
        settings.timerCarousel; //timer for the carousel        
        
        if (settings.totalItem > 1) {        
        
        	// SET CONTAINER STYLE      
        	$(this).find('br').remove();
        	$(this).css('display', 'relative');
        	$(this).width(settings.imageList.eq(0).width()); 	
	        $(this).height(settings.imageList.eq(0).height());        	
        	
        	// IMAGE LIST SETUP
        	settings.imageList.each(function (n) { 
        		//set the index of each image  
        		this.index = n;
        		
        		//set css properties
        		$(this).css('position', 'absolute'); 
        		if(n > 0){
        			$(this).css('display', 'none'); 
        			$(this).css('z-index', '5'); 
        		}else{
        			$(this).addClass('default');
        			$(this).css('z-index', '10'); 
        		}
    		});			    		
	        
			// CODE EXECUTED ONCE IMAGES ARE LOADED
	        $(window).load(
	            function () {
	           		
	           		
	        });        	                        
            
            //start the carousel            
            var loadItemCall = function () { loadItem(settings, 1); };
            settings.timerCarousel = window.setTimeout(loadItemCall, settings.slideDuration);            
        }
        return this;
    };

    var loadItem = function (settings, index) {
        //Change the background image then display the new content        
        var currentImage = $(settings.imageList[settings.currentIndex]);        
        var nextImage = $(settings.imageList[index]);

        /* DEBUG */
        if (settings.debug == 1) {
            console.log('[Barousel loadItem] currentImage:' + currentImage.attr('src'));
            console.log('[Barousel loadItem] nextImage:' + nextImage.attr('src'));
        }

        if (!currentImage.hasClass('default')) { 
        	currentImage.attr('class', 'previous');         	
        	currentImage.css('z-index', '5');         	
    	}
        nextImage.attr('class', 'current');
        nextImage.css('z-index', '10');

        //fade-in effect
        if (settings.fadeIn == 0) {
            nextImage.show();
            currentImage.hide();            
        } else {                        
            nextImage.fadeIn(settings.fadeInSpeed, function () {
                currentImage.hide();
                currentImage.removeClass('previous');            
            });            
        }

        //carousel functionnality        
        settings.currentIndex = index;
        var nextIndex;

        if (settings.currentIndex == settings.totalItem - 1) {
            nextIndex = 0;
        } else {
            nextIndex = parseInt(settings.currentIndex) + 1;
        }
        var loadItemCall = function () { loadItem(settings, nextIndex); };
        settings.timerCarousel = window.setTimeout(loadItemCall, settings.slideDuration);        
    };
})(jQuery);
