/*
	- helpers for dealing with player/friend/etc Requests
*/

// accept a request
function request_accept(url, req_id, req_type) {
  new Ajax.Request(url,
    {
      asynchronous:true, 
      onSuccess:function(t) { request_accept_onSuccess(t, req_id, req_type); }
    });
}
function request_accept_onSuccess(t, req_id, req_type) {
  var result = t.responseJSON;

  if (result.success) {
    // kill the old request
    request_kill(req_id);
    if (result.response_message)
      display_notification(result.response_message);
  
    // raise the event so that everything else that cares can update itself
    Event.fire(document, 'request:accept_'+req_type, result);
  }
}

// ignore a friend request
function request_ignore(url, req_id, req_type) {
  new Ajax.Request(url,
    {
      asynchronous:true, 
      onSuccess:function(t){ request_ignore_onSuccess(t, req_id, req_type); }
    });
}
function request_ignore_onSuccess(t, req_id, req_type) {
  var result = t.responseJSON;

  if (result.success) {
    // kill the old request
    request_kill(req_id);
  
    // raise the event so that everything else that cares can update itself
    Event.fire(document, 'request:ignore_'+req_type, result);
  }
}

function request_kill(req_id) {
  // kill the old request
  if ($(req_id))
    $(req_id).hide();

  // make sure there is something displaying in the requests box  
  var elms = $$('#box_requests .request');
  for(var i = 0;i < elms.length;i++)
  {
    if(elms[i].visible())
    {
      return;
    }
  }
  
  $('box_requests').down('.nothing-to-see').show();
  $('box_requests').down('.something-to-see').hide();
}
