// staticka trida SuggesterInfo - obsahuje pomocne promenne pri praci s mnozinou naseptavacu
function SuggesterInfo() {}
SuggesterInfo.activeItem;	// aktivni naseptavac


// trida SuggesterItem - zobrazovana polozka naseptavace
function SuggesterItem(id, value) {
	this.id = id;		// id polozky
	this.value = value;		// zobrazena hodnota
}


// trida Suggester - naseptavac
function Suggester(id, textBoxId, suggesterUrl) {
	// verejne vlastnosti
	this.id = id;		// id naseptavace
	this.items = null;	// pole vsech zobrazovanych polozek (As SuggesterItem())
	this.suggesterUrl = suggesterUrl;	// url serverove stranky poskytujici data
	this.textBox = document.getElementById(textBoxId);	// textoveho pole, nad kterym bude zapnut naseptavac
	this.ignoreDataLoad = false;	// ignoruj nahravani dat do naseptavace (nastavuje se pri vyberu polozky z menu pomoci klavesy 1..9)
	if (!this.textBox) {return;}
	// privatni vlastnosti
	this._loader = document.getElementById('SuggesterLoader');	// iframe pro nahravani dat
	this._loadTextBoxValue = this.textBox.value;	// hodnota texboxu pri zobrazeni formulare
	// verejne udalosti
	this.onchange = null;		// nastava pri zmene hodnoty textoveho pole
	
	// nastav odchytavani udalosti na textovem poli
	var me = this;
	this.textBox.onkeydown = function(e) {me._textBoxKeyDown(e);}
	this.textBox.onkeyup = function(e) {me._textBoxKeyUp(e);}
	this.textBox.onblur = function(e) {me._textBoxOnBlur(e);}
	this.textBox.onfocus = function(e) {me._textBoxOnFocus(e);}
}

// zobrazeni/skryti naseptavace podle obsahu promenne this.items
Suggester.prototype.show = function() {
	ShowMenuSuggester(this);
}

// nacteni dat z dane adresy podle obsahu textboxu
Suggester.prototype._loadData = function() {
	var value = this.textBox.value;
	var trimValue = value.replace(/ /g, '');
	if (trimValue.length == 0) {
		// skryti naseptavace
		this._hide();
	} else {
		// nacteni dat
		//alert(value + ':' + escape(value));
		this._loader.src = this.suggesterUrl.replace(/\{0\}/g, escape(value));
	}
}

// skryti naseptavace
Suggester.prototype._hide = function() {
	this.items = null;
	this.show();
}

// stisk klavesy na textovem poli
Suggester.prototype._textBoxKeyDown = function(e) {
	this.ignoreDataLoad = false;
	var key = Helper.UI.getKey(e);
	// stisk klavesy enter
	if (key == 13) {
		if (this.onchange && (this.textBox.value != this._loadTextBoxValue)) {
			this.onchange(this.textBox.value);
		}
	}
	// stisk escape
	if (key == 27) {this._hide();}
}

// stisk klavesy na textovem poli
Suggester.prototype._textBoxKeyUp = function(e) {
	if (this.ignoreDataLoad) {return;}
	if (! Helper.UI) {return;}
	var key = Helper.UI.getKey(e);
	if ((key >= 48 && key <= 57) ||(key >= 96 && key <= 122) || (key >= 65 && key <= 90) || (key == 32) || (key == 8) || (key == 46)) { 
		var me = this;
		if (this._loadDataTimer) {clearTimeout(this._loadDataTimer);}
		this._loadDataTimer = setTimeout(function() {me._loadData();}, 300);
	}
}

// ztraceni aktivity u textboxu
Suggester.prototype._textBoxOnBlur = function(e) {
	var me = this;
	if (this.onchange && (this.textBox.value != this._loadTextBoxValue)) {
		// udalost generuj se zpozdenim, aby se mohli nejdrive zpracovat jine udalosti
		var me = this;
		setTimeout(function() {me.onchange(me.textBox.value); SuggesterInfo.activeItem = null;}, 100)
	}
}

// ziskani aktivity u textboxu
Suggester.prototype._textBoxOnFocus = function(e) {
	// udalost generuj se zpozdenim, aby se mohla drive zpracovat udalost _textBoxOnBlur predchoziho naseptavace
	var me = this;
	setTimeout(function() {SuggesterInfo.activeItem = me; me._hide();}, 100)
}
