﻿var Modo = 
{
    Ida:1,
    Volta:2
};

Aereo.MasterPricer =
{
    pageIndex: 1,
    pageSize: 9999,
    qtdePag: 0,
    Modo: Modo.Ida,
    ultimoOrdenacao: ['tarifaADT', 'dataSaidaIda', 'false'],
    codOpcaoVooEmail: null,

    getMasterPricer: function() {
        var variaveis = location.search.split("?");
        var quebra = variaveis[1].split("=");
        var idRequest = quebra[0];

        var isLowFareSearch = false;
        if (location.pathname.indexOf('pesquisa-passagens-internacionais.aspx') < 0) {
            isLowFareSearch = true;
        }
        
        // variavel criada para acessar os métodos das classes de MasterPricer (internacional) ou LowFare (nacional) 
        // utilizei esse recurso para acessar o mesmo método de classes diferentes de acordo 
        var MasterPricerGen;

        if (isLowFareSearch) {
            MasterPricerGen = LowFare;
        }
        else {
            MasterPricerGen = MasterPricer;
        }

        if (idRequest == "idRequest") {
            var idRequestNumber = quebra[1].split("&")[0];
            SiteGeral.GetDataSetByRequestId(idRequestNumber, Aereo.MasterPricer.callbacks.getMasterPricer);
        }
        else {
            MasterPricerGen.GetMasterPricer($('dadosPesquisa_geral').value, Aereo.MasterPricer.callbacks.getMasterPricer);
        }
    },
    atualizaDados: function() {
        //debugger;
        Aereo.LayoutGeral.ParseOrder();
        Aereo.LayoutGeral.ParseTitulo();
        Aereo.LayoutGeral.ParseMotorBusca();
        Aereo.LayoutGeral.ParseMelhoresPrecos(true);
        Aereo.LayoutGeral.ParseFiltro(false);
        Aereo.LayoutGeral.ParseBottom();

    },
    mudarPagina: function(pagina) {
        Aereo.MasterPricer.pageIndex = pagina;
        Aereo.MasterPricer.mostraMasterPricer(true);
    },
    mostraMasterPricerCallback: function(dt) {
        Aereo.stopCarregando();
        if (dt) { masterPricerDS = dt; };

        //Aereo.LayoutGeral.ParseFiltro(false);
        ordenaResultadoMasterPricer(Aereo.MasterPricer.ultimoOrdenacao[0], Aereo.MasterPricer.ultimoOrdenacao[1], Aereo.MasterPricer.ultimoOrdenacao[2]);
        Aereo.MasterPricer.mostraMasterPricer(false);
    },
    mostraMasterPricer: function(Ordenacao) {
        var sbResultados = new StringBuilder();
        var t = new Template("Aereo");


        // renderiza a casca          
        if (!Ordenacao) {
            Aereo.MasterPricer.atualizaDados();
        }

        var resultado = montaVoosMasterPricer(masterPricerDS, '0', Aereo.MasterPricer.pageIndex, Aereo.MasterPricer.pageSize);

        if (resultado[0] != "") {
            sbResultados.append(resultado[0]);
            resultado[0] = sbResultados.toString();
            resultado[3] = Aereo.MasterPricer.montaPaginacao(resultado[2]);
            Aereo.MasterPricer.montaResultado(resultado);
            //$(tpl.MasterPricer.geral.idMelhorPreco).innerHTML = montaMelhorPrecoReduzidaMasterPricer(1);
            return true;
        }
        else {
            return false;
        }
    },
    mostraDetalhesSelecionado: function(codOpcaoVoo) {
        $('divDetalhesVoosSelecionados_' + codOpcaoVoo).style.display = ($('divDetalhesVoosSelecionados_' + codOpcaoVoo).style.display == 'block' ? 'none' : 'block');
        $('divVoosSelecionados_' + codOpcaoVoo).style.display = ($('divVoosSelecionados_' + codOpcaoVoo).style.display == 'block' ? 'none' : 'block');
    },
    montaPaginacao: function(resultsCount) {
        var qtdePag = 0;
        qtdePag = parseInt((resultsCount / Aereo.MasterPricer.pageSize));
        if ((resultsCount % Aereo.MasterPricer.pageSize) != 0) qtdePag += 1;
        Aereo.MasterPricer.qtdePag = qtdePag;
        return montaPaginacao(qtdePag);
    },
    montaResultado: function(result) {
        $(tpl.MasterPricer.geral.idResultadoVoo).innerHTML = result[0] + result[3];

        if ($('_idTotalParcial') && $('_idTotal')) {
            $('_idTotalParcial').innerHTML = result[2];
            $('_idTotal').innerHTML = result[1];
        }
    },
    ordenaVoo: function(coluna, coluna2, sort) {
        //debugger;
        Aereo.MasterPricer.ultimoOrdenacao[0] = coluna;
        Aereo.MasterPricer.ultimoOrdenacao[1] = coluna2;
        Aereo.MasterPricer.ultimoOrdenacao[2] = sort;
        ExibeAguardeComThread(function() {
            ordenaResultadoMasterPricer(coluna, coluna2, sort);
            Aereo.MasterPricer.mostraMasterPricer(true);
            return true;
        });
    },
    mostraDetalhes: function(codOpcaoVoo, pinado) {
        var complemento = (pinado ? 'Pinada' : '');
        var pin = (pinado ? ' pin' : '');
        //if ($('divDetalhes_' + complemento + codOpcaoVoo).style.display == 'none') 
        //{
            var htmlDetalhes = montaDetalhes(codOpcaoVoo, Aereo.MasterPricer.Modo);
            $('divDetalhes_' + complemento + codOpcaoVoo).innerHTML = htmlDetalhes;
        //}

        $('divDetalhes_' + complemento + codOpcaoVoo).style.display = ($('divDetalhes_' + complemento + codOpcaoVoo).style.display == 'block' ? 'none' : 'block');
        //$('opcoesVoos_tr_'+complemento+codOpcaoVoo).style.display = ($('opcoesVoos_tr_'+complemento+codOpcaoVoo).style.display == 'block' ? 'none' : 'block');
    },
    selecionaOpcaoVoo: function(codOpcaoDeVoo) {
        marcaSelecaoOpcaoVoo(Aereo.MasterPricer.Modo, codOpcaoDeVoo);

        //$(tpl.MasterPricer.geral.idVoosSelecionados).innerHTML = marcaSelecaoOpcaoVoo(Aereo.MasterPricer.Modo,codOpcaoDeVoo);

        //Aereo.MasterPricer.exibeSelecionados();
    },

    //==================================================================================================//
    //retira selecao uma opcao de um Voo
    retiraSelecaoOpcaoVoo: function(codTrecho) {
        $(tpl.geral.idVoosSelecionados).innerHTML = desmarcaSelecaoOpcaoVoo(codTrecho);


        //Aereo.MasterPricer.trocaNumeroTrecho();
    },
    //==================================================================================================//
    exibeSelecionados: function() {
        $(tpl.MasterPricer.geral.idResultadoVoo).style.display = 'none';
        $(tpl.MasterPricer.geral.idFiltro).style.display = 'none';
        $(tpl.MasterPricer.geral.idMelhorPreco).style.display = 'none';
        $(tpl.MasterPricer.geral.idVoosSelecionados).style.display = 'block';
    },
    //=================================================================================================//    
    pinarOpcaoVoo: function(codOpcaoDeVoo, incluir_remover) {
        ExibeAguardeComThread(function() {
            if (incluir_remover == 0)
                setaOpcaoPinada(Aereo.MasterPricer.Modo, codOpcaoDeVoo);
            else
                retiraOpcaoPinada(Aereo.MasterPricer.Modo, codOpcaoDeVoo);

            Aereo.MasterPricer.mostraMasterPricer(true);

            return true;
        });
    },
    trocaNumeroTrecho: function() {

        if ($(tpl.geral.idResultadoVoo).style.display == 'none') {
            $(tpl.geral.idResultadoVoo).style.display = 'block';
            $(tpl.geral.idFiltro).style.display = 'block';
            $(tpl.geral.idMelhorPreco).style.display = 'block';
            $(tpl.geral.idVoosSelecionados).style.display = 'none';
        }

        //Aereo.startCarregando();


        ExibeAguardeComThread(function() {
            ordenaResultadoMasterPricer(Aereo.MasterPricer.ultimoOrdenacao[0], Aereo.MasterPricer.ultimoOrdenacao[1], Aereo.MasterPricer.ultimoOrdenacao[2]);
            Aereo.MasterPricer.mostraMasterPricer(true);
            //Aereo.stopCarregando();
            return true;
        }
         );
    },
    descartarVoo: function(codOpcaoVoo) {
        marcaVoo(Aereo.MasterPricer.Modo, codOpcaoVoo);
        //ordenaResultadoMasterPricer(Aereo.MasterPricer.ultimoOrdenacao[0], Aereo.MasterPricer.ultimoOrdenacao[1]);
        Aereo.MasterPricer.mostraMasterPricer(true);
    },
    callbacks: {
        getMasterPricer: function(res) {
            //debugger;

            if (res.error == null) {
                Aereo.MasterPricer.mostraMasterPricerCallback(res.value);
            }
            else {
                if (res.value != null) {
                    if (res.value.tables.Erros.rows.length > 0 != null) {
                        alert('Não foram encontrados voos com os critérios informados. Por favor refaça a busca.' + res.value.tables.Erros.rows[0].Message);
                        window.location = 'javascript:history.go(-1)';
                    }
                }
                else {
                    alert('Não foram encontrados voos com os critérios informados. Por favor refaça a busca.' + res.error.message);
                    window.location = 'javascript:history.go(-1)';
                }
            }

            // insere o trace da busca
            //debugger;

            if (res.value != null) {
                $("divTrace").innerHTML += res.value.tables.Tempos.rows[0].sessionToken;
            }
            else {
                $("divTrace").innerHTML += res.error.message;
            }

        },
        enviaEmailVoo: function(res) {
        }
    }
}
