jQuery(document).ready(function () {
    if (jQuery('.gallery').length) {
        jQuery('.gallery .controls .next').everyTime(15000, 'galleryAutoMove', function () {
            galleryPerformAction(jQuery('.gallery .controls .next'));
        });

        jQuery('.gallery').mouseover(function () {
            jQuery('.gallery .controls .next').stopTime('galleryAutoMove');
        });

        jQuery('.gallery').mouseout(function () {
            jQuery('.gallery .controls .next').everyTime(6666, 'galleryAutoMove', function () {
                galleryPerformAction(jQuery('.gallery .controls .next'));
            });
        });
    }

    if (jQuery('#newsticker').length) {
        jQuery('#newsticker').everyTime(5000, 'newstickerAutoMove', function () {
            newstickerPerformAction(jQuery('#newsticker'));
        });

        jQuery('#newsticker .news').mouseover(function () {
            jQuery('#newsticker').stopTime('newstickerAutoMove');
        });

        jQuery('#newsticker .news').mouseout(function () {
            jQuery('#newsticker').everyTime(5000, 'newstickerAutoMove', function () {
                newstickerPerformAction(jQuery('#newsticker'));
            });
        });
    }
});


function galleryPerformAction(linkElement) {

    var pagerItems = linkElement.parent().children();
    var numPagerItems = pagerItems.length;
    var contentItems = linkElement.parent().prev().find('.item');

    // determine the index of the currently active pager item
    var activePagerIndex = 0;

    pagerItems.each(function () {
        if (jQuery(this).hasClass('active')) {
            activePagerIndex = jQuery(this).index();
        }
    });

    // determine the index of the pager item that should be set active
    var newPagerIndex = 1;

    if (linkElement.index() == 0) {
        newPagerIndex = (activePagerIndex > 1) ? (activePagerIndex - 1) : (numPagerItems - 2);
    }
    else if (linkElement.index() == (numPagerItems - 1)) {
        newPagerIndex = (activePagerIndex < numPagerItems - 2) ? (activePagerIndex + 1) : 1;
    }
    else {
        newPagerIndex = linkElement.index();
    }

    pagerItems.removeClass('active');
    pagerItems.eq(newPagerIndex).addClass('active');

    contentItems.eq(activePagerIndex - 1).fadeOut(666);
    contentItems.eq(newPagerIndex - 1).fadeIn(666);
}

function newstickerPerformAction(linkElement) {

    var pagerItems = linkElement.children('.news');
    var numPagerItems = pagerItems.length;

    // determine the index of the currently active pager item
    var activePagerIndex = 0;

    pagerItems.each(function () {
        if (jQuery(this).hasClass('active')) {
            activePagerIndex = jQuery(this).index();
        }
    });

    // determine the index of the pager item that should be set active
    activePagerIndex++;

    if (activePagerIndex > numPagerItems - 1) {
        activePagerIndex = 0;
    }

    pagerItems.removeClass('active');
    pagerItems.eq(activePagerIndex).addClass('active');
}

