
var slideshowLeft = {
    currentId: 0,
    heightEnd: 0,
    duration: 400,
    diffBottomBottom: 0,

    init: function (id) {
        var objMain = $("#" + id).find("#wpProductSlider");
        var objLeft = $(".arrowUp");
        var objRight = $(".arrowDown");
        var lengthElt = $("#" + id + " .wpProduct").length;

        $(".wpProduct").eq(2).addClass("wpProductLast");
        $(".wpProduct").eq(0).addClass("wpProductFirst");


        for (i = 0; i < lengthElt; i++) {
            slideshowLeft.heightEnd += $(".wpProduct").eq(i).outerHeight();
        }

        if (parseInt(lengthElt) < 4) {
            objRight.css('opacity', '.2')
        }
        else {
            objRight.click(function (e) {
                slideshowLeft.move("bottom", id);
                e.preventDefault();
            });
        }
        objLeft.css('opacity', '.2');

        objMain.css('height', slideshowLeft.heightEnd);

    },
    moveTopFromBottom: function (id) {
        var objMain = $("#" + id).find("#wpProductSlider");
        var objRight = $(".arrowDown");
        var objLeft = $(".arrowUp");
        objMain.animate({
            top: "+=" + slideshowLeft.diffBottomBottom + "px"
        }, slideshowLeft.duration / 2, function () {

        })

        objRight.unbind('click');
        objLeft.unbind('click');

        objLeft.click(function (e) {
            slideshowLeft.move("top", id);
            e.preventDefault();
        });
        objRight.css('opacity', '1');
        objRight.click(function (e) {
            slideshowLeft.moveToBottom(id);
            e.preventDefault();
        });
    },
    moveToBottom: function (id) {
        var objMain = $("#" + id).find("#wpProductSlider");
        var objRight = $(".arrowDown");
        var objLeft = $(".arrowUp");
        var heightContent = $("#" + id).outerHeight();
        objRight.css('opacity', '.2');
        objRight.unbind('click');
        objLeft.unbind('click');

        objLeft.click(function (e) {
            slideshowLeft.moveTopFromBottom(id);
            e.preventDefault();
        });


        heightBottom = slideshowLeft.heightEnd - heightContent;
        slideshowLeft.diffBottomBottom = heightBottom - Math.abs(parseInt(objMain.css('top')));

        objMain.animate({
            top: "-" + heightBottom + "px"
        }, slideshowLeft.duration / 2, function () {

        })
    },
    move: function (dir, id) {

        if (dir == "top") {
            arr = "+"
            var e = $(".wpProductFirst").prev();
        } else {
            arr = "-";
            var e = $(".wpProductFirst");
        }
        var objMain = $("#" + id).find("#wpProductSlider");
        var objLeft = $(".arrowUp");
        var objRight = $(".arrowDown");
        var objProduct = $(".wpProduct");
        var heightElt = e.outerHeight();
        var heightMask = parseInt($("#" + id).css("height"));
        var lengthElt = $("#" + id + " .wpProduct").length;

        var heightMain = heightElt * lengthElt;
        var end = heightMain - (heightMask + 12);
		var limit = (objProduct.size()%3 === 0) ?(0):(3-(objProduct.size()%3))
	
       slideshowLeft.currentId = (arr=="+")?(slideshowLeft.currentId-1):(slideshowLeft.currentId+1);
		if(slideshowLeft.currentId < 0){
			slideshowLeft.currentId=0;
		}
		else if(slideshowLeft.currentId >= objProduct.size()-limit){
			slideshowLeft.currentId=objProduct.size()-1;
		}
       		
		if(slideshowLeft.currentId >= 0 && slideshowLeft.currentId < objProduct.size()-limit){
			 objProduct.removeClass("wpProductLast");
        objProduct.eq(slideshowLeft.currentId+2).addClass("wpProductLast");
        objProduct.removeClass("wpProductFirst");
        objProduct.eq(slideshowLeft.currentId).addClass("wpProductFirst");
			
	        objMain.animate({
	            top: arr+"="+heightElt+"px"
	        }, slideshowLeft.duration, function(){		
				
	            objRight.unbind('click');
	            objLeft.unbind('click');	
				
	            //if(objMain.css('top')== 0 || objMain.css('top') == "0px"){
				if(slideshowLeft.currentId===0){
	                objLeft.css('opacity','.2');
						
	            } else {
						
	                objLeft.css('opacity','1');
	                objLeft.bind('click',function(e){
	                    slideshowLeft.move("top",id);
	                    e.preventDefault();
	                });
						
	            }		
	            //if($(".wpProduct:last").hasClass("wpProductLast")){
				if(slideshowLeft.currentId===objProduct.size()-(limit+1)){
					 objRight.css('opacity','.2');
	               /* objRight.bind('click',function(e){
	                  // slideshowLeft.moveToBottom(id);
	                    e.preventDefault();
	                });*/
	            } else {
	                objRight.css('opacity','1');
	                objRight.bind('click',function(e){
	                    slideshowLeft.move("bottom",id);
	                    e.preventDefault();
	                });
						
	            }
	        })
		}
    }
}

function mycarousel_initCallback(carousel)
{
    carousel.buttonNext.hover(function() {
        carousel.options.animation = 500;
        carousel.options.direction = 'R';
    }, function () {
        carousel.options.animation = 1500;
        carousel.options.direction = 'R';
    });
    carousel.buttonPrev.hover(function () {
        carousel.options.animation = 500;
        carousel.options.direction = 'L';
    }, function () {
        carousel.options.animation = 1500;
        carousel.options.direction = 'R';
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function () {
        carousel.stopAuto();
    }, function () {
        carousel.startAuto(1);
    });
};

var carrouselHero = {
    last: 4,
    current: 1,
    timerHeight: 0,
    heightItem: 70,
    timerRed: null,
    init: function () {

        $("#tabLeft1").click(function () {
            carrouselHero.stopAnim();
            carrouselHero.current = 1;
            carrouselHero.changeItem(1);
        });
        $("#tabLeft2").click(function () {
            carrouselHero.stopAnim();
            carrouselHero.current = 2;
            carrouselHero.changeItem(2);
        });
        $("#tabLeft3").click(function () {
            carrouselHero.stopAnim();
            carrouselHero.current = 3;
            carrouselHero.changeItem(3);
        });
        $("#tabLeft4").click(function () {
            carrouselHero.stopAnim();
            carrouselHero.current = 4;
            carrouselHero.changeItem(4);
        });

        $("#wpCarrouselContainer").mouseenter(function () {
            carrouselHero.stopAnim();
        });
        $(".wpCarrouselContent").mouseleave(function () {
            // carrouselHero.current = (carrouselHero.current===1)?4:carrouselHero.current-1;
            carrouselHero.timerHeight = Math.ceil($('#tabLeft' + carrouselHero.current + ' .timer').height());
            currentDelay = (Number($('#tab' + carrouselHero.current + 'Delay').text()) < 1000) ? (Number($('#tab' + carrouselHero.current + 'Delay').text()) * 1000) : ($('#tab' + carrouselHero.current + 'Delay').text());
            currentDelay = Number(currentDelay);
            waitingTime = Math.ceil(((carrouselHero.heightItem - carrouselHero.timerHeight) * currentDelay) / carrouselHero.heightItem);
            waitingHeight = carrouselHero.heightItem - carrouselHero.timerHeight;

            carrouselHero.launchTimer(waitingTime, waitingHeight);
        });
        carrouselHero.changeItem(carrouselHero.current);
    },
    stopAnim: function () {

        $('.wpCarrouselContent').stop(true, true);
        $('.timer').stop();
        // if(carrouselHero.timerRed){
        // clearTimeout(carrouselHero.timerRed);
        $('.timer').css('height', 0);
        // }
    },
    launchTimer: function (time, height) {
        if (height >= carrouselHero.heightItem) $('#tabLeft' + carrouselHero.current + ' .timer').css("height", "0");

        $('#tabLeft' + carrouselHero.current + ' .timer').show().animate({
            height: "+=" + height
        },
		{
		    duration: Number(time),
		    step: function () {
		        if ($(this).height() > carrouselHero.heightItem) {
		            $('.timer').css('height', 0);
		        }
		    },
		    complete: function () {
		        $('.timer').css('height', 0);
		        carrouselHero.current = (carrouselHero.current < 4) ? (carrouselHero.current + 1) : (1);
		        carrouselHero.changeItem(carrouselHero.current);
		    }
		});
        //
        /* if(carrouselHero.timerRed){
        clearTimeout(carrouselHero.timerRed);
        }*/
        //$('.timer').css('height',0);
        //carrouselHero.timerRed = setTimeout("carrouselHero.changeItem("+carrouselHero.current+")",time);

    },
    changeItem: function (n) {
        $('.timer').css('height', 0).hide();

        $("#tabLeft1").removeClass("tabFirstOn");
        $(".tab").removeClass("tabOn");
        $("#tabLeft" + carrouselHero.last).removeClass("tabLastOn");

        tabDelay = $('#tab' + n + 'Delay').text();
        $(".tabBtnShadowTop, .tabBtnShadowBtm").hide();
        if (n == 1) {
            $("#tabLeft" + n).addClass("tabFirstOn");
            $(".tabBtnShadowTop").hide();
            $(".tabBtnShadowBtm").show();
            $(".tabBtnShadowBtm").css('top', '77px');
            $(".tabBtnShadowTop").hide();
             $(".tabBtnShadowBtm").show();
            $(".tabBtnShadowBtm").css('top','77px');
        }
        else if (n == carrouselHero.last) {
            $("#tabLeft" + n).addClass("tabLastOn");
            $(".tabBtnShadowTop").show();
            $(".tabBtnShadowTop").css('top', '203px');
            $(".tabBtnShadowTop").show();
            $(".tabBtnShadowTop").css('top','203px');
        }
        else {
            $("#tabLeft" + n).addClass("tabOn");
        }

        if (n == 2) {
            $(".tabBtnShadowTop").show();
            $(".tabBtnShadowTop").css('top', '60px');
            $(".tabBtnShadowBtm").show();
            $(".tabBtnShadowBtm").css('top', '148px');
        }
        if (n == 3) {
            $(".tabBtnShadowTop").show();
            $(".tabBtnShadowTop").css('top', '131px');
            $(".tabBtnShadowBtm").show();
            $(".tabBtnShadowBtm").css('top', '220px');
        }
        
		if(cufonEnabled=='true'){
                Cufon.replace('.heading1', {
                                color: '#49495C'
                });
                Cufon.replace('.heading2', {
                                color: '#A09EA8'
                });
                Cufon.replace('#tabLeft'+n+' .heading1, #tabLeft'+n+' .heading2', {
                                color: '#D41654'
                });
			}
			else{           
                $('.heading1').css('color','#49495C');
                $('.heading2').css('color','#A09EA8');
                $('#tabLeft'+n+' .heading1, #tabLeft'+n+' .heading2').css('color','#D41654');
			}
 


        $('.tabActive').fadeOut('slow').removeClass('tabActive');
        $('#tab' + n).fadeIn('slow').addClass('tabActive');

        currentDelay = $('#tab' + n + 'Delay').text();

        currentDelay = (Number($('#tab' + carrouselHero.current + 'Delay').text()) < 1000) ? (Number($('#tab' + carrouselHero.current + 'Delay').text()) * 1000) : ($('#tab' + carrouselHero.current + 'Delay').text());
        currentDelay = Number(currentDelay);

        carrouselHero.launchTimer(currentDelay, carrouselHero.heightItem);

    }
}
