function Billboard(init){
    this.billboard_change = function(tab_nr){
        clearTimeout(this_object.timeout);
        if ($(this_object.item_selector).length == tab_nr){
            tab_nr = 0;
        }
        else if (tab_nr < 0){
            tab_nr = $(this_object.item_selector).length-1;
        }
        if (!this_object.changing && tab_nr != this_object.current){
            this_object.changing = true;
            $(this_object.item_selector).eq(this_object.current).fadeOut('fast',function() {
                $(this_object.item_selector).eq(tab_nr).fadeIn('normal',function() {
                    this_object.current = tab_nr;
                    this_object.changing = false;
                });
                $(this_object.tab_selector).removeClass("current");
                $(this_object.tab_selector).eq(tab_nr).addClass("current");
            });
        }
        this_object.set_timer();
    }
    this.billboard_timer = function(){
        this_object.billboard_change(this_object.current+1)
    }
    this.set_timer = function(){
        this_object.timeout = setTimeout(function(){this_object.billboard_timer()}, this_object.timing);
    };
    var this_object = this;
    for (attr in init){this[attr] = init[attr]}
    this.slider_queue = 0;
    this.current = 0;
    this.changing = false;
    this.set_timer()
    $(this.tab_selector).click(function(){
        this_object.billboard_change($(this).prevAll().length);
        return false;
    });
    $(this.next_selector).click(function(){
        this_object.billboard_change(this_object.current+1)
        return false;
    });
    $(this.prev_selector).click(function(){
        this_object.billboard_change(this_object.current-1)
        return false;
    });
}

$(document).ready(function(){
    var index_billboard = new Billboard({timing:5000,item_selector:"#billboard .billboard-item",tab_selector:"#billboard .button",next_selector:"#billboard .nextOne",prev_selector:"#billboard .previousOne"})
    var snap_dist = 138;
    var active_box_index = 0;
    var drag_left = 0;
    var left = "0px";
    function change_slider(){
        $(".product-box-description").stop();
        $(".product-box-description").removeAttr("style");
        $(".product-box-description:not(:eq("+active_box_index+"))").removeClass("product-box-description-active").addClass("product-box-description-inactive");
        $(".product-box-description").eq(active_box_index).removeClass("product-box-description-inactive").addClass("product-box-description-active");
        $(".product-box-description").eq(active_box_index).switchClass("product-box-description-active", "product-box-description-inactive");
        left = $(".product-slider-handle").css("left");
        drag_left = Number($(".product-slider-handle").css("left").slice(0,-2));
        if (drag_left){
            active_box_index = drag_left/snap_dist;
        }
        else {
            active_box_index = 0;
        }
        $(".product-box-description").eq(active_box_index).switchClass("product-box-description-inactive", "product-box-description-active");
        $(".product-slider-name").removeClass("product-slider-name-active");
        $(".product-slider-name").eq(active_box_index).addClass("product-slider-name-active");
    }
    $(".product-slider-name").click(function(){
        $(".product-slider-handle").css("left", $(this).prevAll().length*snap_dist);
        change_slider()
    });
    $(".product-box").click(function(){
        if ($(this).prevAll(".product-box").length != $(".product-slider-name-active").prevAll().length){
            $(".product-slider-handle").css("left", $(this).prevAll(".product-box").length*snap_dist);
            change_slider()
        }
    });
    $(".product-slider-left-arrow").click(function(){
        if ($(".product-slider-name-active").prevAll().length){
            $(".product-slider-handle").css("left", ($(".product-slider-name-active").prevAll().length-1)*snap_dist);
        }
        else {
            $(".product-slider-handle").css("left", ($(".product-slider-name").length-1)*snap_dist);
        }
        change_slider()
    });
    $(".product-slider-right-arrow").click(function(){
        if ($(".product-slider-name-active").nextAll(".product-slider-name").length){
            $(".product-slider-handle").css("left", ($(".product-slider-name-active").prevAll().length+1)*snap_dist);
        }
        else {
            $(".product-slider-handle").css("left", 0);
        }
        change_slider()
    });
    $(".product-slider-handle").draggable({"axis":"x", "grid":[snap_dist,snap_dist],"containment":".product-slider-container","drag":function(event,ui){
        if (left != $(".product-slider-handle").css("left")){
            change_slider()
        }
    }});
     $(".prodder").click(function(event){
       $(".products").toggle();
       $(".border").toggle();
       event.preventDefault();
     });

     $(".toggleSpeech").mouseover(function(event){
       $(this).next().show();
       event.preventDefault();
     });
     $(".toggleSpeech").mouseout(function(event){
       $(this).next().hide();
       event.preventDefault();
     });
});
