/*
	author:lgp
	date  :2008-10-09
	useage: $('XXX').combobox();
	dep   : jquery 1.2.6 
*/
(function($){
	$.fn.combobox=function(){
		var select=$(this);
		var useItem=-1;
		var useText=null;
		var top=select.offset().top;
		var left=select.offset().left;
		var height=select.height();
		var width=select.width();
		
		var html='<div class="comboboxContainer">';
		    html+='<div class="comboboxTitle">';
		    html+='</div><div class="comboboxBtn"></div>';
		    html+='</div>';
		    html+='<div class="comboboxItemsDiv">';
		    html+='<ul class="comboboxItems">';
		   	html+='</ul>';
		   	html+='</div>';
		var container=$(html);
		select.after(container.eq(0));
		$("#ts06_sysframe").after(container.eq(1));
		
		var title=container.eq(0).children().eq(0); //div
		var titleBtn=container.eq(0).children().eq(1); //div
		var options=container.eq(1).children().eq(0); //ul
		
		hover(container.eq(0).children());
		container.css({width:width,height:height});
		title.css({left:0,top:0,width:width-18,height:height});
		titleBtn.css({left:width-18,top:0,width:18,height:height});
		container.eq(1).css({left:left,top:top+height,width:width});
		options.css({left:left,top:top+height,width:width});
		
		//height 不定
		//select.css({"position":"absolute",top:100,left:100,width:0,height:0});
		select.hide();
		select.click(function(){});
		container.eq(0).click(function(){
			show();
		});
		container.eq(0).keypress(function(e){
					combobox.ajaxupdate2(String.fromCharCode(e.keyCode));
		});
		var combobox={
			url:"travel_serch_auto2.php",
			ajaxupdate2:function(char){
				$.post(this.url,$.extend({ch:char,action:"keypress"},combobox.getKeyPressData()),
						function(data){
							var html="";
							if(data.data)
							$.each(data.data,function(k,v){
							html+='<option value="'+v.value+'" html="'+v.html+'" >'+v.text+'</option>';						
							});
							select.empty();
							select.append(html);
							combobox.update();
						},"json");
			},
			ajaxupdate:function(data,method){
				//update select
				$.post(this.url, $.extend({action:"change"},data),
					function(data){
					/*
						var str="";
						$.each(data,function(k,v){
							str+=k+":"+v+"\n";
						});
						alert(str);
					*/
						var html="";
						if(data.data){
						$.each(data.data,function(k,v){
							html+='<option value="'+v.value+'" html="'+v.html+'" >'+v.text+'</option>';						
						});
						}
						select.empty();
						select.append(html);
						combobox.update();	
						show();
						container.eq(0).focus();
					},"json"
				);
			},
			update:function(){
				var html="";
				var useText;
				select.children().each(function(k,v){
				   var temp=$(v).attr("html")?$(v).attr("html"):'';
				   if(v.selected){useItem=v.value;useText=temp+v.text;}
				   html+='<li class="comboboxItem" value="'+v.value+'">'+temp+v.text+'</li>';
				});
				options.html(html);
				title.html(useText);
				options.css({height:(select.children().size()>12?12:select.children().size())*height});
				hover(options.children());
				//select
				options.children().click(function(){
					hid();
					if(select.val()!=this.value){
						title.html($(this).html());
						select.val(this.value);
						select.trigger("change");
					}
					$(document.body).after(select.width());
				});
			}
		};
		combobox.update();
		function isin(x,y,rect)
		{	
			if(y<rect[0]||y>rect[2]||x<rect[1]||x>rect[3])
				return false
			return true
		}
		
		function show(){
			container.eq(1).show();
			container.eq(1).css({"z-index":999999});
		}
		
		function hid(){
			container.eq(1).css({"z-index":10});
			container.eq(1).hide();
		}
		function hover(obj){
			var global_temp=null;
			obj.mouseover(function(){
				$(this).addClass(this.className+'Hover');
			});
			obj.mouseout(function(){
				if(this.className.split(" ").length==2)
					this.className=this.className.split(" ")[0]			
			});
		}
		function sleep(naptime){
      var sleeping = true;
      var now = new Date();
      var alarm;
      var startingMSeconds = now.getTime();
      while(sleeping){
         alarm = new Date();
         alarmMSeconds = alarm.getTime();
         if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; }
      }      
		}
		$(document).click(function(e){
			for(var i=0;i<2;i++){
				var rect=[
				container.eq(i).offset().top,
				container.eq(i).offset().left,
				container.eq(i).offset().top+container.eq(0).height(),
				container.eq(i).offset().left+container.eq(0).width(),
				];
				if(isin(e.clientX,e.clientY,rect))
				return true;
			}
			hid();
		});
		return combobox;
	}
})(jQuery);
