$(document).ready(function(){
  
  reset_forms()
  
  // Handling categories selection
  $('input[type=checkbox]#all').change(function(){
    form = $(this).closest('form')
    $('input[type=checkbox]').attr('checked', $(this).attr('checked'))
  })
  
  $('input[type=checkbox]').change(function(){
    form = $(this).closest('form')
    if (!$(this).attr('checked')) {
      $('input[type=checkbox]#all', form).attr('checked', false)
    } else {
      all_checked = true
      $('input[type=checkbox][id!=all]', form).each(function(index, checkbox) {
        if (!$(checkbox).attr('checked')) {
          all_checked = false
          return
        }
      })
      
      if (all_checked)
        $('input[type=checkbox]#all', form).attr('checked', true)
    }
  })
  
  // Hide / show for the advanced search
  $('#advanced-button').click(function() {
    advanced_options = $('#advanced-options')
    if (advanced_options.is(':visible')) {
      advanced_options.slideUp(100, function() {
        $('#advanced-button').removeClass('active')
      })
    } else {
      $(this).addClass('active')
      advanced_options.slideDown(100)
    }
    
    return false;
  })
  
  $('.no_focus').focus(function(){ this.blur();} );
  
  // Autocomplete for the advanced search 
  
  $('input[name=locations[]][type=text], input[name=location_tags[]][type=text]').livequery(function() {
    var input = this;

    $(input).boxable({
      value: function(result) {
        return result.label
      }
    }).autocomplete({
      url: '/search/locations',
      submitOnEnter: false,
      onSelect: function(result) {
        $(input).trigger('box',result);
        $(input).val('');
      }
    }).keydown(function(e) {
      if (e.which == 188)
        $(input).trigger('box',{
          label: input.value
        })
    }).keyup(function(e) {
      if (e.which == 188)
        $(input).val('').blur().focus();
    });
  });

  $('input[name=interests[]][type=text], input[name=interest_tags[]][type=text]').livequery(function() {
    var input = this;

    $(input).boxable({
      value: function(result) {
        return result.label
      }
    }).autocomplete({
      url: '/search/interests',
      submitOnEnter: false,
      onSelect: function(result) {
        $(input).trigger('box',result);
        $(input).val('');
      }
    }).keydown(function(e) {
      if (e.which == 188)
        $(input).trigger('box',{
          label: input.value
        })
    }).keyup(function(e) {
      if (e.which == 188)
        $(input).val('').blur().focus();
    });
  });

  $('form#search').submit(function() {
    if ($('input#searchbar').val() != $('input#searchbar').attr('defaultValue')) {
      adv = $('#advanced-options', this)
      console.log(adv)
      $('input[type=hidden]', adv).remove()
      $('li', adv).remove()
    }
  })

})

function reset_forms() {
  $('input[type=submit]').addClass('active')
  $('input[type=password]').addClass('active')

  $('input[type=text], textarea').each(function(index, input) {
    if (input.value) {
      if (input.value != $(input).attr('title')) {
        $(input).addClass('active')
      }
    } else if ($(input).attr('title')) {
      $(input).val($(input).attr('title'))
    } else {
      $(input).addClass('active')
    }
  })

  $('input[type=text], textarea').focusin(function() {
    if ($(this).attr('title')) {
      if ($(this).val() == $(this).attr('title')) {
        $(this).val('')
        $(this).addClass('active')
      }
    }
  })
  
  $('input[type=text], textarea').focusout(function() {
    if ($(this).attr('title')) {
      if ($(this).val() == "") {
        $(this).removeClass('active')
        $(this).val($(this).attr('title'))
      }
    }
  })
  
  $('form').submit(function() {
    $('input[type=text], textarea').each(function(index, input) {
      if ($(input).attr('title'))
        if ($(input).val() == $(input).attr('title'))
          $(input).val('')
    })
  })
  
  $('a.clear').click(function() {
    field = $(this).closest('.description').next()
    $('input[type=hidden]', field).remove()
    $('li', field).remove()
    return false
  })
}

