// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function toggle_checkbox(id) {
	if($F(id)) {	
		$(id).checked=false
	}	else {
		$(id).checked=true
	}
}

function advanced_search_show() {
	Element.hide('simple_submit');
	Element.hide('advanced_toggle');
	Element.show('simple_toggle');
	Element.show($$('label.cuisine')[0]);
	Element.show($$('label.neighborhood')[0]);
	Effect.Appear('advanced_search')
}
function advanced_search_hide() {
	Effect.Fade('advanced_search')
	Element.hide('simple_toggle');
	Element.show('simple_submit');
	Element.show('advanced_toggle');
}

Event.observe(window,'load', function() {
	/* If URL has anchor (#advanced) show advanced form */
	if (location.href.split('#').length > 1) {
		anchor = location.href.split('#')[1]
		if(anchor == 'advanced') {
			advanced_search_show();
		}
	}	
})


Event.observe(window,'load', function() {
  $$('input.clearonfocus').map(function(element) {
    element.setAttribute("autocomplete", "off")
    var form = element.up('form')
    if(form) {
      if(!form.clearOnFocusElements) {
        form.clearOnFocusElements = []
      } 
      form.clearOnFocusElements.push(new ClearOnFocus(element))
    }
    return form
  }).compact().uniq().each(function(element){
    new ClearOnSubmit(element)
  })
})

var ClearOnSubmit = Class.create()
Object.extend(ClearOnSubmit.prototype, {
  initialize: function(form) {
    this.form = form
    this.originalOnSubmit = this.form.onsubmit
    this.form.observe('submit', this.onSubmit.bind(this))
  },
  onSubmit: function(event) {
    this.form.clearOnFocusElements.each(function(element) {
      element.onFocus()
    })
  }
})

var ClearOnFocus = Class.create()
Object.extend(ClearOnFocus.prototype, { 
  initialize: function(element) { 
    this.element = $(element)
    this.originalValue = $F(element)
    this.element.observe('blur', this.onBlur.bind(this))
    this.element.observe('focus', this.onFocus.bind(this))
  }, 
  onFocus: function(event) { 
    if($F(this.element) == this.originalValue) { 
      this.element.value = ''
      this.element.removeClassName('clearonfocus')
    } 
  }, 
  onBlur: function(event) { 
    if($F(this.element).match(/^\s*$/)) { 
      this.element.value = this.originalValue
      this.element.addClassName('clearonfocus')
    } 
  } 
})

