/**
 *  User
 *
 */
(function($) {


  var Community = Ultra.app.Community;
	var User = Ultra.addNamespace('User', Community);


	User.updatePermissions = function(value, obj) {
		$.extend(obj.submitdata, {
			action: 'User.updatePermissions'
		});
		return Community.apiUpdate(value, obj);
	};




	/**
	 *  acceptFriendRequest
	 *
	 */
	User.acceptFriendRequest = function(uid, from_user) {

	  var Community = Ultra.app.Community;

	  Community.makeAPIRequest({
	    data: {
				action: 'User.acceptFriendRequest',
				uid: uid,
				from_user: from_user
			},

	    // Handle error    
	    error: function() {
        Community.handleError('User.acceptFriendRequest');
	    },

	    // Handle success
	    success: function(xml) {
	      var msg = $('#UserFriendRequestReceived_' + from_user);

        msg.fadeOut(function() {
          msg.remove();
          if (msg.parent('div').find('.message').size() === 0) {
            $('#no_requests').show();
          }
        });

        User.decrementFriendRequestCount();
	    }
	  });
		
		return false;
	};




	/**
	 *  denyFriendRequest
	 *
	 */
	User.denyFriendRequest = function(uid, from_user) {

	  var Community = Ultra.app.Community;

	  Community.makeAPIRequest({
	    data: {
				action: 'User.denyFriendRequest',
				uid: uid,
				from_user: from_user
			},

	    // Handle error    
	    error: function() {
        Community.handleError('User.denyFriendRequest');
	    },

	    // Handle success
	    success: function(xml) {
	      var msg = $('#UserFriendRequestReceived_' + from_user);

        msg.fadeOut(function() {
          msg.remove();
          if (msg.parent('div').find('.message').size() === 0) {
            $('#no_requests').show();
          }
        });

        User.decrementFriendRequestCount();
	    }
	  });
		
		return false;
	};





	/**
	 *  addBlock
	 *
	 */
	User.addBlock = function() {

	  var Community = Ultra.app.Community;

	  Community.makeAPIRequest({
	    data: {
				action: 'User.addBlock',
				child: this.rel
			},

	    // Handle error    
	    error: function() {
        Community.handleError('User.addBlock');
	    },

	    // Handle success
	    success: function(xml) {
	      Community.executeAPIActions(xml);
	    }
	  });
		
		return false;
	};




	/**
	 *  removeBlock
	 *
	 */
	User.removeBlock = function() {

	  var Community = Ultra.app.Community;

	  Community.makeAPIRequest({
	    data: {
				action: 'User.removeBlock',
				child: this.rel
			},

	    // Handle error    
	    error: function() {
        Community.handleError('User.removeBlock');
	    },

	    // Handle success
	    success: function(xml) {
	      Community.executeAPIActions(xml);
	    }
	  });
		
		return false;
	};




	/**
	 *  decrementCount
	 *
	 */
	User.decrementCount = function(count) {
  	var n = parseInt(count.text().replace(/\(([0-9]+)\)/, '$1')) - 1;
    count.text('(' + n + ')');
  	if (n === 0) {
  	 count.fadeOut('slow');
  	}
	};




	/**
	 *  decrementFriendRequestCount
	 *
	 */
  User.decrementFriendRequestCount = function() {
    User.decrementCount($('#friendRequestCount'));
	};




	/**
	 *  decrementNewMessageCount
	 *
	 */
  User.decrementNewMessageCount = function() {
    User.decrementCount($('#newMessageCount'));
	};




	/**
	 *  addFriend
	 *
	 */
	User.addFriend = function() {

	  var Community = Ultra.app.Community;
	  var form = $(this);
	  var data = form.getValues();
	  var username = data.to_user;

	  // Display loading indicator
	  var indicator = $('.loading_indicator').fadeIn('fast');

	  // Submit data
	  Community.makeAPIRequest({
	    data: data,

	    // Handle error    
	    error: function() {
        Community.handleError('User.addFriend');
	      indicator.fadeOut('fast');
	    },

	    // Handle success
	    success: function(xml) {
				var error = $('error', xml);

				if (error.size() > 0) {
				  var errorDialog = $('#errorDialog');
  				errorDialog.find('#errorMsg').text(error.text()).end().show();
				} else {
/*
				  var dialog = $('#dialog');
  				dialog.find('#dialogMsg').text('Your request has been sent to ' + username).end().show();
  				setTimeout(function() {
						dialog.fadeOut('fast');
					}, 1500);

*/				}

				form.find('input[type=reset], button[type=reset]').click();	      
	      indicator.fadeOut('fast');
	      
	      $('#addFriend_' + username).replaceWith($('<span class="addFriendPending"><div class="icon"></div>Friend Request Sent</span>'));
	    }
	  });

	  return false;
	};




	/**
	 *  removeFriend
	 *
	 */
	User.removeFriend = function(id, child) {

	  var Community = Ultra.app.Community;

	  Community.makeAPIRequest({
	    data: {
				action: 'User.removeFriend',
				id: id,
				child: child
			},

	    // Handle error    
	    error: function() {
        Community.handleError('User.removeFriend');
	    },

	    // Handle success
	    success: function(xml) {
	      $('#removeFriend_' + child).replaceWith($('<span class="removeFriendPending"><div class="icon"></div>Friend Removed</span>'));
	      $('#unMakeBestFriendButton_' + child).remove();
	      $('#makeBestFriendButton_' + child).remove();
	      var uf = $('#UserFriend_' + child);
	      uf.fadeOut(function() {
  	      uf.remove();
          if (uf.parent('div').find('.UserFriend').size() === 0) {
            $('#no_friends').show();
          }
	      });
	    }
	  });
		
		return false;
	};




	/**
	 *  makeBestFriend
	 *
	 */
	User.makeBestFriend = function(id, child) {

	  Community.makeAPIRequest({
	    data: {
				action: 'User.makeBestFriend',
				id: id,
				child: child
			},

	    // Handle error    
	    error: function() {
        Community.handleError('User.makeBestFriend');
	    },

	    // Handle success
	    success: function(xml) {
	      $('#makeBestFriendButton_' + child).replaceWith($('<span class="makeBestFriendPending"><div class="icon"></div>Best Friend Added</span>'));
        Community.executeAPIActions(xml);
	    }
	  });
		
		return false;
	};




	/**
	 *  unMakeBestFriend
	 *
	 */
	User.unMakeBestFriend = function(id, child) {

	  Community.makeAPIRequest({
	    data: {
				action: 'User.unMakeBestFriend',
				id: id,
				child: child
			},

	    // Handle error    
	    error: function() {
        Community.handleError('User.unMakeBestFriend');
	    },

	    // Handle success
	    success: function(xml) {
	      $('#unMakeBestFriendButton_' + child).replaceWith($('<span class="unMakeBestFriendPending"><div class="icon"></div>Best Friend Removed</span>'));
        Community.executeAPIActions(xml);
	    }
	  });
		
		return false;
	};
	

})(jQuery);
