//$Id: suggest_user.js,v 1.1.2.6 2009/01/15 20:21:23 lilstargazer Exp $


function suggest(event, user, type, suggestion_box, recipient)
{
  if (typeof type == 'undefined')
    type = 'friends';

  if (typeof suggestion_box == 'undefined')
    suggestion_box = 'suggestions';

  if (user.length == 0)
  {
    $('#'+suggestion_box).hide();
    return;
  }
  if ((event.keyCode >= 48 && event.keyCode <= 90) || //a-z, 0-9
      (event.keyCode == 109) || // dash/underscore
      (event.keyCode == 190) || //period
      (event.keyCode == 32) || //spacebar
      (event.keyCode == 8))
  {
      $.ajax({
        type: "POST",
        url: docRoot + '/utilities/suggest_user.php',
        data: {'type' : type,
               'value' : user},
        dataType: 'json',
        success : function(response)
        {
          if (response)
          {
            if (!response.loggedin)
            {
              $('.errorBox').text('Please login.');
              $('.errorBox').show();
            }
            $('#'+suggestion_box).show();
            $('#'+suggestion_box+'_list').html(response.result);
            $('#'+suggestion_box+'_list li').hover(function(){$(this).addClass('highlighted');},
                                            function(){$(this).removeClass('highlighted')});
          }
          else
          {
            $('#'+suggestion_box).hide();
            $('#'+suggestion_box+'_list').html('');
          }
        }
      });
  }
  else if ((event.keyCode == 40) || //up and down arrrows
           (event.keyCode == 38))
  {
    if (!$('#'+suggestion_box).is(':visible'))
      return true;
    if (event.keyCode == 40)
      var direction = 1;
    else
      var direction = -1;
    if ($('#'+suggestion_box+'_list li.highlighted').length > 0)
    {
      var selectedPos = parseInt($('#'+suggestion_box+'_list li.highlighted').attr('alt'));
      var newPos = selectedPos + direction;
    }
    else
    {
      var selectedPos = 0;
      var newPos = 0;
    }
    if ($('#'+suggestion_box+'_list li[alt='+newPos+']').length <= 0)
    {
      newPos = selectedPos;
    }
    $('#'+suggestion_box+'_list li.highlighted').removeClass('highlighted');
    $('#'+suggestion_box+'_list li[alt='+newPos+']').addClass('highlighted');
  }
  else if (event.keyCode == 13) //enter key
  {
    if ($('#'+suggestion_box).is(':visible'))
    {
      if ($('#'+suggestion_box+'_list li.highlighted').length == 1)
        select_suggestion($('#'+suggestion_box+'_list li.highlighted span').html(), suggestion_box, recipient);
      return false;
    }
  }
  return true;
}

function select_suggestion(value, suggestion_box, recipient)
{
  if (typeof suggestion_box == 'undefined')
    suggestion_box = 'suggestions';
  if (typeof recipient == 'undefined')
    recipient = 'recipient';

  if (value)
    $('#'+recipient).val(value);
  setTimeout("$('#"+suggestion_box+"').hide();", 200);
}

function check_suggestion(suggestion_box)
{
  if (typeof suggestion_box == 'undefined')
    suggestion_box = 'suggestions';
  if ($('#'+suggestion_box).is(':visible'))
    return false;
  return true;
}

function clear_default(default_text, recipient)
{
  if (typeof recipient == 'undefined')
    recipient = 'recipient';
  if ($('#'+recipient).val() == default_text)
    $('#'+recipient).val('');
}
function reset_default(default_text, recipient)
{
  if (typeof recipient == 'undefined')
    recipient = 'recipient';
  if ($('#'+recipient).val() == '')
    $('#'+recipient).val(default_text);
}
$(document).ready( function ()
{
  $('#recipient_jumpto').focus(function (event)
  {
    var input = $(event.target);
    if(input.val() == 'Jump To')
      input.val("");
    input.css("color",'#000000');
  });
  $('#recipient_jumpto').blur(function (event)
  {
      var input = $(event.target);
      if(input.val().length == 0 || input.val() == 'Jump To'){
        input.val('Jump To');
        input.css("color",'#aaaaaa');
      }else
        input.css("color",'#000000');
  });
  $('#recipient_jumpto').change(function (event)
  {
      var input = $(event.target);
      if(input.val().length == 0 || input.val() == 'Jump To'){
        input.val('Jump To');
        input.css("color",'#aaaaaa');
      }else
        input.css("color",'#000000');
  });
});
