function FriendRequests(requestTable, ajaxFunction)
{
    this._requestTable = requestTable
    this._ajaxFunction = ajaxFunction
}

FriendRequests.prototype.__CheckAll = function FriendRequests_CheckAll()
{
    $(this._requestTable).getElementsByClassName('friendReqCheckBox').each(
        function(checkBoxElt)
        {
            checkBoxElt.checked = $('checkall').checked
        }
    )
}

FriendRequests.prototype.__HandleMultipleRequests = function FriendRequests_HandleMultipleRequest(action)
{
    var msgIds = new Array();    
    var requestCnt = 0;
    
    $(this._requestTable).getElementsByClassName('friendReqCheckBox').each(
        function(checkBoxElt)
        {
            if (checkBoxElt.checked)
            {
                msgIds[requestCnt] = parseInt(checkBoxElt.id.substring(5));
                requestCnt = requestCnt + 1;
            }
        }
    )

    if (requestCnt > 0)
    {
        this.__ProcessAction(msgIds, action)
    }
}

FriendRequests.prototype.__HandleOneRequest = function FriendRequests_HandleOneRequest(msgId, action)
{
    var msgIds = new Array();
    msgIds[0] = msgId;
    this.__ProcessAction(msgIds, action)
}


FriendRequests.prototype.__ProcessAction = function FriendRequests_ProcessAction(msgIds, action)
{
    this._msgIds = msgIds

    new Ajax.Request(
        this._ajaxFunction + "?msgIds=" + this._msgIds.toString() + "&action=" + action,
        {
            onSuccess: this.__ProcessActionComplete.bind(this)
        }
    )
}

FriendRequests.prototype.__ProcessActionComplete = function FriendRequests_ProcessActionComplete()
{    
    var newFriendReqProcessedCnt = 0;
    var friendReqProccessedCnt = 0;
    this._removeCnt = 0;
    for (var cnt=0; cnt < this._msgIds.length; cnt++)
    {
        new Effect.DropOut('msgid' + this._msgIds[cnt],
            { duration:0.3,
              afterFinish: this.__AfterFinish.bind(this)
            }
        );
        
        if ($('msgid' + this._msgIds[cnt]).hasClassName("newfriendrequest"))
        {
            newFriendReqProcessedCnt++;
        }
        friendReqProccessedCnt++;
    }

    // The new friend count in the Secondary header
    if ($('headernewfriendreqcnt') && (newFriendReqProcessedCnt > 0))
    {
        $('headernewfriendreqcnt').innerHTML = parseInt($('headernewfriendreqcnt').innerHTML) - newFriendReqProcessedCnt;
    }

    // The two counts inside the h2 of the friendrequests.vm
    if ($('newfriendreqcnt') && (newFriendReqProcessedCnt > 0))
    {
        $('newfriendreqcnt').innerHTML = parseInt($('newfriendreqcnt').innerHTML) - newFriendReqProcessedCnt;
    }
    
    if ($('totalfriendreqcnt') && (friendReqProccessedCnt > 0))
    {
        $('totalfriendreqcnt').innerHTML = parseInt($('totalfriendreqcnt').innerHTML) - friendReqProccessedCnt;
    }
    
}

FriendRequests.prototype.__AfterFinish = function FriendRequests_AfterFinish()
{
    // Does this protect against multiple AfterFinish getting called? Hope so.
    var removeCnt = this._removeCnt++;
    
    $('msgid' + this._msgIds[removeCnt]).remove();
    if ($(this._requestTable).getElementsBySelector('tr').length == 1)
    {
        $('actionButtons').hide();
        $('noFriendRequests').show();
    }
}

function FriendActivity(elt, name, mostRecentJournalId, ajaxFunction, topRowEltId)
{
    this._elt = $(elt);
    this._name = name;
    this._mostRecentJournalId = mostRecentJournalId;
    this._ajaxFunction = ajaxFunction
    this._topRowEltId = topRowEltId;
    this.__Init();
	this._refreshInterval = 120000;
}

FriendActivity.prototype.__Init = function FriendActivity_Init()
{
    this._friendActivityTimeout = setTimeout(this.__TimerFired.bind(this), this._refreshInterval);
}

FriendActivity.prototype.__TimerFired = function FriendActivity_TimerFired()
{
    if (this._friendActivityTimeout)
    {
        clearTimeout(this._friendActivityTimeout);
    }

    this._ajaxDiv = $(document.createElement("div"));
    this._ajaxDiv.hide();
    var firstChild = this._elt.down(0);
    if (firstChild)
    {
        this._elt.insertBefore(this._ajaxDiv, firstChild);
    }
    else
    {
        this._elt.appendChild(this._ajaxDiv);
    }

    var args = $H({ name: this._name, mostrecentjournalid: this._mostRecentJournalId, topRowEltId: this._topRowEltId });

    new Ajax.Updater(
        {  success: this._ajaxDiv },
        this._ajaxFunction,
        {
            method: 'post',
            insertion: Insertion.Top,
            parameters: args.toQueryString(),
            evalScripts: true,
            onComplete:this.__OnComplete.bind(this)
        });
        
    this._friendActivityTimeout = setTimeout(this.__TimerFired.bind(this), this._refreshInterval);
}

FriendActivity.prototype.__OnComplete = function FriendActivity_OnComplete(transport, json)
{
    var newEntriesCnt = $H(json)["newEntriesCnt"];
    if (newEntriesCnt > 0)
    {
        var maxRows = $H(json)["maxRows"];
        this._mostRecentJournalId = $H(json)["mostRecentJournalId"];
        this._topRowEltId = this._topRowEltId + newEntriesCnt
        
        new Effect.SlideDown(this._ajaxDiv, {duration:0.3});
        
        for (var x = 1; x <= newEntriesCnt; x++)
        {
            var removeThisOne = this._topRowEltId - newEntriesCnt - maxRows + x;
            $('friendactivity' + removeThisOne).remove();    
        }
    }
    else
    {
        this._ajaxDiv.remove();
    }
}

function removeFriend(ajaxFunction, name, id)
{
	if(window.confirm("Are you sure you want to remove '" + name + "' as a friend?"))
	{
		new Ajax.Request(ajaxFunction + "?name=" + name, { onSuccess: function() { Effect.DropOut('friend' + id); } });
	}
}
