﻿//Método a ser disparado sempre que é chamado métodos ajax
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(function (sender, args) {
    everyExecute();
});

//Método a ser disparado sempre que a pagina é carregada
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function (sender, args) {
    everyExecute();
});
function everyExecute() {
    $.noConflict();
    jQuery(document).ready(function ($) {

        if ($(".topo").html() != undefined) {
            scriptsTopo();
        }

        if ($(".home").html() != undefined) {
            scriptsHome();
        }

        if ($(".paginacao").html() != undefined) {
            paginacao();
        }

        if ($(".menuInterno").html() != undefined) {
            scriptsMenuLateral();
        }

        if ($(".pageAtendimento").html() != undefined) {
            scriptsValidateForm();
        }

        if ($(".pageOuvidoria").html() != undefined) {
            scriptsValidateForm();
        }

        function scriptsMenuLateral() {
            $('.menuInterno li a.atv').each(function (index) {
                var obj = $(this);
                while (obj.attr("class") != "menuInterno") {
                    obj = obj.parent();
                    obj = obj.show();
                }
            });

            $(".boxPageInterno .mid .colLeft .menuInterno > li:last").css("border", "0px");

            //Menu lateral
            $(".menuInterno2").each(function () {
                if ($(this).find("a").attr("class") == "openParent") {
                    $(this).removeAttr("style");
                    $(this).css("display", "block");
                }
            })

            $(".menuInterno3").each(function () {
                if ($(this).find("a").attr("class") == "openParent") {
                    $(this).removeAttr("style");
                    $(this).css("display", "block");
                }
            })

            $(".menuInterno a").click(function () {
                if ($(this).attr("class") == "open") {
                    $(this).parent().parent().find("ul").slideUp();
                    $(this).removeAttr("class");
                } else {
                    if ($(this).parent().next().attr("class") == "menuInterno2") {
                        $(this).parent().parent().find(".menuInterno2").slideDown(200);

                    } else if ($(this).parent().next().attr("class") == "menuInterno3") {
                        $(this).parent().parent().find(".menuInterno3").slideDown(200);
                    }
                    $(this).attr("class", "open");
                }
            })
        }

        function scriptsTopo() {
            
            //Mantém o menu com estilo ativo de acordo com a sessão atual
            var segmento = window.location.toString().split('/')[3]
            var segmentoItemMenu = $(".menu");
            switch (segmento) {
                case "ensino":
                    segmentoItemMenu.find(".ensino").addClass("active");
                    break;
                case "processo-seletivo":
                    segmentoItemMenu.find(".processoSeletivo").addClass("active");
                    break;
                case "quem-somos":
                    segmentoItemMenu.find(".quemSomos").addClass("active");
                    break;
                case "certificacao-fgv":
                    segmentoItemMenu.find(".certificacao").addClass("active");
                    break;
                case "fique-por-dentro":
                    segmentoItemMenu.find(".fiqueDentro").addClass("active");
                    break;
                case "noticia":
                    segmentoItemMenu.find(".fiqueDentro").addClass("active");
                    break;
                case "evento":
                    segmentoItemMenu.find(".fiqueDentro").addClass("active");
                    break;
                case "servicos":
                    segmentoItemMenu.find(".servicos").addClass("active");
                    break;
//                case "videos":
//                    segmentoItemMenu.find(".videos").addClass("active");
//                    break;
                case "fale-conosco":
                    segmentoItemMenu.find(".faleConosco").addClass("active");
                    break;
            }

            $(".topoLogin img").click(function () {
                $(".boxLogin").fadeToggle();
            })

            $(".topo .menu li").hover(function () {
                $(this).find("ul").css("display", "block");
            }, function () {
                $(this).find("ul").css("display", "none");
            })

            $(".topo .menu li ul li:last").css("background", "none");

            $('.boxLogin .botao').click(function () {
                var login = $('.boxLogin .txtLogin');
                var pass = $('.boxLogin .txtSenha');
                var loader = $('.boxLogin .loader');
                var info = $('.boxLogin .info');

                if (login.attr('value') == '' || pass.attr('value') == '') {
                    info.fadeIn();
                    login.attr('value', '');
                    pass.attr('value', '');
                    return false;
                }

                //                var button = $(this);
                //                button.hide();

                //                if (login.attr('value') != '' && pass.attr('value')) {
                //                    $.ajax({
                //                        type: "POST",
                //                        url: "../Ajax/Login.aspx/LoginUser",
                //                        contentType: "application/json; charset=utf-8",
                //                        dataType: "json",
                //                        data: "{'login':'" + login.attr('value') + "', 'pass':'" + pass.attr('value') + "'}",
                //                        beforeSend: function () {
                //                            loader.fadeIn();
                //                        },
                //                        success: function (retorno) {
                //                            var logou = retorno.d.toString();

                //                            if (logou == "true") {
                //                                location.href = "../painel/home";
                //                            }
                //                            else {
                //                                button.show();
                //                                loader.hide();
                //                                info.fadeIn();
                //                                login.attr('value', '');
                //                                pass.attr('value', '');
                //                            }
                //                        },
                //                        error: function (erro) {
                //                            alert(erro);
                //                            button.show();
                //                            loader.hide();
                //                        }
                //                    });
                //                } else {
                //                    info.fadeIn();
                //                    button.show();
                //                }

                //                return false;

            })
        }

        function scriptsHome() {
            $(".area3 .agenda ul li").eq(2).css("border", "0");
        }

        function paginacao() {
            $(".colRight").pajinate({
                num_page_links_to_display: 6,
                items_per_page: 20,
                item_container_id: '.listaPaginate',
                nav_panel_id: ".paginacao",
                nav_label_prev: "anterior",
                nav_label_next: "próximo"
            });
        }


        function scriptsValidateForm() {
            $(".botao").live("click", function (evt) {
                evt.stopPropagation();

                var erro = "";
                var er = /^[a-zA-Z0-9][a-zA-Z0-9\._-]+@([a-zA-Z0-9\._-]+\.)[a-zA-Z-0-9]{2}/;

                if ($(".nome").val() == "") {
                    erro += "<li>Nome</li>";
                }

                if (!(er.exec($(".email").val()))) {
                    erro += "<li>E-mail</li>";
                }

                if ($(".fone").val() == "") {
                    erro += "<li>Telefone</li>";
                }

                if ($(".assunto").find("option:selected").val() == "Selecione") {
                    erro += "<li>Assunto</li>";
                }

                if ($(".vinculoSelect").find("option:selected").val() == "Selecione") {
                    erro += "<li>Vinculo</li>";
                }

                if ($(".mensagem").val() == "") {
                    erro += "<li>Mensagem</li>";
                }

                if (erro != "") {
                    $(".boxErro").html('<span>Os campos abaixo n&atilde;o foram preenchidos corretamente:</span><ul>' + erro + "</ul>").css("display", "block");
                    return false;
                }
                return true;
            })

        }
    });
}






