﻿var Modo = {
    Ida:1,
    Volta:2
};

Aereo.MasterPricer = {
    pageIndex: 1,
    pageSize: 150,
    qtdePag: 0,
    Modo: Modo.Ida,
    ultimoOrdenacao: ['tarifaADT','dataSaidaIda'],
    codOpcaoVooEmail: null,

    getMasterPricer: function() {
        //debugger;
        MasterPricer.GetMasterPricer($('dadosPesquisa_geral').value, Aereo.MasterPricer.callbacks.getMasterPricer);
    },
    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.mostraMasterPricer();
    },
    mostraMasterPricer: function(naoVoltarPagina) {      
        if (!naoVoltarPagina) Aereo.MasterPricer.pageIndex = 1;

        var sbResultados = new StringBuilder();
        var t = new Template("Aereo");
        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.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.geral.idResultadoVoo).innerHTML = result[0] + result[3];
        
        if($('_idTotalParcial') && $('_idTotal'))
        {
            $('_idTotalParcial').innerHTML = result[2];
            $('_idTotal').innerHTML = result[1];
        }
    },
    ordenaVoo: function(coluna,coluna2,radioElement) {
    //debugger;
        Aereo.MasterPricer.ultimoOrdenacao[0] = coluna;
        Aereo.MasterPricer.ultimoOrdenacao[1] = coluna2;
         ExibeAguardeComThread(function() {
                ordenaResultadoMasterPricer(coluna,coluna2);
                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') $('divDetalhes_'+complemento+codOpcaoVoo).innerHTML = montaDetalhes(codOpcaoVoo, Aereo.MasterPricer.Modo);
        
        $('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');
    },
    
    //Seleciona uma opcao de um Voo
    selecionaOpcaoVoo: function(codOpcaoDeVoo)
    {
        
        $(tpl.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.geral.idResultadoVoo).style.display = 'none';
            $(tpl.geral.idFiltro).style.display = 'none';
            $(tpl.geral.idMelhorPreco).style.display = 'none';
            $(tpl.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.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.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;
            
            $("divTrace").innerHTML += "<a href='" + MasterPricer.GetURLTraceVTP().value + "'>Trace</a>";
 
        },
        enviaEmailVoo:function(res) 
        {
        }
	}
}