
var animationInterval = 5000;
var animationInProgress = true;
var newProductsCount = 0;
var switchTeaserEntryInterval   = 5000;
var activeTeaserEntryIdx        = 1;
var nextTeaserEntryIdx          = activeTeaserEntryIdx + 1;
var switchTeaserEntriesIntervalFunction;
var switchTeaserTriggeredClick  = false;

var animateNewProducts = function() {
    if ($('.hTab_link.active').length == 0) {
        return;
    }
    if (animationInProgress == false) {
        return;
    }
    var hTabId = 0;
    var hTabIdActive = $('.hTab_link.active').attr('id');
    hTabIdActive = hTabIdActive.substr(5);
    $('.hTabs li a.active').removeClass('active');
    if (hTabIdActive == newProductsCount-1) {
        hTabId = 0;
    } else {
        hTabId = ++hTabIdActive;
    }
    $('#hTab_' + hTabId).addClass('active');
    $('.hTab_content.active').fadeOut();
    $('#hTab_' + hTabId + '_content').fadeIn();
    $('.hTab_content.active').removeClass('active');
    $('#hTab_' + hTabId + '_content').addClass('active');
    setTimeout("animateNewProducts();",animationInterval);
}

/**
 * Switches the teaser entries in a set interval (see document.ready function).
 * 
 * @author Sascha Koehler <skoehler@pixeltricks.de>
 * @since 28.07.2011
 */
var switchTeaserEntries = function() {
    var listEntries         = $('#TeaserEntries > li');
    var listEntriesMaxIdx   = listEntries.length - 1;

    listEntries.each(
        function(idx, element) {
            if (idx === nextTeaserEntryIdx) {
                switchTeaserTriggeredClick = true;
                $(this).find('a').click();
                $(this).find('a').addClass('active');
                activeTeaserEntryIdx = nextTeaserEntryIdx;
                return false;
            }
        }
    );
    
    nextTeaserEntryIdx++;
    
    if (nextTeaserEntryIdx >= listEntriesMaxIdx) {
        nextTeaserEntryIdx = 1;
    }
}

$('document').ready(function(){
    $('.vTab_link').click(function(){
        if (!switchTeaserTriggeredClick) {
            clearInterval(switchTeaserEntriesIntervalFunction);
        }
        switchTeaserTriggeredClick = false;
        if ($(this).hasClass('active')) {
            return;
        }
        var artKat = $(this).attr('id').substring(5);
        $('.hBox-with-tabs').attr('id','hBox' + artKat);
        var vTabId = $(this).attr('id');
        $('.vTabs li a.active').removeClass('active');
        $(this).addClass('active');
        $('.vTab_content.active').fadeOut();
        $('#' + vTabId + '_content').fadeIn();
        $('.vTab_content.active').removeClass('active');
        $('#' + vTabId + '_content').addClass('active');
    });
    $('.hTab_link').click(function(){
        animationInProgress = false;
        if ($(this).hasClass('active')) {
            return;
        }
        var hTabId = $(this).attr('id');
        $('.hTabs li a.active').removeClass('active');
        $(this).addClass('active');
        $('.hTab_content.active').fadeOut();
        $('#' + hTabId + '_content').fadeIn();
        $('.hTab_content.active').removeClass('active');
        $('#' + hTabId + '_content').addClass('active');
    });

    setTimeout("animateNewProducts();",animationInterval);


    switchTeaserEntriesIntervalFunction = setInterval('switchTeaserEntries();', switchTeaserEntryInterval);
});
