var menuSpeed = 500;

$(document).ready(function () {
    $("body").prepend('<img src="images/submenu.png" style="position: absolute; width: 1px; height: 1px" />');

    $(document).bind("contextmenu", function (event) {
        if (!event || !event.target || (event.target.nodeName != "A" && event.target.nodeName != "INPUT" && event.target.nodeName != "TEXTAREA"))
            return false;
        else {
            if (event.target.nodeName == "A" && $(event.target).hasClass("button")) return false;
            else return true;
        }
    });

    $("a[href=#suporte-ao-vivo]").click(function (event) {
        var x = (screen.width / 2) - 212;
        var y = (screen.height / 2) - 250;
        var janela = window.open("http://www.neepz.com/chat/request_email.php?l=neepz&x=1&deptid=0", "chat",
            "left = " + x + ", top = " + y + ", height = 461, width = 357");
        janela.resizeTo(470, 435);
        janela.moveTo(x, y);
        event.preventDefault();
        return false;
    });

    $("#side_menu a.submit-link").click(function (event) {
        $(this).parents("form").submit();
        event.preventDefault();
        return false;
    });

    $("#side_menu input[type=text]:not(.password)").focus(function () {
        $(this).addClass("focus");
        if ($(this).val() == $(this).attr("placeholder")) $(this).val("");
    }).blur(function () {
        $(this).removeClass("focus");
        if ($(this).val() == "") $(this).val($(this).attr("placeholder"));
    });

    $("#side_menu .password-clear").show();
    $("#side_menu .password-password").hide();

    $("#side_menu .password-clear").focus(function () {
        $("#side_menu .password-clear").hide();
        $("#side_menu .password-password").show();
        $("#side_menu .password-password").focus();
    });
    $("#side_menu .password-password").blur(function () {
        if ($("#side_menu .password-password").val() == "") {
            $("#side_menu .password-clear").show();
            $("#side_menu .password-password").hide();
        }
    });

    if ($("#side_menu").height() > $("#content_left .content-main").height() + $("#content_left h1").height())
        $("#content_left .content-main").css("height", ($("#side_menu").height() - $("#content_left h1").height()) + "px");

    ajustarMenus();
});

function menuEnter() {
    fecharMenus();

    $(window).stopTime("fecharMenus");

    var subMenu = $("div", this);
    if (subMenu.length == 0)
        return;

    subMenu.show();

    var height = subMenu.height();
    if (subMenu.attr("total-height") == null) {
        subMenu.attr("total-height", height);
    }

    subMenu.css("height", 0);

    if ($.support.opacity) {
        subMenu.fadeTo(0, 0);
        subMenu.stop(false, true).animate({ height: [height, "swing"], opacity: [1, "easeInQuad"] }, { queue: false, duration: menuSpeed });
    } else {
        subMenu.stop(false, true).animate({ height: height }, { queue: false, duration: menuSpeed, easing: "swing" });
    }
}

function menuLeave() {
    $(window).oneTime(600, "fecharMenus", fecharMenus);
}

function fecharMenus() {
    $("div.hospedagem, div.seja-neepz, div.a-neepz, div.suporte", "#top_container .menu > ul").each(function () {
        if ($(this).is(":visible")) {
            var height = $(this).attr("total-height");
            if ($.support.opacity) {
                $(this).stop(true, true).animate({ height: 0, opacity: 0 }, { queue: false, duration: menuSpeed - 200, easing: "easeInCirc", complete: function () {
                    $(this).hide();
                    $(this).css("height", height + "px");
                }
                });
            } else {
                $(this).stop(true, true).animate({ height: 0 }, { queue: false, duration: menuSpeed - 200, easing: "easeInCirc", complete: function () {
                    $(this).hide();
                    $(this).css("height", height + "px");
                }
                });
            }
        }
    });

    $("a.last", "#top_container .menu > ul").hide();
}

function ajustarMenus() {
    $("#top_container .menu > ul").append("<li><a class=\"last\"></a></li>");

    $("a.hospedagem, a.seja-neepz, a.a-neepz, a.suporte, a.last", "#top_container .menu > ul").parent()
        .mouseenter(menuEnter)
        .mouseleave(menuLeave)
        .children("a").click(function (event) {
            event.preventDefault();
            return false;
        });

    $("a.suporte", "#top_container .menu > ul").parent()
        .mouseenter(function () {
            $("a.last", ".menu > ul").show();
        });

    $("a.last", "#top_container .menu > ul").parent()
        .mouseenter(function () {
            var suporte = $("a.suporte", "#top_container .menu > ul").parent().find("div");
            suporte.stop(true, true).hide();
        });

    $("#top_container #company_title")
        .mouseenter(function () {
            $("a.hospedagem, a.seja-neepz, a.a-neepz, a.suporte", "#top_container .menu > ul").parent().find("div").stop(true, true).hide();
        });
}