var suggest_id='suggestId';  
var suggest_title='suggestTitle';
arrList = new Array();
arrListId = new Array();
var dataType='';
var intIndex = 0;
var first_flag=1;
var suggest_style=new Array();
var suggest_conf=new Array();
var inputObj = '';

var anime_finished = '';
suggest_style['bgcolor']='#E8F7EB';
//onSuggestConfirm

function smanPromptList(arrList, objInputId ,type){
	if(!type)
		dataType='anime';
	else
		dataType=type;
	inputObj=objInputId;
	//init();
	$(objInputId).style.color="#A8A8A8";
	//if(dataType=='anime')
		//$(objInputId).value='请搜索动画';
	//else if(dataType=='book')
		//$(objInputId).value='请搜索漫画';
	this.style = "background:#FFFFFF;border: 1px solid #CCCCCC;font-size:14px;cursor: default;"
	if (arrList.constructor != Array) {
		return;
	}
	var selece_div_width=199;
	window.onload = function(){
		if(dataType=='anime'){
			var oJSResults = document.createElement('script');
			oJSResults.setAttribute("language","JavaScript");
			oJSResults.setAttribute("src", '/js/anime4suggest.js');
			document.body.appendChild(oJSResults);
		}
		
		if(dataType=='book'){
			var oJSResults = document.createElement('script');
			oJSResults.setAttribute("language","JavaScript");
			oJSResults.setAttribute("src", '/js/book4suggest.js');
			
			document.body.appendChild(oJSResults);
		}
		var ie = (document.all) ? true : false
		if(!ie){
			return;//disable in other browser
		}
		var newElement = document.createElement('div');
		newElement.id = '__smanDisp';
		newElement.style.position='absolute';
		newElement.style.background='#FFFFFF';
		newElement.style.border='1px solid #CCCCCC';
		document.body.appendChild(newElement);
		document.getElementById("__smanDisp").style.display='none';
		document.getElementById("__smanDisp").innerHTML='';

		arrList.sort(function(a, b){
			if (a.length > b.length) 
				return 1;
			else 
				if (a.length == b.length) 
					return a.localeCompare(b);
				else 
					return -1;
		})
		var objouter = document.getElementById("__smanDisp") //显示的DIV对象
		var objInput = document.getElementById(objInputId); //文本框对象
		var selectedIndex = -1;
		var intTmp; //循环用的:)
		if (objInput == null) {
			return;
		}
		objInput.onblur = function(){
			objouter.style.display = 'none';
			objInput.style.color='';
			
		}
		window.onfocus = function(){
			objouter.style.display = 'none';
		}
	
		function showResponse(input_str){
	    		data_match(input_str);
			if(arrList.length==0)
				return;
			checkAndShow()
			divPosition()
		}
		
		objInput.onkeyup = function(event){
			var event=document.all?window.event:arguments[0];
			var keyCode = event.keyCode;
			if(keyCode != 13 && keyCode != 40 && keyCode != 38 && keyCode != 37 && keyCode != 39) {
				arrList.length=0;
				arrListId.length=0;
				objouter.innerHTML='';
				if(dataType=='anime')
					anime_finished = '';
				onSuggestChange();
			}
			
			checkKeyCode(event);
			if (keyCode == 13) 
				return;
			
			if (keyCode != 40 && keyCode != 38 && keyCode != 37 && keyCode != 39) {
				showResponse(objInput.value);
				if(arrList.length==0){
					objouter.style.display = 'none';
					return;
				}
				checkKeyCode(event);
			}
		}

		function checkKeyCode(event){
			var ie = (document.all) ? true : false
			if (1) {
				var keyCode = event.keyCode
				if (keyCode == 40 || keyCode == 38) { //下上
					var isUp = false
					if (keyCode == 40) 
						isUp = true;
					chageSelection(isUp)
				}
				else 
					if (keyCode == 13) {//回车
						outSelection(selectedIndex);
					}
					else if( keyCode != 37 && keyCode != 39) { //左右不清空select
						checkAndShow()
					}
			}
			else {
				checkAndShow()
			}
			divPosition()
		}
		
		function checkAndShow(){
			var strInput = objInput.value
			if (strInput != "") {
				divPosition();
				selectedIndex = -1;
				objouter.innerHTML = "";
				if (!arrList.length) 
					return;
				
				for (intTmp = 0; intTmp < arrList.length; intTmp++) {
					if (dataType == 'anime'||dataType == 'book') {
						var title_str = '<font class="title_text">' + arrList[intTmp] + '</font>';
						addOption(arrList[intTmp], title_str , strInput);
					} else if(dataType == 'game'){
						var title_str = '<font class="title_text">' + arrList[intTmp] + '</font>';
						var type_str = '<font class="total_text">(' + arrListType[intTmp] + ')</font>';
						addOption(arrList[intTmp], title_str + ' ' + type_str , strInput);
					} 
				}
				if(arrList.length!=0)
					objouter.style.display = '';
			}
			else {
				objouter.style.display = 'none';
			}
			function addOption(value, view_str, keyw){
				objouter.innerHTML += "<div style='z-index:99;text-align:left;white-space:nowrap;width:194px;overflow:hidden;margin-left:5px;' onmouseover=\"this.className='sman_selectedStyle'\" onmouseout=\"this.className=''\" onmousedown=\"document.getElementById('" + objInputId + "').value='" + value + "';getClickId('" + value + "');onSuggestConfirm();onSuggestConfirmStyle();onSuggestEnter();window.focus();\">" + view_str + "</div>"
			}
		}
		
		function chageSelection(isUp){
			if (objouter.style.display == 'none'&&arrList.length!=0) {
				
				objouter.style.display = '';
			}
			else {
				if (isUp) 
					selectedIndex++
				else 
					selectedIndex--
			}
			var maxIndex = objouter.children.length - 1;
			if (selectedIndex < 0) {
				selectedIndex = 0
			}
			if (selectedIndex > maxIndex) {
				selectedIndex = maxIndex
			}
			for (intTmp = 0; intTmp <= maxIndex; intTmp++) {
				if (intTmp == selectedIndex) {
					objouter.children[intTmp].className = "sman_selectedStyle";
				}
				else {
					objouter.children[intTmp].className = "";
				}
			}
			if (arrList.length != 0) {
				canSearch = 0;
			}else{
				canSearch = 1
			}
		}
		
		function outSelection(Index){
			$(suggest_id).value = arrListId[Index];
			$(suggest_title).value = arrList[Index];
			getClickId(arrListId[Index]);
			objouter.style.display = 'none';
			
			if (objouter.children[Index]) {
			
				objInput.value = arrList[Index];
				if(Index>=0) {
					onSuggestConfirmStyle();
					onSuggestConfirm();
					}
				onSuggestEnter();
			}
			else {
				onSuggestEnter();
				return;}
		}
		function divPosition(){
			objouter.style.top = getAbsoluteHeight(objInput) + getAbsoluteTop(objInput);
			objouter.style.left = getAbsoluteLeft(objInput);
			objouter.style.width = 199;
			if(objouter.style.top==0){
				objouter.style.top = getAbsoluteHeight(objInput.parentNode.parentNode) + getAbsoluteTop(objInput.parentNode.parentNode);
			}
		}
	}
	
	document.write("<style>.sman_selectedStyle{background-Color:#102681;color:#FFFFFF;white-space:nowrap;overflow:hidden;width:194px;}</style>");
	document.write("<style>.title_text{font-size:12px}</style>");
	document.write("<style>.total_text{font-size:11px;color:#00CD00;}</style>");

	function getAbsoluteHeight(ob){
		return ob.offsetHeight
	}
	
	function getAbsoluteWidth(ob){
		return ob.offsetWidth
	}
	
	function getAbsoluteLeft(ob){
		var s_el = 0;
		el = ob;
		while (el) {
			s_el = s_el + el.offsetLeft;
			el = el.offsetParent;
		};
		return s_el
	}
	
	function getAbsoluteTop(ob){
		var s_el = 0;
		el = ob;
		while (el) {
			s_el = s_el + el.offsetTop;
			el = el.offsetParent;
		};
		return s_el
	}

	function cleanup(){
		arrList.length=0;
		arrListId.length=0;
		anime_finished = '';
		$(suggest_id).value='';
		$(suggest_title).value='';
	}

}

function onSuggestConfirmStyle() {

}

function onSuggestConfirm(){
	if (dataType == 'anime') {
		window.location = '/detail/' + $(suggest_id).value;
	}
	if (dataType == 'book') {
		window.location = '/book/' + $(suggest_id).value;
	}
}

function onSuggestEnter() {

}

function onSuggestChange() {
//		$(suggest_id).value='';
//		$(suggest_title).value='';
}

function getClickId(value){
	var item_id=0;
	for(var i=0;i<arrList.length;i++) {
		if(arrList[i]==value){
			$(suggest_id).value=arrListId[i];
			$(suggest_title).value=arrList[i];
			return 1;
		}
	}
}
function firstClick(){
		if(first_flag) {
			first_flag=0;
			$(inputObj).value='';
			$(inputObj).style.color='#000000';
		}
}

function init(){
	$(inputObj).onchange=function(){
		getClickId($(inputObj).value);
	}
	$(inputObj).onfocus=function(){
			firstClick();
			$(inputObj).style.cssText='';
			$(inputObj).value=$(inputObj).value.replace(' [修改]', '');
	}
}

//uq
var suggestDataType=0;
function frontSuggestInit(type){
	suggestDataType=type;
}

function loadSuggestData(){
	//....
}
function data_match(str){
	var j=0;
	var input_str='';
	var compare_str='';
	var pinyin_str='';
	
	for(var i=0;i<data4suggestTitle.length;i++){
		if(j==10) break;
		//str.indexOf
		input_str=str.toLowerCase();
		compare_str=data4suggestTitle[i].toLowerCase();
		pinyin_str=data4suggestPinyin[i].toLowerCase();
		if(compare_str.indexOf(input_str)==0){
			arrList[j]=data4suggestTitle[i];
			arrListId[j]=data4suggestId[i];
			j++;
		}else if(pinyin_str.indexOf(input_str)==0){
			arrList[j]=data4suggestTitle[i];
			arrListId[j]=data4suggestId[i];
			j++;
		}
	}
}
