YAHOO.namespace("Youniverse");

YAHOO.Youniverse.block = function()
{
    var posDialogBoxTop = 450;
    
    this.addBlock = function (blockedUserId)
    {
        if (!confirm(msg.translate('user02')))
        {
            return false;
        }
        
        var callback =
		{
			success: this.completeAddBlock,
			failure: this.ajaxFailure,
			cache: false
		};
		
        var postData = '&data[Block][blockedUserId]=' + blockedUserId;
        
        var request = YAHOO.util.Connect.asyncRequest('POST', '/block/add_block', callback, postData);
        
        return true;
    }
    
    this.completeAddBlock = function (o)
    {
        eval(o.responseText);
        
        if (blockSuccess == true)
        {
            // show success message
            dialogBox.displayError('<span style="color:#fff;">' + successMessage + '</span>', 200, posDialogBoxTop);
            
            // show the remove block button
			var blockSection = document.getElementById('blockSection');
			while (blockSection.hasChildNodes()) blockSection.removeChild(blockSection.firstChild);
			
            blockSection.innerHTML = '<li class="fakeTwoLines"><div id="blockBtn" class="imageLabel" style=""><a href="#" class="image" onclick="block.removeBlock(\'' + blockedUserId + '\');" title="Unblock this user" style="float: left;"></a><div class="caption" style="float:left"><a href="#" class="captionLink" onclick="block.removeBlock(\'' + blockedUserId + '\');" title="Unblock this user">Unblock this user</a></div></div></li>';
        }
        else
        {
            dialogBox.displayError('<span style="color:#fff;">' + errorMessage + '</span>', 200, posDialogBoxTop);			
        }
    }
    
    this.removeBlock = function (blockedUserId)
    {
        if (!confirm(msg.translate('user03')))
        {
            return false;
        }
        
        var callback =
		{
			success: this.completeRemoveBlock,
			failure: this.ajaxFailure,
			cache: false
		};
		
        var postData = '&data[Block][blockedUserId]=' + blockedUserId;
        
        var request = YAHOO.util.Connect.asyncRequest('POST', '/block/remove_block', callback, postData);
        
        return true;
    }
    
    this.completeRemoveBlock = function (o)
    {
        eval(o.responseText);
        
        if (blockSuccess == true)
        {
            // show success message
            dialogBox.displayError('<span style="color:#fff;">' + successMessage + '</span>', 200, posDialogBoxTop);
            
            // show the add block button
			var blockSection = document.getElementById('blockSection');
			while (blockSection.hasChildNodes()) blockSection.removeChild(blockSection.firstChild);
			
            blockSection.innerHTML = '<li class="fakeTwoLines"><div id="blockBtn" class="imageLabel" style=""><a href="#" class="image" onclick="block.addBlock(\'' + blockedUserId + '\');" title="Block this user" style="float: left;"></a><div class="caption" style="float:left"><a href="#" class="captionLink" onclick="block.addBlock(\'' + blockedUserId + '\');" title="Block this user">Block this user</a></div></div></li>';
        }
        else
        {
            dialogBox.displayError('<span style="color:#fff;">' + errorMessage + '</span>', 200, posDialogBoxTop);			
        }
    }
    
    /**
	 * Ajax Failure - usually happens when the error occurs server side and a 500 response is returned.
	 * 
	 */
	this.ajaxFailure = function (o)
	{
		if (o.status > -1) // if not user abort
		{
			alert(msg.translate('err01'));
		}
	}
    
    this.userBlocked = function ()
    {
        alert(msg.translate('user04'));
    }
}