$(document).ready(function(){
    function promoSlide(back){
        if(back == undefined){back = false}
        var $promoLeft = $(".promoContentLeft");
        var $promoRight = $(".promoContentRight");
        var next;
        $promoLeft.each(function(index) {
            if(parseInt($(this).css('left'),10) == 0){
                $(this).animate({left: -$(document).outerWidth()},750);
                $promoRight.eq(index).animate({top: 275},750,function(){
                    $promoRight.eq(index).css('top',-275);
                });
                if(!back){next = index+1;}
                else{next = index-1;}
            }
        });
        if(next == $promoLeft.length){next = 0;}
        else if(next < 0){next = $promoLeft.length-1}
        $promoLeft.eq(next).animate({left: 0},750);
        $promoRight.eq(next).animate({top: 0},750);
    }
    var changeSlide = setInterval(function() {promoSlide()}, 6500);
        
    function setSlides(){
        var $promoLeft = $(".promoContentLeft");
        $promoLeft.each(function(index) {
            if(parseInt($(this).css('left'),10) != 0){
                $(this).css('left',-$(document).outerWidth());
            }
        });
    }
    setSlides();

    $('#promoControlForward').click(function() {promoSlide();});
    $('#promoControlBack').click(function() {promoSlide(true);});
    $('#promoControlForward').mouseover(function() {$(this).attr("src", "images/forward1.png");});
    $('#promoControlForward').mouseout(function() {$(this).attr("src", "images/forward2.png");});
    $('#promoControlBack').mouseover(function() {$(this).attr("src", "images/back1.png");});
    $('#promoControlBack').mouseout(function() {$(this).attr("src", "images/back2.png");});

    $('#promotionContainerHolder').mouseover(function() {
        clearInterval(changeSlide);
        $('#promotionContainerControls').stop().animate({opacity:0.5},500)
    });
    $('#promotionContainerHolder').mouseout(function() {
        changeSlide = setInterval(function() {promoSlide()}, 6500);
        $('#promotionContainerControls').stop().animate({opacity:0},500)
    });
});  

