var bPageLoaded = false;

var cache = {};

function hideAutoComplete() {
	Element.hide('acsearch');
	cache = {};
}

function popUpAutoComplete(ctl) {
	var	leftpos = 0;
	var	toppos  = 0;

	ctl = $(ctl);
	if (bPageLoaded) {
		if ($('acsearch')) {
			aTag = ctl;

			leftpos = Position.cumulativeOffset(ctl)[0]; 
			toppos = Position.cumulativeOffset(ctl)[1]; 

			$("acsearch").style.left = leftpos + 11 + 'px'; //dla 11 FF
			$("acsearch").style.top  = toppos + 29 + 'px';

			Element.show("acsearch");
		}
	}
	else {
		bPageLoaded = true;
		popUpAutoComplete(ctl);
	}
  	new AC(ctl);	
}

function handleKeyUp(event, id) {
	var splitid = id.split('-');
	var str = $F(id);

	if (str.length > 3) {
		acsearch(id);
	}
	else {
		if ($(splitid[1] + '/acsrc')) {
			hideAutoComplete();
		}
	}
}

function acsearch(id) {
	if ($('header') && !($('acsearch'))) {
		new Insertion.Before('header', "<div id='acsearch' class='div-acsearch'><div id='acsearch-content'></div></div>");
	}

	var splitid = id.split('-');
	var search  = $F(id).toLowerCase();

	if (this.cache[search]) {
		popUpAutoComplete(id);
		$('acsearch').innerHTML = this.cache[search];
	}
	else {
		if (search.length > 1) {
			$('acsearch-content').innerHTML = '<a title="Kasuj wyszukiwanie" id="close" class="expand-close" onclick="hideAutoComplete();"><img src="'+ImgRoot+'"spacer.gif"/></a> <div class="acscore" id="'+splitid[0]+'/count"></div><div id="'+splitid[0]+'/acsrc" class="expand-src"></div>';

			popUpAutoComplete(id);
//			sru('0/global/autocomplete/' + splitid[0] + '/' + splitid[1], search);

			var uid    = '0/global/autocomplete/' + splitid[0] + '/' + splitid[1];
			var status = 0;
			var req    = new Ajax.Request(AppAjaxPath + uid, {
				method: 'post',
				parameters: 'eval=' + search,
				onSuccess: function(req) {
					status = 1;
					sruComplete(req);
				},
				onCreate: function() {
					Element.addClassName(id, 'activ');
				},
				onComplete: function() {
					Element.removeClassName(id, 'activ');
					if (status == 0) DisplayMessage('Wystąpił błąd: ' + error, 'crit');
				}
			});

			$(splitid[0] + '/acsrc').style.display = 'block';
			$(splitid[0] + '/count').style.display = 'block';
		}
	}
}

function ACsearchSave(search_str) {
	this.cache[search_str] = $('acsearch').innerHTML;
}

function AC(input) {
	var ac = this;
	this.input = input;
	this.input.onkeydown = function (event) { return ac.onkeydown(this, event); };
	this.input.onkeyup = function (event) { ac.onkeyup(this, event) };
	this.popup = $('acsearch');
	this.popup.id = 'acsearch';
}

AC.prototype.hidePopup = function (keycode) {
	hideAutoComplete();
	this.selected = false;
}

AC.prototype.selectDown = function () {
	if (this.selected && this.selected.nextSibling) {
		this.highlight(this.selected.nextSibling);
	}
	else {
		var lis = this.popup.getElementsByTagName('li');
		if (lis.length > 0) {
			this.highlight(lis[0]);
		}
	}
}

AC.prototype.highlight = function (node) {
	if (this.selected) {
		Element.removeClassName(this.selected, 'selected');
	}
	Element.addClassName(node, 'selected');
	this.selected = node;
}

AC.prototype.selectUp = function () {
	if (this.selected && this.selected.previousSibling) {
		this.highlight(this.selected.previousSibling);
	}
}

AC.prototype.onkeyup = function (input, e) {
	if (!e) {
		e = window.event;
	}
	switch (e.keyCode) {
		case 16: // shift
		case 17: // ctrl
		case 18: // alt
		case 20: // caps lock
		case 33: // page up
		case 34: // page down
		case 35: // end
		case 36: // home
		case 37: // left arrow
		case 38: // up arrow
		case 39: // right arrow
		case 40: // down arrow
			return true;

		case 9:  // tab
		case 13: //
			Event.stopObserving(document, 'keypress', obs);
		case 27: // esc
			Event.stopObserving(document, 'keypress', obs);
			hideAutoComplete();
			return true;

		default:
			if (input.value.length > 1)
				acsearch(this.input.id);
			else
				this.hidePopup(e.keyCode);
			return true;
	}
}

AC.prototype.onkeydown = function (input, e) {
	if (!e) {
		e = window.event;
	}
	switch (e.keyCode) {
		case 13: 
			this.selected.onclick();	
			Event.stopObserving(document, 'keypress', obs);
			return false;
		case 40: 
			this.selectDown();
			return false;
		case 38:
			this.selectUp();
			return false;
		default: 
			return true;
	}
}
