$(function () {
    if ($('.hero-content-wrap').length != 0) {
        // Hero panel wrap
        var $heroContentContainer = $('.hero-content-wrap').css('overflow', 'hidden');
        // Hero panels
        var $heroContentPanels = $('.hero-content-wrap').children('div.hero-tab-panel');
        // Hero tab nav wrap
        var $heroNavTabsContainer = $('.hero-tab-nav');
        // Hero nav anchors
        var $heroNavTabs = $heroNavTabsContainer.find('li a').addClass('inactive');

        // Cycle count
        var stopCount = $heroNavTabs.size();
        stopCount = (stopCount * 2) + 1;

        function onAfter(curr, next, opts) {
            var index = opts.currSlide;
            $heroNavTabs.fadeTo(1000, .95);
            $heroNavTabs.eq(index).removeClass('inactive');
        }
        function onBefore(curr, next, opts) {
            $heroNavTabs.fadeTo(250, .75);
            $heroNavTabs.addClass('inactive');
        }
        var cycleDefaults = {
            fx: 'fade',
            speed: 500,
            timeout: 8000,
            autostop: 1,
            autostopCount: stopCount,
            pause: 1,
            sync: 1,
            cleartype: !$.support.opacity,
            before: onBefore,
            after: onAfter

        };
        $heroContentContainer.cycle(cycleDefaults);

        // Nav click event, hover
        $heroNavTabs.each(function (i) {
            $(this).click(function () {
                $heroNavTabs.addClass('inactive');
                $heroContentContainer.cycle({
                    fx: 'fade',
                    speed: 500,
                    timeout: 0,
                    before: onBefore,
                    after: onAfter
                });
                $heroContentContainer.cycle(i);
                $(this).removeClass('inactive');
                return false;
            });
            $(this).hover(function () {
                $heroContentContainer.cycle('pause');
            }, function () {
                $heroContentContainer.cycle('resume');
            });
        });
        // Show tab nav
        if ($heroNavTabs.length > 1) {
            $heroNavTabsContainer.removeClass('hide');
        }

        $heroContentContainer.click(function () {
            $heroContentContainer.cycle('stop');
        });

    }
});
