﻿
// ---------- deixa caixa baixa e tira todos os acentos ----------
function trata(valor) {
    if (valor == undefined) {
        valor = "";
    } else {
        var com_acento = "á,à,ã,â,ä,é,è,ê,ë,í,ì,î,ï,ó,ò,õ,ô,ö,ú,ù,û,ü,ç, ,&,%20,º,%2f,'".split(",");
        var sem_acento = "a,a,a,a,a,e,e,e,e,i,i,i,i,o,o,o,o,o,u,u,u,u,c,_,e,_,_,/,_".split(",");
        valor = valor.toLowerCase();
        for (i = 0; i < com_acento.length; i++) {
            n2 = valor.split(com_acento[i]).length - 1;
            for (n = 0; n < n2; n++) {
                valor = valor.replace(com_acento[i], sem_acento[i]);
            }
        }
    }
    return valor;
}

// ---------- converte querystring para formato ancora ----------
function converte_url(valor) {
    if (valor == undefined) {
        valor = "";
    } else {
        n2 = valor.split("&").length - 1;
        for (n = 0; n < n2; n++) {
            valor = valor.replace("&", "/");
        }
    }
    return "/" + valor + "/";
}

// ---------- retorna qualquer variavel e ancora ----------
function retorna_variaveis(valor, url) {
    url = "/" + url;
    variaveis = url.split("/" + valor + "=");
    if (variaveis.length > 1) {
        return variaveis[1].split("/")[0];
    } else {
        return "";
    }
}

// ---------- funcao que carrega o conteudo do ajax em div dinamica ----------
function carrega_pagina_div_extra(div, v_URL) {
    $.ajax({
        url: v_URL,
        success: function () {
            $("#" + div).load(v_URL);
            //alert("div extra: " + div);
        }
    });
}

// ---------- carrega o jaquery para a galeria ----------
function carrega_galeria_jquery(div) {
    //$('#' + div + ' a').prettyPhoto();
    $('[id$=' + div + '] a').prettyPhoto();
}

// ---------- envia variaveis para o flash ----------
function envia_variavel_flash() {
    var flashs = destino_flash.split(",")
    for (var i = 0; i < flashs.length - 1; i++) {
        if (flashs[i] != "") {
            //alert("carregou FLASH: " + flashs[i]);
            document.getElementById("pagina_conteudo__" + flashs[i]).recebe();
        }
    }
    destino_flash = "";
}

// ---------- envia redimensiona frame de conteudo na pagina ----------
function redimensiona(valor, admin) {
    if (admin == "admin") {
        valor.height = document.getElementById(valor.id).contentWindow.document.getElementById("conteudo").offsetHeight;
    } else {
        if (document.getElementById(valor.id).contentWindow.document.forms[0] != undefined) {
            if (document.getElementById(valor.id).contentWindow.document.forms[0].clientHeight > document.getElementById(valor.id).contentWindow.document.body.clientHeight) {
                valor.height = document.getElementById(valor.id).contentWindow.document.forms[0].clientHeight;
            } else {
                valor.height = document.getElementById(valor.id).contentWindow.document.body.clientHeight;
            }
        } else {
            valor.height = document.getElementById(valor.id).contentWindow.document.body.clientHeight;
        }
    }
}

// ---------- funcao que conta os elementos que possam atualizar ----------
function conta_elementos() {
    var paginas = $('[id*=pagina_conteudo]');
    var retorno = "/";
    for (i = 0; i < paginas.length; i++) {
        retorno = retorno + $(paginas).eq(i).attr('id').split("pagina_conteudo")[1].replace("__", "") + "/";
    }
    return retorno;
}

// ---------- funcao que linkam conteudos vindo de botoes ----------
function linka2(nova_url) {
    verifica_lingua(nova_url);
    if (sep == "?") {
        if (window.location.href.indexOf("_escaped_fragment_=") > -1) {
            linka(nova_url, "", "", "google");
        } else {
            linka(nova_url, "", "", "querystring");
        }
    } else {
        linka(nova_url, conta_elementos(), retorna_variaveis('secao', window.location.href.split(sep)[1]), "click");
    }
}

// ---------- funcao que linkam conteudos ----------
function linka(nova_url, elementos, secao, origem) {
    nova_url = "/" + converte_url(trata(nova_url)) + "/"
    //alert(origem);
    //alert(conta_elementos());
    $.ajax({
        type: "POST",
        url: "index.aspx/gerecia_variaveis",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{variaveis:'" + nova_url + "',div_secoes:'" + elementos + "',secao_base:'" + secao + "',lingua:'" + lingua + "',origem:'" + origem + "'}",
        success: function (json) {
            var retorno = json.d;
            //alert("|| lingua = " + lingua + "\n|| nova_url = " + nova_url + "\n|| origem = " + origem + "\n|| destino = " + destino + "\n|| destino_flash = " + destino_flash + "\n|| retorno destino = " + retorno['destino'] + "\n|| retorno secao = " + retorno['secao'] + "\n|| retorno variaveis = " + retorno['variaveis'] + "\n|| retorno destino_flash = " + retorno['destino_flash']);
            secao = retorno['secao'];
            switch (origem) {
                case 'click':
                    v_origem = "click_volta";
                    destino = retorno['destino'];
                    destino_flash = retorno['destino_flash'];
                    window.location.href = "index.aspx#!/" + retorno['variaveis'];
                    break;
                case 'load':
                    v_origem = "volta";
                    destino = retorno['destino'];
                    destino_flash = retorno['destino_flash'];
                    Carregar(retorno['secao']);
                    break;
                case 'volta':
                    destino = retorno['destino'];
                    destino_flash = retorno['destino_flash'];
                    Carregar(retorno['secao']);
                    break;
                case 'google':
                    window.location.href = "index.aspx?_escaped_fragment_=/" + retorno['variaveis'];
                    break;
                case 'querystring':
                    window.location.href = "index.aspx?" + retorno['variaveis'];
                    break;
            }
            funcoes_extras();
        },
        error: function (XMLHttpRequest, textStatus, error) {
            alert(XMLHttpRequest.responseText);
        }
    });
    return false;
};

// ---------- funcao que carrega todos os elementos ----------
function Carregar(secao) {
    if (destino_flash != "") {
        envia_variavel_flash();
    }
    if (destino != "") {
        var secao2;
        var destinos = destino.split(",")
        for (var i = 0; i < destinos.length - 1; i++) {
            if (destinos[i] == "base") {
                secao2 = secao;
            } else {
                secao2 = destinos[i];
            }
            $('div[id$=pagina_conteudo__' + destinos[i] + ']').load("le_conteudo.aspx?secao=" + secao2);
            //alert("carregou DIV: " + destinos[i] + " - " + secao2);
        }
        destino = "";
    }
}

// ---------- base de funcoes extras ----------
function funcoes_extras() {
    //alert(secao);
}

function verifica_lingua(v_url) {
    if (retorna_variaveis('lingua', v_url) != "") {
        lingua = retorna_variaveis('lingua', v_url);
    }
}


// ---------- funcao de controle de navegacao pela mudanca da url ----------
jQuery(document).ready(function ($) {
    $.ajaxSetup({ cache: false });
    $.history.init(function (url) {
        if (url == "") {
            if (url_google == "") {
                window.location.href = "index.aspx#!/secao=" + pagina_inicial;
            }
        } else {
            verifica_lingua(window.location.href.split(sep)[1]);
            if (v_origem == "click_volta") {
                v_origem = "volta";
                Carregar(retorna_variaveis('secao', window.location.href.split(sep)[1]));
            } else {
                linka(url, conta_elementos(), retorna_variaveis('secao', window.location.href.split(sep)[1]), v_origem);
            }
        }
    });
});

