	 function sleep ( naptime )
	 {
	 	
         var sleeping = true;
         var alarm;
         var now = new Date();
         var startingMSeconds = now.getTime();
         while( sleeping )
         {
             alarm = new Date();
             alarmMSeconds = alarm.getTime();
             if(alarmMSeconds - startingMSeconds > naptime)
             { 
             	sleeping = false; 
             }
         }        

     }

     
     	var old_query = "";
		function check()
		{
			element = document.getElementById('query')
			q = element.value

			if( old_query == q  )
				return;
				
			old_query = q;
					
			$('#gesla').hide("fast")
			
			$.ajax(
			{
				type: "POST",
				url: "/include/extra/searchKey.php",
				dataType: "html",
				data: 'query=' + q,
				error: function()
			    {
			    	
			    	$('#gesla').html("Napaka")
				    $('#gesla').show("slow")
			    },
			    success: function(html)
			    {
			    	$('#gesla').html(html)
				    $('#gesla').show("slow")
				    
				    $('.keys').click( func );
			    }
			
			});
		}
     
     	var callSleep = false;
		var old_time = 0;
		
    	var func = function(event)
		{
			event.preventDefault();
			temp = this.name.split("_");
			id = temp[1];
			
			myquery = $('#query').val()
			
			
			$('#gesla').hide("fast")
			
			$.ajax(
			{
				type: "POST",
  				url: "/include/extra/getKey.php",
  				dataType: "html",
  				data: 'id=' + id + '&query=' + myquery,
  				error: function()
			    {
			    	
			    	$('#gesla').html("Napaka")
				    $('#gesla').show("slow")
			    },
			    success: function(html)
			    {
			    	$('#gesla').html(html)
				    $('#gesla').show("slow")
				    
				    $('#back').click( func2 );
			    }

			}
			
			) 
		}
		
		var func2 = function ( event )
		{
			event.preventDefault();
			if ( !callSleep )
			{
				var now1 = new Date();
				myquery = document.getElementById('query').value
	 
				t  = now1.getTime()
				
				if( ( old_time + 2000 ) > t  )
				{
					time = 2000 - t + old_time
					callSleep = true;
					sleep( time )
					callSleep = false;
				}
				old_time = t
					
				$('#gesla').hide("fast")
				
				$.ajax(
				{
					type: "POST",
					url: "/include/extra/searchKey.php",
					dataType: "html",
					data: 'query=' + myquery,
					error: function()
				    {
				    	
				    	$('#gesla').html("Napaka")
					    $('#gesla').show("slow")
				    },
				    success: function(html)
				    {
				    	$('#gesla').html(html)
					    $('#gesla').show("slow")
					    
					    $('.keys').click( func );
				    }
				
				});
			}
			else
			{
				
			}
			 
		}
	
	var now2 = new Date();

	$(document).ready(function() 
	{
		
		
		
		
		
		
		
		$('#erase').click(function(event)
		{
			
			$('input[name="query"]').val("")
			
		});
		
		$('.keys').click( func );
		

		setInterval ( 'check()', 1500 );
		
		
		
		
		
	});
