Complete = new Object();

Complete.create = function (topic_id) 
{
   this.url = "/suggest/";
   this.input_request = $(".input-request");
   this.topic_id = topic_id;
   this.is_list_displayed = false;
   this.active_id = -1;
   this.total_div_count = 0;
   this.first_request = '';
   this.preview_timer_id = -1;
   this.t_complete_wait = 100; 
   this.request_letter_count = 40;
   this.complete_results = null;

   this.input_request.keydown(this.onKeydown);
}
 
Complete.onKeydown = function(event)
{
   var event = (event) ? event : window.event;
   var c = Complete;

   if (c.is_list_displayed)
   {
      switch (event.keyCode)
      {
         case 38:
            if(c.active_id >= 0)
               $('#a_' + c.active_id).removeAttr('class');

            if (c.active_id <= 0)
               c.active_id = c.total_div_count;
            else
               c.active_id--;

            c.SetActiveRequest(c.active_id);
            return true;
         break;

         case 40:
            if(c.active_id >= 0) 
               $('#a_'+c.active_id).removeAttr('class');

            if ((c.total_div_count == c.active_id) || (c.active_id < 0))
               c.active_id = 0;
            else
               c.active_id = c.active_id + 1;

            c.SetActiveRequest(c.active_id);
            return true;
         break;

         case 27:
            c.input_request.val(c.first_request); 
            c.CloseDiv();
            return true;
         break;

         case 13:
            c.CloseDiv();
            return true;
         break;
      }
   }

   c.active_id = -1;
   c.SetPreviewTimer(); 
   return true;
}

Complete.SetPreviewTimer = function()
{
   if (Complete.preview_timer_id > 0) clearTimeout(Complete.preview_timer_id);
   Complete.preview_timer_id = setTimeout(Complete.SendRequest, Complete.t_complete_wait);
}                                

Complete.SendRequest = function()
{
   var c = Complete;
   var req_words = c.input_request.val();
   if ((req_words.length < c.request_letter_count) && (req_words.length > 0))
   {
      req_words = encodeURIComponent(req_words);
      var pars = "request=" + req_words + "&topicid=" + c.topic_id;
      c.SendQuery(c.url, pars);
      c.is_list_displayed = false;        
   }
   else
      c.CloseDiv();

   if (req_words.length == 0)
      c.CloseDiv();
}

Complete.SendQuery = function(url, param)
{
   $.ajax({
      type: "GET",
      url: url,
      data: param,
      dataType   : "json",
      success: Complete.CompleteList
   });
}
 
Complete.CloseDiv = function()
{
   $(".complete").remove();
   Complete.active_id = -1;
   clearTimeout(Complete.preview_timer_id);
   Complete.preview_timer_id = -1;
}

Complete.CompleteList = function(data)
{
   var c = Complete;
   var input_req_width = c.input_request.width() - 2;

   if(input_req_width > 400)
      input_req_width = 400;
   else if(input_req_width > 280)
      input_req_width = 280;

   if(data != null){
      var ul = $('<ul/>');                                                     
      $.each(data, function(name, value)
      {
         var req = decodeURIComponent(name);
         var a = $("<a title='" + req + "'/>")
                 .text(req)
                 .click(c.SendReq);
         $("<li/>")
         .append(a)
         .mouseover(Complete.liOver)
         .mouseout(Complete.liOut)
         .appendTo(ul); 
      });

      $('.complete').remove();
      var complete = $('<div class="complete"/>');
      if(ul.html() != '')
         complete.append(ul);

      if (complete.html() != '')
      {
         $("<a/>").addClass('close').click(c.CloseDiv).appendTo(complete);
         complete.css({'display':'block','width':input_req_width+'px'}).insertAfter('.query-form');
         c.is_list_displayed = true;
         c.total_div_count = $('.complete li').size() - 1;
      }
      else
         c.CloseDiv();
   }
   else
   {
      $(".complete").remove();
   }	
}

Complete.liOver = function()
{
   var c = Complete;	
   c.active_id = $(this).index();
   c.SetActiveRequest(c.active_id);
}                                       

Complete.liOut = function(){
   $(this).removeAttr('class');
}                                       

Complete.SetActiveRequest = function(id)
{
   var c = Complete;	
   $(".complete li").each(function()
   {
      $(this).removeAttr('class');
   });
 
   $('.complete li').eq(c.active_id).addClass("active");
   var req = $('.complete a').eq(c.active_id).text();
   c.input_request.val(req);
} 

Complete.SendReq = function(a)
{
   var c = Complete;
   $(".query-form").submit();
   c.CloseDiv();
}
