/**
 * AutoComplete Field - JavaScript Code
 * This is a sample source code provided by fromvega.
 * Search for the complete article at http://www.fromvega.com
 * Enjoy!
 * @author fromvega
 */
var autocomplete_url = null;
var autocomplete_field = null;
var autocomplete_results  = null;
var autocomplete_current = 0;
var autocomplete_total = 0;

function setAutoComplete(field_id, results_id, get_url) {
	autocomplete_field = jQuery('#' + field_id);
	autocomplete_results = jQuery('#' + results_id);
	autocomplete_url = get_url;
	// on blur listener
	autocomplete_field.blur(function(){
		setTimeout("clearAutoComplete()", 200)
	});
	// on key down listener
	autocomplete_field.keyup(function (e) {
		var keyCode = e.keyCode || window.event.keyCode; // window.event is for IE
		var lastVal = autocomplete_field.val();
		if (lastVal.length < 3) return false;
		// check and treat up and down arrows
		if (updownArrow(keyCode)) return;
		// check for an ENTER or ESC
		if(keyCode == 13 || keyCode == 27){
			clearAutoComplete();
			return;
		}
		// if is text, call with delay
		setTimeout(function () {autoComplete(lastVal)}, 500);
	});
}

// treat the auto-complete action (delayed function)
function autoComplete(lastValue) {
	autocomplete_results.show();
	// get the field value
	var part = autocomplete_field.val();
	// if it's empty clear the resuts box and return
	if(part == ''){
		clearAutoComplete();
		return;
	}
	// if it's equal the value from the time of the call, allow
	if(lastValue != part) return;
	jQuery.ajax( { url: autocomplete_url + part, cache: false,
		success: function(json) {
			autocomplete_results.html(json);
			autocomplete_results.show();
			var lis = jQuery("#suggest_list li");
			autocomplete_current_total = lis.length;
			lis.mouseover(function() { // on mouse hover clean previous selected and set a new one
				lis.each(function(){ jQuery(this).removeClass("selected"); });
				jQuery(this).addClass("selected");
			});
			lis.click(function() {
				var li = jQuery(this);
				var id = li.attr('rel');
				if (id) {
					window.location = '/products/product.html?id=' + id;
				} else {
					var ptext = li.children('p').text();
					var pall = ptext.indexOf(' ') + 1;
					var value = ptext.substring(pall);
					autocomplete_field.val(value);
					clearAutoComplete();
					jQuery('#search_go').click();
				}
			});

		}
	});
	//jQuery.get(autocomplete_url + part, function(json){ });
}

// clear auto complete box
function clearAutoComplete() {
	autocomplete_results.html('');
	autocomplete_results.hide();
}

// reposition the results div accordingly to the search field
function repositionResultsDiv() {
	// get the field position
	var sf_pos    = autocomplete_field.offset();
	var sf_top    = sf_pos.top;
	var sf_left   = sf_pos.left;
	// get the field size
	var sf_height = autocomplete_field.height();
	var sf_width  = autocomplete_field.width();
	// apply the css styles - optimized for Firefox
	autocomplete_results.css("position","absolute");
	autocomplete_results.css("left", sf_left - 2);
	autocomplete_results.css("top", sf_top + sf_height + 5);
	autocomplete_results.css("width", sf_width - 2);
}

// treat up and down key strokes defining the next selected element
function updownArrow(keyCode) {
	if (keyCode != 40 && keyCode != 38) {
		autocomplete_current = 0; // reset
		return false;
	}
	if (keyCode == 38) { // keyUp
		if (autocomplete_current > 1) autocomplete_current--;
		else autocomplete_current = autocomplete_total; // go to bottom of list
	}
	if (keyCode == 40) { // keyDown
		if (autocomplete_current == autocomplete_total) autocomplete_current = 0;
		else autocomplete_current++;
	}
	// loop through each result div applying the correct style
	autocomplete_results.children().each(function(i){
		if (i == autocomplete_current) {
			autocomplete_field.val(this.childNodes[0].nodeValue);
			jQuery(this).addClass("selected"); //.className = "selected";
		} else {
			jQuery(this).removeClass("selected"); //.className = "unselected";
		}
	});
	return true;
}
