(function($) {
    $.fn.coverflowCurio = function(options) {
        var defaults = {
            width: "960px",
            classSlideContainer: "slide",
            delay: 15000,
            vitesseSlide: 500,
            idPaginationContainer: "coverflowRessourcePagination",
            activeDiapo: true
        };

        var opts = $.extend(defaults, options);
        var timer = new Object;
        var timerStopSlide = new Object;
        var idObjet = this.selector;
        var $Slides = $(idObjet);
        var nbSlide = getNbSlide();
        var numCurrentSlide = 1;
        var nextSlide = numCurrentSlide + 1;
        var nbPassage = 0;
        function init()
        {
        	createPagination();
        	if (opts.activeDiapo)
        	{
        		window.clearTimeout(timer);
        		timer = setTimeout(function(){
    				callMove(nextSlide);
    			},opts.delay);
        	}
        	
        	
        	/*$(idObjet).mousewheel(function(event, delta) {
				if (delta < 0) {
					if (parseInt(numCurrentSlide) < nbSlide)
					{
						slideToGo = parseInt(numCurrentSlide) + 1;
			        	moveSlide(slideToGo);
					}
				}else if (delta > 0){
					if (parseInt(numCurrentSlide) > 1)
					{
						slideToGo = parseInt(numCurrentSlide) - 1;
			        	moveSlide(slideToGo);
					}
				}
			});*/
        	
        }
        
        function createPagination()
        {
        	pictoPage = "";
        	for (i=1; i<=nbSlide; i++) {
        		pictoPage = pictoPage + "<span class='off' id='gotoSlide_" + i + "'></span>";
    		}
        	$("#" + opts.idPaginationContainer).html(pictoPage);
        	activPictoPagination();
        }
        
        function activPictoPagination()
        {
        	$("#" + opts.idPaginationContainer + "> span").each(function(i){
        		if (i == numCurrentSlide - 1)
        		{
                	$(this).removeClass("off");
        			$(this).addClass("on");
        		}
        		else
        		{
        			$(this).removeClass("on");
                	$(this).addClass("off");
        		}
        	});
        	$("#" + opts.idPaginationContainer + "> span").not("#" + opts.idPaginationContainer + "> .on").unbind("click").click(function() {
        		numPageToGo = this.id;
        		numPageToGo = numPageToGo.split("_");
        		numPageToGo = numPageToGo[1];
        		moveSlide(numPageToGo);
        	});
        }
        
        function moveSlide(numSlideToGo)
        {
        	if (opts.activeDiapo)
        	{
        		window.clearTimeout(timer);
        	}
        	// EFFET SUR LES ELEMENTS "PAR DESSUS"
        	$(".infoSlide").animate({bottom: -200}, 100);
        	$(".periode").animate({marginTop: -100}, 100);
        	$(".objetContainer").fadeOut("fast");
        	
        	var tailleMoveSlide=opts.width;
        	//vitesse du slide
        	var vitesseSlide=opts.vitesseSlide;

        	if (numSlideToGo == 1)
			{
				deplacement = 0;
			}
			else if (numSlideToGo == 2)
			{
				deplacement = tailleMoveSlide;
			}
			else
			{
				deplacement = tailleMoveSlide * (numSlideToGo - 1);
			}
			//detecteHtml(numSlideToGo,parseInt($Slides.data("currentSlide")));
        	
        	$Slides.animate(
    			{right:deplacement},
    			parseInt(vitesseSlide),
    			function(){
    					$Slides.data("currentSlide",numSlideToGo);
    					// EFFET SUR LES ELEMENTS "PAR DESSUS"
    					setTimeout(function(){
    						$(".infoSlide").animate({bottom: 0}, 200);
    						$(".periode").animate({marginTop: 0}, 200);
    						$(".objetContainer").fadeIn("slow");
    					},200);
    					numCurrentSlide = numSlideToGo;
    					window.clearTimeout(timer);
    					nextSlide = getNextSlide();
    					activPictoPagination();
    					$Slides
    						.data("currentSlide",numSlideToGo);
    					timer = setTimeout(function(){
    						callMove(nextSlide);
    					},opts.delay);
    				}
    		);
        	
        	/*$Slides
				.animate({right: deplacement}, parseInt(vitesseSlide));
			*/
			if (opts.activeDiapo)
        	{
				window.clearTimeout(timer);
				timer = setTimeout(function(){
					callMove(nextSlide);
				},opts.delay);
        	}
        }
        
        
        function callMove(numSlideToGo)
        {
        	nbPassage = nbPassage + 1;
        	if (nbPassage <= nbSlide)
        	{
        		moveSlide(numSlideToGo);
        	}
        	else
        	{
        		window.clearTimeout(timer);
        		window.clearTimeout(timerStopSlide);
        		timer = setTimeout(function(){
    				callMove(nextSlide);
    			},60000);        		
        	}
        }
        function getNextSlide()
        {
        	if (numCurrentSlide < nbSlide)
        	{
        		leProchain = parseInt(numCurrentSlide) + 1;
        	}
        	else
        	{
        		leProchain = 1;
        	}
        	return parseInt(leProchain);
        }
        
        function detecteHtml(slideId,currentSlide)
        {
        	if (opts.widthDecalage > 0)
        	{
        		if (slideId > currentSlide)
            	{
        			htmlSlideRight(slideId);
            	}
            	else
            	{
            		htmlSlideLeft(slideId);
            	}
        	}
        }
        
        function getNbSlide()
        {
        	var j = 0;
        	$(idObjet + "> ." + opts.classSlideContainer).each(function(i){
        		j = j + 1;
        	});
        	return j;
        }
        
        // initialisation du slide
        $(this).each(init);

        return $(this);
    };
})(jQuery);



/*! Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net)
 * Licensed under the MIT License (LICENSE.txt).
 *
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 *
 * Version: 3.0.3-pre
 *
 * Requires: 1.2.2+
 */
(function($) {

	var types = ['DOMMouseScroll', 'mousewheel'];

	$.event.special.mousewheel = {
	    setup: function() {
	        if ( this.addEventListener )
	            for ( var i=types.length; i; )
	                this.addEventListener( types[--i], handler, false );
	        else
	            this.onmousewheel = handler;
	    },

	    teardown: function() {
	        if ( this.removeEventListener )
	            for ( var i=types.length; i; )
	                this.removeEventListener( types[--i], handler, false );
	        else
	            this.onmousewheel = null;
	    }
	};

	$.fn.extend({
	    mousewheel: function(fn) {
	        return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
	    },

	    unmousewheel: function(fn) {
	        return this.unbind("mousewheel", fn);
	    }
	});


	function handler(event) {
	    var args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true;

	    event = $.event.fix(event || window.event);
	    event.type = "mousewheel";

	    if ( event.wheelDelta ) delta = event.wheelDelta/120;
	    if ( event.detail     ) delta = -event.detail/3;

	    // Add event and delta to the front of the arguments
	    args.unshift(event, delta);

	    return $.event.handle.apply(this, args);
	}

	})(jQuery);
