/**
 * @author		Maarten Molenschot
 * @version		0.1
 * @created		2008-12-04
 * @modified	2008-12-05
**/

var StarcqSuggest = new Class({
	
	options: {
		request_url:		'/default/requests/plaatsen.php',
		setfocus:			false,
		validatePlaats:		false
	},
	
	initialize: function(element, options){
		this.setOptions(options);
		this.element = $(element);
		
		this.autocompleter = new Autocompleter.Ajax.Xhtml(this.element, this.options.request_url, {
			delay: 500,
			parseChoices: function(el) {
				if(el.getFirst()){
					var value = el.getFirst().innerHTML;
					el.inputValue = value;
					this.autocompleter.addChoiceEvents(el).getFirst().setHTML(this.autocompleter.markQueryValue(value));
				}
			}.bind(this),
			multiple: false,
			zIndex: 10000000000
		});
		
		if(this.options.setfocus) this.element.focus();
		if(this.options.validatePlaats) this.validate();
	},
	
	validate: function(){
		this.getForm(this.element);
			this.form.addEvent('submit', function(){
				if(this.element.getValue()==''){
					alert('U heeft geen plaats geselecteerd.');
					return false;
				}
			}.bind(this));
	},
	
	getForm: function(el){
		var tmp_el = el.getParent();
		if(tmp_el.getTag()=='form'){
			this.form = tmp_el;
		}else{
			this.getForm(tmp_el);
		}
	}
	
});
StarcqSuggest.implement(new Options);
StarcqSuggest.implement(new Events);


window.addEvent('domready', function(){
	if($('locatie')){
		var searchSuggest = new StarcqSuggest('locatie', {
			validatePlaats:	true
		});
	}
});
