
// global variables
var acListTotal   =  0;
var acListCurrent = -1;
var acDelay		  = 400;
var acURL		  = jsstrdirectory + "includes/asp/ajax_searchfuncs_v2.asp";
var resultsDivWidth = 0;
var searchtype = "initsearch";

function setAutoComplete(field_id, results_id, get_url, stype, offsettop, offsetleft) {
	
	// initialize vars
	if(get_url=='') get_url = acURL;
	if(stype=='') stype = searchtype;
	
	switch(stype)
	{
		case 'expsearch':
		    $("body").append('<div id="' + results_id + '_outer"><div class="' + results_id + '_header"><div class="' + results_id + '_title"></div><div class="' + results_id + '_showall"><a href="javascript:document.sf.Submit.click();" class="close">View All Results</a> <a href="javascript:clearAutoComplete(\'' + results_id + '\');" class="close">Close</a></div></div><div id="' + results_id + '"></div><div class="' + results_id + '_footer"></div><div class="' + results_id + '_bridge"></div></div>');
		default:
		    $("body").append('<div id="' + results_id + '_outer"><div class="' + results_id + '_header"><div class="' + results_id + '_title"></div><div class="' + results_id + '_showall"><a href="javascript:clearAutoComplete(\'' + results_id + '\');" class="close">Close</a></div></div><div id="' + results_id + '"></div><div class="' + results_id + '_footer"></div></div>');
	}
	var acSearchFieldElement = document.getElementById(field_id);
	var acSearchField	= $('#' + field_id);
	var acResultsDiv	= $('#' + results_id);
	var acResultsContainerDiv	= $('#' + results_id + '_outer');
	
	acSearchFieldElement.resultsOffset = {top: offsettop, left: offsetleft}

	// reposition div
	repositionResultsDiv(field_id, results_id);
	
	// on blur listener
	acResultsContainerDiv.blur(function(){ setTimeout(function () {clearAutoComplete(results_id)}, 200) });

	// on key up listener
	acSearchField.keyup(function (e) {
		clearTimeout(acSearchFieldElement.timer)

		// get keyCode (window.event is for IE)
		var keyCode = e.keyCode || window.event.keyCode;
		var lastVal = acSearchField.val();

		// check an treat up and down arrows
		if(updownArrow(results_id, keyCode)){
			return;
		}

		// check for an ENTER
		if(keyCode == 13 || keyCode == 10){
			$("#quickaddform").submit();
			return;
		}
		// check for an ESC
		if(keyCode == 27){
			clearAutoComplete(results_id);
			return;
		}
		
		// if is text, call with delay
		acSearchFieldElement.timer = setTimeout(function () {autoComplete(lastVal, field_id, results_id, stype)}, acDelay);
	});
}

// treat the auto-complete action (delayed function)
function autoComplete(lastValue, field_id, results_id, stype)
{
	// RESET WORKING VARS
	var acSearchField	= $('#' + field_id);
	var acResultsDiv	= $('#' + results_id);
	var acResultsContainerDiv	= $('#' + results_id + '_outer');
	
	// get the field value
	var part = acSearchField.val();
	//alert(part);
		
	// if it's empty clear the results box and return
	if(part == ''){
		clearAutoComplete(results_id);
		return;
	} 
	// if it's equal the value from the time of the call, allow
	if(lastValue != part){
		return;
	}
	//alert(stype);
	//alert(acSearchField.attr("name"));
	var args = {action: stype};
	args[acSearchField.attr("name")] = part;
	//alert(args);
	// get remote data as HTML
	
	$.ajax({
		type: "POST",
		url: acURL,
		data: args,
		dataType: "html",
		success: function(html){		
		// if there are results populate the results div
		if(html!=""){
			var newData = html;
			// update the results div
			acResultsDiv.html(newData);
			acResultsContainerDiv.css("display","block");
			
			// for all divs in results
			var divs = $('#' + results_id + " > div > div");
		
			// on mouse over clean previous selected and set a new one
			divs.mouseover( function() {
				divs.each(function(){ this.className = "listitem unselected"; });
				this.className = "listitem selected";
				//acSearchField.val(this.title)
			})
			
			// on click copy the result text to the search field and hide
			//divs.click( function() {
				//acSearchField.val(this.childNodes[0].nodeValue);
				//clearAutoComplete();
			//});

		} else {
			clearAutoComplete(results_id);
		}
	}});
}

// clear auto complete box
function clearAutoComplete(results_id)
{
	var acResultsDiv	= $('#' + results_id);
	var acResultsContainerDiv	= $('#' + results_id + '_outer');

	acResultsDiv.html('');
	acResultsContainerDiv.css("display","none");
}

// reposition the results div accordingly to the search field
function repositionResultsDiv(field_id, results_id)
{
	var acSearchFieldElement = document.getElementById(field_id);
	var acSearchField	= $('#' + field_id);
	var acResultsContainerDiv	= $('#' + results_id + '_outer');

	// get the field position
	var sf_pos    = acSearchField.offset();
	var sf_top    = sf_pos.top;
	var sf_left   = sf_pos.left;

	// get the field size
	var sf_height = acSearchField.height();
	// ORIGINAL --> var sf_width  = acSearchField.width();
	// apply the css styles - optimized for Firefox
	acResultsContainerDiv.css("position","absolute");
	acResultsContainerDiv.css("left", sf_left + acSearchFieldElement.resultsOffset.left);
	acResultsContainerDiv.css("top", sf_top + sf_height - acSearchFieldElement.resultsOffset.top);
	
	// DONE IN THE CSS --> acResultsDiv.css("width", sf_width - 2);
}


// treat up and down key strokes defining the next selected element
function updownArrow(results_id, keyCode) {
	if(keyCode == 40 || keyCode == 38){

		var acResultsDiv	= $('#' + results_id);
		if(keyCode == 38){ // keyUp
			if(acListCurrent == 0 || acListCurrent == -1){
				acListCurrent = acListTotal-1;
			}else{
				acListCurrent--;
			}
		} else { // keyDown
			if(acListCurrent == acListTotal-1){
				acListCurrent = 0;
			}else {
				acListCurrent++;
			}
		}

		// loop through each result div applying the correct style
		acResultsDiv.find('div.listitem').each(function(i){
			if(this.className != "row"){
				if(i == acListCurrent){
					//acSearchField.val(this.title);
					this.className = "listitem selected";
				} else {
					this.className = "listitem unselected";
				}
			}
		});

		return true;
	} else {
		// reset
		acListCurrent = -1;
		return false;
	}
}