// search suggestion
suggest = {
	// configuration parameters and required object instances
	acListTotal   :  0,
	acListCurrent : -1,
	acDelay       : 300,
	acURL         : null,
	acFormId      : null,
	acSearchId	  : null,
	acResultsId	  : null,
	acSearchField : null,
	acResultsDiv  : null,
	fieldDefault  : null,
	
	init : function(formId, fieldId, fieldDefault, resultsId, url) {
		if(!debug) return;
		// initialize vars
		suggest.acFormId = formId;
		suggest.acSearchId = fieldId;
		suggest.acResultsId = resultsId;
		suggest.acURL = url;
		suggest.fieldDefault = fieldDefault;
		
		// disable browser auto comlete
		$(fieldId).writeAttribute("autocomplete", "off");
		
		// create the results div
		$("searchtextbox").insert({after: "<div id=\"" + resultsId + "\" class=\"suggestions\"></div>"});
	
		// register mostly used elements
		suggest.acSearchField = $(suggest.acSearchId);
		suggest.acResultsDiv = $(suggest.acResultsId);
	
		// reposition div
		suggest.repositionResultsDiv();
	
		// on blur listener
		Event.observe(suggest.acSearchField, 'blur', function(e){setTimeout("suggest.clear()", 200)});
	
		// on key up listener
		Event.observe(suggest.acSearchField, 'keyup', function(e){
			// get keyCode (window.event is for IE)
			var keyCode = e.keyCode || window.event.keyCode;
			var lastVal = suggest.acSearchField.getValue();
			// check an treat up and down arrows
			if(suggest.updownArrow(keyCode)){
				return;
			}
			// check for an ENTER or ESC
			if(keyCode == 13 || keyCode == 27) {
				suggest.clear();
				return;
			}
			// if is text, call with delay
			setTimeout(function() { suggest.suggest(lastVal) }, suggest.acDelay);
		});
		
		// on focus listener (clear default value)
		Event.observe(suggest.acSearchField, 'focus', function(e){
			var val = suggest.acSearchField.getValue();
			if(val == suggest.fieldDefault) {
				suggest.acSearchField.setValue("");
			}
		});
		
		// on submit we do not submit the form, but change the window location
		// in order to avoid https to http warnings in the browser		
		Event.observe(suggest.acFormId, 'submit', function(e) {
			var searchUrl = $(suggest.acFormId).readAttribute("action");
			var searchTerm = suggest.acSearchField.getValue();
			window.location = searchUrl + "?q=" + searchTerm;
			return false;
		});
		
		// on resize reposition layer
		Event.observe(window, 'resize', function(e){
			suggest.repositionResultsDiv();
		});
	},
	
	// trigger suggest action
	suggest : function(lastValue) {
		// get the field value
		var part = suggest.acSearchField.getValue();
	
		// if it's empty clear the resuts box and return
		if(part == "") {
			suggest.clear();
			return;
		}
	
		// if it's equal the value from the time of the call, allow
		if(lastValue != part) {
			return;
		}
		
		// build the request url
		var reqUrl = suggest.acURL + "?suggest=" + part;
		
		// get remote data as JSON
		jsonLoader = new Ajax.Request(reqUrl, {
			method:'get',
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON();
				// get the total of results
				var ansLength = suggest.acListTotal = json.suggestions.length;
		
				// if there are results populate the results div
				if(ansLength > 0) {
					var newData = "";
					// create a div for each result
					for(i=0; i < ansLength; i++) {
						newData += "<div class=\"unselected\"><div class=\"suggestionterm\">" + json.suggestions[i].suggestion + "</div>";
						newData += "<span class=\"hits\"></span></div>";
					}
					// update the results div
					suggest.acResultsDiv.update(newData);
					suggest.acResultsDiv.setStyle({display:"block"});
					// reposition in case user resizes browser between searches
					suggest.repositionResultsDiv();
					
					// for all divs in results
					var divs = $$('#'+suggest.acResultsId+' > div');
					// on mouse over clean previous selected and set a new one
					divs.each(function(obj){
						Event.observe(obj, 'mouseover', function(e){
							divs.each(function(iobj){iobj.className = "unselected"});
							obj.className = "selected";
						});
					});
					
					// on click copy suggestion to search field, hide the list and submit the search
					divs.each(function(obj){
						Event.observe(obj, 'click', function(e){
							suggest.acSearchField.setValue(obj.childNodes[0].innerHTML);
							suggest.clear();
							$(suggest.acFormId).submit();
						});
					});
				} else {
					suggest.clear();
				}
			}
		});
	},
	
	// clear suggestions
	clear : function() {
		suggest.acResultsDiv.update("");
		suggest.acResultsDiv.setStyle({display:"none"});
	},
	
	// reposition the results div accordingly to the search field
	repositionResultsDiv : function() {
		// get the input position
		var inPos = suggest.acSearchField.cumulativeOffset();
		var inTop = inPos.top;
		var inLeft = inPos.left;
		
		// get the field size
		var inHeight = suggest.acSearchField.getHeight();
		var inWidth = suggest.acSearchField.getWidth();
		
		var top = inTop + inHeight;
		
		// apply the css styles
		suggest.acResultsDiv.addClassName("suggestions");
		suggest.acResultsDiv.setStyle({position:"absolute", left: inLeft+"px", top: top+"px", width: inWidth+"px", zIndex: "7777"});
	},
	
	// treat up and down key strokes defining the next selected element
	updownArrow : function(keyCode) {
		if(keyCode == 40 || keyCode == 38) {
			if(keyCode == 38) { // keyUp
				if(suggest.acListCurrent == 0 || suggest.acListCurrent == -1) {
					suggest.acListCurrent = suggest.acListTotal-1;
				} else {
					suggest.acListCurrent--;
				}
			} else { // keyDown
				if(suggest.acListCurrent == suggest.acListTotal-1) {
					suggest.acListCurrent = 0;
				} else {
					suggest.acListCurrent++;
				}
			}
			
			// loop through each result div applying the correct style
			var divs = suggest.acResultsDiv.childElements();
			divs.each(function(obj, index) {
				if(index == suggest.acListCurrent) {
					suggest.acSearchField.setValue(obj.childNodes[0].innerHTML);
					obj.className = "selected";
				} else {
					obj.className = "unselected";
				}
			});
			return true;
		} else {
			// reset
			suggest.acListCurrent = -1;
			return false;
		}
	}
};