$(document).ready(function() {
   var suggestions = $('<ul class="suggestions"></ul>').appendTo('#box-datakuutio .autocomplete').hide();
   var searchvalue = false;
   var placeholder;

   // keyword search
   //$('#box-datakuutio input[type=submit]').hide();
   $('#box-datakuutio input[type=text]').keydown(function(e){
       var current = suggestions.find('li.active');

       switch (e.which) {
           case 40: // down
               if (!current.length) {
                   current = suggestions.find('li:first-child');
               } else {
                   current.removeClass('active');
                   current = current.next('li');
               }
               
               if (current.length) {
                   current.addClass('active');
               }
               
               return false;
               
           case 38: // up
               if (!current.length) {
                   current = suggestions.find('li:last-child');
               } else {
                   current.removeClass('active');
                   current = current.prev();
               }

               if (current.length) {
                   current.addClass('active');
               }
               
               return false;
           
           case 13:
               if (current.length) {
                   $(this).val(current.text());
               }
               
               $('#box-datakuutio form').submit();
               return false;
       }
       
       searchvalue = $(this).val();
   }).keyup(function(){
       var input = $(this);
       
       if (searchvalue == input.val()) {
           return false;
       }
       
       searchvalue = $(this).val();
       
       if (searchvalue.length > 2) {
           $.ajax({
               data: {q: searchvalue, srch: 'keyword'},
               dataType: 'json',
               success: function (data) {
                   suggestions.empty();
                   
                   for (var i = 0; i < data.length; i++) {
                       $('<li class="' + (i % 2 ? 'even' : 'odd') + '">' + data[i] + '</li>').appendTo(suggestions).hover(function(){
                           suggestions.find('li.active').removeClass('active');
                           $(this).addClass('active').click(function(){
                               input.val($(this).text());
                               $('#box-datakuutio form').submit();
                           });
                       }, function(){
                           suggestions.find('li.active').removeClass('active');
                       });
                   }
                   
                   if (suggestions.find('li').length) {
                       suggestions.show();
                   } else {
                       suggestions.hide();
                   }
               },
               type: 'get',
               url: '/avustustoiminta/datakuutio/ajaxQuery.php'
           });
       } else {
           suggestions.hide();
       }
   }).focus(function(){
       if (searchvalue == false) {
           placeholder = $(this).val();
           $(this).val('');
       }
   }).blur(function(){
       if ($(this).val() == '') {
           $(this).val(placeholder);
       }
   });
});