// JavaScript Document
;(function($){
		   $.fn.jDropdown=function(options){
			   var settings={
				   defaultSelectedCity:"",
				   defaultHotCity:"",
				   tips:""
				   };
			   var op=$.extend(settings,options);
			   var $this=$(this);
			   var citydata=null;
			   var windowid="selectcity";
			   var timeoutid=null;
			   var searchcitypopwincontent=null;
			   var resizeBox=function(){
				   //alert(letterNumber)
				   $this.css({"width":38+$this.find("#lable").text().length/1*12+"px"})
				   }
			   resizeBox();

//			   var tempcity={};//临时的城市json对象;
//			   $(this).blur(function(){
//									 $.msgbox.closemsgbox(windowid);
//									 })
			   
			   var hideSelectCity=function(){
				   $("#"+windowid).remove();
				   }
			   var checkExternalClick=function(event){
				   var $target = $(event.target);
				   if ($target.parents('#' + windowid).length==0 && !$target.hasClass("hasselectcity")){
					   setTips();
					   hideSelectCity();
					   }
				   }
			   var setTips=function(){
				   if ($this.val()==""){$this.val(op.tips).attr("title",op.tips)}
				   }
			   var initSelectCity=function(){
				   setTips();
				   $this.addClass("hasselectcity");
				   $.getJSON("../images/citydata.js",function(data){citydata=data;})
				   $.get(
						 "../images/searchcity.html",
						 function(data){
							 searchcitypopwincontent=data;
							 }
						 )
				   $(document).mousedown(function(event){checkExternalClick(event)});
				   }
			   initSelectCity();
			   var showHotCity=function(obj){
				   var offsetleft=$(obj).offset().left;
				   var offsettop=$(obj).offset().top+$(obj).innerHeight();
				   $.get(
						 "../images/hotcity.html",
						 function(data){
							 var $popwin=$.msgbox.popupwin(windowid,260,100,"<a id='hotcity' class='current' href='#' title='热门城市'>热门城市</a> <a id='allcity' href='#' title='所有城市'>所有城市</a>",data).css({"top":offsettop+"px","left":offsetleft+"px"}).addClass("popwin_"+windowid);
							 $popwin.find("#hotcitys li").click(function(){
																		 /*alert($this.parent().find("div[id='header_changecity_lable']").html());*/
																		 $this.parent().find("div[id='header_changecity_lable']").html($(this).text()+"： ")
																		$this.attr("value",$(this).text());$.msgbox.closemsgbox(windowid);return false;
																		 })
							 $popwin.find("#hotcity").click(function(){return false;})
							 $popwin.find("#allcity").click(function(){showAllCity($this);return false;})
							 }
						 )
				   }
			   
			   var showAllCity=function(obj){
				   var offsetleft=$(obj).offset().left;
				   var offsettop=$(obj).offset().top+$(obj).innerHeight();
				   $.get(
						 "../images/allcity.html",
						 function(data){
							 var $popwin=$.msgbox.popupwin(windowid,260,100,"<a id='hotcity'href='#' title='热门城市'>热门城市</a> <a id='allcity'  class='current' href='#' title='所有城市'>所有城市</a>",data).css({"top":offsettop+"px","left":offsetleft+"px"}).addClass("popwin_"+windowid);
							 $popwin.find("#allcitys a").click(function(){
																		 var temp=$(this).text();
																		 var spantemp=$(this).find("span").text();
																		 $this.attr("value",temp.replace(spantemp,""));//spantemp+" "+
																		 $this.parent().find("div[id='header_changecity_lable']").html($this.attr("value")+"： ")
																		 $.msgbox.closemsgbox(windowid);
																		 return false;
																		 })
							 $popwin.find("#allcity").click(function(){return false;})
							 $popwin.find("#hotcity").click(function(){showHotCity($this);return false;})
							 }
						 )
				   }
			   
			   var searchCity=function(obj){
				   var offsetleft=$(obj).offset().left;
				   var offsettop=$(obj).offset().top+$(obj).innerHeight();
				   var $popwin=$.msgbox.popupwin(windowid,260,300,"<a id='hotcity'href='#' title='热门城市'>热门城市</a> <a id='allcity'  class='current' href='#' title='所有城市'>所有城市</a>",searchcitypopwincontent).css({"top":offsettop+"px","left":offsetleft+"px"}).addClass("popwin_"+windowid);
				   $popwin.find("#searchmessage").html($(obj).val()+", 按拼音排序");
				   $popwin.find("#resultcitys a").click(function(){var temp=$(this).text();var spantemp=$(this).find("span").text();$this.attr("value",temp.replace(spantemp,""));																		$.msgbox.closemsgbox(windowid);return false;});//spantemp+" "+
				   //显示搜索结果
				   var keyword=$(obj).val().toLowerCase();
				   var $resultcitys=$popwin.find("#resultcitys");
				   if (citydata!=null){
					   $.each(citydata.citys,function(i,item){
//													  var j=0;
													  if (item.enname.toLowerCase().indexOf(keyword)>-1 || item.cnname.toLowerCase().indexOf(keyword)>-1 || item.code.toLowerCase().indexOf(keyword)>-1){
														  var $resultcity=$popwin.find("#resultcitys a:first").clone(true);
														  $resultcity.html("<span>"+item.enname+"</span>"+item.cnname);
														  $resultcitys.append($resultcity.show());
//														  tempcity[j]=item;
//														  j++;
														  }
													  })
					   }
				   if ($resultcitys.find("a").length==1){
					   $popwin.find("#searchmessage").html("对不起，找不到："+$(obj).val());
					   }
//				   $.each(tempcity,function(i,item){
//											var $resultcity=$popwin.find("#resultcitys a:first").clone(true);
//											$resultcity.html("<span>"+item.enname+"</span>"+item.cnname);
//											$resultcitys.append($resultcity.show());
//											})				   
				   $popwin.find("#allcity").click(function(){return false;})
				   $popwin.find("#hotcity").click(function(){showHotCity($this);return false;})
				   //$.get(
//						 "html/searchcity.html",
//						 function(data){
//							 var $popwin=$.msgbox.popupwin(windowid,260,300,"<a id='hotcity'href='#' title='热门城市'>热门城市</a> <a id='allcity'  class='current' href='#' title='所有城市'>所有城市</a>",data).css({"top":offsettop+"px","left":offsetleft+"px"})
//							 $popwin.find("#searchmessage").html($(obj).val()+", 按拼音排序")
//							 $popwin.find("#resultcitys a").click(function(){var temp=$(this).text();var spantemp=$(this).find("span").text();$this.attr("value",spantemp+" "+temp.replace(spantemp,""));																		$.msgbox.closemsgbox(windowid);return false;})
//							 //显示搜索结果
//							 var keyword=$(obj).val().toLowerCase();
//							 
//							 var $resultcitys=$popwin.find("#resultcitys");
//							 if (citydata!=null){
//								 $.each(citydata.citys,function(i,item){
//																if (item.enname.toLowerCase().indexOf(keyword)>-1 || item.cnname.toLowerCase().indexOf(keyword)>-1 || item.code.toLowerCase().indexOf(keyword)>-1){
//																	var $resultcity=$popwin.find("#resultcitys a:first").clone(true);
//																	$resultcity.html("<span>"+item.enname+"</span>"+item.cnname);
//																	$resultcitys.append($resultcity.show())
//																	
//																	}
//																//console.info(item.enname.toLowerCase()+" : "+keyword+"="+item.enname.toLowerCase().indexOf(keyword))
//																})
//								 }
//							 $popwin.find("#allcity").click(function(){return false;})
//							 $popwin.find("#hotcity").click(function(){showHotCity($this);return false;})
//							 }
//						 )
				   }
			   

			   
			   $(this).click(function(){
									 if ($(this).attr("value")==op.tips || $(this).attr("value")==""){
										 $(this).attr("value","")
										 showHotCity(this);
										 }
									 else{//搜索
										 searchCity(this);
										 }
									 
									 })
			   $(this).keyup(function(){
									  var thisobj=this;
									  if (timeoutid != null){clearTimeout(timeoutid);timeoutid = null;}
									  timeoutid=setTimeout(function(){searchCity(thisobj)},250);
									  })
			   }
		   })( jQuery );
