function smartBoxInit(){
	Zhishi.oldSearchText ='';
	Zhishi.searchOldTime = -1;
	Zhishi.Event.attachEventListener(document, 'click', hideSb);
	Zhishi.g_msg = [];
	Zhishi.jsAutoInstance = new jsAuto("Zhishi.jsAutoInstance", "divc", "searchText");
	Zhishi.Event.attachEventListener(window, 'resize', moveSb);
}
function hideSb() {
	Zhishi.jsAutoInstance._o.style.visibility = "hidden";
//	Zhishi.jsAutoInstance._o.style.height = "2px";
	Zhishi.jsAutoInstance._f = false;
}
function onSuggest(data) {
	if (data.length <= 1) Zhishi.g_msg = [];
	 else {
		Zhishi.g_msg = data.replace(/(^\s*)|(\s*$)/g, "").split('\n');
		if (Zhishi.Browser.isFirefox)
			Zhishi.g_msg[Zhishi.g_msg.length - 1] = Zhishi.g_msg[Zhishi.g_msg.length - 1].replace(/\0/g, "");
	}
	try{Zhishi.jsAutoInstance.handleEvent(get("searchText").value.trim(), 'searchText', g_event);}catch(e){}
}
function userControl(fValue, fID, event) {
	var event = event ? event : window.event;
	g_event = event.keyCode ? event.keyCode : event.which;
	if (g_event != 40 && g_event != 38) return;
	Zhishi.jsAutoInstance.directionKey();
}
function userInput(fValue, fID, event) {
	var newTime = (new Date()).getTime();
	if (fValue.replace(/\s/g, "") == "") {
		Zhishi.jsAutoInstance.handleEvent("", 0, g_event);
		return;
	}
	if(Zhishi.searchOldTime==-1 || (newTime - Zhishi.searchOldTime > 200 && (g_event>40 || g_event<37))){
		var event = event ? event : window.event;
		g_event = event.keyCode ? event.keyCode : event.which;
		Zhishi.searchOldTime = (new Date()).getTime();
		if (fValue.replace(/\s/g, "") == "") {
			Zhishi.jsAutoInstance.handleEvent("", 0, g_event);
			return;
		}
		if(g_event != 40 && g_event !=38 && g_event !=27) Zhishi.oldSearchText = get("searchText").value.trim();
		var targetUrl = "/SmartBox.e?sp=S" + encodeURIComponent(Zhishi.oldSearchText);
		Zhishi.Ajax.sendRequest('GET',targetUrl,{'onSuccess':onSuggest,'cacheTime':600});
	}
}
function jsAuto(instanceName, objID, fID) {
	this._msg = [];
	this._x = null;
	this._o = get(objID);
	if (!this._o)
		return;
	this._f = false;
	this._i = instanceName;
	this._r = get(fID);
	this._c = -1;
	this._v = null;
	this._e = 0;
	return this;
};
jsAuto.prototype.directionKey = function() {
	with (this) {
		if(_o.style.visibility == "hidden" && get('searchText').value!=''){
//			_i.showBox();
			showBox();
			if(document.sf.searchText.value!=Zhishi.oldSearchText){
				for(var i=0;i<Zhishi.g_msg.length;i++){
					var text = Zhishi.g_msg[i].split(',')[1];
					if(text==document.sf.searchText.value){
						_c = i;
						document.sf.searchText.value = _x[_c].replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&');
						_o.childNodes[_c].className = "sb_mouseover";
						document.sf.searchText.focus();
					}
				}
			}
		}
		else{
			
			if (_o.childNodes.length < 1) return;
			var e = g_event;
			var l = _o.childNodes.length - 1;
			if (e == 40) {
				
				if (_c >= 0 || _c == l) _o.childNodes[_c].className = "sb_mouseout";
				_c++;
				if (_c >= 0 && _c<l+1) {
					_o.childNodes[_c].className = "sb_mouseover";
					document.sf.searchText.value = _x[_c].replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&');
				}
				else if(_c == l+1){
					_c = -1;
					document.sf.searchText.value = Zhishi.oldSearchText;
				}
			} else if (e == 38) {
				if(_c==-1) _c = _o.childNodes.length;
				if (_c < l || _c ==l) _o.childNodes[_c].className = "sb_mouseout";
				_c--;
				if (_c >= 0) {
					_o.childNodes[_c].className = "sb_mouseover";
					document.sf.searchText.value = _x[_c].replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&');
				}
				else if(_c==-1){
					document.sf.searchText.value = Zhishi.oldSearchText;
				}
			}
			if (_o.scrollTop > _c * 21)
				_o.scrollTop = _c * 21;
			else if (_o.scrollTop < (_c - 9) * 21) 
				_o.scrollTop = (_c - 9) * 21;
			document.sf.searchText.focus();
		}
	}
};
jsAuto.prototype.domouseover = function(obj) {
	obj.tagName == "DIV" ? obj.className = "sb_mouseover" : obj.parentElement.className = "sb_mouseover";
}
jsAuto.prototype.domouseout = function(obj) {
	obj.tagName == "DIV" ? obj.className = "sb_mouseout" : obj.parentElement.className = "sb_mouseout";
}
jsAuto.prototype.doclick = function(msg) {
	if (this._r){
		document.sf.searchText.value = msg.replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&');
		document.sf.submit();
		//window.location.href = "/Search.e?sp=S" + encodeURIComponent(msg);
	}
};
jsAuto.prototype.append = function(msg) {
	with (this) {
		_i ? "" : _i = eval(_i);
		var div = document.createElement("DIV");
		div.style.lineHeight = "175%";
		div.className = "sb_mouseout";
		var index = msg.indexOf(',');
		if (index < 0)
			return;
		var lastIndex = msg.lastIndexOf(',');
		if (lastIndex < 0 || lastIndex == index)
			return;
		info = [msg.substring(0, index), msg.substring(index+1, lastIndex), msg.substring(lastIndex+1)];
		num = info[0];
		if(num!=null && num.indexOf("|")>=0) {
			var patt=/(\|+)/g;
			num = num.replace(patt,",");
		}
		var word = info[1];
		var flag = eval(info[2]);
		_x.push(word);
		var escaped = Zhishi.oldSearchText.replace(/&/g,'&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
		var word2 = word.replace(escaped, '<b>'+escaped+'</b>');
		lv = _v.toLowerCase();
		//msg = "<li class='sb_itemLeft'>" + ((flag == 0)? word2: ('<span class="sb_zd">'
		//		+ word2 + '&nbsp;<img src="http://cache.soso.com/img/sb0.gif" align="absmiddle"></img>' + '</span>'))
		//		+ "</li><li class='sb_itemRight'>"+ num 
		//		+ ((flag != 0) ? '' : "&nbsp;\u7ed3\u679c") + "</li><br />";
		msg = "<li class='sb_itemLeft'>" + word2 + "</li><br />";
		if (_v) div.innerHTML = msg;
		div.style.fontFamily = "verdana";
		div.onmouseover = function() {_i.domouseover(this)};
		div.onmouseout = function() {_i.domouseout(this)};
		div.onclick = function() {_i.doclick(word)};
		_o.appendChild(div);
//		this._o.style.height = parseInt(this._o.style.height) + 21;
	}
};
jsAuto.prototype.handleEvent = function(fValue, fID, event) {
	with (this) {
		var e = event;
		_v = fValue;
		_i = eval(_i);
		if (e == 27){
			get('searchText').value = Zhishi.oldSearchText;
			_o.innerHTML = "";
//			_o.style.height = "2px";
			_o.style.visibility = "hidden";
			
			return;
		}
		if (e == 38 || e == 40 || e == 13) {
			if(_o.style.visibility=="hidden")
				_i.showBox();
			return;
		}
		_i.showBox();
	}
};
jsAuto.prototype.showBox = function() {
	with (this) {
		_c = -1;
		_x = [];
		_o.innerHTML = "";
//		_o.style.height = "2";

		_f = false;
		for (var i = 0; i < Zhishi.g_msg.length; i++) {
			_i.append(Zhishi.g_msg[i]);
			_f = true;
		}
		if ((_f && _v != "") || _e == -1) {
			var l = _r.offsetLeft;
			var t = _r.offsetTop + _r.offsetHeight;
			_o.style.width = _r.offsetWidth-2 + "px";
			var tmp = _r;
			while (tmp = tmp.offsetParent) {
				l += tmp.offsetLeft;
				t += tmp.offsetTop;
			}
			_o.style.left = l + "px";
			_o.style.top = t + "px";

//			_o.style.height = _o.childNodes.length * 21 + "px";
			_o.style.visibility = "visible";
		} else {
			_o.style.visibility = "hidden";
		}
	}
};
function moveSb() {
	with (Zhishi.jsAutoInstance) {
		if (_o.style.visibility == "visible") {
			var l = _r.offsetLeft;
			var t = _r.offsetTop + _r.offsetHeight;
			_o.style.width = (_r.offsetWidth-2) + "px";
			var tmp = _r;
			while (tmp = tmp.offsetParent) {
				l += tmp.offsetLeft;
				t += tmp.offsetTop;
			}
			_o.style.left = l + "px";
			_o.style.top = t + "px";
		}
	}
};
smartBoxInit();
