//constants
var id_slovakia = 1;
var server_url  = "/";
var ajax_url    = server_url + "public/ajax/get_xml.php";

function set_location_visibility() {
  if ($("#location_country :selected").val() == id_slovakia) {
    $("#custom_town").css('display', 'none');
    $("#location_selects").css('display', 'block');
  } else {
    $("#custom_town").css('display', 'block');
    $("#location_selects").css('display', 'none');
  }
}

function create_select(xml_url, parent_select_id, dest_select_id, add_all_statement, selected_value) {
  if (add_all_statement == null)
    add_all_statement = true;
    
  if (selected_value == null)
    selected_value = 0;
  
  var parent_id = $("#" + parent_select_id + " :selected").val();
  
  $("#"+dest_select_id).empty();
  
  $.get(xml_url, {}, function(xml) {
    $("item", xml).each(function(i) {
      if (selected_value==$(this).attr('id')) {
        $("#"+dest_select_id).append($(document.createElement("option")).
                            attr("value",$(this).attr('id')).
                            attr("selected", true).
                            text($(this).text()));
      } else {
        $("#"+dest_select_id).append($(document.createElement("option")).
                            attr("value",$(this).attr('id')).
                            text($(this).text()));
      }
    });
  });
  
  //every select we are using has one special value
  if (add_all_statement)
    $("#"+dest_select_id).append($(document.createElement("option")).attr("value","0").text("Všetko"));
}

$(document).ready(function() {

  //location visibility
  set_location_visibility();

  $("#location_country").change(function() {
    set_location_visibility();
    
    var id_location_country = $("#location_country :selected").val();
    create_select(ajax_url + "?data=location_regions&id_location_country=" + id_location_country, "location_country", "location_region");
  });
  
  $("#location_region").change(function() {
  
    var id_location_region = $("#location_region :selected").val();
    create_select(ajax_url + "?data=location_districts&id_location_region=" + id_location_region, "location_region", "location_district");
  });
  
  $("#location_district").change(function() {
  
    var id_location_district = $("#location_district :selected").val();
    create_select(ajax_url + "?data=location_towns&id_location_district=" + id_location_district, "location_district", "location_town");
  });
  
  $("#real_type").change(function() {
    
    var id_real_type = $("#real_type :selected").val();
    create_select(ajax_url + "?data=real_subcategories&id_real_type=" + id_real_type, "real_type", "real_subcategory");
  });
  
  //adv
  $("#adv_location_region").change(function() {
  
    var id_location_region = $("#adv_location_region :selected").val();
    create_select(ajax_url + "?data=location_districts&id_location_region=" + id_location_region, "adv_location_region", "adv_location_district");
  });
  
  $("#adv_real_type").change(function() {
    
    var id_real_type = $("#adv_real_type :selected").val();
    create_select(ajax_url + "?data=real_subcategories&id_real_type=" + id_real_type, "adv_real_type", "adv_real_subcategory");
  });
  
  //adm
  $("#adm_location_region").change(function() {
  
    var id_location_region = $("#adm_location_region :selected").val();
    var xml_url = ajax_url + "?data=location_districts&id_location_region=" + id_location_region;
    create_select(xml_url, "adm_location_region", "adm_location_district", false);
    
    var id_location_district = 0;
    
    $.get(xml_url, {}, function(xml) {
      $("item", xml).each(function(i) {
        if (i == 0) {//little ugly hack
          id_location_district = $(this).attr('id');
          create_select(ajax_url + "?data=location_towns&id_location_district=" + id_location_district, "adm_location_district", "adm_location_town", false);
        }
      });
    });
    
  });
  
  $("#adm_location_district").change(function() {
  
    var id_location_district = $("#adm_location_district :selected").val();
    create_select(ajax_url + "?data=location_towns&id_location_district=" + id_location_district, "adm_location_district", "adm_location_town", false);
  });
  
  $("#adm_real_type").change(function() {
    
    var id_real_type = $("#adm_real_type :selected").val();
    create_select(ajax_url + "?data=real_subcategories&id_real_type=" + id_real_type, "adm_real_type", "adm_real_subcategory", false);
  });
  
});

