$(document).ready(function(){ 


$("#id_adv-custom_city3").parent().hide();
$("input#id_adv-offer_type_1").attr("checked", true);
$("#id_adv-price_type").parent().hide();


  /* Quick search */
  $("select#id_adv-country").change(function(){
    if ( $(this).val() ) {
        var ac = $("#id_adv-custom_city3")[0].autocompleter;
        ac.setExtraParams( { country: $("select#id_adv-country").val() } );
        ac.flushCache();
        $("#id_adv-custom_city3").val('');
        $('#id_adv-custom_city3').parent().hide();

        $("select#id_adv-region").html('<option value="" selected="selected">...loading</option>');
        $("select#id_adv-city").html('<option value="" selected="selected">...loading</option>');
        $.getJSON("/" + lang + "/catalog/qsearch_both/",{id: $(this).val(), ajax: 'true'}, function(j){
          var options = '<option value="" selected="selected">---------</option>';
          for (var i = 0; i < j[0].length; i++) {
            options += '<option value="' + j[0][i].optionValue + '">' + j[0][i].optionDisplay + '</option>';
          }
          $("select#id_adv-region").html(options);
          
          var options = '<option value="" selected="selected">---------</option>';
          for (var i = 0; i < j[1].length; i++) {
            options += '<option value="' + j[1][i].optionValue + '">' + j[1][i].optionDisplay + '</option>';
          }
          options += '<option value="-1">'+other+'</option>';
          $("select#id_adv-city").html(options);          
          
        });
    
    }
  })
  

  $("select#id_adv-region").change(function(){
    if ( $(this).val() ) {
        var ac = $('#id_adv-custom_city3')[0].autocompleter;
        ac.setExtraParams( { 
            country: $("select#id_adv-country").val(),
            region: $("select#id_adv-region").val()
         } );
        ac.flushCache();
    
        $("select#id_adv-city").html('<option value="" selected="selected">...loading</option>');
        $.getJSON("/" + lang + "/catalog/qsearch/",{id: $(this).val(), ajax: 'true'}, function(j){
          var options = '<option value="" selected="selected">---------</option>';
          for (var i = 0; i < j.length; i++) {
            options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
          }
          options += '<option value="-1">'+other+'</option>';
          $("select#id_adv-city").html(options);
        })
    } else {
        var ac = $('#id_adv-custom_city3')[0].autocompleter;
        ac.setExtraParams( { 
            country: $("select#id_adv-country").val()
         } );
        ac.flushCache();    
    }
  })
  
  $("select#id_adv-city").change(function(){
    if ( $(this).val() ) {      
        if($(this).val() ==-1){
            $('#id_adv-custom_city3').val('');
            $('#id_adv-custom_city3').parent().show();
            $('#id_adv-custom_city3').focus();
        } else {
            $('#id_adv-custom_city3').parent().hide();
        }
        $.getJSON("/" + lang + "/catalog/get_region/",{id: $(this).val(), ajax: 'true'}, function(j){
            $('select#id_adv-region').val(j.region);
        });
    }
  });  
  /* end of Quick search */


function selectItem3(li) {
    $('select#id_adv-region').val( li.extra[2] );
    $("select#id_adv-city").html('<option value="" selected="selected">...loading</option>');
    $.getJSON("/" + lang + "/catalog/qsearch/",{id: li.extra[2], ajax: 'true', required_city: li.extra[1]}, function(j){
      var options = '<option value="" selected="selected">---------</option>';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      }
      options += '<option value="-1">'+other+'</option>';
      $("select#id_adv-city").html(options);
      $("select#id_adv-city").val(li.extra[1]);
      
      var ac = $("#id_adv-custom_city3")[0].autocompleter;
      ac.setExtraParams( { 
          country: $("select#id_adv-country").val(),
          region: $("select#id_adv-region").val()
       } );
      ac.flushCache();
      
    })
}

function formatItem(row) {
	return row[0] + "<br><i>" + row[1] + "</i>";
}

      $("#id_adv-custom_city3").autocomplete("/" + lang + "/catalog/ajax_suggest/", 
        {
        delay:10,
        minChars:3,
        matchSubset:1,
        autoFill:false,
        maxItemsToShow:10,
        lineSeparator: '\n',
        cellSeparator: '|',
        onItemSelect:selectItem3,
        formatItem: formatItem,
        extraParams: { 
          country: $("select#id_adv-country").val() 
          }
        }
      );  

    $("input#id_adv-offer_type_0").click(function() 
    { 
        $("#id_adv-price_type").parent().show();

    });

    $("input#id_adv-offer_type_1").click(function() 
    { 
        $("#id_adv-price_type").parent().hide();

    });

}); 

