// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var DescriptiveInputFunctional = {
	changed: new Hash(),
	descriptions: new Hash(),

	init: function() {
		$$('.descriptive').each(function(el) {
			el.observe('focus', DescriptiveInput.onFocus);
			el.observe('blur', DescriptiveInput.onBlur);
			el.observe('change', DescriptiveInput.onChange);
			if ($(el.id+'_description')) {
				DescriptiveInput.descriptions.set(el.id, $(el.id+'_description').value);
				DescriptiveInput.changed.set(el.id, true);
			} else {
				DescriptiveInput.descriptions.set(el.id, el.value);
				DescriptiveInput.changed.set(el.id, false);
			}
			el.setValue = DescriptiveInput.setValue.bind(el);
		});
	},
	onFocus: function(event) {
		input = event.element();
		if (!DescriptiveInput.changed.get(input.id)) {
			input.value = '';
		}
	},
	onBlur: function(event) {
		input = event.element();
		if (!DescriptiveInput.changed.get(input.id))
			input.value = DescriptiveInput.descriptions.get(input.id);
	},
	onChange: function(event) {
		input = event.element();
		if (input.value != '')
			DescriptiveInput.changed.set(input.id, true);
		else
			DescriptiveInput.changed.set(input.id, false);
	},
	clearForSubmit: function() {
		DescriptiveInput.changed.each(function(pair) {
			if(!pair.value) {
        alert(pair.key);
        alert($(pair.key).value);
			  $(pair.key).clear();
        // alert($(pair.key).value);
			}
		});
	},
	setValue: function(value) {
		this.value = value;
		DescriptiveInput.changed.set(this.id, true);
	}
};

var SearchForm = Class.create({
	initialize: function() {
		args = $A(arguments);
		this.form = $(args.first());
		this.form.getInputs().each(function(input) {
			input.observe('keydown', this.inputOnKeyDown);
		}.bind(this));
	},
	
	inputOnKeyDown: function(event) {
		if (event.keyCode == Event.KEY_RETURN) {
			event.element().blur();
			submitSearch();
		}
	}
});

function submitSearch() {
	DescriptiveInput.clearAllForSubmit();
  $('search_form').submit();
}

function submitSearchWithCategory(id) {
  // $('search_category_id').value = id;
	submitSearch();
}

function submitSearchWithNeighborhood(id) {
	var opts = $A($('search_neighborhood_id').options);
	var opt = opts.find(function(el) {return el.value == id;});
	opt.selected = 'selected';
	submitSearch();
}

function submitSearchWithLocationQuery(query) {
	$('search_location_query').setValue(query);
	submitSearch();
}

function submitSearchWithSort(sort) {
	$('search_sort').value = sort;
	submitSearch();
}

function appendLoadFunction(func) {
 	if (window.onload) {
		var old = window.onload;
		window.onload = function() {
			old(); func();
		}
	} else {
		window.onload = func;
	}
}







/*********
 * 
 * JAVASCRIPTS FOR SIGNUP FORM
 *
 *********/
 
function toggle_business_exists(toggler) {
  $('business_exists_div').toggle();
  $('business_noexists_div').toggle();

  if ($('business_noexists_div').visible()) {
    $('business_id').selectedIndex = 0;
  } else {
    $('business_title').clear();
  }
}
