/**
 *  ##### 可控自动定位下来列表双输出版本 UTF-8版 ####
 *  #         @author MrJ
 *  #         @Email:jindaibin@126.com
 *  #         @VerSion 1.0
 *  #         @CopyRight www.cts.cn
 *  #         @Date 2009.10.27
 *  ############################################
 */
 
 var menu_list = {                   //数据显示对象
     width        :100,              //外框宽度
	 //height       :100,            //外框高度
	 b_colr       :"#ccc",           //外框颜色
	 b_bgclr      :"#fff",           //外框背景色
	 color        :"#EF7602",        //字体默认颜色
	 mcolor       :"#F5CC90",        //鼠标选中颜色
	 html         :'',               //内容容器
	 outobj       :'',               //输出对象
	 outvalobj    :'',               //输出对象id值
	 showboxobj   :'',               //对话框对象
	 showbox      :1,                //判断是否显示了box
	 showboxmouse :0,                //判断是否隐藏
	 shownum      :10,               //默认一次性展示多少条数据
	 showboxchlid :'',               //框内对象
	 showboxselect:0,                //选中的对象
	 datalen      :0,
	 fSetObj:function(obj){
	   //绑定第二输出对象
	   this.outvalobj=obj;
	 },    
	 fShow:function(obj)
	 {
	   //展示内容
	   this.showboxmouse  = 0;
	   this.outobj        = obj;              //绑定对象
	   if(this.showbox)                       //防止重复添加box
	   {this.showboxobj    = this.fCreateTag(document.body,"div");}
	   else
	   {this.showboxobj.style.display="block";}
	   this.showbox = 0;
	   this.showboxobj.style.width  = this.outobj.clientWidth+"px";
	   //this.showboxobj.style.height = this.height+"px";
	   this.showboxobj.style.backgroundColor=this.b_bgclr;
	   var A = this.fSetXY(this.outobj);
	   this.showboxobj.style.top      = A.y+this.outobj.clientHeight+10+"px";
	   this.showboxobj.style.left     = A.x+4+"px";
	   this.showboxobj.style.position ="absolute";
	   this.showboxobj.style.border   ="1px solid "+this.b_colr;
	   this.showboxobj.style.border   ="1px solid "+this.b_colr;
	   this.showboxobj.style.color    =this.color ;
	   this.showboxobj.onmouseover   = function()
	   {
	      menu_list.showboxmouse = 0;
	   }
	   this.showboxobj.onmouseout    = function()
	   {
	      menu_list.showboxmouse = 1;
	   }
	   this.showboxobj.innerHTML = "支持拼音或汉字查询";
	   this.fSetObjChange();  //设置输入对象事件
	 },
	 fSetXY:function(obj)
 	 {//获得坐标
		var A = {x:0,y:0}; //保存对象
		var temp = obj;
	    do{
		  A.y+=temp.offsetTop;
		  A.x+=temp.offsetLeft;
		  temp=temp.offsetParent;
	    }while(temp.tagName!="BODY")
		return A;
	 },
	 fCreateTag:function(o,tag)
	 {//创建元素
	     var a = document.createElement(tag);
		 //a.innerHTML='a';  // #test
         o.appendChild(a);
		 return a;
	 },
	 fSetObjChange:function()
	 {
	     //设置对象事件 兼任ie forefox
		 if(Browser)
		 {
		   this.outobj.onpropertychange=function(e){
		    //ie 输入事件
		    menu_alist.fShow(this.value);
		   }//end onpropertychange
		 }//else if
		 else
		 {
		   this.outobj.addEventListener("input",function(){
		   //ff 输入事件
		    menu_alist.fShow(this.value);
		   },false);
		 }
	 },//end fSetObjChange
	 fShowAlist:function(data)
	 {
	    //显示内容
		var html = ''; //输出容器
		var num  = this.shownum;//一次性展示多少条数据
		this.showboxobj.innerHTML = '';//清除框中内容
		this.datalen = data.length;
		for(var i=0;i<data.length&&i<num;i++)
		{
		  //html += "<dt value='"+data[i].id+"'>"+data[i].name+"</dt>";
		  html = this.fCreateTag(this.showboxobj,"dt");
		  html.innerHTML = data[i].title;
		  html.setAttribute('value',data[i].did);
		}
		//this.showboxobj.innerHTML=html;
		this.fSetBoxListSty(data);
	 },//end fShowAlist
	 fSetBoxListSty:function(str)
	 { //设定内容样式
	     if(str.length>0)
		 {
		    this.showboxchlid = this.showboxobj.getElementsByTagName("dt");
			for(var i=0;i<this.showboxchlid.length;i++)
			{
			    this.showboxchlid[i].style.listStyle ='none';
				this.showboxchlid[i].style.padding   = 4+"px";
				this.showboxchlid[i].style.margin    = 0+"px";
				this.showboxchlid[i].style.color     = this.color;
				this.showboxchlid[i].setAttribute('i',i);
				this.showboxchlid[i].style.cursor    = 'pointer';
				this.showboxchlid[i].onmouseover     = function()
				{
				   var j = menu_list.showboxselect;   //保存的对象
				   menu_list.showboxselect = this.getAttribute('i');
				   menu_list.showboxchlid[j].style.backgroundColor = menu_list.b_bgclr;
				   this.style.backgroundColor=menu_list.mcolor;
				};
				this.showboxchlid[i].onclick      = function(){
				   menu_list.fObjSelect();
				};
			
			}//end for
		 }//end if
	 },//end fSetBoxListSty
	 fObjSelect:function()
	 {
	     this.outvalobj.value=this.showboxchlid[this.showboxselect].getAttribute('value');
		 this.outobj.value   =this.showboxchlid[this.showboxselect].innerHTML;
		 menu_list.showboxobj.style.display="none";
	 }//end fObjSelect
 }; //end menu_list
 
 var menu_alist = {              //数据处理对象
    //框内对象
	data:'',                      //绑定目的json 数据
	cx  :'2',                    //默认查询为汉字
	redata:'',                   //输出对象            
	fShow:function(str)
	{
	   //显示对象
	   if(str.length>0)
	   {
	      //有输入内容
		  this.cx = "name";
		  if(str.match(/^[a-zA-Z]+$/))
		  { //拼音查询
			  this.cx = "1";
		  }
		  this.fSearchData(str); //开始查询
		  menu_list.showboxobj.style.display="block"; //关闭对象
	   }//end if
	   else
	   {
	      menu_list.showboxobj.style.display="none"; //关闭对象
	   }
	},//end function
	fShowI:function(i)
	{
	    var newData = "this.data["+i+"]."+this.cx+"";
		//alert(eval(newData));
		return eval(newData);
	},//end fShowI
	fSearchData:function(str)
	{
		//alert(str);
	    $.get('ajax.php',{"action":"trip","ac":"market_trip_address","type":this.cx,"keys":str},function(data){
				//alert(data);
				data = eval(data);
				menu_alist.redata = data;
				//alert(data);
				menu_list.fShowAlist(data); //回传到UI现实对象
		});
		//alertobj(data[0]);
		//this.redata = data;
		//menu_list.fShowAlist(data); //回传到UI现实对象
	}//end fSearchData
 }; //end menu_alist

var Browser =(function(){
//获取浏览器版本
  var temp = window.navigator.appName;
  if(temp.indexOf("Microsoft")==-1)
  {
     //为非微软浏览器
	 return false;
  }
  return true;
})();
 //##公共事件
document.onclick=function()
{
   if(menu_list.showboxmouse)
   {
      menu_list.showboxobj.style.display="none";
   }
}
document.onkeyup=function(e)
{
   var evt = e;      //ie
   if(typeof(e)=='undefined')
   {
      evt = window.event;
   }
   var kyc = evt.keyCode;
   var num = menu_list.datalen-1;
   var now = menu_list.showboxselect;
   if(true)
   {
      if(kyc==38)
	  {
	      now--;
	  }
	  else if(kyc==40)
	  {
	     now++;
	  }
	  else if(kyc==13)
	  { 
	     menu_list.fObjSelect();
	  }
	  if(now<0)
	  {
	    now = num;
	  }
	  else if(now>num)
	  {
	    now = 0;
	  }
	  
	   var j = menu_list.showboxselect;   //保存的对象
       menu_list.showboxchlid[j].style.backgroundColor = menu_list.b_bgclr;
	   menu_list.showboxchlid[now].style.backgroundColor=menu_list.mcolor;
	   menu_list.showboxselect = now;
   }//end if
}//end function
