﻿/* Init tabs */
$(".tab-menus .tab-menu-item").click(function () {
    var $me = $(this);
    var allMenuItems = $me.parents(".tab-menus:first").find(".tab-menu-item");
    allMenuItems.removeClass("current");
    $me.addClass("current");

    allMenuItems.each(function () {
        $($(this).attr("href")).hide();
    });

    $($me.attr("href")).show();

    return false;
});

/* Init footer change-language effects */
(function () {
    var changeLangBtn = $("#btn-change-lang");
    var langList = $("#lang-list");
    var notHideLangList = false;

    changeLangBtn.hover(function () {
        notHideLangList = true;
        langList.slideDown();
    }, function () {
        notHideLangList = false;
        hideLangList();
    });

    langList.hover(function () {
        notHideLangList = true;
    }, function () {
        notHideLangList = false;
        hideLangList();
    });

    function hideLangList() {
        setTimeout(function () {
            if (!notHideLangList) {
                langList.slideUp();
            }
        }, 20);
    }
})();

/* Header dropdown */
(function () {
    var notFadeOut = false;
    var currentNavItem = null;
    var currentDropdown = null;
    var overlay = $("#dropdown-overlay");
    var showDropdownDelay = 100;
    var hideDropdownDelay = 20;

    $(".new-header .top-nav .top-nav-item[section-id]").hover(function () {
        notFadeOut = true;
        if (currentNavItem != null) {
            currentNavItem.removeClass("hover");
        }
        currentNavItem = $(this);
        currentNavItem.addClass("hover");
        if (currentDropdown != null) {
            currentDropdown.hide();
        }
        currentDropdown = $("#section-drop-" + $(this).attr("section-id"));
        showDropdown();
    }, function () {
        notFadeOut = false;
        hideDropdown();
    });

    $(".new-header div.header-drop").hover(function () {
        notFadeOut = true;
    }, function () {
        notFadeOut = false;
        hideDropdown();
    });

    function showDropdown() {
        setTimeout(function(){
            if (notFadeOut){
                currentDropdown.show();
                showOverlay();
            }
        }, showDropdownDelay);
    }

    function hideDropdown() {
        setTimeout(function () {
            if (!notFadeOut) {
                hideOverlay();
                currentDropdown.hide();
                currentNavItem.removeClass("hover");
            }
        }, hideDropdownDelay);
    }

    function showOverlay() {
        var targetWidth = $(document).width();
        var targetHeight = $(document).height() - 115;
        overlay.width(targetWidth);
        overlay.height(targetHeight);
        overlay.css("filter", "alpha(opacity=0)");
        overlay.show();
        overlay.fadeTo('normal', 0.6);
    }

    function hideOverlay() {
        overlay.fadeTo('fast', 0, function () { $(this).hide(); });
    }
})();

/* Dialogs */
$(".jqmWindow").jqm({
    modal: true,
    overlayClass: 'popup-overlay'
});

$("a.dialog-trigger").click(function () {
    var dialog = $($(this).attr("href"));
    dialog.jqmShow();
    return false;
});
$("a.closeDialogBtn").click(function() {
    $(this).parents(".jqmWindow:first").jqmHide();
    return false;
});