/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function initializeAcordos(){
    mostrarCampos('');
    
    $('fieldset#fieldRegiao').show();
    $('fieldset#fieldEspecialidade').hide();
    $('ul#medicoUL').hide();
    $('ul#entidadeUL').hide();
    $('div#distritosDiv').hide();
    $('div#concelhosDiv').hide();
    $('input#pesquisaInput').hide();
    
    
    $('input:radio[name=pesquisarPor]').change(function(){
       mostrarCampos($(this).val()); 
    });
    
      
        
    $('td.mostrar').live('click',function(){
        verMedicos($(this));
    });
    
    // para as listas de  especialidade
    $('select#especialidade').change(function() {
        alteraEspecialidades(this);    
    });
    
    // para as listas de distritos
    $('select#distrito').change(function() {
        alteraDistritos(this,'select#especialidade');
    });
    // para as listas de concelhos
    $('select#concelho').live('change', function() {
        alteraConcelho();
    });
    
    // para as listas de regiao distritos
    $('select#regDistrito').change(function() {
        alteraRegDistritos(this);
    });
    
    // para as listas de regiao concelhos
    $('select#regConcelho').change(function() {
        alteraRegConcelhos(this,'select#regDistrito');
    });
    
     
    // para as listas de regiao especialidade
    $('select#regEspecialidade').live('change', function() {
        alteraRegEspecialidades();
    });
    
    
    $('ul#medicoUL li').live('click',function(){
        medicoSelecionado(this);
    });
    
    $('ul#entidadeUL li').live('click',function(){
        entidadeSelecionada(this);
    });
    
    $('input#pesquisaInput').click(function(){
        pesquisar();
    });
    
    
    
    $('input#medicoTxt').live('keyup', function() {
        activarBotao(this);
    });
    
    $('input#entidadeTxt').live('keyup', function() {
        activarBotao(this);
    });
    
    $('.verDetalhes').live('click', function() {
        checkDetalhes(this);
    });
    
    $('.mapa').live('click', function() {
        mostraMapa(this);
    });
}
function alteraConcelho(){
    $('table#resultadospesquisa > tr').remove();
}
function alteraDistritos(elemento,elemento1){
    
    if($(elemento).val()==0){
        resetConcelho();
        $('input#pesquisaInput').hide();
    } else {
        getAcordosConcelhos($(elemento).val(),$(elemento1).val());
        $('div#concelhosDiv').show();
        $('input#pesquisaInput').show();
    }
    
    $('table#resultadospesquisa > tr').remove();
}
function alteraEspecialidades(elemento){
    
    if($(elemento).val()==0){
        resetDistrito();
        resetConcelho();
        
    } else {
        getAcordosDistritos($(elemento).val());
        $('div#distritosDiv').show();
        resetConcelho();
    }
    $('input#pesquisaInput').hide();
    $('table#resultadospesquisa > tr').remove();
}

function alteraRegDistritos(elemento){
    
  if($(elemento).val()==0){
        resetRegConcelho();
        resetRegEspecialidade();
        $('input#pesquisaInput').hide();
    } else {
        getRegAcordosConcelhos($(elemento).val())
        $('div#regConcelhosDiv').show();
        resetRegEspecialidade();
  }
    
    $('table#resultadospesquisa > tr').remove();
}

function alteraRegConcelhos(elemento){
    
   if($(elemento).val()==0){
        resetRegEspecialidade();
        $('input#pesquisaInput').hide();
    } else {
        getAcordosEspecialidades($(elemento).val())
        $('div#regEspecialidadesDiv').show();
        $('input#pesquisaInput').show();
    }
    
    $('table#resultadospesquisa > tr').remove();
}

function alteraRegEspecialidades(){
    $('table#resultadospesquisa > tr').remove();
}

function mostrarCampos(value){
    
    $('table#resultadospesquisa > tr').remove();
    
    resetRegiao();
    resetEspecialidade();
    
    $('input#pesquisaInput').hide();
    
    $('fieldset#fieldEntidade').hide();
    
    $('fieldset#fieldMedico').hide();
    $('input#medicoTxt').val('');
    $('ul#medicoUL li').hide();
    
    $('fieldset#fieldEntidade').hide();
    $('input#entidadeTxt').val('');
    $('ul#entidadeUL li').hide();
    
    $('input#pesquisaInput').removeAttr('disabled');
    
        
    
    if(value == 'medico'){
        if($('ul#medicoUL').html().length < 15){
            getAcordosMedicos();
        }
        $('fieldset#fieldMedico').show();
        $('input#pesquisaInput').show();
        $('input#pesquisaInput').attr('disabled','disabled');
    }else{
        
        if(value == 'entidade'){
            if($('ul#entidadeUL').html().length < 15){
                getAcordosEntidades();
            }
            $('fieldset#fieldEntidade').show();
            $('input#pesquisaInput').show();
            $('input#pesquisaInput').attr('disabled','disabled');
          
            
        }else{
            if(value == 'regiao'){
                getAcordosDistritos(0);
                $('fieldset#fieldRegiao').show();
                $('div#regDistritosDiv').show();
            }else{
                getAcordosListaEspecialidades();
                $('fieldset#fieldEspecialidade').show();
                $('div#especialidadesDiv').show();
            }
        }
    }
    
}
function resetFormulario(){
    resetRegiao();
    resetEspecialidade();
    resetMedico();
    resetEntidade();
    $('table#resultadospesquisa > tr').remove();
}

function resetEspecialidade(){
    
    $('table#resultadospesquisa > tr').remove();
    $('fieldset#fieldEspecialidade').hide();
    resetEspecialidadeDiv();
}

function resetEspecialidadeDiv(){
    $('select#especialidade > option').remove();
    $('div#especialidadesDiv').hide();
    resetDistrito();
    $('table#resultadospesquisa > tr').remove();
}
function resetDistrito(){
    $('select#distrito > option').remove();
    $('div#distritosDiv').hide();
    resetConcelho();
    $('table#resultadospesquisa > tr').remove();
}

function resetConcelho(){
    $('select#concelho > option').remove();
    $('div#concelhosDiv').hide();
    $('table#resultadospesquisa > tr').remove();
}

function resetRegiao(){
    
    $('table#resultadospesquisa > tr').remove();
    $('fieldset#fieldRegiao').hide();
    resetRegDistrito();
    
    
}
function resetRegDistrito(){
    $('select#regDistrito > option').remove();
    $('div#regDistritosDiv').hide();
    resetRegConcelho();
    $('table#resultadospesquisa > tr').remove();
}
function resetRegConcelho(){
    $('select#regConcelho > option').remove();
    $('div#regConcelhosDiv').hide();
    resetRegEspecialidade();
    $('table#resultadospesquisa > tr').remove();
}

function resetRegEspecialidade(){
    $('select#regEspecialidade > option').remove();
    $('div#regEspecialidadesDiv').hide();
    $('table#resultadospesquisa > tr').remove();
 
}
function activarBotao(elemento){
    
    var pesquisa = $(elemento).val();
    if(trim(pesquisa).length > 2){
        $('input#pesquisaInput').removeAttr('disabled');
    } else {
        $('input#pesquisaInput').attr('disabled','disabled');
    }
        
}
 
function verMedicos(elemento){
    jQuery('head').append('<script type="text/javascript" src="js/jquery.simplemodal.1.4.1.min.js"></script>')
    var listaMedicos = $(elemento).parent().find('td.esconder').html();
    $.modal('<div>' + listaMedicos + '</div>');
}
function pesquisar(){
    
    $('table#resultadospesquisa > tr').remove();
    
    var value = $('input:radio[name=pesquisarPor]:checked').val();
 
    if(value == 'medico'){
        mostarMedicos();
    }else{
        if(value == 'entidade'){
            mostarEntidades();      
        }else{
            if(value == 'regiao'){
                getAcordos($('select#regDistrito').val(), $('select#regConcelho').val(), $('select#regEspecialidade').val(), 0, 0); 
            }else{
                getAcordos($('select#distrito').val(), $('select#concelho').val(), $('select#especialidade').val(), 0, 0);
            }
        }
    }
    
}
function mostarMedicos(){
     
     var pesquisa = $('input#medicoTxt').val().toUpperCase();
     $('ul#medicoUL li').hide();
     $('ul#medicoUL li span.upperCase:contains(' + pesquisa + ')').parent().show();
     $('ul#medicoUL').show();
     
}
function mostarEntidades(){
     
     var pesquisa = $('input#entidadeTxt').val().toUpperCase();
     $('ul#entidadeUL li').hide();
     $('ul#entidadeUL li span.upperCase:contains(' + pesquisa + ')').parent().show();
     $('ul#entidadeUL').show();
     
}
function medicoSelecionado(elemento){
    
    getAcordos(0, 0, 0, $(elemento).find('span.value').html(), 0);    
    $('ul#medicoUL li').hide();
    
}
function entidadeSelecionada(elemento){
    getAcordos(0, 0, 0, 0, $(elemento).find('span.value').html());    
    $('ul#entidadeUL li').hide();
    
}
