/*
 * Onload
 */
$(document).ready(function(){

    //Global variables:
    var number_of = $("#slide > li").length-2;
    var sld = new Slider(number_of);

    //Add the previous button event:
    $("#sld_prev").attr("onclick", "").click(function() {
        sld.prev();
    });

    //Add the events to the element markers:
    for(var i = 1 ; i <= number_of ; i++) {
        $("#sld_"+i).attr("onclick", "").click(function() {
            sld.select(this);
        });
    }

    //Add the next button event:
    $("#sld_next").attr("onclick", "").click(function () {
        sld.next();
    });

    //Slider timer:
    window.setTimeout(function() {
        slideTimer(sld);
    }, 5000);

    //Add event listeners for each performance:
    $("td.performance").each(function () {
        $(this).mouseover(function () {
            $(this).find("div").show();
        });
        $(this).mouseout(function () {
            $(this).find("div").hide();
        });
    });

    //Font size:
    $(".large-font").click(function() {
        $("html").css("font-size", "14px");

        $.cookie("tdv_fontsize", "14px", {expires: 7});
    });

    $(".small-font").click(function() {
        $("html").css("font-size", "12px");

        $.cookie("tdv_fontsize", "12px", {expires: 7});
    });

    //Check if a different font size has been selected:
    if($.cookie("tdv_fontsize") != "" && $.cookie("tdv_fontsize") != null) {
        $("html").css("font-size", $.cookie("tdv_fontsize"));
    }


});

function slideTimer(sld) {
    sld.next();
    window.setTimeout(function() {
        slideTimer(sld);
    }, 5000);
}

/*
 * Slider
 *
 * Change the background image and content for the slider.
 */
var Slider = (function(nr_items) {

    //Current selected slide:
    var current = 1;

    /*
     * Next slide
     *
     * Private member function
     */
    this.next = function() {

        clear();

        if(current == nr_items) {
            //Update the current variable:
            current = 1;
        }
        else if(current >= 1) {
            //Update the current variable:
            ++current;
        }

        update()
    }

    /*
     * Previous slide
     *
     * Private member function
     */
    this.prev = function() {

        clear();

        //If the current slide is the first one:
        if(current == 1) {
            //Update the current variable:
            current = nr_items;
        }
        else if(current <= nr_items) {
            //Update the current variable:
            --current;
        }

        update()
    }

    /*
     * Select slide
     *
     * Private member function
     */
    this.select = function(focus) {

        //Fetch the specified marker number:
        var marker = $(focus).attr("alt");

        clear();

        //If the variable is a valid number:
        if(!isNaN(marker)) {
            //If the variable is in a valid range:
            if((marker > 0) && (marker <= nr_items)) {
                //Update the current variable:
                current = marker;
            }
        }

        update()
    }

    /*
     * Clear the current slide
     */
    function clear() {
        //Clear the marker of the current slide:
        $("#sld_"+current).attr("src", "style/gfx/slider_empty.gif");
    }

    /*
     * Set the new slide
     */
    function update() {
        //Set the new slide as the active one:
        $("#sld_"+current).attr("src", "style/gfx/slider_current.gif");
        //Update the content
        $("#slider > h1").text($("#sld_"+current).attr("title"));
        $("#slider").attr("style", "background-image: url('media_files/images/"+$("#sld_"+current).attr("name")+"');");
        $("#slider > #information > p").text($("#sld_"+current).attr("longdesc"));
    }

});
