var initial_options;

$(function(){
  
  // $('body').bind('mouseup', function(){
  //   var closed = $('.area-dropdown').data('collapsed')
  //   if(closed == false)
  //   {$('.area-dropdown').trigger('click')}
  // })
  $('.area-dropdown').data('collapsed', true);
  $('.area-dropdown').click(function(){
    if($('.area-dropdown').data('collapsed'))
    {
      $('.area-dropdown').addClass('active')
      $('.area-dropdown-box').show();
      $('.area-dropdown').data('collapsed', false);
    }else{
      $('.area-dropdown-box').hide();
      $('.area-dropdown').removeClass('active')
      $('.area-dropdown').data('collapsed', true);
      submit_form(true);
    }
  })
  
  // Open and close the advanced section of the filters
  $('.more_options,.close_button').css('cursor', 'pointer')
  
  $('#searchfilter').data('collapsed', true);
  $('.more_options,.close_button').click(function() {
    var menuItem = $('#searchfilter')
    var panel = menuItem.find('.advanced');
    if (menuItem.data('collapsed')) {
      menuItem.data('collapsed', false);
      panel.show();
      $('.more_options').hide();
    } else {
      menuItem.data('collapsed', true);  
      panel.hide();  
      $('.more_options').show();
    } 
    return false; 
  });
  //$('.close_button').trigger('click')
  //$('.close_button').trigger('click')
  
  $('.parent:checkbox').click(function(){ 
    if($(this).attr('checked')){
      $(this).nextAll('ul').find(':checkbox').attr('checked', true).blur();
    }else{
      $(this).nextAll('ul').find(':checkbox').removeAttr('checked').blur();
    }
  })
  $('.areas :checkbox').click(function(){
    update_area();
  })
  
  
  $("#price").slider({
    range: true,
    min: 50000,
    max: 2000000,
    step: 25000,
    slide: function(event, ui) {
      $("#filter_min_price").val("Min: "+to_currency(Math.round(ui.values[0])));

      var max_price = Math.round(ui.values[1]);
      if(max_price >= 2000000)
      {max_price = to_currency(2000000)+"+"}
      else
      {max_price = to_currency(max_price)}

      $("#filter_max_price").val("Max: "+max_price);
    }/*,
    stop: function(event, ui) {
      submit_form();
    }*/
  });

  $("#bedrooms").slider({
    min: 1,
    max: 4,
    slide: function(event, ui) {
      $("#filter_bedrooms_amount").val(Math.round(ui.value)+"+");
    }
  });

  $("#footage").slider({
    min: 500,
    max: 10000,
    step: 100,
    slide: function(event, ui) {
      $("#filter_footage_amount").val(Math.round(ui.value)+"+");
    }
  });

  $("#bathrooms").slider({
    min: 1,
    max: 4,
    slide: function(event, ui) {
      $("#filter_bathrooms_amount").val(Math.round(ui.value)+"+");
    }
  });

  $("#year").slider({
    min: 1900,
    max: current_year(),
    slide: function(event, ui) {
      $("#filter_year_amount").val(Math.round(ui.value)+"+");
    }
  });

  $('#year, #bathrooms, #footage, #price, #bedrooms').bind('slidestop', function(event, ui) {
    submit_form();
  });
  $('.booleans :checkbox').click(function(){
    submit_form(true);
  })

  if($('#price').length) {
	init_sliders();
  }
  update_area();
  $('.areas').data('checked', false);
  initial_options = serialize_options();
});

function serialize_options()
{
  var t = new Array();
  $('#searchfilter :input').each(function(){
    t.push($(this).val())
  })
  return t.join("").replace(/\D/gi, "")
}

function toggle_all()
{
  if($('.areas').data('checked') == false)
  {
    $('.areas :checkbox').attr('checked', true)
    $('.areas').data('checked', true)
  }else{
    $('.areas :checkbox').attr('checked', false)
    $('.areas').data('checked', false)
  }
  update_area();
}

function update_area()
{
  var selected_cities = new Array();
  $('.parent:checkbox').each(function(){
    if($(this).attr('checked')){
      selected_cities.push($(this).attr('id'))
    }else{
      var neighborhoods = $(this).nextAll('ul').find(':checkbox')
      for(var i=0; i<neighborhoods.length; i++)
      {
        if($(neighborhoods[i]).attr('checked'))
        {
          selected_cities.push($(neighborhoods[i]).attr('id'))
        }
      }
    }
  })
   
  switch(selected_cities.length){
    case 1:
      var dropdown_text = selected_cities[0];
      break;
    case 0:
      var dropdown_text = "Choose an area";
      break;
    default: 
      var dropdown_text = "Multiple";
  }
  
  $("#current_city").html(dropdown_text)
}


function submit_form(force_sumbit)
{
  // Serialize options of it was a checkbox that was changed
  if(($("#site.homepage").length <= 0))
  {
    if(force_sumbit == true || (initial_options != serialize_options()))
    {
      show_spinner();
      $('form#filter_form').submit();
    }
  }
}

function show_spinner()
{
  $('#count_spinner').show();
}
