var suggest =
{
	line : 0,
	count : 0,
	keyword : "",
	type : "all",
	move : function (_evt)
	{
		switch (_evt.keyCode)
		{
			case 38: //up
				if (this.line > 1)
				{
					document.getElementById("suggest_"+this.line).className = "";
					this.line = this.line - 1;
					document.getElementById("suggest_"+this.line).className = "current";
				}
				break;
			case 40: //down
				if (this.line > 0 && this.line < this.count)
				{
					document.getElementById("suggest_"+this.line).className = "";
				}
				if (this.line < this.count)
				{
					this.line = this.line + 1;
					document.getElementById("suggest_"+this.line).className = "current";
				}
				break;
			case 13: //enter
				if (this.line >= 1 && this.line <= this.count)
				{
					document.finance_search.select_type.disabled = false;
					document.finance_search.select_code.disabled = false;
					document.finance_search.select_type.value = suggest_json[this.line-1]["t"];
					document.finance_search.select_code.value = suggest_json[this.line-1]["c"];
				}
				break;
			default:
				
				break;
		}

	},
	display : function (_display)
	{
		if (_display == "inline")
		{
			var table_html = document.getElementById("suggest_list_finance").innerHTML;
			if (table_html != "")
			{
				document.getElementById("suggest_list_finance").style.display = _display;
			}
		}
		else if (_display == "none")
		{
			document.getElementById("suggest_list_finance").style.display = _display;
		}
	},
	mark_keyword : function (_str, _keyword)
	{
		return _str.replace(_keyword, "<span style=\"color:red;\">"+_keyword+"</span>");
	},
	work : function ()
	{
		var _type_conf = {"stock":"股票","fund":"基金","hkstock":"港股","forex":"外汇","bond":"债券"};

		var table_html = "";

		this.line = 0;
		this.count = 0;

		document.finance_search.select_type.disabled = true;
		document.finance_search.select_code.disabled = true;
		document.finance_search.select_type.value = "";
		document.finance_search.select_code.value = "";
		
		for(var i=0; i<suggest_json.length; i++)
		{
			this.count++;

			var code = suggest_json[i]["c"];
			var symbol = suggest_json[i]["s"];
			var name = suggest_json[i]["n"];
			var pinyin = suggest_json[i]["p"];
			var type = suggest_json[i]["t"];
			
			var _type = _type_conf[type];

			var bgcolor = "";
			if (i % 2 == 1)
			{
				bgcolor = "bgcolor='#F5F5F5'";
			}

			if (type == "forex")
			{
				table_html += "  <tr id=\"suggest_"+(parseInt(i)+1)+"\" onmouseover=\"javascript:this.className='current';this.style.cursor='pointer'\" onmouseout=\"javascript:this.className=''\" onclick=\"javascript:window.open('http://finance.ifeng.com/app/hq/"+type+"/"+symbol+"/')\" >";
			}
			else 
			{
				var code_begin = code.substring(0,4);
				var open_url = null;

				if(type == 'fund')
				{
					open_url = "http://finance.china.com.cn/money/fund/quote/"+code+"/";
				}
				else if(type == 'stock')
				{
					open_url = "http://finance.china.com.cn/stock/quote/"+code+"/";
				}

				table_html += "  <tr id=\"suggest_"+(parseInt(i)+1)+"\" onmouseover=\"javascript:this.className='current';this.style.cursor='pointer'\" onmouseout=\"javascript:this.className=''\" onclick=\"javascript:window.open('"+open_url+"')\" >";
			}
			table_html += "    <td "+bgcolor+">"+suggest.mark_keyword(symbol.toUpperCase(), this.keyword.toUpperCase())+"</td>";
			table_html += "    <td "+bgcolor+">"+suggest.mark_keyword(name, this.keyword)+"</td>";
			table_html += "    <td "+bgcolor+">"+suggest.mark_keyword(pinyin.toUpperCase(), this.keyword.toUpperCase())+"</td>";
			table_html += "    <td "+bgcolor+">"+_type+"</td>";
			table_html += "  </tr>";

		}
		if (table_html != "")
		{
			table_html = "<div class=\"xl_table\"><table width=\"237\" height=\"\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table_s\"><tr class=\"tr_title\"><td width=\"54\">代码</td><td width=\"70\">名称</td><td width=\"60\">拼音</td><td width=\"53\">类型</td>"+table_html+"</table></div>";
		}
		document.getElementById("suggest_list_finance").innerHTML = table_html;
		if (table_html != "")
		{
			suggest.display("inline");
		}
		else
		{
			suggest.display("none");
		}
	},
	typewrite : function (_keyword)
	{
		if (_keyword != this.keyword)
		{
			this.keyword = _keyword;
			
			var oHead = document.getElementsByTagName('HEAD').item(0);
			var oScript = document.createElement("script");
			oScript.type = "text/javascript";
			oScript.src = "http://app.finance.china.com.cn/search/suggest.php?type="+this.type+"&keyword="+_keyword+"&cb=suggest.work();";
			oHead.appendChild(oScript); 
		}
		
	}
}
