var path = "http://"+document.domain+"/";

function SAddEvent(obj,event_t,event_f)
{
	if(typeof window.addEventListener != "undefined")
	{
		if(obj)
			obj.addEventListener(event_t, event_f, false );
		else
			window.addEventListener(event_t, event_f, false );
		return 1;
	}

	if(typeof window.attachEvent != "undefined" )
	{
		if(obj)
			obj.attachEvent("on"+event_t, event_f);
		else
			if(document.attachEvent("on"+event_t, event_f))
				window.attachEvent("on"+event_t, event_f)
		return 1;
	}
	return 0;
}

var tsug = function ()
{
	var Ts = function()
	{
		var _this = this;
		var oq, sc, he, s3, f, to, h = null;
		_this.oq = new Array();
		_this.h = false;
		SAddEvent(null,"keyup",function(e){ _this.a(e); });
	};
	
	Ts.prototype.a = function(e)
	{
		
		var _this = this;
		var el;
		if(!_this.s3)
		{
			_this.s3 = getelement("s3");
			_this.s3.onmouseout = function(){_this.h = true; _this.to = window.setTimeout(function(){_this.d();}, 1000, true);};
		}
		el = e.target ? el = e.target : e.srcElement;
		if(el.tagName == "INPUT" && el.name == "phrase" && el.value.length > 0)
		{
			if(_this.oq[el.value]) { _this.c(_this.oq[el.value]); return; }
	
			if(_this.f == null)
				_this.f = document.getElementsByTagName("FORM")['search'];
				
			if(_this.he == null)
			{
				_this.he = document.getElementsByTagName("HEAD")[0];
			}
			if(_this.sc != null)
			{
				_this.he.removeChild(_this.sc);
			}
			var jc=document.createElement("SCRIPT");
		    jc.setAttribute("type", "text/javascript");
		    jc.setAttribute("charset", "ISO-8859-2");
		    jc.setAttribute("src", path+"sugestion/index.php?q=" + el.value);
			_this.he.appendChild(jc);
			_this.sc = jc;
		}
	};
	
	Ts.prototype.b = function(xv)
	{
		var _this = this;
		_this.oq[xv[0]] = xv[1];
		_this.c(_this.oq[xv[0]]);
	};
	
	Ts.prototype.c = function(xv)
	{
		var _this = this;
		var c = _this.s3.childNodes;
		var i = c.length;
		_this.s3.style.visibility = "";
		
		for(var x=0; x<i; x++)
		{
			_this.s3.removeChild(c[0]);
		}
		if(xv.length > 0)
		{
			for(var x=0; x<xv.length; x++)
			{
				var ds=document.createElement("div");
				ds.innerHTML = xv[x][0];
				ds.style.width = "100%";
				ds.onclick = function(e){_this.mca(e,this);};
				ds.onmouseover = function(e){_this.mcb(e,this);};
				ds.onmouseout = function(e){_this.mcc(e,this);};
				ds.style.cursor = "pointer";
				_this.s3.appendChild(ds);
			}
		}
		else
		{
			_this.s3.style.visibility = "hidden";
		}
		
	};
	
	Ts.prototype.d = function()
	{
		var _this = this;
		if(_this.h)
			_this.s3.style.visibility = "hidden";
	};
	
	Ts.prototype.mca = function(e, o)
	{
		var t = this;
		t.f.osearch.value = t.f.phrase.value;
		t.f.phrase.value = o.innerHTML;
		t.f.submit();
	};
	
	Ts.prototype.mcb = function(e, o)
	{
		var t = this;
		t.h = false;
		t.si();
		o.style.backgroundColor = "#eee";
	};
	
	Ts.prototype.mcc = function(e, o)
	{
		var t = this;
		t.h = true;
		o.style.backgroundColor = "#fff";
	};
	
	Ts.prototype.si = function()
	{
		var t = this;
		window.clearTimeout(t.to);
	};
	
	window.sugs = new Ts();
	
};
tsug();