var Location = Class.create();

Location.prototype = {
	initialize: function(blah) {
		this.first_time = true;
		this.pre_actions = [];
		this.post_actions = [];
		this.auto_completer = new Ajax.Autocompleter("autocomplete", "autocomplete_choices", "loc_p.php?c=search&a=location_complete", {paramName: "value", minChars: 3, indicator: 'indicator1', callback: (function(value, querystring) {  return this.getLocation(value, querystring); }).bind(this), updateElement: (function(item) { return this.getParams(item); }).bind(this)});

		$('indicator1').hide();

		Event.observe('country', 'change', (function(e) {
			if ($F('country').length == 0) {
				$('autocomplete').disable();
			}
			else {
				$('autocomplete').enable();
			}
			this.clearHidden();
		}).bind(this));
	},

	clearHidden: function() {
		$('loc_id').value = '';
		$('loc_zip').value = '';
		$('loc_lat').value = '';
		$('loc_lon').value = '';
		$('loc_city').value = '';
		$('loc_state').value = '';
		$('autocomplete').value = '';
	},

	addPreAction: function(name) {
		this.pre_actions.push(name);
	},

	addPostAction: function(name) {
		this.post_actions.push(name);
	},

	getLocation: function(value, querystring) {
		if (this.first_time) {
			this.first_time = false;
			this.auto_completer.activate();
		}
		$A(this.pre_actions).each((function(s, index) {
			s(value, querystring);
		}).bind(this));
		return querystring + "&country=" + $F('country');
	},

	getParams: function(item) {
		$A(this.post_actions).each((function(s, index) {
			s(item);
		}).bind(this));

		$('autocomplete').value = item.firstChild.nodeValue;
		$(item.firstChild.nextSibling).childElements().each(function(s, index) {
			if (s.nodeType == 1) {
				$(s.className).value = s.firstChild.nodeValue;
			}
		});
	}
};
var location_finder = new Location(null);
