RegExp.escape = function(str) 
{
  var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g"); // .*+?|()[]{}\
  return str.replace(specials, "\\$&");
}

function highlightWordsNoCase(line, word)
{
  var regex = new RegExp("(" + RegExp.escape(word) + ")", "gi");
  return line.replace(regex, "<span class='hilitehelpword'>$1</span>");
}


function Search(id,params) {
	
	this.form = undefined;
	if (params != undefined) {
		this.form = params.form;
	}
	this.mouseon = false;
	this.tmshow = 0;
	this.heplimit = 10;
	this.helpcnt = 0;
	this.sercharr = [];
	this.oldsearch = '';	
	this.id = id;
	this.iid = "#"+id;
		
	this.ssearch = function()
	{
		var self = this;
		str = $.trim($(this.iid).val());
		
		if (str!='')
		{
			str = str.replace(/\s+/g," ");
			$.post("/c_search_jq.php",{"foo":"jq_search", "word":str}, function(data) {
				if (data.length==0) {
					self.hidehelpt();
				} else {
					self.heplimit = data.length;
					self.drawsearchdiv(data);
				}
			}, "json");
		}
		else {
			this.hidehelpt();
		}
	}	
	$(this.iid).attr('autocomplete',"off");
	$(this.iid).bind('blur',{obj:this},function(e) {
		if (!e.data.obj.mouseon)
			e.data.obj.hidehelpt();
	});
			
	$(this.iid).bind('keyup',{obj:this},function(e)
	{
	
		code = e.keyCode;

		switch (code) {
			case 38:
				if (e.data.obj.helpcnt == 0)
					e.data.obj.oldsearch = $(e.data.obj.iid).val();
				if (--e.data.obj.helpcnt<0) {
					e.data.obj.helpcnt = e.data.obj.heplimit;
				}
				e.data.obj.drawsearchdiv();
				break;
			case 40:
				if (e.data.obj.helpcnt == 0)
					e.data.obj.oldsearch = $(e.data.obj.iid).val();
			
				if (++e.data.obj.helpcnt>e.data.obj.heplimit) {
					e.data.obj.helpcnt = 0;
				}		
				e.data.obj.drawsearchdiv();
				break;
			default:
				e.data.obj.ssearch();
	
		}
		 
	});

this.drawsearchdiv = function(ar) {
	var self = this;
	if (ar != undefined) {
		sercharr = ar;
		self.helpcnt = 0;
		self.oldsearch='';

		tb = "<table class='htt'>";
		$.each(sercharr,function(a,b) {
			if (a==(self.helpcnt-1))	{
				tr_class = "htb";
				$(self.iid).val(b['str']);
			} else {
				tr_class = "hta";
			}
			tmpost = self.oldsearch==''?$(self.iid).val():self.oldsearch;
			tmpost = $.trim(tmpost);
			str = highlightWordsNoCase(b['str'],tmpost);
			cnt = b['cnt']>0?"<span class='htcntspan'>"+b['cnt']+" товаров</span>":''
			tb += "<tr class="+tr_class+"><td>"+str+"</td><td style='text-align: right'>"+cnt+"</td></tr>";
		});
		tb += "</table>";
		
		if (document.getElementById('searchdiv')==null)
		{
			inputpos = $(self.iid).position();
			$("<div id='searchdiv' class='hlpdiv' style='top: "+(inputpos.top+$(self.iid).outerHeight())+"px;left: "+inputpos.left+"px; width: 450px;'>"+tb+"</div>").appendTo("body");
			//		$("<div id='searchdiv' class='hlpdiv' style='top: "+(inputpos.top+$("#searchinput").outerHeight())+"px;left: "+inputpos.left+"px; width: "+$("#searchinput").width()+"px;'>"+tb+"</div>").appendTo("body");
		}
		else
			$("#searchdiv").html(tb);	

		$(".hta, .htb").mouseover(function(){
			$(".htb").removeClass("htb");
			$(this).addClass("htb").click(function() {
				$(self.iid).val($(".htb > td:eq(0)").text());
				self.hidehelpt();
				if (self.form!=undefined) {
					$('#'+self.form).submit();
				}
								
			})
		});
		$("#searchdiv").mouseover(function(){ self.mouseon = true; }).mouseout(function(){self.mouseon = false;});
		
		
	} else {
		$(".htb").removeClass("htb"); 
		if (self.helpcnt == 0) {
			$(self.iid).val(self.oldsearch);
		} else {
//			$(self.iid).val(".hta:eq("+(self.helpcnt-1)+")");
			$(".hta:eq("+(self.helpcnt-1)+")").addClass("htb");
			$(self.iid).val($(".htb > td:eq(0)").text());
		}
	}
}



this.hidehelpt = function() {
	if($("#searchdiv").html()!=null) 
		$("#searchdiv").remove();
}
}
