var $ = function(elmId) { return document.getElementById(elmId); }

// property uniqueID pro vsechny prohlizece ================= //
function uId(elm)                                             //
{                                                             //
	if(!elm.uniqueID)                                          //
	{                                                          //
	   var date = new Date();                                  //
	   elm.uniqueID = 'E' + date.getTime()                     //
	}                                                          //
																				  //
	return elm.uniqueID;                                       //
}                                                             //
// ========================================================== //

// Třída pro našeptávač 1.2

// v1.1 - přidáno vybrání na kliknutí, hover efekt při najetí myši
// v1.2 - přidán parametr minChars pro nastavení minimálního počtu znaků

function newSuggest(elmId, url)
{
	var Obj = {
	   'url' 			: url,
	   'elmId' 			: elmId,
	   'minChars' 		: 1,
		'activeIdx' 	: -1,
		'cancleSubmit' : false,
		'selfName' 		: '',
		'notShow' 		: false,
		'Input' 			: document.getElementById(elmId),
		'Box' 			: document.getElementById(elmId+'SuggestBox'),


		'getKey' : function(e)
		{
			var code;

			if(!e)
				var e = window.event;
			if(e.keyCode)
				var code = e.keyCode;
			else if(e.which)
				var code = e.which;

			return code;
		},

		'get' : function(e)
		{
		   if(!xmlHttp)
		      return;

			var code = this.getKey(e);

			if(code != 40 && code != 38 && code != 37 && code != 39 && code != 13 && this.Input.value.length >= this.minChars)
			{
	// 				alert(this.url + '&suggest=' + this.selfName + '&text=' + encodeURIComponent(this.Input.value));
	// 				return false;
				Ajax.sendRequest(this.url + '&suggest=' + this.selfName + '&text=' + encodeURIComponent(this.Input.value));
				this.activeIdx = -1;
			}
		},

		'test' : function(e)
		{
			var code = this.getKey(e);
			var List = document.getElementById(this.elmId + 'SuggestList');

			if(code == 40 && typeof(List) == 'object' && List != null)
			{
				if(this.activeIdx < (List.childNodes.length-1))
				{
					if(this.activeIdx > -1 && List.childNodes[this.activeIdx])
						List.childNodes[this.activeIdx].className = '';

					this.activeIdx++;
					List.childNodes[this.activeIdx].className = 'active';
				}
			}
			else if(code == 38 && typeof(List) == 'object' && List != null)
			{
				if(this.activeIdx > -1)
				{
					List.childNodes[this.activeIdx].className = '';

					this.activeIdx--;
					if(this.activeIdx > -1)
					{
						List.childNodes[this.activeIdx].className = 'active';
					}
				}
			}
			else if(code == 13)
			{
				this.cancleSubmit = true;
			}
		},

		'set' : function(data)
		{
			this.activeIdx = -1;
			this.Box.innerHTML = '';

			if(data.length)
			{
			   if(!this.notShow)
			   {
					this.Box.style.display = 'block';
				}
				else
				{
					this.notShow = false;
				}

				var HTML = '';
				var itemText = '';
				var srch = new RegExp('(' + this.Input.value + ')', 'gi');

				HTML += '<ul id="'+this.elmId+'SuggestList">';

				for(id in data)
				{
					var mouseEvents = '';
					if(this.selfName.length)
					{
					   mouseEvents += ' onmouseover="'+this.selfName+'.setHover(this)" ';
						mouseEvents += 'onmouseout="'+this.selfName+'.resetHover(this)" ';
						mouseEvents += 'onclick="'+this.selfName+'.submit()" ';
						mouseEvents += 'id="'+data[id].url+'" ';
					}

					itemText = data[id].name;
					itemText = itemText.replace(srch, '<u>$1</u>');

					HTML += '<li'+mouseEvents+'>'+itemText+'</li>';
				}
				HTML += '</ul>';

				this.Box.innerHTML = HTML;
			}
			else
			{
				this.Box.style.display = 'none';
			}
		},

		'submit' : function()
		{
			var List = document.getElementById(this.elmId+'SuggestList');

			if(List)
			{
				if(List.childNodes.length && this.activeIdx > -1 && List.childNodes[this.activeIdx])
				{
					this.Input.value = List.childNodes[this.activeIdx].innerHTML.replace(/<\/?u>/gi, '');
										
// 					if(List.childNodes[this.activeIdx].id != 'undefined')
// 					{
// 						window.location.href = List.childNodes[this.activeIdx].id;
// 						return false;
// 					}

					if(xmlHttp)
					{
					   Ajax.sendRequest(this.url + '&suggest=' + this.selfName + '&text=' + encodeURIComponent(this.Input.value));
					   this.notShow = true;
	  				}
				}
	 		}

			if(this.cancleSubmit)
			{
				this.Box.style.display = 'none';
				this.Input.blur();
				this.cancleSubmit = false;
				
				this.Input.form.submit();

				return false;
			}
			else
			{
				this.Input.form.submit();
				return true;
			}
		},

		'setHover' : function(elm)
		{
		   elm.className = 'active';
 			this.activeIdx = this.getIdx(elm);
		},

		'resetHover' : function(elm)
		{
			elm.className = '';
			this.activeIdx = this.getIdx(elm);
 			this.activeIdx = null;
		},

		'getIdx' : function(elm)
		{
			var List = document.getElementById(this.elmId+'SuggestList');
			var i;
			
			for(i = 0; i < List.childNodes.length; i++)
			{
			   if(uId(List.childNodes[i]) == uId(elm))
			      return i;
			}
			
			return -1;
		},

		'show' : function()
		{
			var List = document.getElementById(this.elmId+'SuggestList');

			if(List)
			{
				this.Box.style.display = 'block';
			}
		},

		'hide' : function()
		{
			var List = document.getElementById(this.elmId+'SuggestList');

			if(typeof(List) == 'object')
			{
				if(List && this.activeIdx > -1)
				{
					this.Input.value = List.childNodes[this.activeIdx].innerHTML;
					this.submit();
	 			}
	 		}

			this.Box.style.display = 'none';

			if(!this.Input.value.length)
				this.set([]);
		},

		'hide2' : function()
		{
			this.Box.style.display = 'none';
		}
	};
	
	return Obj;
}

