var dOffsets    = null;
var MSGQUEUE = new MessageQueue();

function MessageQueue()
{
    this.uniqueID    = 0;
    this.msgList     = new Array();

    this.addMessage = function( msg )
    {
        ++this.uniqueID;
        var alertObj = new MessageAlert(msg, "msg_"+this.uniqueID, $('msg_'+(this.uniqueID-1)));
        this.msgList.push( alertObj );

        /*
        if ( this.msgList.length >= 6 ) {
            var ref = this.msgList.shift();
            if ( ref && ref.refDiv ) {
                this.remMessage( ref.refDiv );
            }
        }
        */
    }

    this.remMessage = function( refDiv )
    {
        for(var idx=0; idx<this.msgList.length; idx++) {
            if ( this.msgList[idx].refDiv == refDiv ) {
                this.msgList.slice(idx, 1);
                break;
            }
        }

        $('alert_box').removeChild( refDiv );
    }
}


function MessageAlert( msg, msgID, refBefore )
{
    this.strMsg  = msg;
    this.refDiv  = null;

    this.refDiv    = document.createElement('div');
    this.refDiv.setAttribute('id',         msgID);
    this.refDiv.setAttribute("class",      "alert_notice");
    this.refDiv.setAttribute("className",  "alert_notice");
    this.refDiv.setAttribute("onClick",    "MSGQUEUE.remMessage( this );");

    reposition_alert();

    $('alert_box').insertBefore( this.refDiv, refBefore );

    this.refDiv.innerHTML = '<div style="padding:10px;" align="left"><img src="/gfx/badges/EVENT.png" align="left">'+this.strMsg+'</div>';
}




function reposition_alert()
{
    dOffsets = document.viewport.getScrollOffsets();
    var ab = $('alert_box');
    if ( ab == null ) return;
    ab.style.top = dOffsets.top + "px";
}




function refer( email )
{
  var grabAjax = new Ajax.Request( '/xmlhttp/refer_friend.php', { method: 'post', parameters: 'email='+email, onComplete: onRefer} );
}

function onRefer( originalReq )
{
  var obj = JSON.parse(originalReq.responseText);
  if ( obj.success == 0 ) {
    alert(obj.message);
    return;
  }

  if ( obj.command_stack.length > 0 )
  {
      var i;
      for(i=0; i<obj.command_stack.length; i++) {
          eval( obj.command_stack[i] );
      }
  }
}





function rateSite( site_id, rating )
{
  if ( !confirm("Rating BBMMO's affects their ranking on the site. It also costs 25 points.\r\nFor each rating you give it, You will receive 1 point for every person who rates it identically to you.\r\nRate this BBMMO?") ) return;
  var grabAjax = new Ajax.Request( '/xmlhttp/rate_site.php', { method: 'post', parameters: 'site_id='+site_id+'&rating='+rating, onComplete: onRateSite} );
}

function onRateSite( originalReq )
{
  var obj = JSON.parse(originalReq.responseText);
  if ( obj.success == 0 ) {
    alert(obj.message);
    return;
  }

  if ( obj.command_stack.length > 0 )
  {
      var i;
      for(i=0; i<obj.command_stack.length; i++) {
          eval( obj.command_stack[i] );
      }
  }
}







function rateCheat( cheat_id, rating )
{
  if ( !confirm("Rating cheats costs 5 points. Rating them high raises them in the list, but raising them low can result in their removal. Continue rating this cheat?") ) return;
  var grabAjax = new Ajax.Request( '/xmlhttp/rate_cheat.php', { method: 'post', parameters: 'cheat_id='+cheat_id+'&rating='+rating, onComplete: onRateCheat} );
}

function onRateCheat( originalReq )
{
  var obj = JSON.parse(originalReq.responseText);
  if ( obj.success == 0 ) {
    alert(obj.message);
    return;
  }

  if ( obj.command_stack.length > 0 )
  {
      var i;
      for(i=0; i<obj.command_stack.length; i++) {
          eval( obj.command_stack[i] );
      }
  }
}







function props( post_id )
{
  if ( !confirm('Do you want to use 5 points to give props?') ) return;
  var grabAjax = new Ajax.Request( '/xmlhttp/give_props.php', { method: 'post', parameters: 'post_id='+post_id, onComplete: onProps} );
}

function onProps( originalReq )
{
  var obj = JSON.parse(originalReq.responseText);
  if ( obj.success == 0 ) {
    alert(obj.message);
    return;
  }

  if ( obj.command_stack.length > 0 )
  {
      var i;
      for(i=0; i<obj.command_stack.length; i++) {
          eval( obj.command_stack[i] );
      }
  }
}












function getMessage()
{
  var grabAjax = new Ajax.Request( '/xmlhttp/check_messages.php', { method: 'post', onComplete: onGetMessage} );
}



function onGetMessage( originalReq )
{
  var obj = JSON.parse(originalReq.responseText);
  if ( obj.total_messages == 0 ) return;

  if ( String(obj.points) == 'null' ) return;

  if ( obj.points != 0 )
  {
    if ( obj.points < 0 ) MSGQUEUE.addMessage( obj.message + '<br>' + obj.points + ' pts.');
    else  MSGQUEUE.addMessage( obj.message + '<br>+' + obj.points + ' pts.');
  }
  else
  {
    MSGQUEUE.addMessage( obj.message);
  }

  if ( obj.command_stack.length > 0 )
  {
      var i;
      for(i=0; i<obj.command_stack.length; i++) {
          eval( obj.command_stack[i] );
      }
  }
}



setInterval( "reposition_alert()", 1000);
setInterval( "getMessage()",       4000);
