function carregaEstado(estados) {

    DWRUtil.removeAllOptions("estado");
    DWRUtil.addOptions("estado",estados,"id","sigla");

    if (estados.length>0) {
        for ( i = 0; i < estados.length; i++ ) {
            if ( estados[i].selected ) {
                DWRUtil.setValue("estado",estados[i].id);
            }
        }
    }
    
    LocalDWR.getCidades($('#estado').val(),carregaCidade);
}
            
function carregaCidade(cidades) {
    DWRUtil.removeAllOptions("cidade");
    DWRUtil.addOptions("cidade",cidades,"id","nome");

    if (cidades.length>0) {
        for ( i = 0; i < cidades.length; i++ ) {
            if ( cidades[i].selected ) {
                DWRUtil.setValue("cidade",cidades[i].id);
            }
        }
    }

    LocalDWR.getLocalidades($('#cidade').val(),carregaLocalidade);
}
            
function carregaLocalidade(localidades) {
    DWRUtil.removeAllOptions("localidade");
    DWRUtil.addOptions("localidade",localidades,"id","nome");

    if (localidades.length>0) {
        for ( i = 0; i < localidades.length; i++ ) {
            if ( localidades[i].selected ) {
                DWRUtil.setValue("localidade",localidades[i].id);
            }
        }
    }
}
    
function QueryProdutosPrincipal() {

    $('#autenticacao').html("");

    value = $.trim($('#campoBusca').val()) 
    
    if ( value == "" ) {
        $('#contentNode').html("");
    } else {
        
        $('#campoBusca').addClass("ac_loading");
      
        BuscaDWR.getProdutos($('#campoBusca').val(),

        function(produtos) {
            var html ="<div class='conteudo'><div class='links'>";

            if (produtos.retorno.length>0)
            {
                html += "<h2><span>Produtos Encontrados: </span></h2>";
                html += "<p>Os seguintes produtos foram localizados:</p>";
                html += "";
            
                for ( i = 0; i < produtos.retorno.length; i++ ) {
                    html += "<a class='link' href='precos.jsp?idProduto="+produtos.retorno[i].id+"'>"+produtos.retorno[i].nome+"</a> <br>";
                }

                html += "</div></div>";

                if (produtos.refinarBusca) {
                    html += "<p>Você deve refinar sua busca pois muitos produtos foram encontrados</p>";
                }
            }
            else {
                html += "<h2><span>Produtos Encontrados: </span></h2><p>Não foram encontrados produtos com este nome</p>";
            }
            $('#contentNode').html(html);
          
            $('#campoBusca').removeClass("ac_loading");
        });
    }
}

(function($){
    $.fn.delay = function(options) {

        var timer;
        function count(scope){
            if (timer != null) {
                clearTimeout(timer);
            }
            var newFn = function() {
                options.fn.apply(scope);
            };
            timer = setTimeout(newFn, options.delay);
        }
       
        return this.each(function() {
            var obj = $(this);
            obj.bind(options.event, function () {
                count(this);  
            });
        });
    };
})(jQuery);

//$(document).ready(function() {
//    $('#campoBusca').delay({
//        delay: 500,
//        event: 'keyup',
//        fn: function() {
//            QueryProdutosPrincipal();
//        }
//    });
//})

function submitLog(){
    $('#formLogin').submit();
}

function submitBusca(){
    if (($('#campoBusca').val() != "informe o nome do produto") )
        $('#formBusca').submit();
    else
        alert("Você deve preencher o nome do produto para realizar a busca");
}