/**
 * 入力補完候補
 * @param searchClass
 * @param node
 * @param tag
 * @return
 */
   var getElementsByClassName = function (searchClass, node, tag) {
       node = node || document, tag = tag ? tag.toUpperCase() : "*";
       if(document.getElementsByClassName){
           var temp = node.getElementsByClassName(searchClass);
           if(tag=="*"){
               return temp;
           } else {
               var ret = new Array();
               for(var i=0; i<temp.length; i++)
                   if(temp[i].nodeName==tag)
                       ret.push(temp[i]);
               return ret;
           }
       }else{
           var classes = searchClass.split(" "),
               elements = (tag === "*" && node.all)? node.all : node.getElementsByTagName(tag),
               patterns = [], returnElements = [], current, match;
           var i = classes.length;
           while(--i >= 0)
               patterns.push(new RegExp("(^|\\s)" + classes[i] + "(\\s|$)"));
           var j = elements.length;
           while(--j >= 0){
               current = elements[j], match = false;
               for(var k=0, kl=patterns.length; k<kl; k++){
                   match = patterns[k].test(current.className);
                   if(!match) break;
               }
               if(match) returnElements.push(current);
           }
           return returnElements;
       }
   };
   var addEvent=(function(){
       if(document.addEventListener){
           return function(type, fn){ this.addEventListener(type, fn, false); };
       }else if(document.attachEvent){
           return function(type,fn){
               this.attachEvent('on'+type, function () {
                   return fn.call(this, window.event);
               });
           };
       }
   })();
   ;(function(window){

   var autoComplete=function(o){
       var handler=(function(){
           var handler=function(e,o){ return new handler.prototype.init(e,o); };
           handler.prototype={
               e:null, o:null, timer:null, show:0, input:null, popup:null,
               init:function(e,o){
                   this.e=e, this.o=o,
                   this.input=this.e.getElementsByTagName(this.o.input)[0],
                   this.popup=this.e.getElementsByTagName(this.o.popup)[0],
                   this.initEvent();
               },
               match:function(quickExpr,value,source){
                   this.popup.innerHTML = '';
                   var li = null;
                   for(var i in source){
                       var tmp = source[i];
                       var tmp = decodeURIComponent(source[i]);
//                       var tmp = decodeURI(source[i]);

                       if( value.length>0 && tmp !=null ){
                           li = document.createElement('li');
                           li.className = 'autoc';
                           li.innerHTML = '<a href="javascript:;">'+tmp+'</a>';
                           this.popup.appendChild(li);
                       }
                   }
                   
                   //最後の行
                   li = document.createElement('li');
                   li.className = 'autoend';
                   li.innerHTML = '<a href="javascript:change_sw();">リスト表示機能をOFFにする</a>';
                   this.popup.appendChild(li);
                   
                   if (this.input.value != '' && this.popup.getElementsByTagName('a').length > 1) {
                       this.popup.style.display='block';
                   } else {
                       this.popup.style.display='none';
                       this.popup.innerHTML = '';
                   }
               },
               ajax:function(type,url,quickExpr,search){
                   //スイッチ
                   var sw = document.getElementById('sw_key').value;
                   if (sw == 'close') {
                       return ;
                   }
                    var that=this;
                    $.post( url, 
                           { sw: search} ,
                           function(data) {
                               that.match(quickExpr,search,data);
                           },"json"
                       );
               },
               fetch:function(ajax,search,quickExpr){
                   var that=this;
                   
                   if (document.getElementById('cat_jn') == undefined) {
                       //特に対応
                       //すべて検索
                       var dt = 0;
                       //著者名
                       var obj = document.getElementById('cat02');
                       if (obj.checked) {
                       	dt = 1;
                       }
                       //収載誌名
                       var obj = document.getElementById('cat03');
                       var fieldObj = document.getElementById('field');
                       if (obj.checked && fieldObj.value == 'jn') {
                       	dt = 2;
                       }
                   } else {
                      dt = 2;
                   }
                   
                   url = ajax.url+ '&dt=' + dt + '&sw=' + search;
                   this.ajax(ajax.type,url,quickExpr,search);
               },
               initEvent:function(){
                   //スイッチ
                   var sw = document.getElementById('sw_key').value;
                   if (sw == 'close') {
                       return ;
                   }
                   
                   var that=this;
                   this.input.onfocus = function(){
                       if(this.inputValue) this.value = this.inputValue;
                       var value=this.value, quickExpr=RegExp('^'+value,'i'), self=this;
                       var els = that.popup.getElementsByTagName('a');

                       if(els.length>0 && this.inputValue) that.popup.style.display = 'block';
                       that.timer=setInterval(function(){
                           if(value!=self.value){
                               value=self.value;
                               //追加
                               that.popup.innerHTML='';
                               that.popup.style.display='none';
                               if(value!=''){
                                   quickExpr=RegExp('^'+value);
                                   if(that.o.source) that.match(quickExpr,value,that.o.source);
                                   else if(that.o.ajax) that.fetch(that.o.ajax,value,quickExpr);
                               }
                           }
                       },500);
                   };
                   this.input.onblur = function(){
                       //clearの対応　削除
                       //if(this.value!=this.defaultValue) this.inputValue = this.value;
                       clearInterval(that.timer);
                       var current=-1;
                       var els = that.popup.getElementsByTagName('a');
                       var len = els.length-1;
                       var aClick = function(){
                           if (this.firstChild.nodeValue != 'リスト表示機能をOFFにする'){
                               var chosed_str = this.firstChild.nodeValue;
                               if(chosed_str.indexOf(' ') > 0){
                                   chosed_str = '"' + chosed_str + '"';
                               }
                               that.input.inputValue = chosed_str;
                           }
                           that.popup.innerHTML='';
                           that.popup.style.display='none';
                           that.input.focus();
                       };
                       var aFocus = function(){
                           for(var i=len; i>=0; i--){
                               if(this.parentNode===that.popup.children[i]){
                                   current = i;
                                   break;
                               }
                           }
                           //that.input.value = this.firstChild.nodeValue;
                           for(var k in that.o.elemCSS.focus){
                               this.style[k] = that.o.elemCSS.focus[k];
                           }
                       };
                       var aBlur= function(){
                           for(var k in that.o.elemCSS.blur)
                               this.style[k] = that.o.elemCSS.blur[k];
                       };
                       var aKeydown = function(event){
                           document.getElementsByTagName('html')[0].style.overflow= 'hidden';
                           document.getElementsByTagName('body')[0].style.overflow = 'hidden';
                           event = event || window.event;
                           if(current === len && event.keyCode===9){
                               that.popup.style.display = 'none';
                           }else if(event.keyCode==40){
                        	   event.preventDefault ? event.preventDefault() : event.returnValue = false;
                               current++;
                               if(current<-1) current=len;
                               if(current>len){
                                   current=-1;
                                   that.input.focus();
                               }else{
                                   that.popup.getElementsByTagName('a')[current].focus();
                               }
                           }else if(event.keyCode==38){
                        	   event.preventDefault ? event.preventDefault() : event.returnValue = false;
                               current--;
                               if(current==-1){
                                   that.input.focus();
                               }else if(current<-1){
                                   current = len;
                                   that.popup.getElementsByTagName('a')[current].focus();
                               }else{
                                   that.popup.getElementsByTagName('a')[current].focus();
                               }
                           }
                       };
                       for(var i=0; i<els.length; i++){
                           els[i].onclick = aClick;
                           els[i].onfocus = aFocus;
                           els[i].onblur = aBlur;
                           els[i].onkeydown = aKeydown;
                       }
                   };
                   this.input.onkeydown = function(event){
                       event = event || window.event;
                       var els = that.popup.getElementsByTagName('a');
                       if(event.keyCode==40){
                    	   event.preventDefault ? event.preventDefault() : event.returnValue = false;
                           if(els[0]) els[0].focus();
                       }else if(event.keyCode==38){
                    	   event.preventDefault ? event.preventDefault() : event.returnValue = false;
                           if(els[els.length-1]) els[els.length-1].focus();
                       }else if(event.keyCode==9){
                           if(event.shiftKey==true) that.popup.style.display = 'none';
                       }
                   };
                   this.e.onmouseover = function(){ that.show=1; };
                   this.e.onmouseout = function(){ that.show=0; };
                   addEvent.call(document,'click',function(){
                       if(that.show==0){
                           that.popup.style.display='none';
                       }
                   });
               }
           };
           handler.prototype.init.prototype=handler.prototype;
           return handler;
       })();
       if(this.length){
           for(var a=this.length-1; a>=0; a--){
               handler(this[a],o);
           }
       }else{
           handler(this,o);
       }
       return this;
   };
   return window.autoComplete = autoComplete;

   })(window);

   addEvent.call(null,'load',function(){
      autoComplete.call( getElementsByClassName('autoComplete'), {
                  ajax:{ type:'post',url:'/index.php?module=Advanced&action=SuppWord' },
                      elemCSS:{ focus:{'color':'#3399FF','background':'#F0F0F0'}, blur:{'color':'black','background':'transparent'} },
                      input:'input',
                      popup:'ul'
       });
   });
   
   function change_sw(){
       document.getElementById('sw_key').value = 'close';
       var cookie = new Cookie;
       cookie.set('sw_key', 'close');
   }
   
   function trim(str){
       return str.replace(/(^\s*)|(\s*$)/g, "");
   }

