jQuery(document).ready(function() {



   jQuery('#more_options_area').each(function(){

         if( display_search==false)
         {
           jQuery(this).hide();
           jQuery('#more_options').show();
           jQuery('#less_options').hide();
         }
         else
         {
           jQuery('#more_options').hide();
           jQuery('#less_options').show();
         }


         var hide = true;
        jQuery('#search_options_area ul.options_button_list li').each(function(){
          if(jQuery(this).children("input").val()!=""){
            hide = false;
            return true;
          }
        });

        if(jQuery("#restaurant_filters_member_criteria").val() != ""){
          hide = false;
        }

         if(jQuery("#region_id input.selected_option_value").val() != "" ||
            (jQuery("#district_id input.selected_option_value").length && jQuery("#district_id input.selected_option_value").val() != "") ||
            jQuery("#region_id .selected_option_value").val() != "") {
           hide = false;
         }

         if(hide){
           jQuery(this).hide();
           jQuery('#more_options').show();
           jQuery('#less_options').hide();
         }
         else {
           jQuery(this).show();
           jQuery('#more_options').hide();
           jQuery('#less_options').show();
         }


   });

   //dirty code > do like select field (helper or widget)
  jQuery('.advanced_search_box a.red_button').click(function() {

        var thisid = jQuery(this).attr('id');
        var reg=new RegExp("[-]+", "g");
        var thistid=thisid.split(reg);

        if(jQuery(this).hasClass("active")==false)
        {
          jQuery(this).addClass("active");
          if(thistid[1]=="button") {
            jQuery("#"+thistid[0]).val(thistid[2]);
          }
        }
        else {
          jQuery(this).removeClass("active");
          if(thistid[1]=="button")
            jQuery("#"+thistid[0]).val("");
        }

        jQuery('.advanced_search_box a.red_button').each(function(){
          var id = jQuery(this).attr('id');
          var reg=new RegExp("[-]+", "g");
          var tid=id.split(reg);
          if(thisid != id)
          {
            jQuery(this).removeClass("active");
            //if(tid[1]=="button")
              //jQuery("#"+tid[0]).val("");
          }
        });

        return false;

  });
  jQuery('.advanced_search_box a.green_button').click(function() {

      var id = jQuery(this).attr('id');
      var reg=new RegExp("[-]+", "g");
      var tid=id.split(reg);
      if(jQuery(this).hasClass("active")==false) jQuery(this).addClass("active");
      else jQuery(this).removeClass("active");
      if(tid[1]=="button")
      {
          if(jQuery("#"+tid[0]).val()==1)
            jQuery("#"+tid[0]).val(0);
          else
            jQuery("#"+tid[0]).val(1);
      }
      return false;
  });


  jQuery('#less_options').click(function(){

       jQuery('#search_options_area').fadeOut('normal');
       jQuery('#search_district_area').fadeOut('normal');
       jQuery('#search_region_map_area').fadeOut('normal');
       jQuery('#search_region_area').fadeOut('normal');
       jQuery('#members_criteria_area').fadeOut('normal');
       jQuery('#more_options').show();
       jQuery('#less_options').hide();

       jQuery("#more_options_area").animate({height: "toggle"}, 800);

  });

  jQuery('#more_options').click(function(){

       jQuery('#search_options_area').fadeIn(1000);
       jQuery('#search_district_area').fadeIn(1000);
       jQuery('#search_region_map_area').fadeIn(1000);
       jQuery('#search_region_area').fadeIn(1000);
       jQuery('#members_criteria_area').fadeIn(1000);
       jQuery('#more_options').hide();
       jQuery('#less_options').show();

       jQuery("#more_options_area").animate({height: "toggle"}, 800);
  });


});

function initLocality(){
  var lid = jQuery('#locality_id input.selected_option_value').val();
  jQuery('#locality_id').empty();
   jQuery.get(page_localityId,
     { region_id: jQuery('#region_id input.selected_option_value').val(), locality_id: lid },
     function(data){
       jQuery('#locality_id').append(data);
     }
   );
}

function initRegion(){
    initSelect('region_id');
}


// district
function toggleDistrict(){
  jQuery('#district_id').fadeOut('fast');
  initSelect('district_id');
  // retrieve all districts and find if those are selected
  jQuery('#locality_id li.district').each(function(){
    if(jQuery('#locality_id .selected_option_value').val() == getSelectedValue(this)){
      jQuery('#district_id').fadeIn('fast');
    }
  });
}

// init district select
function initDistrict(){
    initSelect('district_id');
}