var initSearchFlag = false;
orderID = 0;
searchType = 1;

function setSearchType(arg){
	searchType = arg;
}

	function SelectFrame(){
		this.SelectNowFalg = true;
	}
	
	SelectFrame.prototype.getResult = function(){
		var keyWord = document.getElementById('KeyWordInput').value;
		if(keyWord == '' || keyWord == '名称/拼音/代码'){
			this.hideFrame();
			return;
		}
		
		switch(searchType){
			case 1:
				//url_s = 'http://quotes.money.163.com/query_result.jsp?code='+keyWord;
				return;
				break;
			case 3:
			case 4:
				url_s = 'http://quotes.money.163.com/query_list_result.jsp?key='+keyWord;
				break;
			case 2:
				url_s = 'http://quotes.fund.163.com/fund_ajax.jsp?t=of&q='+keyWord;
				break;
			case 5:
				url_s = 'http://quotes.fund.163.com/fund_ajax.jsp?q='+keyWord;
				break;
			default:
		}
		
		var obj_s=document.getElementById('data_ajax_search');
		if(obj_s){
			obj_s.parentNode.removeChild(obj_s);
			//obj_s.src=url_s;
		}
		
		var newscript=document.createElement("script");
		newscript.type="text/javascript";
		newscript.src=url_s;
		newscript.id="data_ajax_search";
		document.body.appendChild(newscript);
	}
	
function loadDataFinish(arg){
	//alert(arg+'\n'+__SearchArrayData__.length);
	//var arr = vvv.replace(/\s*/,'').split('|');
	var hc = '<ul class="c_ul">';
	orderID = 0;
	
	switch(searchType){
		case 1:
		case 3:
		case 4:
			for(var i=0;i<__SearchArrayData__.length;i++){
				if(__SearchArrayData__[i] != null){
					hc += '<li class="c_ul_li" id="_li_element_'+orderID+'" code="'+__SearchArrayData__[i][2]+'" onmouseover=serachUtil.focus(this); onmouseout=serachUtil.blur(this);><div class="leftDiv"><input type="checkBox" value="'+__SearchArrayData__[i][2]+'" name="selectOne" id="selectThisID_'+orderID+'" /></div><div class="centerDiv" onclick="serachUtil.select(\''+__SearchArrayData__[i][2]+'\');"><a href="javascript:void(0)" target="_self">'+__SearchArrayData__[i][1]+'</a></div><div class="rightDiv" onclick="serachUtil.select(\''+__SearchArrayData__[i][2]+'\');"><a href="javascript:void(0)" target="_self">'+__SearchArrayData__[i][0]+'</a></div><div class="clear"></div></li>';
					orderID++;
				}
			}
			break;
		case 2:
		case 5:
			for(var i=0;i<__SearchArrayData__.length;i++){
				if(__SearchArrayData__[i] != null){
					hc += '<li class="c_ul_li" id="_li_element_'+orderID+'" code="'+__SearchArrayData__[i][0]+'" onmouseover=serachUtil.focus(this); onmouseout=serachUtil.blur(this);><div class="leftDiv"><input type="checkBox" value="'+__SearchArrayData__[i][0]+'" name="selectOne" id="selectThisID_'+orderID+'" /></div><div class="centerDiv" onclick="serachUtil.select(\''+__SearchArrayData__[i][0]+'\');"><a href="javascript:void(0)" target="_self">'+__SearchArrayData__[i][1]+'</a></div><div class="rightDiv" onclick="serachUtil.select(\''+__SearchArrayData__[i][0]+'\');"><a href="javascript:void(0)" target="_self">'+__SearchArrayData__[i][0]+'</a></div><div class="clear"></div></li>';
					orderID++;
				}
			}
			break;
		default:
	}
				
	hc += '</ul>';
	document.getElementById('resultListDiv').innerHTML = hc;
	document.getElementById('resultListDiv').style.display = 'block';
}
	
function openFund(arg){
	window.open("http://quotes.fund.163.com/fundDetail.html?code="+arg);
}

function openFundBBS(arg){
	window.open("http://fund8.money.163.com/bbs/list.jsp?boardid="+arg);
}

function openQuote(arg){
	window.open("http://quotes.money.163.com/"+arg+".html");
}

function openQuoteBBS(arg){
	if(arg != null && arg.substring(0,1) == '0'){
		window.open('http://guba.money.163.com/bbs/list.jsp?boardid=sh'+arg.substring(1,arg.length));
	}else if(arg != null){
		window.open('http://guba.money.163.com/bbs/list.jsp?boardid=sz'+arg.substring(1,arg.length));
	}
}
	
	SelectFrame.prototype.select = function(arg){
		this.SelectNowFalg = false;
		switch(searchType){
			case 1:
			case 3:
				openQuote(arg);
				break;
			case 4:
				openQuoteBBS(arg);
				break;
			case 2:
				openFund(arg);
				break;
			case 5:
				openFundBBS(arg);
				break;
			default:
		}
	}
	
	SelectFrame.prototype.FrameFocus = function(){
		if(document.getElementById('KeyWordInput').value == '名称/拼音/代码' || document.getElementById('KeyWordInput').value == '名称/拼音/代码  同时查询多只用","隔开'){
			document.getElementById('KeyWordInput').value = '';
		}
		if(initSearchFlag == false){
			initSearch();
		}
	}
	
	SelectFrame.prototype.FrameBlur = function(){
		if(document.getElementById('KeyWordInput').value == ''){
			if(searchType != 1){
				document.getElementById('KeyWordInput').value = '名称/拼音/代码';
			}else{
				document.getElementById('KeyWordInput').value = '名称/拼音/代码  同时查询多只用","隔开';
			}
		}
	}
	
	SelectFrame.prototype.focus = function(arg){
		this.SelectNowFalg = true;
		arg.style.background = "#D18BD0";
	}
	
	SelectFrame.prototype.blur = function(arg){
		arg.style.background = "#EDF6FE";
		this.SelectNowFalg = false;
	}
	
	SelectFrame.prototype.hideFrame = function(){
		if(this.SelectNowFalg == true){
		}else{
			document.getElementById('resultListDiv').style.display = 'none';
		}
	}
	
	submitAllSelect = function(){
		if(searchType == 1){
			window.open('http://quotes.money.163.com/query_result.jsp?code='+document.getElementById('KeyWordInput').value);
		}else
		for(var i=0;i<orderID;i++){
			var obj = document.getElementById('selectThisID_'+i);
			if(obj){
				if(obj.checked == true){
					switch(searchType){
						//case 1:
						case 3:
							openQuote(obj.value);
							break;
						case 4:
							openQuoteBBS(obj.value);
							break;
						case 2:
							openFund(obj.value);
							break;
						case 5:
							openFundBBS(obj.value);
							break;
						default:
					}
				}
			}
		}
	}

var serachUtil = new SelectFrame();


function getLeft(e){
	var offset=e.offsetLeft;
	if(e.offsetParent!=null) offset+=getLeft(e.offsetParent);
	return offset;
}

function getTop(e){
	var offset=e.offsetTop;
	if(e.offsetParent!=null) offset+=getTop(e.offsetParent);
	return offset;
}

function initSearch(){
	if(initSearchFlag == false){
		initSearchFlag = true;
	}else{
		return;
	}
	var divEle = document.createElement('div');
		divEle.id = 'resultListDiv';
		divEle.style.position="absolute";
		divEle.style.top = (getTop(document.getElementById('KeyWordInput'))+22)+'px';
		divEle.style.left = getLeft(document.getElementById('KeyWordInput'))+'px';
		divEle.style.width = 220;
		divEle.style.background = 'url(http://data.money.163.com/dataimg/bg.gif)';
		divEle.style.zIndex = 90001;
		divEle.style.display = 'none';
		document.body.appendChild(divEle);
		document.body.onmousedown = function(){
			serachUtil.hideFrame();
		}
	
	var currentLI_Index = -1;
	
	document.getElementById('KeyWordInput').onkeyup = function(aEvent) {
		if(document.getElementById('KeyWordInput').value == ''){
			currentLI_Index = -1;
			return;
		}
			var myEvent = window.event ? window.event : aEvent;
			if(myEvent.keyCode !=13 && myEvent.keyCode !=38 && myEvent.keyCode !=40){
				serachUtil.getResult();
				//return;
			}
			
			var currentLI_ElementList = new Array();
			for(var i=0;i<orderID;i++){
				currentLI_ElementList.push(document.getElementById('_li_element_'+i));
			}
			
			if(currentLI_ElementList.length<1){return;}
			
		    switch (myEvent.keyCode) {
				case 13://回车
					//document.getElementById('_li_'+currentLI_Index)
					if(searchType == 1){
						window.open('http://quotes.money.163.com/query_result.jsp?code='+document.getElementById('KeyWordInput').value);
					}else{
						serachUtil.select(currentLI_ElementList[currentLI_Index].getAttribute('code'));
					}
			        break;
			    case 33://pgUP
			    	break;
			    case 34://pgDN
			    	break;
				case 16://shift
			    	break;
		        case 17://Ctrl
			        break;
			    case 38://向上
			    	if(currentLI_Index>0){
						currentLI_Index--;
						currentLI_ElementList[currentLI_Index+1].style.background = "#EDF6FE";
						currentLI_ElementList[currentLI_Index].style.background = "#D18BD0";
					}
					break;
			    case 40://向下
			    	if(currentLI_Index<currentLI_ElementList.length - 1){
						currentLI_Index++;
						if(currentLI_Index-1>=0)currentLI_ElementList[currentLI_Index-1].style.background = "#EDF6FE";
						currentLI_ElementList[currentLI_Index].style.background = "#D18BD0";
					}
					break;
				default:
					if(currentLI_ElementList[currentLI_Index] != null){
						currentLI_ElementList[currentLI_Index].style.background = "#EDF6FE";
					}
			    	if(currentLI_Index < 0 || currentLI_Index >= currentLI_ElementList.length){
						currentLI_Index = 0;
						currentLI_ElementList[0].style.background = "#D18BD0";
					}
			}
	}
}