$(document).ready(function(){
    stop = false;
    $("#prodCarousel ul li").each(function(){
        $(this).find("a").css("margin-top", "-"+$(this).find("a").innerHeight()+"px");
    });
    $("#prodCarousel ul li").bind({
        mouseenter: function(){
            window.clearInterval(start);
        },
        mouseleave: function(){
            if(!stop){
                productCarInt();
            }
        }
    });

    $(".showHideWrapper .tabbertab h2").click(function(){
        if($(this).hasClass("active")){
            $(this).removeClass("active");
        }else{
            $(this).addClass("active");
        }
        var h = $(this).attr("data-id");
        $(this).next(".inner").slideToggle("slow", function(){
            window.location.hash = h;
        });
      
    });

    $(".demo_link").bind("click", function(){
        moveIt(false);
        stop = true;
        window.clearInterval(start);
    });

    $("#sb-nav-close").live("click", function(){
        stop = false;
        productCarInt();
    });

    $(".close").click(function(){
       $(this).parent().slideToggle();
    });

    startCheckout();
    productCarInt();
    productCarNav();
}); // END DOM

function productCarousel() {
    var mrgnLft = $("#prodCarousel ul").css("margin-left");
    mrgnLft = mrgnLft.replace("px", "");
    var c = $("#prodCarousel ul li").length;
    parseInt(c);
    parseInt(mrgnLft);
    mrgnLft = Math.round(mrgnLft);
    c = (c - 1) * 1000;
    c = c - (c * 2);

    if(mrgnLft > c){
        if(!$("#prodCarousel ul").is(":animated")){
            $("#prodCarousel ul").animate({
                marginLeft: "-=1000px"
            }, 1000);
        }
    }else{
        $("#prodCarousel ul").css("margin-left", "0px");
        clearInterval(start);
        productCarInt();
    }
}

function productCarInt() {
    //Set time for Carousel Movement
    start = setInterval('productCarousel()', 7000);
}


function productCarNav() {
    
    $(".prodCarouselNav").show().css("cursor", "pointer");    
    
    $(".prodCarouselNav").find("li").click(function(){

        window.clearInterval(start);

        if($(this).hasClass("next") && $("#prodCarousel ul").css("margin-left") != '-4000px'){
            if(!$("#prodCarousel ul").is(":animated")){
                moveIt('-=1000px');
            }
        }
       
        if($(this).hasClass("prev") && $("#prodCarousel ul").css("margin-left") != '0px'){
            if(!$("#prodCarousel ul").is(":animated")){
                moveIt('+=1000px');
            }            
        }
       
        if($(this).hasClass("pause")){
            moveIt(false);
            if(!stop){
                stop = true;
                $(this).addClass("paused");
            }else{
                stop = false;
                $(this).removeClass("paused");
            }
        }
       
    });
}

function moveIt(direction){
    
    window.clearInterval(start);
    
    if(direction && !$("#prodCarousel ul").is(":animated")){
        if(!stop){
            $("#prodCarousel ul").animate({
                marginLeft: direction
            }, 1000, function(){
                productCarInt();
            });
        }
    }    
    
}

function startCheckout(){

    $(".addProduct").click(function(){
        $(this).closest("form").submit();
    });
}
