function getTop(obj){
	var curtop = 0;
	while (obj.offsetParent){
		curtop += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return curtop;
}
function getLeft(obj){
	var curleft = 0;
	while (obj.offsetParent){
		curleft += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return curleft;
}
/* STUPID IE
Element.prototype.appendEvent = function(oEvent, func){
		
	var parent = this;
	var containsOn = oEvent.indexOf('on') == 0;
		
	if(window.attachEvent){
		if(!containsOn) oEvent = 'on'+oEvent;
			parent.attachEvent(oEvent, func);
	} else {
		if(containsOn) oEvent = oEvent.substring(2);
			parent.addEventListener(oEvent, func, false);
	}
};*/
function AutoSuggest(){
	
	var oThis = this;
	var textfield = null;
	var suggestions = null;
	/*var textfieldID = textfield;
	var suggestionsArray = suggestions;
	
	var oThis = this;
	var fnc = function(){
		// once the document is loaded, grab DOM element based on its ID
		var textfieldObject = document.getElementById(textfieldID); 
		oThis.init(textfieldObject, suggestionsArray, max_results);
	}

	if(window.attachEvent) window.attachEvent('onload', fnc); 
	else window.addEventListener('load', fnc, false);*/
}
AutoSuggest.prototype.init = function(textfield, suggestions, max_res){

	if( typeof(jQuery) == 'undefined' ){
		alert('jquery is not defined!');
	}

	this.textfield = document.getElementById(textfield);
	var textfield = this.textfield;
	this.textfield.setAttribute("autocomplete", "off");
	
	this.suggestions = suggestions;
	this.max_results = max_res;
	
	this.current_selected = -1;
	
	this.search_box = document.createElement('div');
	this.search_box.className = 'rsn_search';
	this.search_box.style.width = textfield.offsetWidth+'px';
	this.hideSuggestions(); 
	// hide suggestions at first right? cause there are none?
	document.body.appendChild(this.search_box);
	
	var oThis = this; //save reference to this scope
	
	this.textfield.onblur = function(){
		oThis.hideSuggestions();
	};
	
	$(oThis.textfield).keydown(function(evt){
										oThis.handleKeyPress(evt);
										});
	$(oThis.textfield).keyup(function(evt){
									  oThis.handleKeyUp(evt);
									  });
										
	
	$(oThis.search_box).mousedown(function(evt){						  
			oThis.textfield.value = evt.target.firstChild.nodeValue;
			oThis.hideSuggestions();
	});
	
	$(oThis.search_box).mouseup(function(evt){
									oThis.textfield.focus();
										 });
	$(oThis.search_box).mouseover(function(evt){
										   oThis.SelectItem(evt.target);
										   });
	this.installCSS();
	this.revalidate();
	
	
	$(window).resize(function(evt){
					oThis.revalidate();
							  });

};
AutoSuggest.prototype.installCSS = function(){
	var str = "div.rsn_search div {padding:3px;}div.rsn_search {z-index:100;position:absolute;" + 
	"margin-top:1px;background-color: #FFF;font-size:11px;color:#000;border-width:1px 1px 1px 1px;" + 
	"border-color:#000; border-style:solid; cursor:default;}" + 
	"div.rsn_search div.current { background-color:#3399ff; color:#FFF;}";
	var headTag = document.getElementsByTagName('head')[0] ;
	var css = document.createElement('style');
	css.type = 'text/css';
	css.media = 'screen';
	if(css.styleSheet) css.styleSheet.cssText = str;// IE method
	else css.appendChild(document.createTextNode(str));// others
	headTag.appendChild(css);
};
AutoSuggest.prototype.handleKeyPress = function(oEvent){
	
	var keycode = oEvent.keyCode || oEvent.which;
	
	if(keycode == 40){
		this.nextSuggestion();
	} else if(keycode == 38){
		this.previousSuggestion();
	} else if(keycode == 13){
		this.nextSuggestion();
		this.hideSuggestions();
	}
};

AutoSuggest.prototype.handleKeyUp = function(oEvent){
	
	var keyValue = oEvent.keyCode || oEvent.which;
	
	if(keyValue == 40 || keyValue == 38 || keyValue == 13){
		return false;
	}

	var value = this.textfield.value.toLowerCase().replace(/([^0-9a-z])/g, ' ');
	var suggestions = [];
		
	for(var i=0; i<this.suggestions.length && value.length > 0; i++){
		if(this.suggestions[i].toLowerCase().indexOf(value) > -1){
			suggestions[suggestions.length]= this.suggestions[i];
		}
	}
	
	this.bla_suggestions = suggestions;
	// 15 - 17 milliseconds
	this.showSuggestions(suggestions);
};
AutoSuggest.prototype.nextSuggestion = function(){
	var suggestions = this.search_box.childNodes;
	if(suggestions.length > 0 && this.current_selected < suggestions.length-1){
		this.SelectItem(suggestions[++this.current_selected]);
		this.textfield.value = suggestions[this.current_selected].firstChild.nodeValue;
	}
};
AutoSuggest.prototype.previousSuggestion = function(){
	
	var suggestions = this.search_box.childNodes;
	if(this.current_selected > 0 && this.current_selected < suggestions.length){
		this.SelectItem(suggestions[--this.current_selected]);
		this.textfield.value = suggestions[this.current_selected].firstChild.nodeValue;
	}
};
AutoSuggest.prototype.SelectItem = function(oNode){
	
	// fix current_selected NODE if mouse event was used
	var suggestions = this.search_box.childNodes;
	for(var i=0; i<this.search_box.childNodes.length; i++){
		var thisNode = suggestions[i];
		if(thisNode == oNode){
			thisNode.className = 'current';
			this.current_selected = i;
		} else {
			thisNode.className = '';
		}
	}
};
AutoSuggest.prototype.revalidate = function(){
	var obj = this.textfield;
	
	this.search_box.style.left = getLeft(obj)+'px';
	this.search_box.style.top = getTop(obj)+obj.offsetHeight+'px';
};
AutoSuggest.prototype.hideSuggestions = function(){
	this.search_box.innerHTML = '';
	this.search_box.style.display = 'none';
};
AutoSuggest.prototype.showSuggestions = function(suggestions){

	this.search_box.innerHTML = '';
	this.search_box.style.display = suggestions.length > 0 ? 'block' : 'none';
	this.current_selected = -1;
	
	var max_results = this.max_results;
	
	for(var i=0; i<suggestions.length && i<max_results; i++){
		var itm = document.createElement('div');
		itm.appendChild(document.createTextNode(suggestions[i]));
		this.search_box.appendChild(itm);
	}
	// 10 - 1 milliseconds
}