
 
 
 var ajax_lister = Class.create();
 ajax_lister.prototype = {
	initialize : function(input, opener, result, url, xmldata) {
		this.input = input;
		this.opener = opener;
		this.result = result;
		this.resultlist = result+'res';
		this.fulltext = result+'fulltext';
		this.url = url;
		
		this.xmldata = xmldata;
		this.parstmp = $H(Object.extend({}, arguments[5] || {})).toQueryString();
		this.pars='';
	  this.last_search='';
	  var t;
	  
	  $(this.opener).style.cursor = 'pointer';
	  
	  if ($(this.opener)){
      $(this.opener).onclick = function(){
        this.visible = $(this.result).style.display=='none' ? false : true;
        if (!this.visible) {this.showhidden();}
        if (($(this.input).value != '') && ($(this.input).value != $(this.input).getAttribute('title')) ){
          this.getxml();
        }
        Field.activate(this.input);
      }.bind(this);
      }
	  
	   if ($(this.input)) {
      $(this.input).onkeyup = function(){
      //alert($(this.result).style.display)
        if (($(this.result).style.display!='none')&& ($F(this.input)!=$(this.input).getAttribute('title'))) { 
         if (this.timer) {window.clearTimeout(this.timer);}
	       this.timer = setTimeout(this.getxml.bind(this),700);
        }
      }.bind(this);
      }
    
    
    
	},
	showhidden: function(){
	    
        Effect.toggle(this.result, 'appear',{duration: 0.45});
      this.visible = $(this.result).style.display=='none' ? false : true;  
        $(this.resultlist).innerHTML='';
          
     
      
  },

	getxml: function (){
          var searchval = String($(this.input).value);
          searchval = searchval.trim();
          if ((this.last_search!=searchval) && (searchval.length>=1)){
          last_search=$(this.input).value;
          searchval = searchval.replace('&','*amp*');
          this.pars = this.parstmp+'&keyword='+searchval;
          var myAjax = new Ajax.Request( this.url, { method: 'post', parameters: this.pars, onComplete: this.fillResult.bind(this)});
          }
  },
  fillResult: function(originalRequest){           
          xmldoc = originalRequest.responseXML;
          var companys = xmldoc.getElementsByTagName(this.xmldata);
          var prevlist;
          $(this.resultlist).innerHTML = '';
          $(this.fulltext).innerHTML = 'Gépelje be a keresőszót...';
          var list = document.createElement("ul");
          list.id='col';
          $(this.resultlist).appendChild(list);
          var divide = Math.ceil(companys.length/4);
          
          for (var i = 0; i < companys.length; i++) 
            {
              var id = companys[i].getAttribute("id");
              if (companys[i].textContent){
                var text = unescape(companys[i].textContent);
              } else {
                var text = unescape(companys[i].childNodes(0).nodeValue);
              }
              text = text.replace('*amp*','&');
              var textlength = text.length;
              var title = text;
              text = textlength > 20 ?  text.substring(0,18)+'...' : text;
              
              //var img = document.createElement('img');
              //img.src='images/tudakozo_pluszjel.jpg';
              var newText = document.createTextNode(text);
	            var newNode = document.createElement('li');
	            //newNode.appendChild(img);
	            newNode.appendChild(newText);
	            newNode.setAttribute('title', title);
	            newNode.onclick = function(e){
	              var obj;
	               if (!e) var e = window.event;
	               if (e.target) obj = e.target;
	                 else if (e.srcElement) obj = e.srcElement;
	               if (obj.nodeType == 3) // defeat Safari bug
		                obj = obj.parentNode;
                $(this.input).value = obj.getAttribute('title') =='-' ? $(this.input).getAttribute('title') : obj.getAttribute('title');
                this.showhidden();
                Field.activate(this.input);
              }.bind(this);
              
              newNode.onmouseover = function(e){
	              var obj;
	               if (!e) var e = window.event;
	               if (e.target) obj = e.target;
	                 else if (e.srcElement) obj = e.srcElement;
	               if (obj.nodeType == 3) // defeat Safari bug
		                obj = obj.parentNode;
		                
                $(this.fulltext).innerHTML = obj.getAttribute('title') !='-' ? obj.getAttribute('title') : 'Bezárja ezt az ablakot.';
              }.bind(this);
	            list.appendChild(newNode);
	            if ((i+1)%divide==0){
                
                if (prevlist) {prevlist.parentNode.insertBefore(list,prevlist.nextSibling);}
                prevlist = list;
                var list = document.createElement("ul");
                list.id='col'+1;
              }
	            
	            
            }
            
            $(this.resultlist).appendChild(list);
          }
  }
//function initpager() { mypager =  new Pager('pagerdiv', 'pager', 15, '/pager.php', {type: 'content'});} 
//Event.observe(window, 'load', initpager, false);
