jQuery.fn.ComZaspxTextBox = function(options){   
       var $ = jQuery;
       var defaults = {    
          height:8,
          cache:false,
          maxCount:8,
          ajaxUrl:'/ajax/textBoxSelect.ashx'
       }; 
       var o = $.extend(defaults, options);
       var obj = {};
       obj.target = this;      
       obj.divDisplay = false; 
       obj.selectIndex = -1;
       obj.isSelectStatus = false;
       obj.lastValue = "";
       obj.maxCount = o.maxCount;
       obj.textBoxDiv = $(".ComZaspxtextBox");      
       obj.target.keyup(function(evt){  //键盘按下去事件         
           obj.textBoxDivli = $(".ComZaspxtextBox>ul>li");             
           var id = $(this);       
            if($.trim(id.val()) == ""){               
                if(obj.divDisplay){     
                     obj.fun.divClose();
                }                
                obj.lastValue = "";
                return;               
           }   
           if(evt.keyCode == 13)  //回车
           {
                if(obj.divDisplay){
                    obj.fun.divClose();                    
                }               
           }
           if(evt.keyCode == 40 || evt.keyCode == 38){          
            if(obj.textBoxDivli.length==0){               
                 return ;  //如果数据还在请求中,则退出 不操作
             }  
             if(!obj.divDisplay){  
                obj.fun.divShow(id);   
                if(obj.textBoxDivli[obj.selectIndex].className.indexOf('ComZaspxtextBox_Select') == -1){
                    obj.textBoxDivli[obj.selectIndex].className = 'ComZaspxtextBox_Select';
                } 
                return ;
             }             
           if(evt.keyCode == 40 ){  //下            
             if(obj.selectIndex == obj.maxCount-1){ //如果选中下拉菜单到了最下面,则退出
                return ;
             }                
             if(obj.selectIndex !=-1){       //如果选中的不是第一个菜单,则要先去掉原来菜单的样式               
                $(obj.textBoxDivli[obj.selectIndex]).removeClass("ComZaspxtextBox_Select");
             }           
             var selectItem = $(obj.textBoxDivli[obj.selectIndex+1]);             
             selectItem.addClass("ComZaspxtextBox_Select");   
             var txt = selectItem.attr("txt");               
             obj.target.val(txt);            
             obj.selectIndex += 1;      
             obj.lastValue = txt;             
           }else if(evt.keyCode == 38){ //上             
         //  alert(obj.selectIndex)
             if(obj.selectIndex == 0 || obj.selectIndex == -1){ //如果选中下拉菜单到了最下面,则退出
                if(obj.textBoxDivli[0].className.indexOf('ComZaspxtextBox_Select') == -1){
                    obj.textBoxDivli[0].className = 'ComZaspxtextBox_Select';
                }
                return ;
             } 
             if(obj.selectIndex == obj.maxCount){
                obj.selectIndex = obj.maxCount -1;
             }
             $(obj.textBoxDivli[obj.selectIndex]).removeClass("ComZaspxtextBox_Select");
             var selectItem = $(obj.textBoxDivli[obj.selectIndex-1]);
             selectItem.addClass("ComZaspxtextBox_Select");
             var txt = selectItem.attr("txt");         
             obj.target.val(txt);
             obj.selectIndex -= 1;
             obj.lastValue = txt;     
           }}else{          
                var value = $.trim(id.val());          
                if(obj.lastValue != value){
                    obj.fun.ajaxSelect(value,o);          
                    obj.lastValue = value;
                }
           }    
       }).blur(function(){   //文本框失去焦点事件       
          if(obj.divDisplay){              
               if(obj.textBoxDiv){
                   if(!obj.isSelectStatus){
                       obj.fun.divClose();
                   }                   
               }
          }
       });
      
      obj.fun = {
          divClose:function(txt){  //关闭下拉列表        
             if(txt){
                obj.target.val(txt);
             }
             obj.textBoxDiv.css("display","none");
             obj.divDisplay = false;
          },
          divShow:function(id){                
               if(!obj.divDisplay){              
                    var offset = id.offset();             
                    var top = offset.top+obj.target.height()+o.height;
                    if(obj.textBoxDiv){
                        obj.textBoxDiv.css({top:top,left:offset.left,display:"block"});
                        obj.divDisplay = true;
                     }
               }        
          },
          ajaxSelect:function(val,o){  //真正处理函数           
             $.ajax({
                url:o.ajaxUrl,
                cache:o.cache,
                data:{count:o.maxCount,txt:escape(val)},
                //dataType:"json",
                success:function(msg){
                  //  alert(msg);
                    if(msg!=""){                         
                         obj.fun.divShow(obj.target); //如果没有数据,则隐藏div
                    }
                    obj.textBoxDiv.html(msg);          
                    obj.selectIndex = -1 ; //重新初始化索引   
                    var id =  $(".ComZaspxtextBox>ul>li");  
                    obj.maxCount = id.length;   
                  //  alert($(".ComZaspxtextBox>ul>li").html());
                   id.click(obj.fun.textBoxDivLiClick).hover(obj.fun.textBoxDivLiOver,obj.fun.textBoxDivLiOut);  //重新绑定一下事件                    
                 //   alert(msg);
                },
                error:function(msg){
                    alert(msg);
                }
             });
          },
          textBoxDivLiClick:function(){  //单击下拉列表中的一列事件 
              var id = $(this);
              obj.fun.divClose(id.attr("txt"));                        
         },
         textBoxDivLiOver:function(){      
                obj.selectIndex = parseInt($(this).attr("index"));
                $(".ComZaspxtextBox_Select").removeClass("ComZaspxtextBox_Select");
                $(this).addClass("ComZaspxtextBox_Select");
                obj.isSelectStatus = true;
         },
         textBoxDivLiOut:function(){       
                $(this).removeClass("ComZaspxtextBox_Select");
                obj.isSelectStatus = false;
        }
      }
}
