/**
 *							 
 *  The js code was given by btoc007 on mootools forum,	 
 *  and tuned by mose					 
 *							 
 *  http://forum.mootools.net/topic.php?id=171&replies=29
 *  http://mose.fr/moocomplete/				 
 *							 
 **/

Ajax.implement({
	responseIsSuccess: function(status){
		try {
			if(this.transport.readyState != 4 || this.transport.status == "undefined" || (this.transport.status <= 200 || this.transport.status >= 300)) {
				return true;
			}
		} catch(e) {
			return false;
		}
	},
	responseIsFailure: function(){
		return !this.responseIsSuccess();
	},
	onStateChange: function(){
		if (this.transport.readyState == 4 && this.responseIsSuccess()){
			if (this.options.update) $(this.options.update).setHTML(this.transport.responseText);
			this.options.onComplete.pass([this.transport.responseText, this.transport.responseXML], this).delay(20);
			if (this.options.evalScripts) this.evalScripts.delay(30, this);
			this.transport.onreadystatechange = Class.empty;
			this.callChain();
		} else if(this.transport.readyState == 4 && this.responseIsFailure()) {
			if($type(this.options.onFailure)=='function') this.options.onFailure.pass(this.transport, this).delay(20); 
		}
	}
});

Element.extend({
	hide: function() {
		this.style.display = 'none';
		return this;
	},
	show: function() {
		this.style.display = '';
		return this;
	},
	
	calculateOffsetLeft: function(){
		var pos = this.calculateOffset(this);
		return pos[0];
	},
	
	calculateOffsetTop: function(){
		var pos = this.calculateOffset(this);
		return pos[1];
	},
	
	calculateOffset: function(obj){
		k = "";
		var curleft = curtop = 0;
		if(obj.offsetParent){ k += obj.offsetLeft + " | " + obj.offsetTop + "\n";
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while(obj = obj.offsetParent){ k += obj.offsetLeft + " | " + obj.offsetTop + "\n";
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
	//	alert(k);
		return [curleft,curtop];
	}
});

var AjaxAutoCompleter = new Class({ 
	initialize: function(element, choices, hidden, url, options) { 
		this.options = Object.extend({ 
			minChars: 2, 
			delay: 200, 
			indicator: null 
		}, options || {}); 
		if (this.options.indicator != null) { 
			this.indicator = $(this.options.indicator); 
		} 
		this.element = $(element); 
		this.choices = $(choices);
		this.hidden = $(hidden);
		this.url = url; 
	//	this.choices.hide();
		var pos = this.element.calculateOffset(this.element);
	//	alert(pos);
		this.choices.setStyles({
			'display': 'none',
			'top': pos[1] + this.element.offsetHeight + 'px',
			'left': pos[0] + 'px'});
		this.currentValue = ''; 
		
		this.hover = false;
		this.choices.addEvent('mouseover', this.onChoicesHover.bindAsEventListener(this)); 
		this.choices.addEvent('mouseout', this.onChoicesNoHover.bindAsEventListener(this)); 
		
		this.element.setAttribute("autocomplete", "off"); 
		this.element.addEvent('keyup', this.onTextChange.bindAsEventListener(this)); 
		this.element.addEvent('blur', this.onTextBlur.bindAsEventListener(this)); 
		// need keydown for IE as it will not fire for arrow keys 
		this.element.onkeydown = this.onKeyDown.bind(this); 
		// will fire for enter key on all browsers 
		this.element.onkeypress = this.onKeyPress.bind(this); 
	}, 
	
	onKeyPress: function(e) { 
		if (window.event) { 
			var keyCode = window.event.keyCode;
		} else { 
			var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode; 
		} 
		if (keyCode == 13) { 
			if (this.choices.style.display == '') { 
				var currentSel = $E('li.selected', this.choices); 
				if (currentSel) {
					this.choiceSelect(currentSel); 
				} 
			} 
			else{
				this.element.parentForm.submit();
			}
			return false; 
		} 
		return true; 
	}, 
	
	onKeyDown: function(e) { 
		if (window.event) { 
			var keyCode = window.event.keyCode; 
		} else { 
			var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode; 
		} 
		if (keyCode == 40 || keyCode == 38) { 
			if (this.choices.style.display == '') { 
				// 40 = Down, 38 = Up 
				var currentSel = $E('li.selected', this.choices); 
				if (keyCode == 38) { 
					// Up 
					if (currentSel && currentSel.getPrevious()) { 
						currentSel.removeClass('selected'); 
						currentSel.getPrevious().addClass('selected'); 
					} else if (!currentSel) { 
						var tempSel = $ES('li', this.choices); 
						if (tempSel && tempSel.length > 0) { 
							tempSel[tempSel.length-1].addClass('selected'); 
						} 
					} 
				} else if (keyCode == 40) { 
					// Down 
					if (currentSel && currentSel.getNext()) { 
						currentSel.removeClass('selected'); 
						currentSel.getNext().addClass('selected'); 
					} else if (!currentSel) { 
						var tempSel = $ES('li', this.choices); 
						if (tempSel && tempSel.length > 0) { 
							tempSel[0].addClass('selected'); 
						} 
					}
				} 
			} 
			return false; 
		} 
		return true; 
	}, 
	
	onTextChange: function() { 
		if (this.fetchDelay) { 
			this.fetchDelay = $clear(this.fetchDelay); 
		} 
		this.fetchDelay = this.fetch.delay(this.options.delay, this); 
	}, 
	
	fetch: function() { 
		if (this.element.value.length <= this.options.minChars) { 
			this.choices.hide(); 
		} else { 
			if (this.element.value != this.currentValue) { 
				this.currentValue = this.element.value; 
				this.clearSelected(); 
				this.choices.show(); 
				if (this.indicator) { 
					this.indicator.show(); 
				} 
				var myAjax = new Ajax(this.url + escape(this.element.value), { method: 'get', onFailure:this.onFailure, onComplete:this.onSuccess.bindAsEventListener(this)}).request(); 
			} 
		} 
	}, 
		
	onFailure: function(response) { 
		if (this.indicator) { 
			this.indicator.hide(); 
		} 
		alert('There was a problem! \n' + response.status + ' -- ' + response.statusText);
	}, 
	
	onSuccess: function(response) { 
		if (this.indicator) { 
			this.indicator.hide(); 
		} 
		if (response.trim() == '') { 
			this.choices.hide(); 
		} else { 
			this.choices.setHTML(response); 
			$ES('li', this.choices).each(function (el) { 
				el.addEvent('mouseover', function() {this.choiceOver(el);}.bind(this)); 
				el.addEvent('mousedown', function() {this.choiceSelect(el);}.bind(this)); 
			}.bind(this)); 
		} 
		
		// Gör så att första alternativet markeras automatiskt
		var tempSel = $ES('li', this.choices); 
		if (tempSel && tempSel.length > 0) { 
			tempSel[0].addClass('selected'); 
		} 
	}, 
	
	onTextBlur: function() {
		if(!this.hover)
			this.choices.hide(); 
	},
	
	onChoicesHover: function(){
		this.hover = true;
	},
	
	onChoicesNoHover: function(){
		this.hover = false;
		this.element.focus();
	},
	
	choiceOver: function(element) { 
		this.clearSelected(); 
		element.addClass('selected'); 
	}, 
	
	choiceSelect: function(element) { 
	//	eval(element.id); 
		this.hidden.value = element.id;
		this.element.value = element.firstChild.innerHTML + ', ' + element.lastChild.innerHTML;
		this.currentValue = this.element.value; 
		this.choices.hide(); 
	}, 
	
	clearSelected: function() { 
		$ES('li.selected', this.choices).each(function (el) { 
			el.removeClass('selected'); 
		}); 
	}
}); 
