function slide() {
    return setInterval(function() {
        var active = $('#home_products_tabs .active');

        var next = active.next();
        if (!next.length) {
            next = $('#home_products_tabs div.tab:first');
        }

        active.removeClass('active');
        next.addClass('active');

        var nr = next.attr('id').split('_')[1];
        $('#home_products_product .active').removeClass('active');
        $('#product_'+nr).addClass('active');
    }, 5000);
}

function productTabs() {
    var interval = slide();

    $('#home_products_tabs div.tab').mouseenter(function() {
        $('#home_products_tabs .active').removeClass('active');
        $(this).addClass('active');

        var nr = $(this).attr('id').split('_')[1];
        $('#home_products_product .active').removeClass('active');
        $('#product_'+nr).addClass('active');

        clearInterval(interval);
    }).mouseleave(function() {
        interval = slide();
    });
}

function language(){
    /**
     * Language switcher
     **/
    $('#change-lang').click(function () {
        $('#language-form').submit();
        return false;
    });
}

function showMenu(){
    /**
     * Drop down menu
     **/
    $('li.menu-item').mouseover(function(){
        $(this).find('.exoDropDown').show();
    });

    $('li.menu-item').mouseout(function(){
        $(this).find('.exoDropDown').hide();
    });
}

function slideShowBob() {
    /**
     * Create slideshow using lightbox
     **/
    $('a.lightbox').lightBox({
        imageLoading: '/media/js/lightbox/images/lighbtox-ico-loading.gif',
        imageBtnClose: '/media/js/lightbox/images/lightbox-btn-close.gif',
        imageBtnPrev: '/media/js/lightbox/images/lightbox-btn-prev.gif',
        imageBtnNext: '/media/js/lightbox/images/lightbox-btn-next.gif'
    });

    // Initially set opacity on thumbs and add
    // additional styling for hover effect on thumbs
    var onMouseOutOpacity = 0.67;
    $('#thumbs-adv ul.thumbs li').css('opacity', onMouseOutOpacity)
    .hover(
        function () {
            $(this).not('.selected').fadeTo('fast', 1.0);
        },
        function () {
            $(this).not('.selected').fadeTo('fast', onMouseOutOpacity);
        }
    );

    var galleryAdv = $('#gallery-adv').galleriffic('#thumbs-adv', {
        delay:                  2000,
        numThumbs:              12,
        preloadAhead:           10,
        enableTopPager:         true,
        enableBottomPager:      true,
        imageContainerSel:      '#slideshow-adv',
        controlsContainerSel:   '#controls-adv',
        captionContainerSel:    '#caption-adv',
        loadingContainerSel:    '#loading-adv',
        renderSSControls:       true,
        renderNavControls:      true,
        playLinkText:           'Play Slideshow',
        pauseLinkText:          'Pause Slideshow',
        prevLinkText:           '&lsaquo; Previous Photo',
        nextLinkText:           'Next Photo &rsaquo;',
        nextPageLinkText:       'Next &rsaquo;',
        prevPageLinkText:       '&lsaquo; Prev',
        enableHistory:          true,
        autoStart:              false,
        onChange:               function(prevIndex, nextIndex) {
            $('#thumbs-adv ul.thumbs').children()
            .eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
            .eq(nextIndex).fadeTo('fast', 1.0);
        },
        onTransitionOut:        function(callback) {
            $('#slideshow-adv, #caption-adv').fadeOut('fast', callback);
        },
        onTransitionIn:         function() {
            $('#slideshow-adv, #caption-adv').fadeIn('fast');
        },
        onPageTransitionOut:    function(callback) {
            $('#thumbs-adv ul.thumbs').fadeOut('fast', callback);
        },
        onPageTransitionIn:     function() {
            $('#thumbs-adv ul.thumbs').fadeIn('fast');
        }
    });
}

function doAccordion() {
    /**
     * Create slideshow using lightbox
     **/
    $('#accordion').accordion({
        active: -1,
        icons: {
            'header': 'ui-icon-plus',
            'headerSelected': 'ui-icon-minus'
        }
    });
}

$(function() {
    if ($('#home_products').length) {
        productTabs();
    }

    language();
    showMenu();
    slideShowBob();
    doAccordion();
});
