$(document).ready(function(){
    infos = {
        baseURL: '/popup-consultas/',
        textoCarregando: 'Carregando...',
        charSplit: ','
    }

    
    function pega_profissionais(index) {
        
        objects = {
            conteudo: $(".espec, .resultados"),
            especialidade: $(".espec"),
            listaProfissionais: $('.resultados')
        }
        
        $('.carregando').text(infos.textoCarregando).show();
        
        index = index.split(infos.charSplit);
        
        objects.especialidade.text(index[1]);
        
        $(".resultados li").remove();

        $.getJSON(infos.baseURL+index[0],
            function(data){ 
              $.each(data, function(i,item){ 
                objects.listaProfissionais
                .append('<li>' + item.fields.nome + '</li>');
            });
            if ($(".resultados li").length>0) {
                objects.conteudo.show();
            } else {
                objects.conteudo.hide();
            }
            $('.carregando').text(infos.textoCarregando).hide();
        });
        
    }
    
    function muda_form (index) {
        if (index.length>0) {
            $('#sec-content input, select[name=como_soube], textarea').show().prev().show();
            if (index=='9') {
                $('.orc').show().prev().show();
                $('.pro').hide().prev().hide();
            } else {
                $('.orc').hide().prev().hide();
                $('.pro').show().prev().show();
            }
        } else {
            $('#sec-content input, select[name=como_soube], textarea').hide().prev().hide();
        }
    }
    
    $('#combo-especialidade').live('change', function(){
        pega_profissionais($(this).val());
    });
    
    $('#combo-servico').bind('change', function(){
        muda_form($(this).val());
    });
    
    muda_form($('#combo-servico').val());
});
