/**
 * Functions for the teaser on the homepage.
 */

var MAINTEASER_DATA = {
    // currently visible item
    'current': -1,
    // number of items
    'total': 0,
    // holds the interval
    'interval': -1,
    // how fast shall the teaser switch images
    'interval_duration': 5000
}

/**
 * Highlight the next item.
 */
function highlightNext()
{
    var last = MAINTEASER_DATA.current;
    // if the last entry is active, select the first one as next
    if (MAINTEASER_DATA.current + 1 >= MAINTEASER_DATA.total) {
        highlight(0);
    } else {
        highlight(MAINTEASER_DATA.current + 1);
    }
}

/**
 * Highlights a certain entry.
 * 
 * @param int num Which entry to highlight.
 * 
 * @return void Do not return anything, so this function can be used in html links.
 */
function highlight(num)
{
    // reset the interval
    if (MAINTEASER_DATA.interval != -1) {
        clearInterval(MAINTEASER_DATA.interval);
        MAINTEASER_DATA.interval = setInterval(highlightNext, MAINTEASER_DATA.interval_duration);
    }
    
    // only do sth when the new item is not already active
    if (num != MAINTEASER_DATA.current) {
        var last = MAINTEASER_DATA.current;
        // fade the last item out
        if (last >= 0) {
            $('#MainTeaser .Items li:eq(' + last + ')').fadeOut();
            $('#MainTeaser .Buttons li:eq(' + last + ')').removeClass('Active');
        }
        // fade the new item in
        $('#MainTeaser .Items li:eq(' + num + ')').fadeIn();
        $('#MainTeaser .Buttons li:eq(' + num + ')').addClass('Active');
        // save the new active item
        MAINTEASER_DATA.current = num;
    }
}


// TODO preloader?
$(document).ready(function() {
    // count entries
    MAINTEASER_DATA.total = $('#MainTeaser .Items li').size();
    // hide all
    $('#MainTeaser .Items li').hide();
    // display the first one
    highlightNext();
    // set interval
    MAINTEASER_DATA.interval = setInterval(highlightNext, MAINTEASER_DATA.interval_duration);
});
