$(document).ready(function () {

    var currentTab = null;
    var showCurrentlyActiveTabContentTimeout;
    var $tabs = $('ul.tabs li');
    var $tabsContainer = $('.tab_container');
    var $tabsContent = $('.tab_content');

    var showTabContent = function () {
        clearTimeout(showCurrentlyActiveTabContentTimeout);
        $tabsContent.hide().eq($(this).index()).show();
    };

    var showCurrentlyActiveTabContent = function () {
        showCurrentlyActiveTabContentTimeout = setTimeout(function () {
            if (!$tabsContainer.hasClass('active')) {
                $tabsContent.hide().eq($tabs.filter('.active').index()).show();
            }
        }, 100);
    };

    var getSelectedTab = function () {
        if (currentTab != null) {
            return currentTab;
        }

        var tabs = { 'shop': 0, 'sexinfo': 1, 'community': 2 };

        if (/sexinfo/i.test(window.location) && !(/sexinfo\/guest_toy_reviews/i.test(window.location))) {
            currentTab = tabs.sexinfo;
            return tabs.sexinfo;
        } else if (/community/i.test(window.location) || /about/i.test(window.location) || /sexinfo\/guest_toy_reviews/i.test(window.location)) {
            currentTab = tabs.community;
            return tabs.community;
        } else {
            currentTab = tabs.shop;
            return tabs.shop;
        }
    };

    $tabs.eq(getSelectedTab()).show().addClass('active');
    $tabsContent.eq(getSelectedTab()).show();

    $tabs.hover(showTabContent, showCurrentlyActiveTabContent);

    $tabsContainer.hover(function () {
        $(this).addClass('active');
    }, function () {
        $(this).removeClass('active');
        showCurrentlyActiveTabContent();
    });
});
