function CitySelect(){
	this.selectText="";
	this.selectIndex="";
	this.contextLen=0;
}
var citySelect = new CitySelect();
jQuery(function(){
	
	$("ul[id^='citySearchUl']>li").live("click",function(){
		
		var indexNumber = jQuery(this).parent()[0].id.substr(this.id.length-1);
		var inputv = jQuery(this).html();
		jQuery("#citySearchInput"+indexNumber).val(inputv);
		jQuery("#citySearchUl"+indexNumber).hide();
		
	});
	jQuery(":input[id^='citySearchInput']").live("focusin",function(event){
		var indexNumber = this.id.substr(this.id.length-1);
		jQuery("#citysearcherror"+indexNumber).html("");
		
	});
	jQuery(":input[id^='citySearchInput']").live("blur",function(event){
		var indexNumber = this.id.substr(this.id.length-1);
		var LI = jQuery("#citySearchUl"+indexNumber+">li");
		if(LI.filter(".m_sel").size()==0){//第一个按，即判断li的样式没有被选中的
			jQuery("#citySearchUl"+indexNumber).hide();		
			citySelect.selectIndex = 0; 		
		}else{
			citySelect.selectText = jQuery.trim(LI.filter(".m_sel").html().split("<")[0]);
			jQuery("#citySearchInput"+indexNumber).val(citySelect.selectText);
			jQuery("#citySearchUl"+indexNumber).hide();
			citySelect.selectIndex = 0; 
		}
		
	});
	
	jQuery(":input[id^='citySearchInput']").live("keyup",function(event){//输入后显示相关提示
		var keyCode = window.event ? event.keyCode : event.which;
		var indexNumber = this.id.substr(this.id.length-1);
		var showLI = "";
		var UL = jQuery("#citySearchUl"+indexNumber);
		var LI = jQuery("#citySearchUl"+indexNumber+">li");
		
		var str = city.tipMsg(jQuery.trim(this.value));
		
		if(str==""){//没有结果直接返回
			UL.hide();
			return false;
		}
		
		citySelect.contextLen = str.split("|").length;
		var citys = new Array();
			for(var i=0;i<str.split("|").length;i++){
				citys.push(str.split("|")[i].split(",")[1]+"#"+str.split("|")[i].split(",")[0]);
				showLI += "<li onMouseOver=\"this.className='m_sel'\"  onMouseOut=\"this.className=''\" >"+str.split("|")[i].split(",")[0]+"<span style=\"color:#999\">  ("+str.split("|")[i].split(",")[1]+")</span></li>";
				if(i==9){
					break;
				}
			}
		//citys.sort();
		//for(var i=0;i<citys.length;i++){
		//	showLI += "<li onMouseOver=\"this.className='m_sel'\"  onMouseOut=\"this.className=''\" >"+citys[i].split("#")[1]+"<span style=\"color:#999\">  ("+citys[i].split("#")[0]+")</span></li>";
		//}
		if(str.split("|").length>0){//有结果显示
						
			UL.html(showLI);
			UL.show();
		}else{//无结果清楚以前的
			UL.html("");
			UL.hide();
		}
		if (keyCode==40||keyCode==38)
		{//用户操作：下&上
	
			if(keyCode==40){//下
				if(LI.filter(".m_sel").size()==0){//第一个按，即判断li的样式没有被选中的
					citySelect.selectIndex = 0; 
					jQuery("#citySearchUl"+indexNumber+">li").first().addClass("m_sel");
					citySelect.selectText = jQuery.trim(jQuery("#citySearchUl"+indexNumber+">li").first().html().split("<")[0]);
					
				}else{
					if(citySelect.selectIndex<9 && citySelect.selectIndex<str.split("|").length-1){
						citySelect.selectIndex++;
					}
					
					jQuery("#citySearchUl"+indexNumber+">li:eq("+citySelect.selectIndex+")").addClass("m_sel").siblings().removeClass("m_sel");
					citySelect.selectText = jQuery.trim(jQuery("#citySearchUl"+indexNumber+">li:eq("+citySelect.selectIndex+")").html().split("<")[0]);
				
				}
			}else if(keyCode==38){//上
				if(LI.filter(".m_sel").size()==0){//第一个按，即判断li的样式没有被选中的
						UL.hide();					
				}else{
					if(citySelect.selectIndex>0){
						citySelect.selectIndex--;
					}
					
					jQuery("#citySearchUl"+indexNumber+">li:eq("+citySelect.selectIndex+")").addClass("m_sel").siblings().removeClass("m_sel");
					citySelect.selectText = jQuery.trim(jQuery("#citySearchUl"+indexNumber+">li:eq("+citySelect.selectIndex+")").html().split("<")[0]);
				}
				
			} 
		}else if (keyCode==13)	{//用户操作：回车
			
				if (citySelect.selectText!=""){
					jQuery("#citySearchInput"+indexNumber).val(citySelect.selectText);
				}
		 		
		 		jQuery("#citySearchUl"+indexNumber).hide();
		 		citySelect.selectText = "";
			} 
		
	});
	
	jQuery(":button[id^='citySearchBtn']").live("click",function(event){
		var indexNumber = this.id.substr(this.id.length-1);
		var v = jQuery("#citySearchInput"+indexNumber).val();
		var reg = /^[\u4e00-\u9fa5]+$/i; 
		if(reg.test(jQuery.trim(v))){
			try{
				var testcity = city.ctn2c(jQuery.trim(v));
				if(!testcity){
					
					jQuery("#citysearcherror"+indexNumber).html("请输入正确城市名称！");
					return false;
				}
				jQuery("#citysearcherror"+indexNumber).html("");
				setCurrentCity(jQuery.trim(v));
				if(indexNumber=='3'){//驾车的切换起点城市
					setDriverSC(jQuery.trim(v));
				}else if(indexNumber=='4'){//驾车的切换终点城市
					setDriverEC(jQuery.trim(v));
				}
				var closeId = jQuery(this).parent().parent()[0].id;
				closeWindow(closeId);
			}catch(e){
				jQuery("#citysearcherror"+indexNumber).html("请输入正确城市名称！");
			}
			
		}else{
			jQuery("#citysearcherror"+indexNumber).html("请输入正确城市名称！");
			return false;
		}
	
	});
});