/**
 * @author Mizerski Sławomir
 */

String.prototype.trim = function() {
   return this.replace(/^\s+|\s+$/g, '');
}

var cursorX = 0;
var cursorY = 0;


function AC()
{
  var q;
	var req_param;
	var p;
	var prev;
	var last;
  var q_obj;
	var div;
	var req_url;
	var kraj;

  this.Init = function(q_id,div_id,req_url,req_param)
	{
    this.q_obj = mint.$D(q_id);
    this.div = mint.$D(div_id);
	  this.req_url = req_url;
		this.req_param = req_param;
    document.onmousemove = this.getMouseXY;
    this.div.style.left = this.q_obj.style.left;
	};

  this.setKraj = function(_kraj)
  {
    this.kraj = _kraj;
  };

	this.ac_click = function(i)
	{
		this.q_obj.value = mint.$D(this.req_param+'_td_' + i).innerHTML;
		this.div.style.display = 'none';
	};

  this.ac_close = function()
	{
		this.div.style.display = 'none';
	};

	var tempX = 0;
	var tempY = 0;

	this.getMouseXY = function(e)
	{
		var x = 0;
		var y = 0;

	  var IE = document.all ? true : false;

	  if(!IE)
		  document.captureEvents(Event.MOUSEMOVE);

		if(IE) {
			x = event.clientX + document.body.scrollLeft;
			y = event.clientY + document.body.scrollTop;
		}
		else
		{
			x = e.pageX;
			y = e.pageY;
		}

		if (x < 0) {
		  x = 0;
		}
		if (y < 0) {
			y = 0;
		}

    cursorX = x;
		cursorY = y;

		return true
	};

	this.ac_blur = function()
	{
		if (document.elementFromPoint(cursorX, cursorY).id.match(this.req_param+'_td_') != this.req_param+'_td_')
			this.div.style.display = 'none';
	};

	this.ac_keyup = function(obj, e)
	{
		var keynum;
		if (window.event) // IE
		{
			keynum = e.keyCode;
		}
		else
			if (e.which) // Netscape/Firefox/Opera
			{
				keynum = e.which;
			}

		if (keynum == 38 || keynum == 40 || keynum == 37 || keynum == 39 || keynum == 13)
			return;

		var req = mint.Request();
		req.encoding = "iso-8859-2";
		req.AddParam(this.req_param, obj.value);
		if(this.kraj!=undefined)
		req.AddParam("kraj",this.kraj);

    var that = this;

		req.OnSuccess = function(){
			that.p = 0;
			var dane = this.responseText.split('|');
			that.div.style.display = 'none';
		  that.last = parseInt(dane[0]);
			that.div.innerHTML = dane[1].trim();
			if (dane != '')
				that.div.style.display = 'block';
		};
		req.Send(this.req_url);
		return true;
	};

	this.ac_highlight = function(o, i)
	{
		if (this.prev != undefined) {
			this.prev.style.background = 'white';
			this.prev.style.color = 'black';
		}
		if (o != undefined) {
			o.style.background = 'silver';
			o.style.color = 'white';
			this.p = i;
			this.prev = o;
		}
	};

	this.ac_keydown = function(e)
	{
		var keynum;
		if (window.event) // IE
		{
			keynum = e.keyCode;
		}
		else
			if (e.which) // Netscape/Firefox/Opera
			{
				keynum = e.which;
			}
		if (keynum == 38) // up
		{
			if (this.p == 0) {
	  	  this.q = this.q_obj.value;
	    }
			this.prev = mint.$D(this.req_param+'_tr_'+this.p);
			this.p--;

			if (this.p < 0) {
	  	  this.p = this.last;
	    }
		}
		else
			if (keynum == 40) //down
			{
				if (this.p == 0)
					this.q = this.q_obj.value;
				this.prev = mint.$D(this.req_param+'_tr_'+this.p);
				this.p++;
			}
			else
				if (keynum == 13) {
					this.div.style.display = 'none';
				}

		if (keynum == 38 || keynum == 40) {

			var o = mint.$D(this.req_param+'_tr_'+this.p);

			this.ac_highlight(o, this.p);

			if (o != undefined) {
				this.q_obj.value = mint.$D(this.req_param+'_td_'+this.p).innerHTML;
			}
			else {
				this.p = 0;
				this.q_obj.value = this.q;
			}
			return false;
		}
		return true;
	};

};

