$(function() {

        //on désactive tous les liens dans le slider
        //seul les liens du visuel en premier plan sont activés
        //("#slider a").click(function(){ return false;})

	var totalPanels			= $(".scrollContainer").children().size();

        if(totalPanels < 1) return false;

	var regWidth			= $(".panel").css("width");
	var regImgWidth			= $(".panel img").css("width");
	var regTitleSize		= $(".panel h2").css("font-size");
	var regParSize			= $(".panel p").css("font-size");
	
	var movingDistance              = 170;
	
	var curWidth			= 280;
	var curHeight			= 250;
	var curImgWidth			= 250;
	var curTitleSize		= "20px";
	var curParSize			= "15px";

	var $panels			= $('#slider .scrollContainer > div');
	var $container			= $('#slider .scrollContainer');

	$panels.css({'float' : 'left','position' : 'relative','opacity' : '0.6'});
    
	$("#slider").data("currentlyMoving", false);

 	$container.css('width', ($panels[0].offsetWidth * $panels.length) + 150 )
        $scroll_center  = $container.width()/2;
        $container.css('left', '0px');

	var scroll = $('#slider .scroll').css('overflow', 'hidden');

	function returnToNormal(element) {
		$(element)
                        //.find(".inside").addClass("small")
			.animate({width: regWidth, top: 0})
			.find("img")
			.animate({width: regImgWidth, opacity: 0.4})
		    .end()
			//.find("h2")
			//.animate({fontSize: regTitleSize})
                        //.fadeIn()
			//.end()
			//.find("p")
			//.animate({fontSize: regParSize})
                        //.fadeIn();
	};

        /*modification aja*/
	function growBigger(element) {
		$(element)
                        //.find(".inside").removeClass("small")
			.animate({width: curWidth, height: curHeight, opacity: 1, top: -35})
			//.animate({width: curWidth, opacity: 1})
			.find("img")
			.animate({width: curImgWidth,opacity: 1})
		    .end();

                //on supprime le texte du scroll
                //on clone le contenu caché des panels
                //on fade dans le box outside
                $(".outside").empty();

                $(element).find("h2").attr({style:""}).hide().clone().appendTo(".outside").fadeIn('slow');
		$(element).find("p").attr({style:""}).hide().clone().appendTo(".outside").fadeIn('slow');
  	}

	
	//direction true = right, false = left
	function change(direction) {
	   
	    //if not at the first or last panel
            if((direction && !(curPanel < totalPanels)) || (!direction && (curPanel <= 1))) {
                return false;
            }
        
            //if not currently moving
            if (($("#slider").data("currentlyMoving") == false)) {

                $("#slider").data("currentlyMoving", true);

                var next         = direction ? curPanel + 1 : curPanel - 1;
                var leftValue    = $(".scrollContainer").css("left");
                var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;

                $(".scrollContainer")
                        .stop()
                        .animate({
                                "left": movement
                        }, function() {
                                $("#slider").data("currentlyMoving", false);
                        });

                //Si Lazyload chargé sur le scrollContainer
                //on attache l'évenement Scroll
                if( jQuery.isFunction( $.fn.lazyload ) )
                {
                    $(".scroll").trigger("scroll");
                }
                
                returnToNormal("#panel_"+curPanel);
                growBigger("#panel_"+next);
                curPanel = next;

                //remove all previous bound functions
                $("#panel_"+(curPanel+1)).unbind();

                //go forward
                $("#panel_"+(curPanel+1)).click(function(){change(true);});

                //remove all previous bound functions
                $("#panel_"+(curPanel-1)).unbind();

                //go back
                $("#panel_"+(curPanel-1)).click(function(){change(false);});

                //remove all previous bound functions
                $("#panel_"+curPanel).unbind();


            }//if not moving
	}//end change direction
	
	// Set up "Current" panel and next and prev
	growBigger("#panel_2");
	var curPanel = 2;
	
	$("#panel_"+(curPanel+1)).click(function(){change(true);});
	$("#panel_"+(curPanel-1)).click(function(){change(false);});
	
	//when the left/right arrows are clicked
	$(".rightButton").click(function(){change(true);});
	$(".leftButton").click(function(){change(false);});
	
	$(window).keydown(function(event){
            switch (event.keyCode) {
                /*
                case 33: //scroll up
                        $(".leftButton").click();
                        break;
                case 34: //scroll down
                        $(".rightButton").click();
                        break;
                case 13: //enter
                        $(".rightButton").click();
                        break;
                case 32: //space
                        $(".rightButton").click();
                        break;
                 */
                case 37: //left arrow
                        $(".leftButton").click();
                        break;
                case 39: //right arrow
                        $(".rightButton").click();
                        break;
                case 38: //up arrow
                        $(".leftButton").click();
                        break;
                case 40: //down arrow
                        $(".rightButton").click();
                        break;
              }
	});

});

