﻿$(document).ready(function() {
    Interface.openExternal();

    $("#lnkIndique").click(function() {
        Modal.show("#modalIndique");
        $(".sucesso").hide();
        $(".erro").hide();
        $("#divIndicacao").show();
        $("#seuNome").focus();
        registraIndiqueAmigo();
    });

    Modal.hide();

    $(".btnLimpar, .btnEnviar").hover(
        function() {
            Interface.overButton($(this));
        },
        function() {
            Interface.outButton($(this));
        }
    );

    Interface.positionFooter();
});

//window.onload = Interface.positionFooter();

var DropDown = {
    /*
    Método que:
    - abre os elementos drop down da página 
    - faz navegação por setas
    - busca por palavras de acordo com a primeira tecla pressionado
    @author Evandro L. Gonçalves
    @return void
    */
    show: function() {
        $(".dropDown").click(function() {
            var drop = $(this);

            $(this).find("ul").slideDown("fast", function() {
                $(document).keyup(function(event) {
                    var padraoLetra = /[A-Za-z0-9]/;
                    var teclaCode = event.which;
                    var tecla = String.fromCharCode(teclaCode);
                    var achou = false;

                    if (padraoLetra.test(tecla)) {
                        var cont = -1;
                        var total = drop.find("li a").size();
                        var i;
                        var ativo = new Array();
                        ativo[0] = null;
                        ativo[1] = null;

                        drop.find("li a").each(function() {
                            cont++;

                            if (achou == false) {
                                var cidade = $(this);

                                if (cidade.html().charAt(0) == tecla || cidade.attr("title").charAt(0) == tecla) {
                                    //alert($(".dropDown li a:eq(" + (cont) + ")").html());
                                    for (i = cont; i < total; i++) {
                                        var elem = drop.find("li a:eq(" + (i) + ")");

                                        if (elem.html().charAt(0) == tecla) {
                                            if (elem.hasClass("atv")) {
                                                ativo[0] = i;
                                                ativo[1] = elem.html();
                                            }
                                        }
                                    }

                                    if (ativo[0] == null) {
                                        var classeCidade = "." + cidade.attr("class");

                                        drop.find(".atv").removeClass("atv");
                                        cidade.addClass("atv");
                                        cidade.focus();

                                        achou = true;
                                    } else {
                                        cont2 = ativo[0] + 1;
                                        var proximo = drop.find("li a:eq(" + (cont2) + ")");

                                        if (proximo.html().charAt(0) == tecla) {
                                            var classeCidade = "." + proximo.attr("class");

                                            drop.find(".atv").removeClass("atv");
                                            proximo.addClass("atv");
                                            proximo.focus();

                                            achou = true;
                                        }
                                    }
                                }
                            }
                        });
                    } else {
                        if (teclaCode == 38 || teclaCode == 40) {
                            var qtdLinkAtivo = drop.find(".atv").size();

                            if (qtdLinkAtivo == 0) {
                                drop.find("li:eq(0) a").addClass("atv").focus();
                            } else {
                                teclaCode == 40 ? posElem = 0 : posElem = -2;
                                var achou = false;

                                drop.find("ul a").each(function() {
                                    if (achou == false) {
                                        ++posElem;

                                        if ($(this).hasClass("atv")) {
                                            achou = true;
                                        }
                                    }
                                });

                                drop.find(".atv").removeClass("atv");
                                var cidade = drop.find("li:eq(" + posElem + ") a");
                                var classeCidade = cidade.attr("class");

                                cidade.addClass("atv").focus();
                            }
                        }
                    }

                    if (teclaCode == "27") {
                        DropDown.hide();
                    }

                    $(".dropDown li a").mouseover(function() {
                        $(".dropDown .atv").removeClass("atv");
                    });
                });

                $(document).click(function() {
                    DropDown.hide();
                });
            });
        });
    },

    hide: function() {
        $(".dropDown ul").hide();
        $(".dropDown .atv").removeClass("atv");
        $(document).unbind("keyup");
    }
}

var Modal = {
    /*
    Método que abre uma modal passada
    @author Evandro L. Gonçalves
    @param String
    @return void
    */
    show: function(elem) {
        var largura = $(elem).width();
        var altura = $(elem).height();
        var margemEsq = "-" + ($(elem).width() / 2) + "px";
        var margemTop = "-" + ($(elem).height() / 2) + "px";

        if (($.browser.msie) && ($.browser.version == "6.0")) {
            var alturaBody = $(document).height() + "px";
            $("#bgrModal").css("height", alturaBody);
        }

        $(elem).css({ "margin-left": margemEsq, "margin-top": margemTop });
        $("#bgrModal").fadeIn("fast");
        $(elem).fadeIn("fast");
    },

    /*
    Método que fecha todos os modais caso o usuário clique no botão fechar, no fundo ou pressione a ESQ
    @author Evandro L. Gonçalves
    @return void
    */
    hide: function() {
        $(".btnFechar, #bgrModal").click(function() {
            $("#bgrModal, .boxModal").fadeOut("fast");
        });

        $(document).keyup(function(event) {
            if (event.keyCode == "27") {
                $("#bgrModal, .boxModal").fadeOut("fast");
            }
        });
    }
}

var Interface = {
    /*
    Método que adiciona uma classe p/ o estado over do botão
    @author Evandro L. Gonçalves
    @param String
    @return void
    */
    overButton: function(elem) {
        $(elem).addClass("over");
    },

    /*
    Método que retira a classe inserida no estado over
    @author Evandro L. Gonçalves
    @param String
    @return void
    */
    outButton: function(elem) {
        $(elem).removeClass("over");
    },

    /*
    Método que abre e fecha conteúdo de 'Tire suas dúvidas'
    @author Evandro L. Gonçalves
    @param String
    @return void
    */
    showContent: function(elem) {
        var parent = elem.parent();

        parent.find(".conteudo").toggle("fast");
        elem.toggleClass("icoAtv");
    },

    /*
    Método que abre links com rel='external em nova janela'
    @author Evandro L. Gonçalves
    @param String
    @return void
    */
    openExternal: function(elem) {
        $("a[rel='external']").click(function() {
            window.open($(this).attr("href"));
            return false;
        });
    },

    /*
    Método que posiciona o footer sempre no rodapé da tela
    @author Evandro L. Gonçalves
    @return void
    */
    positionFooter: function() {
        var alturaBody;
        var alturaConteudo;

        $("#all").height() == null ? alturaConteudo = $("#flash").height() : alturaConteudo = $("#all").height();
        !document.documentElement.clientHeight ? alturaBody = window.innerHeight : alturaBody = document.documentElement.clientHeight;

        if (eval(alturaConteudo + 55) < eval(alturaBody)) {
            $("#footer").css({ "position": "absolute", "bottom": "0" });
        }else {
            $("#footer").css("position", "static");
        }
    },

    /*
    Método responsável por inserir no textarea o código dos banners da seção divulgue
    @author Evandro L. Gonçalves
    @return void
    */
    actionDivulgue: function() {
        $(".dropDivulgue ul a").click(function() {
            var enderecoSite = "http://www.agilizeai.com.br/_img/divulgue/selos/";
            var codigo = "<a href='http://www.chevroletagile.com.br/agilize/'><img src='urlImg' /></a>";
            var identificador = $(this).attr("id");
            identificador = identificador.split("_");

            $("#" + identificador[0]).val(codigo.replace("urlImg", enderecoSite + identificador[0] + "_" + identificador[1] + ".jpg"));
            $("#" + identificador[0]).select();
        });
    },

    /*
    Método que abre a lista de instituições, rola o scroll até ela
    @author Evandro L. Gonçalves
    @param String
    @return void
    */
    openListInstituicoes: function(elem) {
        $("#footer").css("position", "static");
        $(elem).fadeIn("fast");
        $.scrollTo(elem, { speed: 500 });
        $("#vejaInstituicoes").hide();
        $("#escondaInstituicoes").show();
    },

    /*
    Método que fecha a lista de instituições
    @author Evandro L. Gonçalves
    @param String
    @return void
    */
    hideListInstituicoes: function(elem) {
        $("#footer").css("position", "absolute");
        $(elem).fadeOut("fast");
        $("#escondaInstituicoes").hide();
        $("#vejaInstituicoes").show();
    },

    /*
    Método que faz navegação entre abas da seção 'Doe Agora'
    @author Evandro L. Gonçalves
    @param String
    @return void
    */
    openAba: function(elem) {
        var aba = elem.replace("#", "#btn-");

        setTimeout(function() {
            Interface.positionFooter();
        }, 500);
        
        $(".menuComoAjudar a").removeClass("atv");
        $(aba).addClass("atv");
        $(".boxComoAjudar").hide();
        $(elem).fadeIn("fast");
    }
}

