/**
 * 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
 *
 */
//alert(escape('³'));
var acListTotal   =  0;
var acListCurrent = -1;
var acDelay		  = 500;
var acURL		  = null;
var acSearchId	  = null;
var acResultsId	  = null;
var acSearchField = null;
var acResultsDiv  = null;
var search = false;
var firmId = new Array();

function setAutoComplete(field_id, results_id, get_url){

	acSearchId  = "#" + field_id;
	acResultsId = "#" + results_id;
	acURL 		= get_url;

	$("body").append('<div id="' + results_id + '"></div>');

	acSearchField	= $(acSearchId);
	acResultsDiv	= $(acResultsId);

	repositionResultsDiv();

	acSearchField.blur(function(){ setTimeout("clearAutoComplete()", 200) });

	acSearchField.keyup(function (e) {
		var keyCode = e.keyCode || window.event.keyCode;
		var lastVal = acSearchField.val();

		if(updownArrow(keyCode)){
			return;
		}

		if(keyCode == 27){
			clearAutoComplete();
			return;
		}
		if(keyCode == 13){
			acResultsDiv.children().each(function(i){
				if(this.className == "selected"){
					var str = this.innerHTML;
					var a = str.match(/\<strong\>(.*)\<\/strong\>|\<STRONG\>(.*)\<\/STRONG\>/);
					
					$("#autocomplete").attr("action", "firma," + $(this).attr("id") + ".html"); 

					if ( navigator.appVersion.indexOf("MSIE") > -1 ) {
						acSearchField.val(a[2].replace(/&amp;/, "&"));
					}
					else {
						acSearchField.val(a[1].replace(/&amp;/, "&"));
					}
					
					search = true;
					setTimeout('$("#autocomplete").submit()', 200);

					//clearAutoComplete();
				}
			});
			clearAutoComplete();
			return;
		}
		setTimeout(function () {autoComplete(lastVal)}, acDelay);
	});
}

function autoComplete(lastValue) {
	var part = acSearchField.val();

	if(part == ''){
		clearAutoComplete();
		return;
	}
	
	if(lastValue != part){
		return;
	}
	//alert(acURL + part.replace(/&/, "%26"));
	$.getJSON(acURL + part.replace(/&/, "%26"), function(json){
		//alert(json);
		if ( json ) {
			
			var ansLength = acListTotal = json.length; 

			if(ansLength > 0){
	
				var newData = '';

				for(i = 0; i < ansLength; i++) {
					newData += '<div class="unselected" id="' + json[i].id + '"><strong>' + json[i].name + '</strong><br /><span class="desc">' + json[i].desc + '</span></div>';
					firmId[i] = json[i].id;
				}
				
				acResultsDiv.html(newData);
				acResultsDiv.css("display","block");

				var divs = $(acResultsId + " > div");
			
				divs.mouseover( function() {
					divs.each(function(){ this.className = "unselected"; });
					this.className = "selected";
				})

				divs.click( function() {
					var str = this.innerHTML;
					var a = str.match(/\<strong\>(.*)\<\/strong\>|\<STRONG\>(.*)\<\/STRONG\>/);
					//var firmId = str.match(/firm([0-9]+)/);
					//alert($(this).attr("id"));
					
					$("#autocomplete").attr("action", "firma," + $(this).attr("id") + ".html"); 

					if ( navigator.appVersion.indexOf("MSIE") > -1 ) {
						acSearchField.val(a[2].replace(/&amp;/, "&"));
					}
					else {
						acSearchField.val(a[1].replace(/&amp;/, "&"));
					}
					
					search = true;
					setTimeout('$("#autocomplete").submit()', 200);

					clearAutoComplete();
				});
				
	
			} else {
				clearAutoComplete();
			}
		}
		else {
			acResultsDiv.html('<div class="unselected"><strong>Brak wyników</strong></div>');
			acResultsDiv.css("display","block");
		}
	});
}

function clearAutoComplete() {
	acResultsDiv.html('');
	acResultsDiv.css("display","none");
}

function repositionResultsDiv() {
	var sf_pos    = acSearchField.offset();
	var sf_top    = sf_pos.top;
	var sf_left   = sf_pos.left;

	var sf_height = acSearchField.height();
	var sf_width  = 219;

	acResultsDiv.css("position","absolute");
	acResultsDiv.css("left", sf_left - 2);
	acResultsDiv.css("top", sf_top + sf_height + 8);
	acResultsDiv.css("width", sf_width - 2);
}


function updownArrow(keyCode) {
	if(keyCode == 40 || keyCode == 38){

		if(keyCode == 38){
			if(acListCurrent == 0 || acListCurrent == -1){
				acListCurrent = acListTotal-1;
			}else{
				acListCurrent--;
			}
		} 
		if(keyCode == 40) {
			if(acListCurrent == acListTotal-1){
				acListCurrent = 0;
			}else {
				acListCurrent++;
			}
		}

		acResultsDiv.children().each(function(i){
			if(i == acListCurrent){
				//acSearchField.val(this.childNodes[0].nodeValue);
				this.className = "selected";
			} else {
				this.className = "unselected";
			}
		});

		return true;
	} else {
		acListCurrent = -1;
		return false;
	}
}
