function LiveSearchUserNic(field, div) { this.construct(field, div) }
LiveSearchUserNic.prototype = {
  //url:     (window.BASE_SITE || '') + '/dk_improve/live/LiveSearch/load_search.php',
  //url:     '/listForSearch.php',
  url:     (window.BASE_SITE || '') + '/users/listForSearchNic/'+ (window.OUTPUT_VIEW || '') +'layout/toJsHttpRequest',
  field:   null,
  div:     null,
  prevQ:   '',
  prevT:   null,
  timeout: null,

  construct: function(field, div) {
	this.field = field;
    this.div = div;
    this.prevT = new Date();

    var th = this;
    addEvent(field, 'onkeydown', function(e) {
	  th.prevT = new Date();
      if (e.keyCode == 13) th.onchangeControl(this.value, 100);//Enter
      return true;
    })
	/*
    addEvent(field, 'onkeyup', function(e) {
      if (e.keyCode == 13) return true;
      th.onchangeControl(this.value, e.keyCode==32? 1000 : null);
      return true;
    })
    addEvent(field, 'onfocus', function() {
      // stupid Mozilla sometimes loses focus on DIV repainting :-(
      th.focused = true;
      return true;
    })
    addEvent(field, 'onblur', function() {
      th.onchangeControl(field.value, 0);
      th.focused = false;
      return true;
    })
	*/
  },

  onchangeControl: function(text, dt) {
    var t = new Date();
    var wait = 0;
    if (dt == null) dt = 2000;

    if (t.getTime() - this.prevT.getTime() < dt) {
      this.prevT = t;
      wait = dt;
    }

    var th = this;
    if (this.timeout) { clearTimeout(this.timeout); this.timeout=null; }
    this.timeout = setTimeout(function() { th.prevT = t; th.timeout=null; th.onchange(text) }, wait);
  },

  onchange: function(text, force) {
    var q = this.clean(text);
    if (q != this.prevQ && q != "") {
      this.prevQ = q;
	  var th = this;
      var req = new JsHttpRequest();
      req.onreadystatechange = function() {
        if (window.hackerConsole) window.hackerConsole.out(req.responseText, '', 'Shell');
		if (req.readyState != 4) return;
        if (!req.responseJS /*|| 0 == req.responseJS.lenght || !Math.round(req.responseJS[0])*/) {
          th.div.style.display = "none";
          return;
        }
		//alert(req.responseJS.toSource());
        th.div.innerHTML = req.responseJS;//req.responseJS[1];
        if (window.livePreview) {
          var links = th.div.getElementsByTagName('A');
          for (var i=0; i<links.length; i++) {
            window.livePreview.attachLink(links[i]);
          }
        }
        th.div.style.display = "block";
        if (th.focused) th.field.focus();
      }
      req.caching = false;
      req.open('GET', th.url, true);
      req.send({ 'q': q });
    } else {
		//alert(this.toSource());
		if (false != this.focused) {
			this.prevQ = '';
			var th = this;
			//alert(this.toSource());
			//th.div.style.display = "none";
		}
	}
  },

  clean: function(text) {
    var spl = text.split(/[\s~!#&$%^*()\[\]{}:\"<>?`=;\',\/\\|]+/i);
    var words = [];
    for (var i=0; i<spl.length; i++) if (!spl[i].match(/^[a-zа-я_0-9]{0,2}$/))
      words[words.length] = spl[i].toLowerCase();
    return words.join(" ");
  }
};

// Cross-browser addEventListener()/attachEvent() replacement.
function addEvent(elt, name, handler, atEnd) {
  name = name.replace(/^(on)?/, 'on');
  var prev = elt[name];
  var tmp = '__tmp';
  elt[name] = function(e) {
    if (!e) e = window.event;
    var result;
    if (!atEnd) {
      elt[tmp] = handler; result = elt[tmp](e); elt[tmp] = null; // delete() does not work in IE 5.0 (???!!!)
      if (result === false) return result;
    }
    if (prev) {
      elt[tmp] = prev; result = elt[tmp](e); elt[tmp] = null;
    }
    if (atEnd && result !== false) {
      elt[tmp] = handler; result = elt[tmp](e); elt[tmp] = null;
    }
    return result;
  }
  return handler;
}

