
/**
 * Add friend
 *
 */
$('a.addfriend').click(function()
{
    if ($(this).attr('sent') == 'true') {
        return false;
    }

    if ($(this).attr('href') == '#') {
        return false;
    }

    $('a.addfriend').parent().addClass('waiting');

    $.get(this.href, function(resp) {
        $('a.addfriend').parent().addClass('sent').removeClass('waiting').empty().html('Friend Request Sent.').show('slow');
    });

    return false;
});


/**
 * Remove friend
 *
 */
$('a.removefriend').click(function()
{
    if ($(this).attr('removed') == 1) {
        return false;
    }
    
    var res = confirm('Are you sure you want to remove this friend?');
    if (res == false) {
        return false;
    }

    $('a.removefriend').parent().addClass('waiting');

    $.get(this.href, function(resp) {
        $('a.removefriend').parent().addClass('sent').removeClass('waiting').empty().html('Friend Removed.').show('slow');
    });

    return false;
});

/**
 * Approve friend
 *
 */
$('a.approvefriend').click(function()
{

    $('a.approvefriend').parent().addClass('waiting');

    $.get(this.href, function(resp) {
        $('a.approvefriend').parent().addClass('sent').removeClass('waiting').empty().html('You have a new friend!').show('slow');
        $('a.declinefriend').parent().hide("slow");
    });

    return false;
});

/**
 * Decline friend
 *
 */
$('a.declinefriend').click(function()
{
    var res = confirm('Are you sure you want to remove this friend?');
    if (res == false) {
        return false;
    }

    $('a.declinefriend').parent().addClass('waiting');

    $.get(this.href, function(resp) {
        $('a.declinefriend').parent().addClass('sent').removeClass('waiting').empty().html('Friend Request Declined.').show('slow');
        $('a.approvefriend').parent().hide("slow");
    });

    return false;
});

/**
 * Allows a person to select all friend requests
 * @see     http://www.enotes.com/jax/index.php/users/myaccount/area=friends/
 */
$('img.toggle').click(function()
{
    $('form#friend-request input.actiontoggle').each(function()
    {
        this.checked = !this.checked;
    });
});

/**
 * Make sure they've selected a request to delete or decline
 * @see     http://www.enotes.com/jax/index.php/users/myaccount/area=friends/
 */
$('#friend-request').submit(function()
{
    fChosen=false;
    $('input.actiontoggle').each(function()
    {
        if (this.checked) {
            fChosen=true;
        }
    });

    if (fChosen==false) {
        alert('You must select a friend request to approve or delete');
        return false;
    }
    return true;
});


