
var callOnceCounter = 0;

function callOnceWithTimeout(inputName, func, timeout) {
//	input = $(inputName);
	setTimeout('_wakeOnceCall("'+inputName+'",'+(++	callOnceCounter)+',"'+func+'")',timeout);
}

function _wakeOnceCall(inputName, mycounter,func) {
//	input = $(inputName);	
	if (mycounter == callOnceCounter) {
		eval(func);
	}
}

var ac_position = -99;

/*
	obsluha enteru na vyberu
*/
function autoCompleteSelectLine(inputName) {
	ajaxDiv = document.getElementById('ajax_' + inputName);		
	var selected = $(ajaxDiv).find('div.selected a');
	if (selected.size() > 0) {
		selected.click();
		return;
	}		
	//v pripade "nomatch"
	deleteContextMenuElement();	
}

function autoCompleteHighlihtLine(inputName) {
	ajaxDiv = document.getElementById('ajax_' + inputName);				
	if (ac_position < 0) {
		ac_position = 0;
	}		
		
	var lines = $(ajaxDiv).find('div a').parent();
	lines.removeClass('selected');
	if (lines.size() <= ac_position) {
		ac_position = lines.size() - 1;
	}
	lines.eq(ac_position).addClass('selected');		
}

function autoCompleteReset(inputName) {	
	textField = document.getElementById('autocomplete_' + inputName);
	if ($(textField).is('.ajaxInputFieldSelected') && textField.valueLast != textField.value) {
		$(textField).removeClass('ajaxInputFieldSelected');	
		textField.value= '';
		input = document.getElementById(inputName);
		input.value = '';
	}	
	return true;
}


function autoCompleteTryAjax(base, inputName,tClass,method) {	
	textField = document.getElementById('autocomplete_' + inputName);	
	if (textField.ignoreAjax == true) {
		textField.ignoreAjax = false;
		return false;
	}
	if ($(textField).is('.ajaxInputFieldSelected')) {
		return false;
	}
	textField.valueLast = textField.value;
	callOnceWithTimeout(inputName,"autoComplete('"+base+"','"+inputName+"','"+tClass+"','"+method+"')",400);			
	return true;
}


/*
handlovani key eventu na textovem poli 
*/
function autoCompleteObserver(base, inputName,tClass,method, ev) {	
	if (!ev) var ev = window.event
	if (ev.keyCode) var keyCode = ev.keyCode;
	else if (ev.which) var keyCode = ev.which;	
	
	if (keyCode == 40) {	//ARROW DOWN		
		if (ac_position != -99) {
			ac_position++;
			autoCompleteHighlihtLine(inputName);						
		} else {				
			autoComplete(base, inputName,tClass,method);			
		}
		return false;
	}
	if (keyCode == 38) {	//ARROW UP
		ac_position--;
		autoCompleteHighlihtLine(inputName);
		return false;
	}
	if (keyCode == 13) {	//ENTER		
		ev.cancelBubble = true;	//IE fix
		autoCompleteSelectLine(inputName);						
		return false;
	}	
	if (keyCode == 9) {	//TAB
		//alert(textField.value.length + ' | ' + ac_position);
		textField = document.getElementById('autocomplete_' + inputName);
		ajaxDiv = document.getElementById('ajax_' + inputName);		
		//alert(ajaxDiv.childNodes.length + '|' + ajaxDiv.childNodes[0].nodeName + '|' + ajaxDiv.childNodes[0].childNodes.length);
		if (textField.value.length > 0 && ac_position != -99) {
			autoCompleteSelectLine(inputName);				
		} else {
			deleteContextMenuElement();						
		}	
		return true;
	}
	if (keyCode == 27) {	//ESC	
		deleteContextMenuElement();
		return false;
	}	
	autoCompleteTryAjax(base, inputName,tClass,method);
	return true;
}


/*
Zobrazeni DIVu s vyberem moznotsti pro autocomplete
*/
function autoCompleteShow(inputName) {			
	ajaxDiv = document.getElementById('ajax_' + inputName);		
	newContextMenuElement(ajaxDiv,'autoCompleteHide(\''+inputName+'\');',0);										
	ac_position = 0;		
	$(ajaxDiv).show().parent().css('position','relative');
}

/*
Skriti DIVu s vyberem moznotsti pro autocomplete
*/
function autoCompleteHide(inputName) {
	ajaxDiv = document.getElementById('ajax_' + inputName);		
	$(ajaxDiv).hide().parent().css('position','static');
	ac_position = -99;
}

/*
Zavolani AJAX obsluhy.
*/


function autoComplete(handler, inputName, tClass, method) {

	var fvalue = encodeURIComponent(document.getElementById('autocomplete_' + inputName).value);
	
	//if (fvalue != '') {	
		
		var pars = 'tclass=' + tClass + '&value=' + fvalue + '&field=' + inputName + '&method=' + method;

		if (method=='condition' || method.substring(0,9)=='condition'){pars =pars+'&formValues='+ acGetSerializedStringWithTruncatedValues($("form")).replace(/&/g,"|");}		
	
		//small trick due to : in id
		var target = document.getElementById('ajax_' + inputName);		
		$(target).load(handler+"?"+pars,{},function() {
			$('.ajaxDiv .line a').bind('click',function(event) { var params = this.getAttribute('params').split("|",4); return autoCompleteChooseItem(params[1],params[2],params[3]); });
		});										
		autoCompleteShow(inputName)								
	//}
}

function acGetSerializedStringWithTruncatedValues(form){
	  var vals = form.formToArray(false);
	  for(var i=0;i<vals.length;i++){
	  	if(vals[i].value.length > 50){
	  		vals[i].value = vals[i].value.substring(0,49);
	  	}
	  	//valsConv.push(vals[i]);
	  }
	  return $.param(vals);
	
}


function autoCompleteIsMutli(input) {
	return $(input).is('.ajaxMultiValueField');
}


function autoCompleteRemoveItem(inputName, id) {	
	var input = document.getElementById(inputName);	
	newVal = '';
	arr = input.value.split(',');
	for(i=0;i<arr.length;i++){		
		if (arr[i] != '' && arr[i] != id) {
			if (newVal != '') {
				newVal = newVal + ',' + arr[i];	
			} else {
				newVal = arr[i];	
			}
		}
	}
	input.value = newVal;
	var line = document.getElementById('acSelected_'+inputName+'_'+id);
	$(line).remove();
	return false;
}

/*
Volano po vybrani nejake polozky
*/
function autoCompleteChooseItem(inputName, id, name) {			
	input = document.getElementById(inputName);
	if (autoCompleteIsMutli(input)) {
		if ($(input).is('.singleMode') && input.value.length > 0) {
			var removeId = input.value.charAt(0) == ',' ? input.value.substring(1) : input.value;			
			autoCompleteRemoveItem(inputName, removeId);			
		}				
		if (! arrayKeyExists(id,input.value.split(','))) {
			if (input.value == '') {
				input.value = id;				
			} else {
				input.value = input.value  + ',' +id;				
			}			
			//kvuli : v #
			$(document.getElementById('ajaxMulti_' + inputName)).append('<div id="acSelected_'+inputName+'_'+id+'" class="line"><span class="autoCompleteLeft">'+name+'</span><span class="autoCompleteRight"><a href="#" title="Odebrat" onClick="return autoCompleteRemoveItem(\''+ inputName + '\',\''+id+'\');"><img src="'+base_static_dir+'images/admin2/icons/16/delete.gif" height="16" weight="16"/></a></span></div><div class="autoCompleteClearer"></div>');			
		}		
		deleteContextMenuElement();
		textField = document.getElementById('autocomplete_' + inputName);
		textField.value = '';
		textField.valueLast = '';						
	} else {
		input.value = id;		
		deleteContextMenuElement();
		textField = document.getElementById('autocomplete_' + inputName);
		$(textField).addClass('ajaxInputFieldSelected');	
		textField.value = name;
		textField.valueLast = name;	
	}
	textField.ignoreAjax = true;
	textField.focus();
	return false;
}

function arrayKeyExists(item,arr) {		
	for(i=0;i<arr.length;i++){
		if (arr[i] == item) return true;
	}
	return false;
}

/*function autoCompleteBlur(inputName) {	
	//callOnceWithTimeout(inputName,"autoCompleteHide('"+inputName+"')",200);	
	callOnceWithTimeout(inputName,"deleteContextMenuElement()",200);	
}*/
