
(function ($) {
    $.fn.gaCarousel = function (options) {
        var that = this;
        var currentIndex = 0;
        var interval = null;
        var tabLinks = $(".tabs a", this);
        var switchToNext = function () {
             hidePane();
             currentIndex++;
             if (currentIndex >= tabLinks.length) {
                 currentIndex = 0;
             }
             changeTabs();
             showPane();
        };
        var changeTabs = function () {
            //console.log("changeTabs");
            tabLinks.removeClass("current");
            $(tabLinks[currentIndex]).addClass("current");
        };
        var hidePane = function () {
            $($(tabLinks[currentIndex]).attr("href")).fadeOut(200);
        };
        var showPane = function () {
            //console.log("showPane");
            //console.log($($(tabLinks[currentIndex]).attr("href")));
            $($(tabLinks[currentIndex]).attr("href")).fadeIn(400);
        };
        var stopAnimation = function () {
            //console.log("stopAnimation");
            window.clearInterval(interval);
            interval = null;
        };
        var startAnimation = function () {
            //console.log("startAnimation");
            interval = window.setInterval(switchToNext, 8000);
        };
        var clickOnTab = function () {
            var newIndex = currentIndex;
            var tab = this;
            /* */
            tabLinks.each(
                function (idx, elm) {
                    if (tab == elm) {
                        newIndex = idx;
                    }
                }
            );
            /* */
            if (currentIndex !== newIndex) {
                hidePane();
                //$($(tabLinks[currentIndex]).attr("href")).hide('slow');
                currentIndex = newIndex;
                changeTabs();
                showPane();
                //$($(tabLinks[currentIndex]).attr("href")).show('slow');
            }
            return false;
        };
        var initialize = function () {
            //console.log("initialize");
            //console.log(tabLinks);
            changeTabs();
            showPane();
            startAnimation();
        };
        initialize();
        $(this).mouseenter(
            stopAnimation
        );
        $(this).mouseleave(
            startAnimation
        );
        tabLinks.click(
             clickOnTab
        );
    }
})(jQuery);
