var highLightIndex=-1;
$(function(){
	//酒店关键字
	var O_SearchHotel=$("#O_SearchHotel");//搜索
	var O_KeyHotel=$("#O_HotelName");
	if(O_KeyHotel != null) {
		var O_KeyHotelOffset=O_KeyHotel.offset();
		$("<div id=\"O_AutoHotel\"></div>").appendTo("body");
		var O_AutoHotel=$("#O_AutoHotel");
		O_AutoHotel.hide();//隐藏自动补全框
		O_AutoHotel.css("border","1px solid #817F82")
		.css("background-color","#fff")
		.css("position","absolute")
		.css("z-index","1000")
		.css("top",O_KeyHotelOffset.top + O_KeyHotel.height() + 5)
		.css("left",O_KeyHotelOffset.left)
		.css("width",O_KeyHotel.width() + 3);
		O_KeyHotel.keyup(function(event){
			var myEvent = event || window.event;
			var keyCode = myEvent.keyCode;//alert(keyCode);
			var key=O_KeyHotel.val().replace(/ /g,'').replace(/'/g,'');
			if(key.length>0){
				if(keyCode >= 48 && keyCode <= 57 || keyCode >= 65 && keyCode <= 90 || keyCode == 8 || keyCode == 46 || keyCode == 32) {//alert(keyCode);
					highLightIndex=-1;
					var field="hotelName";
					getKeyListHotel(field, key);
				} else if (keyCode == 38 || keyCode == 40) {//alert(keyCode);
					//向上/向下箭头
					var nodes = O_AutoHotel.children("div");
					//使未向上移时的当前项底色去掉
					if(highLightIndex != -1) { nodes.eq(highLightIndex).css("background-color","#fff"); }
					if(keyCode == 38){ //向上
						nodes.css("background-color","#fff").css("border","1px solid #fff");
						highLightIndex--;
						if(highLightIndex == -1) { highLightIndex = nodes.length - 1; }
					}
					if(keyCode == 40){ //向下
						nodes.css("background-color","#fff").css("border","1px solid #fff");
						highLightIndex++;
						if(highLightIndex == nodes.length) { highLightIndex = 0; }
					}
					//使用当前项高亮
					nodes.eq(highLightIndex).css("background-color","#eee").css("border","1px solid #ccc");
				} else if (keyCode == 13) {
					//如果输入的是回车
					//如果下拉框有高亮内容
					if(highLightIndex != -1) {
						O_KeyHotel.val(O_AutoHotel.children("div").eq(highLightIndex).html());
						highLightIndex=-1;
						O_AutoHotel.html("");
						O_AutoHotel.hide();
					} else {
						O_SearchHotel.trigger("click");
					}
				} else {
				}
			}else{
				highLightIndex=-1;
				O_AutoHotel.html("");
				O_AutoHotel.hide();
				//绑定回车键与点击快捷搜索一致
				if (keyCode == 13) { O_SearchHotel.trigger("click"); }
			}
			
		});
		//搜索失去焦点
		O_KeyHotel.blur(function(){
			//alert(highLightIndex);
			if(highLightIndex != -1) {
				var nodes = O_AutoHotel.children("div");
				O_KeyHotel.val(nodes.eq(highLightIndex).html());
				highLightIndex=-1;
			}
			O_AutoHotel.hide();
			O_SearchHotel.focus();
		});
		//点击搜索关键字
		O_KeyHotel.click(function(){
			var field="hotelName";
			var key=O_KeyHotel.val().replace(/ /g,'').replace(/'/g,'');
			getKeyListHotel(field, key);
		});
	}
});
//获取关键字列表
function getKeyListHotel(field, key){//alert(field + "-" + key);
	var basePath = $($("body")[0]).attr("base");
	var O_AutoHotel=$("#O_AutoHotel");//alert(O_AutoHotel.attr("id"));
	var O_KeyHotel=$("#O_HotelName");
	var O_KeyHotelOffset=O_KeyHotel.offset();
				
	if(key.length > 0) {
		$.get(basePath + "common/getHotelSearchKeyList.php",{simpleSearch:field,simpleSearchKey:key},function(data){//alert(data);return false;
			//alert(data.length);
			if(data.length != 0 && data.length != 2 && data.length != -2) {
				O_AutoHotel.html(data);
				O_AutoHotel.css("top",O_KeyHotelOffset.top + O_KeyHotel.height() + 5)
				.css("left",O_KeyHotelOffset.left);
				O_AutoHotel.show();
				
				var nodes = O_AutoHotel.children("div");
				var width = O_AutoHotel.width() - 2;
				nodes.each(function(i){
					var node = $(this);
					node.attr("id", i).css("width",width).css("cursor","pointer").css("overflow","hidden").css("line-height","25px").css("height","25px").css("border","1px solid #fff");
					//鼠标移上
					node.mouseover(function(){
						nodes.css("background-color","#fff").css("border","1px solid #fff");
						highLightIndex = this.id; 
						node.css("background-color","#eee").css("border","1px solid #ccc");
					});
					//鼠标离开
					node.mouseout(function(){
						node.css("background-color","#fff").css("border","1px solid #fff");
						highLightIndex=-1;
					});
					//鼠标点击
					node.click(function(){
						O_KeyHotel.focus();
						O_KeyHotel.val(node.html());
						highLightIndex=-1;
						O_AutoHotel.html("");
						O_AutoHotel.hide();
					});
				});
			} else {
				O_AutoHotel.html("");
				O_AutoHotel.hide();
			}
		});
	} else {
		O_AutoHotel.html("");
		O_AutoHotel.hide();
	}
}