$(document).ready(function() {
   $("body").click( function() {
      $(".search-popup").hide();
   });
   $(".search-form :text").keyup( function(){
      if ($('.search-form :text').val() != ''){
         $.ajax({
            async: false,
            type: 'GET',
            url: 'getting.php',
            data: {'text': $('.search-form :text').val()},
            dataType: 'json',
            success: function (data, textStatus){
               var ul = $('<ul/>');
               var IsAdded = false;
               for (var i = 0; i < data.results.length; i++){
                  var elt = $('<li>' + data.results[i].text + '</li>');
                  elt.bind('click', function(){
                     $(".search-form :text").val( $(this).html() );
                     $('.search-popup').hide();
                  });
                  IsAdded = true;
                  elt.appendTo(ul);
               }
               if (IsAdded){
                  $('.search-popup').empty().append(ul).show();
               }
            },
            error: function(data){
               //$('<div>errr: ' + $('.search-form :text').val() + '</div>').appendTo('#debug');
            }      
         });
      }
      else{
         $('.search-popup').hide();
      }
      return true;
   });
});
