  /**
 * @author Guito
 */

// FRONTEND static object  
var frontend = {  

  extensibleMenu: function(){
    $("a.extendable_menu").click(function(){
      console.log("toogle "+$(this).attr("id"));
      $("div#"+$(this).attr("id")+"_extended").toggle("slow");
    });
  },      
  
  roundedCornerConfig: function(){
    $("div.corner").corner("15px cc:#ebdece");
    $("div.inner_content").corner("15px cc:#ebdece");
    $("div.footer").corner("20px tr tl cc:#ebdece");
  },
  
  peopleStateEventConfig: function(){
    // Configurando onchange para select de estados carregar cidades 
    $("select#people_state").change(function() {
      $("select#people_city option").remove();
      var option = document.createElement("option");
      option.setAttribute("value",'');
      option.innerHTML = "-- Selecione uma cidade --";
      $("select#people_city").append(option);
      $.getJSON(
        "/frontend_dev.php/getCities?state_id="+$("select#people_state option:selected").attr("value"),
        function(data){
          $.each(data.array_cities, function(id,desc){
            var option = document.createElement("option");
            option.setAttribute("value",id);
            option.innerHTML = desc;
            $("select#people_city").append(option);
          });
        }
      );
    }).ajaxStart(function(){
    	var loading = document.createElement("img");
    	loading.setAttribute("src","/images/loading.gif");
    	loading.setAttribute("id","loadingImage");
    	$("div.content:has(select#people_city)").append(loading);  
      $("select#people_city").attr('disabled','disabled');
    }).ajaxComplete(function(event,request, settings){
    	$("div.content img#loadingImage").remove();
      $("select#people_city").removeAttr('disabled');
    });
  },   
  
  peopleStateInit: function(){
    // Verificar se estado jah foi selecionado para carregar cidades
    if($("select#people_state option:selected").attr("value") != "" && $("select#people_city option:selected").attr("value") == "") {
      $.getJSON(
        "/frontend_dev.php/getCities?state_id="+$("select#people_state option:selected").attr("value"),
        function(data){
          $.each(data.array_cities, function(id,desc){
            var option = document.createElement("option");
            option.setAttribute("value",id);
            option.innerHTML = desc;
            $("select#people_city").append(option);
          });
        }
      );
      $("select#people_city").ajaxStart(function(){
      	var loading = document.createElement("img");
      	loading.setAttribute("src","/images/loading.gif");
      	loading.setAttribute("id","loadingImage");
      	$("div.content:has(select#people_city)").append(loading);  
        $("select#people_city").attr('disabled','disabled');
      }).ajaxComplete(function(event,request, settings){
      	$("div.content img#loadingImage").remove();
        $("select#people_city").removeAttr('disabled');
      });   
    }
  },   
  
  peopleIsStudentEventConfig: function(){
    // Configurando onchange da descricao do inscrito
    $("select#people_is_student").change(function(){
      var desc = $("select#people_is_student option:selected").attr("value");
      if (desc == "UNI" || desc == "FORM") {
        $("div#row_organization").show('normal');
        $("div#row_area").show('normal');
      }
      else {
        $("div#row_organization").hide('normal');    
        $("div#row_area").hide('normal');   
        $("select#people_organization option[value='']").attr('selected','true'); 
        $("select#people_area option[value='']").attr('selected','true');
      }
    });
    // Configurando onchange para outra faculdade
    $("select#people_organization").change(function(){
      var desc = $("select#people_organization option:selected").attr("value");
      if (desc == "OTHERS") {
        $("select#people_organization").attr('disabled','disabled');
        $('div.people_others_organization').show('normal');      
      }
    });
    $("span#people_organization_others_close").click(function(){
      $("select#people_organization").removeAttr('disabled');
      $("select#people_organization option[value='']").attr('selected','true');
      $('div.people_others_organization').hide('normal');    
      $('input#people_organization_others').val('');  
    });        
  },  
  
  peopleIsStudentInit: function(){
    // Verificar se o IsStudents foi selecionado
    var desc = $("select#people_is_student option:selected").attr("value");
    if (desc == "UNI" || desc == "FORM") {
      $("div#row_organization").show('normal');
      $("div#row_area").show('normal');
    }
    var org = $("input#people_organization_others").val();
    if (org != "") {
      $('div.people_others_organization').show('normal');
      $("select#people_organization option[value='OTHERS']").attr('selected','true');
      $("select#people_organization").attr('disabled','disabled');
    }
  }
}

// after load DOM
$(document).ready(function(){
  frontend.extensibleMenu();
  frontend.roundedCornerConfig();
  frontend.peopleStateInit();
  frontend.peopleIsStudentEventConfig();
  frontend.peopleStateEventConfig();
  frontend.peopleIsStudentInit();
});

