
var lastkey  ;
var selected=1;
$(document).bind("keydown click",function(e) 
			   {
			    lastkey= (e.keyCode ? e.keyCode : e.which); 
			    $('#keypress').html("lastkey:"+ e.keyCode +"|"+ e.which+"|"+ e.type );   

			   }
	           );

function set_select(id,name,query)
{
  $('#'+name).bind("click keydown", function() {

                                  var listname=id+'_list';
                                  
                                   $('.listbox:not(#'+listname+')').hide();

                                  if ( $("#"+listname).length == 0 ) 
                                  {
                                   $("body").append("<div class='listbox' id='"+listname+"'>lista töltése..<div>");
                                   selected=1;
                                   var offset = $(this).offset(); 
                                   $('#'+listname).offset( offset ) ;			                                   
                                   $('#'+listname).width( $(this).width() );
                                   $('#'+listname).load('load.php?q='+query,
                                                        function() {
                                                                
                                                                $('.inputspace').width( $(this).width() );
                                                                $('.inputspace').height( $(this).height() );

								$('.listboxitem_'+query).click(
					                                   function(){
	                                                                      //alert("clicked");
 	                                                                      $('#'+name).val( $(this).html());
	                                                                      $('#'+id).val( $(this).attr("id") );
	                                                                      $('#'+listname).hide();	                                                                      
	                                                                             }
                                                                                   );
                                                                 
                                                                  }
                                                     );


                                   $('#'+listname).mouseleave(function() {                                       
                                                                     $("#"+listname).hide();
                                                                        }
                                                           );


                                  }

                                  if (lastkey==40)
                                    {
                                     $('#'+listname).show(); 
                                    }
                                    else if (lastkey==38)
                                    {
                                     $('#'+listname).hide(); 
                                    }
                                    else
                                    {
                                      $('#'+listname).toggle();
                                    }

                                  }
                       
                     );
}




function set_search(id,name,query)
{
  /*
  $('#'+name).keydown( function() 
                       {
                        if (lastkey=40)
                       	   {
                       	   
                           }
                       }  
                     );
  */
  $('#'+name).bind('keyup click ',function(e) {

                                  var listname=id+'_list';
                                  var dropdown= 0 ;
                                  if (lastkey=40) {dropdown=1;}


                                  $('.listbox:not(#'+listname+')').hide();

                                  // ----------------------- if empty --------------
                                  if ( $(this).val()=='')
                                  {
                                       $('#'+id).val('');
                                  }

                                  if ( $("#"+listname).length == 0 ) 
                                  {
                                   $("body").append("<div class='listbox' id='"+listname+"'>lista töltése..<div>");   								
                                   var offset = $(this).offset(); 
                                   $('#'+listname).offset( offset ) ;			

                                   $('#'+listname).width( $(this).width() );


                                   $('#'+listname).mouseleave(function() {                                       
                                                                     $("#"+listname).hide();
                                                                        }
                                                           );


                                  }


                                   $('#'+listname).load('load.php?q='+query+'&s='+ escape( $(this).val() ),
                                                        function() {
                                                                result='.listboxitem_'+query+"_exact";
                                                                if ($(result).length !=0 && dropdown == 0)
                                                                   {          
 	                                                                      $('#'+name).val( $(result).html());
	                                                                      $('#'+id).val( $(result).attr("id") );
	                                                                      $('#'+listname).hide();
                                                                   }

                                                                $('.inputspace').width( $(this).width() );
                                                                $('.inputspace').height( $(this).height() );
								$('.listboxitem_'+query).click(
					                                   function(){
	                                                                      //alert("clicked");
 	                                                                      $('#'+name).val( $(this).html());
	                                                                      $('#'+id).val( $(this).attr("id") );
	                                                                      $('#'+listname).hide();	                                                                      
	                                                                             }
                                                                                   );

                                                                  }
                                                     );

                                  $('#'+listname).show();

                                  }
                       
                     );
}





function set_id_search(id,name,query)
{
  $('#'+id).keyup( function() {

                                  var listname=id+'_list';

                                  $('.listbox:not(#'+listname+')').hide();

                                  // ----------------------- if empty --------------
                                  if ( $(this).val()=='')
                                  {
                                  $('#'+name).val('');
                                  }


                                  if ( $("#"+listname).length == 0 ) 
                                  {
                                   $("body").append("<div class='listbox' id='"+listname+"'>lista töltése..<div>");   								
                                   var offset = $('#'+name).offset(); 
                                   $('#'+listname).offset( offset ) ;			

                                   $('#'+listname).width( $('#'+name).width() );


                                   $('#'+listname).mouseleave(function() {                                       
                                                                     $("#"+listname).hide();
                                                                        }
                                                           );


                                  }


                                   $('#'+listname).load('load.php?q='+query+'&s='+ escape( $(this).val() ),
                                                        function() {
                                                                result='.listboxitem_'+query+"_exact";
                                                                if ($(result).length !=0 )
                                                                   {          
 	                                                                      $('#'+name).val( $(result).html());
	                                                                      $('#'+id).val( $(result).attr("id") );
	                                                                      $('#'+listname).hide();
                                                                   }
                                                                $('.inputspace').width( $(this).width() );
                                                                $('.inputspace').height( $(this).height() );
								$('.listboxitem_'+query).click(
					                                   function(){
	                                                                      //alert("clicked");
 	                                                                      $('#'+name).val( $(this).html());
	                                                                      $('#'+id).val( $(this).attr("id") );
	                                                                      $('#'+listname).hide();	                                                                      
	                                                                             }
                                                                                   );

                                                                  }
                                                     );

                                  $('#'+listname).show();

                                  }
                       
                     );
}





function set_autocomplete(id,name,query)
{

  $('#'+name).bind('keyup click ',function(e) {

                                  var listname=id+'_list';
                                  
                                  $('.listbox:not(#'+listname+')').hide();

                                  // ----------------------- if empty --------------

                               if ($('#'+name).val().length >=1  )
                               {

                                  if ( $("#"+listname).length == 0 ) 
                                  {
                                   $("body").append("<div class='listbox' id='"+listname+"'><div>");   								
                                   var offset = $(this).offset(); 
                                   $('#'+listname).offset( offset ) ;			

                                   $('#'+listname).width( $(this).width() );


                                   $('#'+listname).mouseleave(function() {                                       
                                                                     $("#"+listname).hide();
                                                                        }
                                                           );


                                  }
                               //setTimeout(function(){
                                  // 
                               typewatch(function () {
                                   var thisname =  $('#'+name) ;
                                   // alert("load--"+listname); // +$(this).val() 
                                   $('#'+listname).load('load.php?q='+query+'&s='+ escape( thisname.val() ),
                                                        function() {
                                                                result='.listboxitem_'+query+"_exact";
                                                                if ($(result).length !=0 && dropdown == 0)
                                                                   {          
 	                                                                      $('#'+name).val( $(result).html());
	                                                                      $('#'+id+"_ex").val( $(result).attr("id") );
	                                                                      $('#'+listname).hide();
	                                                                      submit();
                                                                   }

                                                                $('.inputspace').width( thisname.width() );
                                                                $('.inputspace').height( thisname.height() );

                                                                // --------show all -----------
								$('.listboxitem_'+query+"_showall").click(
					                                   function(){
	                                                                      $('#'+name).closest("form").submit();
	                                                                      $('#'+listname).hide();	                                                                      
	                                                                             }
                                                                                   );


                                                                // ---------- sho exact ----------
								$('.listboxitem_'+query).click(
					                                   function(){
	                                                                      //alert("clicked");
 	                                                                      //$('#'+name).val( $(this).html());
	                                                                      // $('#'+id).val( $(this).attr("id") );
	                                                                      //$('#'+listname).hide();	                                                                      
	                                                                      $(location).attr('href','?c='+$(this).attr("id"));
	                                                                      //$('#'+name).closest("form").attr('action')='?c='+$(this).attr("id");
	                                                                      //$('#'+name).closest("form").submit();
	                                                                             }
                                                                                   );

                                                                  }
                                                     );


                                   }, 800);

                                  $('#'+listname).show();
                                  };


                                  }
                       
                     );
}

var typewatch = (function(){
  var timer = 0;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  }  
})();



