﻿var jRenderDepoimentosEuFui;
var jEuFui;

function inicializa(){
    jsCommon = new jCommon();        
    jsEstados = new jEstados();  
    jsCidades = new jCidades();
    
    jRenderDepoimentosEuFui = new jRenderDepoimentosEuFui();
    //jRenderDepoimentosEuFui.exec.main();
    
    jEuFui = new jEuFui();
    jEuFui.exec.main();
}                

function jRenderDepoimentosEuFui(){
    var jDt;
    var tpl;    
    var _codCidade;
    
    var render = {
        main: function(){
            tpl.tbDepoimentosEuFui.optionEstado = jsCommon.exec.getOptionSelecione();
            tpl.tbDepoimentosEuFui.optionCidade = jsCommon.exec.getOptionSelecione();
            tpl.tbDepoimentosEuFui.depoimentos = '';
            if(jDt != null && jDt.getTotRegs() > 0){
                tpl.tbDepoimentosEuFui.depoimentos = render.depoimentosEuFui();
                if(jDt.getTotRegs() > 1){
                    tpl.tbDepoimentosEuFui.depoimentos += render.resumoDepoimentoEuFui();
                }
            }
            
            return tpl.tbDepoimentosEuFui.getHtml();
        },
        depoimentosEuFui: function(index){
            tpl.depoimento.dataEstiveLa = jDt.getDrValue(index, "QuandoEstiveLa");
            tpl.depoimento.nome = jDt.getDrValue(index, "NomeUsuario");
            tpl.depoimento.cidade = jDt.getDrValue(index, "CidadeNome");
            tpl.depoimento.uf = jDt.getDrValue(index, "Uf");
            tpl.depoimento.depoimento = jDt.getDrValue(index, "Depoimento");
            return tpl.depoimento.getHtml();
        },
        resumoDepoimentoEuFui: function(){
            var sb = new StringBuilder();
            var tot = jDt.getTotRegs();
            var str;
            for (var i = 0; i < tot; i++) {
                str = jDt.getDrValue(i, "Depoimento").substring(0, 53) + '...';
                tpl.resumoDepoimento.dataEstiveLa = jDt.getDrValue(i, "QuandoEstiveLa");
                tpl.resumoDepoimento.nome = jDt.getDrValue(i, "NomeUsuario");
                tpl.resumoDepoimento.cidade = jDt.getDrValue(i, "CidadeNome");
                tpl.resumoDepoimento.uf = jDt.getDrValue(i, "Uf");
                tpl.resumoDepoimento.resumoDepoimento = str;
                sb.append(tpl.resumoDepoimento.getHtml());
            }
            
            return tpl.resumoDepoimento.getHtml();
        },
        estados: function(){
            jDt = new jDatatable(Estados.listarPorPais('21').value);
            var tot = jDt.getTotRegs();
            for (var i = 0; i < tot; i++) {
	            option = new Option();
		        option.value = jDt.getDrValue(i, "CodEstado");
		        option.text = jDt.getDrValue(i, "Nome");
	            try{
                    $('txt_estado').add(option,null); 
                }catch(ex){
                    $('txt_estado').add(option); 
                }
            }
        }
    };
    
    var on = {
        buscarDepoimentos: function(text){
            alert(text);
        },
        changeEstado: function(){
            $('txt_estado').onchange = function(){
                $html('divCidade', '<select class="form" id="txt_cidade" />' + jsCommon.exec.getOptionSelecione() + '</select>');
                if ($get('txt_estado') != ""){   
                    startEnv.addOptionsCidades(parseInt($get('txt_estado')));
                    on.changeCidade();
                };
            };
        },
        changeCidade: function(){
            $('txt_cidade').onchange = function(){
                _codCidade = $get('txt_cidade');
            }
        }
    };
    
    var startEnv = {
        main: function(){
            jDt = new jDatatable(EuFui.listarDepoimentosEuFui(_codCidade, 2).value);
            $('divMain').innerHTML = render.main();
            render.estados();
            on.changeEstado();
        },
        addOptionsCidades: function(pCodEstado){
            jDt = new jDatatable(Cidades.listarPorEstado(pCodEstado).value);
            var tot = jDt.getTotRegs();
            for (var i = 0; i < tot; i++) {
	            option = new Option();
		        option.value = jDt.getDrValue(i, "CodCidade");
		        option.text = jDt.getDrValue(i, "Nome");
		        try{
                    $('txt_cidade').add(option,null); // standards compliant%
                }catch(ex){
                    $('txt_cidade').add(option); // IE only
                };
            };
        }
    };
    
    this.exec = {
        main: function(){
            return startEnv.main();
        },
        buscar: function(){
            var s = '';
            if(_codCidade != null){
                on.buscarDepoimentos(_codCidade);
            }else{
                alert('É necessário selecionar uma cidade para a busca');
            }
        }
    };
    
     //construtor
    this.constructor = new function(){
        tpl = new Template('euFui');                
    };
}
//var Portlet;
//var jUsuario;
//var jDtUsuario;

//function inicializa(){
//    jUsuario = new jUsuarios();
//    jUsuarioValida = new jUsuarioValida();
//    jsCommon = new jCommon();        
//    jsEstados = new jEstados();  
//    jsCidades = new jCidades();
//    jsPais = new jPais();
//    jsBairros = new jBairros();
//    jsEnderecos = new jEnderecos();
//    
//    Portlet = new jPortlet("");
//    
//    // Apenas para desenvolvimento do relatório de empresas
//    jEmpresa = new jEmpresa();
//    jEuFui = new jEuFui();
//};

//function jPortlet(DirTemplate)
//{
//    var This = this;
//    
//    //variaveis privativas 
//    var jDS;
//    var scope;
//  
//    /* David */
//    var _codEmpresa = 1;
//    var _codUsuario = 1;
//    /* David */
//    
//    //HTML render(pNomeTemplate)
//    var render = {
//        main: function(){
//            return '';
//        }
//    };
//    
//    //DOM node ctrlRefs
//    var ctrlRefs = {
//        siteConteudo: function(){return $('siteCentro');}
//    };
//    
//    //tratativas de eventos da UI
//    var on = {
//    };
//    
//    //metodos publicos
//    this.goTo = {
//        main: function(){
//        }        
//    };       
//    
//    this.exec = {
//    };
//    
//        //void startEbv()
//    var startEnv = {
//        main: function(){
//        }
//    };
//    
//    this.getCodEmpresa = function(){
//        return _codEmpresa;
//    };
//    
//    this.getCodUsuario = function(){
//        return _codUsuario;
//    };
//    
//    this.setCodUsuario = function(value){
//        _codUsuario = value;
//    };
//    
//    //construtor
//    this.constructor = new function()
//    {
//        //startEnv.main();
//    };
//};
