// global variables
var acListTotal2   =  0;
var acListCurrent2 = -1;
var acDelay2 = 500;
var acURL2	= null;
var acSearchId2	  = null;
var acResultsId2  = null;
var acSearchField2 = null;
var acResultsDiv2  = null;

function setAutoComplete2(field_id2, results_id2, get_url2)
{

	// initialize vars
	acSearchId2  = "#" + field_id2;
	acResultsId2 = "#" + results_id2;
	acURL2 		= get_url2;

	// create the results div
	$(".auto2").append('<div id="' + results_id2 + '"></div>');

	// register mostly used vars
	acSearchField2	= $(acSearchId2);
	acResultsDiv2	= $(acResultsId2);

	// reposition div
	repositionResultsDiv2();
	
	// on blur listener
	acSearchField2.blur(function(){ setTimeout("clearautoComplete2()", 200) });

	// on key up listener
	acSearchField2.keyup(function (e) {

		// get keyCode (window.event is for IE)
		var keyCode = e.keyCode || window.event.keyCode;
		var lastVal2 = acSearchField2.val();

		// check an treat up and down arrows
		if(updownArrow2(keyCode)){
			return;
		}

		// check for an ENTER or ESC
		if(keyCode == 13 || keyCode == 27){
			clearautoComplete2();
			return;
		}

		// if is text, call with delay
		setTimeout(function () {autoComplete2(lastVal2)}, acDelay2);
	});
}

// treat the auto-complete action (delayed function)
function autoComplete2(lastValue2)
{
	// get the field value
	var part2 = acSearchField2.val();

	// if it's empty clear the resuts box and return
	if(part2 == ''){
		clearautoComplete2();
		return;
	}

	// if it's equal the value from the time of the call, allow
	if(lastValue2 != part2){
		return;
	}

	// get remote data as JSON
	$.getJSON(acURL2 + part2, function(json){

		// get the total of results
		var ansLength2 = acListTotal2 = json.length;

		// if there are results populate the results div
		if(ansLength2 > 0){

			var newData2 = '';

			// create a div for each result
			for(i=0; i < ansLength2; i++) {
				newData2 += '<div class="unselected">' + json[i] + '</div>';
			}

			// update the results div
			acResultsDiv2.html(newData2);
			acResultsDiv2.css("display","block");
			
			// for all divs2 in results
			var divs2 = $(acResultsId2 + " > div");
		
			// on mouse over clean previous selected and set a new one
			divs2.mouseover( function() {
				divs2.each(function(){ this.className = "unselected"; });
				this.className = "selected";
			})
		
			// on click copy the result text to the search field and hide
			divs2.click( function() {
				acSearchField2.val(this.childNodes[0].nodeValue);
				clearautoComplete2();
			});

		} else {
			clearautoComplete2();
		}
	});
}

// clear auto complete box
function clearautoComplete2()
{
	acResultsDiv2.html('');
	acResultsDiv2.css("display","none");
}

// reposition the results div accordingly to the search field
function repositionResultsDiv2()
{
	// get the field position
	var sf_pos2    = acSearchField2.offset();
	var sf_top2    = sf_pos2.top;
	var sf_left2   = sf_pos2.left;

	// get the field size
	var sf_height2 = acSearchField2.height();
	var sf_width2  = acSearchField2.width();

	// apply the css styles - optimized for Firefox
//	acResultsDiv2.css("position","absolute");
//	acResultsDiv2.css("left", sf_left2 - 2);
//	acResultsDiv2.css("top", sf_top2 + sf_height2 + 5);
//	acResultsDiv2.css("width", sf_width2 - 2);
}


// treat up and down key strokes defining the next selected element
function updownArrow2(keyCode) {
	if(keyCode == 40 || keyCode == 38){

		if(keyCode == 38){ // keyUp
			if(acListCurrent2 == 0 || acListCurrent2 == -1){
				acListCurrent2 = acListTotal2-1;
			}else{
				acListCurrent2--;
			}
		} else { // keyDown
			if(acListCurrent2 == acListTotal2-1){
				acListCurrent2 = 0;
			}else {
				acListCurrent2++;
			}
		}

		// loop through each result div applying the correct style
		acResultsDiv2.children().each(function(i){
			if(i == acListCurrent2){
				acSearchField2.val(this.childNodes[0].nodeValue);
				this.className = "selected";
			} else {
				this.className = "unselected";
			}
		});

		return true;
	} else {
		// reset
		acListCurrent2 = -1;
		return false;
	}
}

