
$(function () {

	
	 var $autoFun;
    var totalPanels = $(".scrollContainer").children().size();

    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 = 464;

    var curWidth = 1000;
    var curImgWidth = 464;
    var curTitleSize = "20px";
    var curParSize = "15px";

    var $panels = $('#slider .scrollContainer > div');
    var $container = $('#slider .scrollContainer');
    autoSlide();
    
    
    
    
    $panels.css({ 'float': 'left', 'position': 'relative' });

    $("#slider").data("currentlyMoving", false);

    $container
		.css('width', ($panels[0].offsetWidth * $panels.length) + 100)
		.css('left', "0px");

    

    var scroll = $('#slider .scroll').css('overflow', 'hidden');

    function returnToNormal(element) {
        $(element)
			.animate({ width: regWidth })
			.find("img")
			.animate({ width: regImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: regTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: regParSize });
    };

    function growBigger(element) {
        $(element)
			.animate({ width: curWidth })
			.find("img")
			.animate({ width: curImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: curTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: curParSize });
    }

    //direction true = right, false = left
   
    function change(direction) {

        //if not at the first or last panel
        if ((direction && !(curPanel < (totalPanels - 1))) || (!direction && (curPanel <= 1))) {
          if(direction)
          {
        	  $(".scrollContainer")
				.stop()
				.animate({
				    "left": 10
				});
        	  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);
				});

            curPanel = next;

            

        }
    }

    var curPanel = 1;

    //when the left/right arrows are clicked
    $(".right").click(function () { change(true); });
    $(".left").click(function () { change(false); });
    
 
    clearFun($(".right"));
    clearFun($(".left"));
    
    //@Mr.Think***事件划入时停止自动滚动
    function clearFun(elem){
        elem.hover(function(){
            clearAuto();
        }, function(){
            autoSlide();
        });
    }
    //@Mr.Think***自动滚动
    function autoSlide(){
    	 $(".right").trigger('click');
        $autoFun = setTimeout(autoSlide, 5000);//此处不可使用setInterval,setInterval是重复执行传入函数,这会引起第二次划入时停止失效
    }
    //@Mr.Think***清除自动滚动
    function clearAuto(){
        clearTimeout($autoFun);
    }
    
});
