var SuggestServer = function () {
	this._elementInput = null;
	this._functionReturn = null;
	this._stringOriginalUrl = "http://suggest2.sinajs.cn/suggest/type=@TYPE@&key=@KEY@&name=@NAME@";
	this._stringUrl = "";
	this._elementScriptLoader = null;
	this._elementContainer = null;
	this._stringOriginalValue = "";
	this._stringLastValue = "";
	this._objectData = {};
	this._objectHtml = {};
	this._intMax = 10;
	this._functionCallback = null;
	this._elementLineCurrent = null;
	this._getTime = function () {
		return (new Date()).getTime().toString();
	};
	this._bind = function(__functionBind) {
		var __this = this;
		return function () {
			return __functionBind.apply(__this, arguments);
		};
	};
	this._fill = function () {
		var __stringValue = this._elementInput.value.match(/[^,]*$/)[0];
		__stringValue = __stringValue.replace(/^s[hz](\d)/, "$1");
		if (("key_" + __stringValue) in this._objectData && this._objectData["key_" + __stringValue] != "") {
			if (this._elementContainer == null) {
				this._elementContainer = document.createElement("div");
				this._elementContainer.style.cssText += "display:none; filter:alpha(opacity=95); opacity:0.95; position:absolute; width:200px; z-index:999;";
				this._elementInput.parentNode.insertBefore(this._elementContainer, this._elementInput);
				this._elementContainer["suggest"] = this;
			}
			//调整位置
			var __intPositionTop = 0;
			var __intPositionLeft = 0;
			var __elementTarget = this._elementInput;
			do {
				__intPositionTop += __elementTarget.offsetTop || 0;
				__intPositionLeft += __elementTarget.offsetLeft || 0;
				if (__elementTarget.style.position != "relative") {
					break;
				}
				__elementTarget = __elementTarget.offsetParent;
			} while (__elementTarget);
			if (this._elementContainer.style.top != __intPositionTop + "px") {
				this._elementContainer.style.top = __intPositionTop + "px";
			}
			if (this._elementContainer.style.left != __intPositionLeft + "px") {
				this._elementContainer.style.left = __intPositionLeft + "px";
			}
			var __stringInputBorderTopWidth = this._elementInput.style.borderTopWidth;
			var __stringInputBorderBottomWidth = this._elementInput.style.borderBottomWidth;
			var __intInputMarginTop = this._elementInput.clientHeight;
			__intInputMarginTop += __stringInputBorderTopWidth != "" ? __stringInputBorderTopWidth.replace("px", "") * 1 : 2;
			__intInputMarginTop += __stringInputBorderBottomWidth != "" ? __stringInputBorderBottomWidth.replace("px", "") * 1 : 2;
			if (this._elementContainer.style.marginTop != __intInputMarginTop + "px") {
				this._elementContainer.style.marginTop = __intInputMarginTop + "px";
			}
			//造table
			var __stringTable = "";
			if (!(("key_" + __stringValue) in this._objectHtml)){
				var __arrayData = this._objectData["key_" + __stringValue].replace(/;$/, "").split(";");
				var __intMax = __arrayData.length > this._intMax ? this._intMax : __arrayData.length;
				for (var i = 0; i < __intMax; i++) {
					var __arrayDataItem = __arrayData[i].split(",");
					__stringTable += '<tr id="' + __arrayData[i] + '" style="cursor:pointer;" onmouseover="this.parentNode.parentNode.parentNode[\'suggest\'][\'mouseoverLine\'](this);" onmouseout="this.parentNode.parentNode.parentNode[\'suggest\'][\'mouseoutLine\'](this);" onmousedown="this.parentNode.parentNode.parentNode[\'suggest\'][\'setLine\'](this);"><td>' + __arrayDataItem[0].replace(__stringValue, '<span style="color:#F00;">' + __stringValue + '</span>') + '</td><td>' + __arrayDataItem[2] + '</td></tr>';
				}
				__stringTable = '<table style="border-collapse:collapse; line-height:18px; border:2px solid #EEE; background-color:#FFF; font-size:12px; text-align:center; color:#999; width:100%;"><tr style="background-color:#F3F3F3;"><td>选项</td><td>名称</td></tr>' + __stringTable + '</table>';
				this._objectHtml["key_" + __stringValue] = __stringTable;
			}
			this._elementLineCurrent = null;
			this._elementContainer.innerHTML = this._objectHtml["key_" + __stringValue];
			this._show();
		}
		else {
			this._hide();
		}
	};
	this._color = function (__elementTr) {
		var __stringColor = "";
		if (__elementTr._booleanArrow && __elementTr._booleanMouse) {
			__stringColor = "#F8FBDF";
		}
		else if (__elementTr._booleanArrow) {
			__stringColor = "#F1F5FC";
		}
		else if (__elementTr._booleanMouse) {
			__stringColor = "#FCFEDF";
		}
		if (__elementTr.style.backgroundColor != __stringColor) {
			__elementTr.style.backgroundColor = __stringColor;
		}
	};
	this["mouseoverLine"] = function (__elementTr) {
		__elementTr._booleanMouse = true;
		this._color(__elementTr);
	};
	this["mouseoutLine"] = function (__elementTr) {
		__elementTr._booleanMouse = false;
		this._color(__elementTr);
	};
	this["setLine"] = function (__elementTr) {
		__elementTr
		var __arrayLineData = __elementTr.id.split(",");
		var __stringKey = __arrayLineData[1];
		if (__arrayLineData.length > 3) {
			__stringKey = __arrayLineData[3] + __stringKey;
		}
		this._stringLastValue = __stringKey;
		this._elementInput.value = this._elementInput.value.replace(/[^,]*$/, __stringKey);
		if (this._elementLineCurrent != null) {
			this._elementLineCurrent._booleanArrow = false;
			this._color(this._elementLineCurrent);
		}
		__elementTr._booleanArrow = true;
		this._color(__elementTr);
		this._elementLineCurrent = __elementTr;
	};
	this._show = function () {
		if (this._elementContainer != null) {
			this._elementContainer.style.display = "";
		}
	};
	this._hide = function () {
		if (this._elementContainer != null) {
			this._elementContainer.style.display = "none";
		}
	};
	this._load = function (__stringValue, __functionCallbackTrue, __functionCallbackFalse) {
		if (this._elementScriptLoader == null) {
			this._elementScriptLoader = document.createElement("div");
			this._elementScriptLoader.style.display = "none";
			this._elementInput.parentNode.insertBefore(this._elementScriptLoader, this._elementInput);
		}
		var __stringName = "suggestdata_" + this._getTime();
		var __elementScript = document.createElement("script");
		__elementScript.type = "text/javascript";
		__elementScript.charset = "gb2312";
		__elementScript.src = this._stringUrl.replace("@NAME@", __stringName).replace("@KEY@", __stringValue);
		__elementScript._object = this;
		if (__functionCallbackTrue) {
			__elementScript._functionCallbackTrue = __functionCallbackTrue;
		}
		if (__functionCallbackFalse) {
			__elementScript._functionCallbackFalse = __functionCallbackFalse;
		}
		__elementScript._stringValue = __stringValue;
		__elementScript._stringName = __stringName;
		__elementScript[document.all ? "onreadystatechange" : "onload"] = function () {
			if (document.all && this.readyState != "loaded" && this.readyState != "complete") {
				return;
			}
			var __stringData = window[this._stringName];
			if (typeof __stringData != "undefined") {
				this._object._objectData["key_" + this._stringValue] = __stringData;
				this._functionCallbackTrue(__stringData);
				window[this._stringName] = null;
			}
			else if (this._functionCallbackFasle) {
				this._functionCallbackFasle("");
			}
			this._object = null;
			this._stringValue = null;
			this._stringName = null;
			this[document.all ? "onreadystatechange" : "onload"] = null;
			this.parentNode.removeChild(this);
		};
		this._elementScriptLoader.appendChild(__elementScript);
	};
	this._check = function () {
		var __stringInputValue = this._elementInput.value;
		if (this._stringLastValue != __stringInputValue) {
			this._stringLastValue = __stringInputValue;
			var __stringValue = __stringInputValue.match(/[^,]*$/)[0];
			__stringValue = __stringValue.replace(/^s[hz](\d)/, "$1");
			if (__stringValue != "") {
				if (("key_" + __stringValue) in this._objectData) {
					this._fill();
				}
				else {
					this._load(__stringValue, this._bind(this._fill), this._bind(this._hide));
				}
			}
			else {
				if (this._elementContainer != null) {
					this._elementLineCurrent = null;
					this._elementContainer.innerHTML = "";
				}
				this._hide();
			}
		}
		else {
			this._show();
		}
	};
	this._eventFocus = function () {
		if (this._elementInput.value == this._stringOriginalValue) {
			this._elementInput.value = "";
		}
		this._check();
	};
	this._eventBlur = function () {
		if (this._elementInput.value == "") {
			this._elementInput.value = this._stringOriginalValue;
		};
		this._hide();
	};
	this._eventButtonUp = function () {
		var __event = arguments[0] || window.event;
		switch (__event.keyCode) {
			case 38: //up
				if (this._elementContainer != null) {
					this["setLine"](this._elementContainer.firstChild.rows[(!this._elementLineCurrent || this._elementLineCurrent.rowIndex == 1) ? this._elementContainer.firstChild.rows.length - 1 : this._elementLineCurrent.rowIndex - 1]);
				}
				break;
			case 40: //down
				if (this._elementContainer != null) {
					this["setLine"](this._elementContainer.firstChild.rows[(!this._elementLineCurrent || this._elementLineCurrent.rowIndex == this._elementContainer.firstChild.rows.length - 1) ? 1 : this._elementLineCurrent.rowIndex + 1]);
				}
				break;
			case 13: //Enter
				if (this._elementContainer != null) {
					if (this._elementLineCurrent != null) {
						this["setLine"](this._elementLineCurrent);
					}
					if (this._functionCallback != null) {
						this._functionCallback(this._elementInput.value);
					}
				}
				this._hide();
				break;
			default:
				this._check();
				break;
		}
	};
	this.getCode = function (__stringValue, __functionCallback) {
		if (/^s[hz]\d{6}$/.test(__stringValue) == true) {
			__stringValue = __stringValue.replace(/^s[hz]/, "");
		}
		if (("key_" + __stringValue) in this._objectData) {
			__functionCallback(this._objectData["key_" + __stringValue]);
		}
		else {
			this._load(__stringValue, __functionCallback, __functionCallback);
		}
	};
	this.boot = function (_objectInput, _stringType, _functionCallback, __intMax) {
		this._elementInput = typeof _objectInput == "string" ? document.getElementById(_objectInput) : _objectInput;
		//1-a股；2-b股；3-港股；4-基金；5-权证
		if (this._elementInput) {
			if (typeof _stringType != "undefined") {
				var __stringType = "";
				switch (_stringType) {
					case "stock":
						__stringType = "1,2";
						break;
					case "hkstock":
						__stringType = "3";
						break;
					case "fund":
						__stringType = "4";
						break;
					case "warrant":
						__stringType = "5";
						break;
					case "usstock":
						__stringType = "6";
						break;
					default:
						__stringType = _stringType;
						break;
				}
				this._stringUrl = this._stringOriginalUrl.replace("@TYPE@", __stringType);
			}
			else {
				this._stringUrl = this._stringOriginalUrl.replace("type=@TYPE@&", "");
			}
			this._stringOriginalValue = this._elementInput.value;
			this._elementInput.autocomplete = "off";
			if (this._elementInput._booleanBindEvent != true) {
				if (window.addEventListener) {
					this._elementInput.addEventListener('focus', this._bind(this._eventFocus), false);
					this._elementInput.addEventListener('blur', this._bind(this._eventBlur), false);
					this._elementInput.addEventListener('keyup', this._bind(this._eventButtonUp), false);
					this._elementInput.addEventListener('mouseup', this._bind(this._eventButtonUp), false);
				} else if (window.attachEvent) {
					this._elementInput.attachEvent('onfocus', this._bind(this._eventFocus));
					this._elementInput.attachEvent('onblur', this._bind(this._eventBlur));
					this._elementInput.attachEvent('onkeyup', this._bind(this._eventButtonUp));
					this._elementInput.attachEvent('onmouseup', this._bind(this._eventButtonUp));
				}
				this._elementInput._booleanBindEvent = true;
			}
			this._functionCallback = typeof _functionCallback == "undefined" ? null : _functionCallback;
			if (typeof __intMax != "undefined") {
				this._intMax = __intMax;
			}
		}
	};
};

