﻿
function converte_em_numero(valor) {
    if (valor.search(/px/i) >= 0) {
        return valor.replace('px', '') * 1;
    } else if (valor.search(/%/i) >= 0) {
        return valor.replace('%', '') / 100;
    } else {
        return valor
    }
}

function converte_em_numero_x(valor) {
    if (valor.search(/%/i) >= 0) {
        return converte_em_numero(valor) * tela_width;
    } else {
        return converte_em_numero(valor) * 1;
    }
}

function converte_em_numero_y(valor) {
    if (valor.search(/%/i) >= 0) {
        return converte_em_numero(valor) * tela_height;
    } else {
        return converte_em_numero(valor) * 1;
    }
}

function seta(elemento, v_left, v_top, v_width, v_height, v_opacity, v_tempo) {

    elemento.stop();
        var v_tempo2 = tempo;
    if (v_tempo != '' && v_tempo != undefined) { v_tempo2 = v_tempo; }

    if (v_left != '' && v_left != undefined) {
        elemento.css("left", converte_em_numero_x(elemento.css("left")));
        v_left_array = v_left.split(";");
        v_left2 = converte_em_numero_x(v_left_array[0]);
        if (v_left_array.length > 1) { v_left2 += converte_em_numero_x(v_left_array[1]) }
        elemento.animate({ left: v_left2 }, { queue: false, duration: v_tempo2 });
    }

    if (v_top != '' && v_top != undefined) {
        elemento.css("top", converte_em_numero_y(elemento.css("top")));
        v_top_array = v_top.split(";");
        v_top2 = converte_em_numero_y(v_top_array[0]);
        if (v_top_array.length > 1) { v_top2 += converte_em_numero_y(v_top_array[1]) }
        elemento.animate({ top: v_top2 }, { queue: false, duration: v_tempo2 })
    }
    
    if (v_width != '' && v_width != undefined) {
        elemento.css("width", converte_em_numero_x(elemento.css("width")));
        v_width_array = v_width.split(";");
        v_width2 = converte_em_numero_x(v_width_array[0]);
        if (v_width_array.length > 1) { v_width2 += converte_em_numero_x(v_width_array[1]) }
        elemento.animate({ width: v_width2 }, { queue: false, duration: v_tempo2 })
    }

    if (v_height != '' && v_height != undefined) {
        elemento.css("height", converte_em_numero_y(elemento.css("height")));
        v_height_array = v_height.split(";");
        v_height2 = converte_em_numero_y(v_height_array[0]);
        if (v_height_array.length > 1) { v_height2 += converte_em_numero_y(v_height_array[1]) }
        elemento.animate({ height: v_height2 }, { queue: false, duration: v_tempo2 });
    }

    if (v_opacity != '' && v_opacity != undefined  && v_opacity != elemento.css('opacity')) {
        if (elemento.css('display') == 'none') { elemento.css("opacity", 0); }
        elemento.css('display', 'block');
        elemento.animate({ opacity: v_opacity },v_tempo2, function () {
            if (elemento.css('opacity') == 0) { elemento.css('display', 'none'); }
        });
    }
}

function seta_tamanho() {
    tela_width = converte_em_numero_y($('#geral').css('width'));
    tela_height = converte_em_numero_y($('#geral').css('height'));
}


function funcoes_extras() {
    if (menu_anterior != "home" || secao != "home") {
        $('html, body').animate({ scrollTop: 0 }, 'slow');
    }
    desliga_banner();
    pinta_menu();
}


function pinta_menu() {
    secao = retorna_variaveis('secao', window.location.href.split(sep)[1]);
    pagina = retorna_variaveis('pagina', window.location.href.split(sep)[1]);
    if ($("#menu_" + menu_anterior).html() != null) {
        $("#menu_" + menu_anterior).attr("class", "");
    }
    if ($("#menu_" + secao).html() != null) {
        menu_anterior = secao;
        $("#menu_" + secao).attr("class", "menu_ativo");
    }
    if ($("#menu_" + pagina).html() != null) {
        menu_anterior = pagina;
        $("#menu_" + pagina).attr("class", "menu_ativo");
    }
}

function controla_banners(n) {
    foto0 = $('#banner_home_id_0');
    foto1 = $('#banner_home_id_1');
    foto2 = $('#banner_home_id_2');
    foto3 = $('#banner_home_id_3');
    foto4 = $('#banner_home_id_4');
    botao0 = $('#link_banner_home_id_0');
    botao1 = $('#link_banner_home_id_1');
    botao2 = $('#link_banner_home_id_2');
    botao3 = $('#link_banner_home_id_3');
    botao4 = $('#link_banner_home_id_4');
    if (n == 0) {
        seta(foto0, '', '', '', '', '1', '1000');
        seta(foto1, '', '', '', '', '0', '1000');
        seta(foto2, '', '', '', '', '0', '1000');
        seta(foto3, '', '', '', '', '0', '1000');
        seta(foto4, '', '', '', '', '0', '1000');
        botao0.css("z-index", 1);
        botao1.css("z-index", 3);
        botao2.css("z-index", 3);
        botao3.css("z-index", 3);
        botao4.css("z-index", 3);
    } else if (n == 1) {
        seta(foto0, '', '', '', '', '0', '1000');
        seta(foto1, '', '', '', '', '1', '1000');
        seta(foto2, '', '', '', '', '0', '1000');
        seta(foto3, '', '', '', '', '0', '1000');
        seta(foto4, '', '', '', '', '0', '1000');
        botao0.css("z-index", 3);
        botao1.css("z-index", 1);
        botao2.css("z-index", 3);
        botao3.css("z-index", 3);
        botao4.css("z-index", 3);
    } else if (n == 2) {
        seta(foto0, '', '', '', '', '0', '1000');
        seta(foto1, '', '', '', '', '0', '1000');
        seta(foto2, '', '', '', '', '1', '1000');
        seta(foto3, '', '', '', '', '0', '1000');
        seta(foto4, '', '', '', '', '0', '1000');
        botao0.css("z-index", 3);
        botao1.css("z-index", 3);
        botao2.css("z-index", 1);
        botao3.css("z-index", 3);
        botao4.css("z-index", 3);
    } else if (n == 3) {
        seta(foto0, '', '', '', '', '0', '1000');
        seta(foto1, '', '', '', '', '0', '1000');
        seta(foto2, '', '', '', '', '0', '1000');
        seta(foto3, '', '', '', '', '1', '1000');
        seta(foto4, '', '', '', '', '0', '1000');
        botao0.css("z-index", 3);
        botao1.css("z-index", 3);
        botao2.css("z-index", 3);
        botao3.css("z-index", 1);
        botao4.css("z-index", 3);
    } else if (n == 4) {
        seta(foto0, '', '', '', '', '0', '1000');
        seta(foto1, '', '', '', '', '0', '1000');
        seta(foto2, '', '', '', '', '0', '1000');
        seta(foto3, '', '', '', '', '0', '1000');
        seta(foto4, '', '', '', '', '1', '1000');
        botao0.css("z-index", 3);
        botao1.css("z-index", 3);
        botao2.css("z-index", 3);
        botao3.css("z-index", 3);
        botao4.css("z-index", 1);
    }
}

function liga_banner(n) {
    n_banner = n;
    $('#banner_home_barra').stop();
    $('#banner_home_barra').css('width', 0);
    $('#banner_home_barra').animate({ "width": "650px" }, (parseInt($('#banner_home_id_' + n_banner).attr('duracao')) * 1000) - 100, 'linear');
    var_timeout_banner = setTimeout('liga_banner2()', (parseInt($('#banner_home_id_' + n_banner).attr('duracao')) * 1000));
}
function liga_banner2() {
    n = n_banner + 1;
    if (n > 4) { n = 0; }
    n_banner = n;
    controla_banners(n_banner);
    $('#banner_home_barra').stop();
    $('#banner_home_barra').css('width', 0);
    $('#banner_home_barra').animate({ "width": "650px" }, (parseInt($('#banner_home_id_' + n_banner).attr('duracao')) * 1000) - 100, 'linear');
    var_timeout_banner = setTimeout('liga_banner2()', (parseInt($('#banner_home_id_' + n_banner).attr('duracao')) * 1000));
}
function desliga_banner() {
    clearTimeout(var_timeout_banner);
    $('#banner_home_barra').css('width', 0);
    $('#banner_home_barra').stop();
}


function envia_agenda() {
    if ($('#select_area').attr('value') != '' || $('#select_cidade').attr('value') != '' || $('#select_mes').attr('value') != '' || $('#select_categoria').attr('value') != '') {
        linka2('secao=agenda/area=' + $('#select_area').attr('value') + '/cidade=' + $('#select_cidade').attr('value') + '/data=' + $('#select_mes').attr('value') + '/categoria=' + $('#select_categoria').attr('value'));
    } else {
       alert('Selecione pelo menos um campo da busca.')
    }
}
function envia_busca_avancada() {
    v_erro = 0;
    v_data_inicio = '';
    v_data_fim = '';
    //if (seleciona_checkbox('select_area') != '' || seleciona_checkbox('select_cidade') != '' || seleciona_checkbox('select_mes') != '' || seleciona_checkbox('select_categoria') != '') {
    if ($('#data_inicio').val() != '' || $('#data_fim').val() != '' || seleciona_checkbox('select_area') != '' || seleciona_checkbox('select_cidade') != '' || seleciona_checkbox('select_categoria') != '') {
        /*
        if ($('#data_inicio').val() != '' && validarData($('#data_inicio').val(), 'data_inicio') != 1) {
            alert('Formato errado de data');
            $('#data_inicio').val('');
            $('#data_inicio').focus();
            v_erro = 1;
        }

        if ($('#data_fim').val() != '' && validarData($('#data_fim').val(), 'data_fim') != 1) {
            alert('Formato errado de data');
            $('#data_fim').val('');
            $('#data_fim').focus();
            v_erro = 1;
        }
        */

        data_inicio = "" + $('#data_inicio').val().split("/")[2] + $('#data_inicio').val().split("/")[1] + $('#data_inicio').val().split("/")[0]
        data_fim = "" + $('#data_fim').val().split("/")[2] + $('#data_fim').val().split("/")[1] + $('#data_fim').val().split("/")[0]

        if ($('#data_fim').val() != '' && parseInt(data_inicio) > parseInt(data_fim)) {
            alert('A Data Fim não pode ser menor que a de início');
            $('#data_fim').focus();
            v_erro = 1;
        }
        if (v_erro == 0) {
            if ($('#data_inicio').val() != '') { v_data_inicio = $('#data_inicio').val().split('/')[1] + '_' + $('#data_inicio').val().split('/')[0] + '_' + $('#data_inicio').val().split('/')[2]; }
            if ($('#data_fim').val() != '') { v_data_fim = $('#data_fim').val().split('/')[1] + '_' + $('#data_fim').val().split('/')[0] + '_' + $('#data_fim').val().split('/')[2]; }
            //linka2('secao=agenda/area=' + seleciona_checkbox('select_area') + '/cidade=' + seleciona_checkbox('select_cidade') + '/data=' + seleciona_checkbox('select_mes') + '/categoria=' + seleciona_checkbox('select_categoria'));
            linka2('secao=agenda/area=' + seleciona_checkbox('select_area') + '/cidade=' + seleciona_checkbox('select_cidade') + '/data_inicio=' + v_data_inicio + '/data_fim=' + v_data_fim + '/categoria=' + seleciona_checkbox('select_categoria'));
        }
    } else {
        alert('Selecione pelo menos um campo da busca.')
    }
}
/*
function validarData(valor,campo) {
    var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
    if ((valor.match(expReg)) && (valor != '')) {
        return 1;
    } else {
        return 0;
    }
}
*/
function seleciona_checkbox(valor) {
    var valores = "";
    var sep = "";
    var boxes = document.getElementsByName(valor);
 
    for (var i = 0; i < boxes.length; i++) {
        if (boxes[i].checked == true) {
            valores = valores + sep + boxes[i].value;
            sep = ",";
        }
    }
    return valores;
}

function imprimir(url) {
    window.open(url, "popup_impressao", "width=670,height=500,scrollbars=1,resizable=1"); 
}

function imprimir_trilha() {
    imprimir('index.aspx?layout=impressao#!/secao=trilha_impressao/ids_curso=' + seleciona_trilha_todos());
}
function salva_trilha() {
    Set_Cookie('trilha', seleciona_trilha_todos('nao'), '30', '/', '', '');
}
function trilha_areas(valor) {
    v_trilha_areas = v_trilha_areas + valor + ";;";
}

function deleta_trilhas() {
    valor_array = v_trilha_areas.split(";;");
    var valores = "";
    var var_html = "";
    var sep = "";
    cookie_array = Get_Cookie('trilha').split(";;");
    for (var n = 0; n < valor_array.length; n++) {
        var boxes = document.getElementsByName('select_trilha_' + valor_array[n]);
        for (var i = 0; i < boxes.length; i++) {
            for (var i2 = 0; i2 < cookie_array.length; i2++) {
                if (boxes[i].value.split(":::")[0] == cookie_array[i2]) {
                    boxes[i].checked = false;
                    seleciona_trilha(valor_array[n], '(fechar)');
                }
            }
        }
    }
    Delete_Cookie('trilha', '/', '');
    Delete_Cookie('trilha_nomes', '/', '');
    Delete_Cookie('trilha_tipos', '/', '');
    Delete_Cookie('trilha_areas', '/', '');
}

function carrega_trilhas(){
    cookie_array = Get_Cookie('trilha').split(";;");
    cookie_array4 = Get_Cookie('trilha_areas').split(";;");
    valor_array = v_trilha_areas.split(";;");
    var valores = "";
    var var_html = "";
    var sep = "";
    for (var n = 0; n < valor_array.length; n++) {
        var boxes = document.getElementsByName('select_trilha_' + valor_array[n]);
        for (var i = 0; i < boxes.length; i++) {
            for (var i2 = 0; i2 < cookie_array.length; i2++) {
                if (boxes[i].value.split(":::")[0] == cookie_array[i2] && cookie_array4[i2] == valor_array[n]) {
                    boxes[i].checked = true;
                    seleciona_trilha(valor_array[n], '(fechar)');
                }
            }
        }
    }
}

function seleciona_trilha_todos(fecha) {

    valor_array = v_trilha_areas.split(";;");
    var valores = "";
    var valores2 = "";
    var valores3 = "";
    var valores4 = "";
    var sep = "";
    for (var n = 0; n < valor_array.length; n++) {
        var boxes = document.getElementsByName('select_trilha_' + valor_array[n]);
        for (var i = 0; i < boxes.length; i++) {
            if (boxes[i].checked == true) {
                if (valores.split(";;").indexOf(boxes[i].value.split(":::")[0]) < 0) {
                    valores = valores + sep + boxes[i].value.split(":::")[0];
                    valores2 = valores2 + sep + boxes[i].value.split(":::")[1];
                    valores3 = valores3 + sep + boxes[i].value.split(":::")[2];
                    valores4 = valores4 + sep + valor_array[n];
                    sep = ";;";
                }
            }
        }
        if (fecha != 'nao') {
            seleciona_trilha(valor_array[n], '(fechar)');
        }
    }
    Set_Cookie('trilha_nomes', valores2, '30', '/', '', '');
    Set_Cookie('trilha_tipos', valores3, '30', '/', '', '');
    Set_Cookie('trilha_areas', valores4, '30', '/', '', '');
    //alert(valores2);
    return valores;
}

function seleciona_trilha(valor, acao) {
    if (acao == undefined) {
        var acao = $('#trilha_link_' + valor).html();
    }
    var valores = "";
    var var_html = "";
    var sep = "";
    if (acao == '(fechar)') {
        var boxes = document.getElementsByName('select_trilha_' + valor);

        for (var i = 0; i < boxes.length; i++) {
            if (boxes[i].checked == true) {
                valores = valores + sep + boxes[i].value.split(":::")[1];
                if (boxes[i].value.split(":::")[2] == "curso") {
                    var_html = var_html + "<a href=javascript:linka2('secao=curso/pagina=programa/id_curso=" + boxes[i].value.split(":::")[0] + "');><b> " + boxes[i].value.split(":::")[1] + "</b></a><br>";
                } else {
                    var_html = var_html + "<a href=javascript:linka2('secao=mba/pagina=" + trata(boxes[i].value.split(":::")[1]) + "');><b> " + boxes[i].value.split(":::")[1] + "</b></a><br>";
                }
                sep = ";;";
            }
        }
        $('#trilha_' + valor).css('display', 'none');
        $('#trilha_resultado_' + valor).css('display', 'block');
        if (valores == '') {
            $('#trilha_resultado_conteudo_' + valor).html(var_html + ' <b>sem seleção</b>');
        } else {
            $('#trilha_resultado_conteudo_' + valor).html(var_html);
        }
        $('#trilha_link_' + valor).html('(expandir)');
    } else {
        $('#trilha_' + valor).css('display', 'block');
        $('#trilha_resultado_' + valor).css('display', 'none');
        $('#trilha_link_' + valor).html('(fechar)');
    }

    //return valores;
}
function seleciona_curso(valor) {
    var_timeout = setInterval('seleciona_curso2("' + valor + '")', 1000);
}
function seleciona_curso2(valor) {
    texto = $('#titulo_' + valor).html();
    if (texto != null) {
        clearInterval(var_timeout);
        $('#titulo_' + valor).html("<b style='color:#ff0000;'>" + texto + "</b>");
    }
}

function muda_foto(foto, valor) {
    $('#' + foto).attr("src","admin/clientes/integracao/images/" + foto + valor + ".gif");
}
function linka_busca() {
    if ($('#busca').attr("value") == "Busca de Cursos" || $('#busca').attr("value") == "") {
        linka2('secao=agenda/busca=');
        //       alert('Preecha o curso que deseja buscar.');
 //       $('#busca').focus();
    } else {
        linka2('secao=agenda/busca=' + $('#busca').attr("value"));
    }
}

function busca_enter(e) {
    if (e.keyCode == 13) {
        alert('porra');
        return false;
        e.returnValue = false;
        e.cancel = true;
    }
}

// ---------- carrega o jaquery para a galeria ----------
function carrega_galeria_jquery(div) {
    //$('#' + div + ' a').prettyPhoto();
    $('[id$=' + div + '] a').prettyPhoto(
        {
            default_width: 700,
            horizontal_padding: 20
        }
    );
}

function linka2_trilha(url) {
    alert('Você está abrindo uma nova janela, sua trilha continua aberta na jenala anterior.');
    window.open('index.aspx#!/'+url)
}



//FUNCOES DE COOKKIE

function Set_Cookie(name, value, expires, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) +
((expires) ? ";expires=" + expires_date.toGMTString() : "") +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
((secure) ? ";secure" : "");
}


// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}


// this deletes the cookie when called
function Delete_Cookie(name, path, domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function redimensiona_solicita(frame, valor) {
    $('#'+frame).css('height',valor);
}
