Number.prototype.padLeft = function (m) {
    return (this + Math.pow(10, m)).toString().substr(1);
}

var abaAtual = null;
var menuSpeed = 500;

$(document).ready(function () {
    $("body").prepend('<img src="img/submenu.png" style="position: absolute; width: 1px; height: 1px" />');
    $("body").prepend('<img src="img/area-cliente.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;
    });

    if ($(this).corner) $(".rounded").corner("12px");
    if ($(".abas").length > 0 && $(".conteudo-aba").length > 1) $(window).everyTime(100, "ajustarAbas", ajustarAbas);

    ajustarMenus();

    $(".area-cliente > a").click(function (event) {
        window.location = "http://www.neepz.com/cliente/";

        // TODO: Reexibir caixa de login quando a programação PHP de
        // comunicação com o WHMCS para verificação de login for realizada.

        //var areaCliente = $("> div", $(this).parent());

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

        //areaCliente.css("height", 0);
        //$(".area-cliente").focus();
        //if ($.support.opacity) {
        //    areaCliente.fadeTo(0, 0);
        //    areaCliente.stop(false, true).animate({ height: [height, "swing"], opacity: [1, "easeInQuad"] }, { queue: false, duration: menuSpeed });
        //} else {
        //    areaCliente.stop(false, true).animate({ height: height }, { queue: false, duration: menuSpeed, easing: "swing" });
        //}

        event.preventDefault();
        return false;
    });

    $(".area-cliente .fechar").click(function (event) {
        var areaCliente = $(".area-cliente > div");
        var height = areaCliente.attr("total-height");
        if ($.support.opacity) {
            areaCliente.stop(true, true).animate({ height: 0, opacity: 0 }, { queue: false, duration: menuSpeed - 400, easing: "easeInCirc", complete: function () {
                areaCliente.hide();
                areaCliente.css("height", height + "px");
            }
            });
        } else {
            areaCliente.stop(true, true).animate({ height: 0 }, { queue: false, duration: menuSpeed - 400, easing: "easeInCirc", complete: function () {
                areaCliente.hide();
                areaCliente.css("height", height + "px");
            }
            });
        }

        event.preventDefault();
        return false;
    });

    $(".area-cliente .login, .area-cliente .senha, .input").each(function () {
        if ($(this).attr("default") != null)
            $(this).val($(this).attr("default"));

        $(this).focus(function () {
            $(this).addClass("focus");
            if ($(this).attr("default") != null && $(this).val() == $(this).attr("default"))
                $(this).val("");
        });

        $(this).blur(function () {
            $(this).removeClass("focus");
            if ($(this).attr("default") != null && $(this).val() == "")
                $(this).val($(this).attr("default"));
        });
    });
});

function ajustarAbas() {
    var path = window.location.toString();
    if (path.indexOf("#") == -1) {
        window.location += $(".abas a:first").attr("href");
        return;
    }

    var hash = path.substr(path.indexOf("#") + 1);

    if (hash != abaAtual) {
        abaAtual = hash;

        var scroll = $(window).scrollTop();

        $(".titulo-02").attr("class", "titulo titulo-02").addClass(abaAtual);
        $(".abas a").removeClass("selected");
        $(".abas a[href=\#" + hash + "]").addClass("selected");
        $(".fade-imagens").cycle("pause");
        $(".conteudo-aba").hide();
        $(".conteudo-aba." + hash).show();
        $(window).scrollTop(scroll);
        $(".fade-imagens", ".conteudo-aba." + hash).cycle(0);
        $(".fade-imagens", ".conteudo-aba." + hash).cycle("resume");
    }
}

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", ".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", ".menu > ul").hide();
}

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

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

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

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

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