/**
* Universal - Social Functionality Javascript
* 
* Contain functions for AJAX and page setup for all social functionality.
**/

var uni = {
	user_id: '',
	anonymous_user_id: '',
	username: '',
	user_profile: '',
	moderator: false,
	pWidth: 660,
	pHeight: 500,
	confirmWidth: 300,
	confirmHeight: 200,
	listWidth: 300,
	listHeight: 400,
	loginWidth: 948,
	loginHeight: 504,
	deactivate: false
}

function uniSetup (user_id, username, anonymous_user_id) {
	uni.user_id = user_id;
	uni.username = username;
	uni.anonymous_user_id = anonymous_user_id;
}

/* USER FUNCTIONS */
function ajaxCheckUsername() {
	if(!$('#username')) { return false; }
	$.ajax({
		type: "POST",
		url: '/userprofile/action',
		data: { 
			action: 'check_username',
			username: $('#input-username').attr('value')
		},
		success: function(data){
			if (data == 'true') {
				$('span#username-check').html('Not Available');
				$('span#username-check').addClass('fail');
			} else {
				$('span#username-check').html('Available');
				if ($('span#username-check').hasClass('fail')) {
					$('span#username-check').removeClass('fail');
				}
			}
		}
	});	
}

function checkLogin () {
	// Obscure links for non-logged in users
	if (uni.user_id == '' || uni.user_id == uni.anonymous_user_id) {
		uni.deactivate = true;
		//$('a.logged-in').attr('href', '/user/login');
		$('a.logged-in').removeAttr('onclick');
		$('a.logged-in').each(function() {
			if (!$(this).hasClass('no-redirect')) {
				$(this).attr('redirect', $(this).attr('href'));
			}
		});
		$('a.logged-in').unbind('click').click(function(){
			if ($(this).attr('redirect') != '' && typeof($(this).attr('redirect')) != 'undefined') {
				url = '/userprofile/popup?action=register&height=' + uni.loginHeight + '&width=' + uni.loginWidth + '&redirect_override=' + encodeURIComponent( $(this).attr('redirect') );
			} else {
				url = '/userprofile/popup?action=register&height=' + uni.loginHeight + '&width=' + uni.loginWidth;
			}
			tb_show('Login or Register', url);
		});
		$('a.logged-in').attr('href', 'javascript:void(0);');
		// Hide welcome
		$('#welcome-user').hide();
		$('#welcome-anonymous').show();
		// Set My Profile link
		//$('#nav-my-profile').attr('href', '/My-Profile');
	} else {
		// Get username
		if (uni.username == '') {
			getUsername();
		}
		// Display welcome
		$('#welcome-anonymous').hide();
		$('#welcome-user').show();
	}
	// Show/hide comments form
	if ($('#comments-form').length) {
		if (uni.deactivate) {
			$('#comments-login').show();
			$('#comments-form').addClass('hide');
		} else {
			$('#comments-login').hide();
			$('#comments-form').removeClass('hide');
		}
	}
	// Show saved
	var block_ids = new Array();
	$('div.function-save').each(function() {
		block_ids.push($(this).attr('id').split('_')[1]);
	});
	if (block_ids.length > 0) {
		isSavedList(block_ids);
	}
	// Show online
	var online_user_ids = new Array();
	$('.user-block').each(function() {
		online_user_ids.push($(this).attr('id').split('_')[1]);
	});
	if (online_user_ids.length > 0) {
		isOnlineList(online_user_ids);
	}
	$('.is-users').each(function(){
		owner_id = $(this).attr('id').split('_')[1];
		if (owner_id == uni.user_id) {
			$(this).show();
		}
	});
	$('.is-not-users').each(function(){
		owner_id = $(this).attr('id').split('_')[1];
		if (owner_id != uni.user_id) {
			$(this).show();
		}
	});
	$('.is-moderator').each(function(){
		if (uni.moderator) {
			$(this).show();
		}
	});
}

function isUsersProfile(contentobject_id) {
	if (contentobject_id == uni.user_id) {
		return true;
	} else {
		return false;
	}
}

function isOnlineList (user_ids) {
	if (uni.deactivate) { return false; }
	$.ajax({
		type: "POST",
		url: '/userprofile/action',
		data: { 
			action: 'is_online_list',
			user_ids: user_ids.toString()
		},
		success: function(data){
			online_ids = jQuery(data.split(','));
			online_ids.each(function() {
				$('.icon-online_'+this).show();
			});
		}
	});
}

function setNavTitle(title) {
	$('#nav-main-title').html(title);
}

function getUsername() {
	$.ajax({
		type: "POST",
		url: '/userprofile/profile',
		async: true,
		data: {
			get_user_attribute_value: 1,
			attribute: 'Login',
			user_id: uni.user_id
		},
		success: function(data){
			uni.username = data;
			$('#welcome-username').html(data);
		}
	});
}

function getProfileUrl() {
	$.ajax({
		type: "POST",
		url: '/userprofile/profile',
		async: true,
		data: {
			get_profile_url: 1,
			user_id: uni.user_id
		},
		success: function(data){
			uni.user_profile = data;
			if ($('#home-button-view-profile')) {
				$('#home-button-view-profile').attr('href', uni.user_profile);
			}
		}
	});
}

function destroySession () {
	$.ajax({
		type: "POST",
		url: '/userprofile/action',
		async: true,
		data: {
			action: 'destroy_session'
		}
	});
}


/* FRIEND FUNCTIONS */
function acceptFriendRequest(friend_id) {
	$.ajax({
		type: "POST",
		url: '/friend/accept',
		data: { 
			user_id: uni.user_id,
			friend_id: friend_id
		},
		success: function(data){
			if (data == '1') {
				$('#friend-request-' + friend_id).remove();
			}
		}
	});	
}

function declineFriendRequest (friend_id) {
	$.ajax({
		type: "POST",
		url: '/friend/decline',
		data: { 
			user_id: uni.user_id,
			friend_id: friend_id
		},
		success: function(data){
			// $('#friend-request_' + friend_id).remove();
			if (data == '1') {
                                $('#friend-request-' + friend_id).remove();
                        }
		}
	});
}

function sendFriendRequest(friend_id) {
	$.ajax({
		type: "POST",
		url: '/friend/request',
		data: { 
			user_id: uni.user_id,
			friend_id: friend_id
		},
		success: function(data){
			if (data == '1') {
				$('div#friend-request_' + friend_id).html('Friend Requested');
				$('div#friend-request_' + friend_id).addClass('request');
			} else if (data == '2') {
				$('div#friend-request_' + friend_id).html('Friend Added');
				$('div#friend-request_' + friend_id).addClass('request');
			}
		}
	});	
}
function getFriendRequest(friend_id) {
	$.ajax({
		type: "POST",
		url: '/friend/hasrequest',
		data: { 
			user_id: uni.user_id,
			friend_id: friend_id
		},
		success: function(data){
			if (data == '1') {
				if ($('div#friend-request_' + friend_id)) {
					$('div#friend-request_' + friend_id).html('Friend Requested');
					$('div#friend-request_' + friend_id).addClass('request');
				}
			} else {
				if ($('div#friend-request_' + friend_id)) {
					$('div#friend-request_' + friend_id + ' a').show();
				}
			}
		}
	});	
}

function unfriend(friend_id) {
	if (uni.deactivate) { return false; }
	url = '/friend/remove?height=' + uni.confirmHeight + '&width=' + uni.confirmWidth + '&user_id=' + uni.user_id + '&friend_id=' + friend_id;
	tb_show('Delete', url);
}
function removeFriend(friend_id) {
	if( friend_id != 689 ) {
	  tb_remove();
	  $('#friend_' + friend_id).html('<img src="/extension/universalnutrition/design/site/images/icon_removedfriend.gif" alt="Removed"/>');
        }
}
function removeFromTopFriends(friend_id) {
	$.ajax({
		type: "POST",
		url: '/friend/removefromtop',
		data: { 
			user_id: uni.user_id,
			friend_id: friend_id
		}
	});
	$('#top-friend_' + friend_id).remove();
	key = topfriends.indexOf(friend_id);
	topfriends.splice(key, 1);
	if (topfriends.length == 0) {
		$('#no-top-friends').show();
		$('#add-more-top-friends').hide();
	}
}

/* CONTENT FUNCTIONS */
function editContent(contentobject_id) {
	if (uni.deactivate) { return false; }
	url = '/usercontent/edit?height=' + uni.pHeight + '&width=' + uni.pWidth + '&action=edit&ajax=1&user_id=' + uni.user_id + '&contentobject_id=' + contentobject_id;
	tb_show('Edit', url);
}
function deleteContent(contentobject_id, redirect) {
	if (typeof(redirect) == 'undefined') { redirect = ''; }
	if (uni.deactivate) { return false; }
	url = '/usercontent/delete?height=' + uni.confirmHeight + '&width=' + uni.confirmWidth + '&user_id=' + uni.user_id + '&contentobject_id=' + contentobject_id + '&redirect=' + redirect;
	tb_show('Delete', url);
}

function populateTags(tags) {
	if (typeof(tags) == 'undefined') { tags = ''; }
	category_id = $('#category').val();
	$.ajax({
		type: "POST",
		url: '/usercontent/tags',
		async: true,
		data: {
			category_id: category_id
		},
		beforeSend: function() {
		},
		success: function(data){
			$('#tags-inject').html(data);
			jQuery(tags).each(function(){
				$('#tag_' + this).attr('checked', 'true');
			});
		}
	});
}

/* COMMENTS */
function reportAbuse(comment_id) {
	$.ajax({
		type: "POST",
		url: '/comment/flag',
		async: true,
		data: {
			user_id: uni.user_id,
			comment_id: comment_id,
			ezfp_form: 'report-abuse'
		},
		success: function(data){
			$('#flag-' + comment_id).html('Flagged');
			$('#flag-' + comment_id).removeAttr('onclick');
		}
	});
}

/* PRIVATE MESSAGES */
function markRead(message_id) {
	if (uni.deactivate) { return false; }
	$.ajax({
		type: "POST",
		url: '/privatemessage/markread',
		data: { 
			message_id: message_id
		}
	});
}
function deleteMessage(message_id) {
	if (typeof(redirect) == 'undefined') { redirect = ''; }
	if (uni.deactivate) { return false; }
	url = '/privatemessage/delete?height=' + uni.confirmHeight + '&width=' + uni.confirmWidth + '&user_id=' + uni.user_id + '&message_id=' + message_id + '&redirect=' + redirect;
	tb_show('Delete', url);
}

function deleteMessages() {

	if(uni.deactivate){
		return false;
	}
	
	if(typeof(redirect) == 'undefined'){
		redirect = '';
	}

	var set=[];	
	$(".private_message_delete").each(function(index) {
		if($(this).attr('checked')){
			set[set.length] = $(this).attr('value');
		}
	});
	if( set.length == 0){
		alert('Please select messages to delete.');
		return false;
	}
	url = '/privatemessage/delete?height=' + uni.confirmHeight + '&width=' + uni.confirmWidth + '&user_id=' + uni.user_id + '&message_id=' + set.join(',') + '&redirect=' + redirect;
	tb_show('Delete', url);
}

function addFriends () {
	if (uni.deactivate) { return false; }
	url = '/privatemessage/addfriends?height=' + uni.listHeight + '&width=' + uni.listWidth;
	tb_show('Friends', url);
}

function checkFriends(obj) {
	var friend_name = obj.find('h4').html().toLowerCase(),
		friend_list = $('#to').val(),
		$friend_block = $('#' + obj.attr('id'));
	
	if (friend_list !== '') {
		friend_list = friend_list.split(';');
		$.each(friend_list, function(index, value) {
			// Clean up
			friend_list[index] = trim(value);
			
			// Check if friend exists
			if (trim(value.toLowerCase()) == friend_name) {
				// Toggle Link (show Remove)
				$friend_block.find('a.toggle-link-add').hide();
				$friend_block.find('a.toggle-link-remove').show();
			}
		});
	} else {
		return false;
	}
}

function updateFriend(friend, friend_id, action) {
	var friend_list = $('#to').val(),
		$friend_block = $('#friend_' + friend_id),
		friend_exists = false,
		friend_index;

	if (friend_list !== '') {
		friend_list = friend_list.split(';');
		$.each(friend_list, function(index, value) {
			// Clean up
			friend_list[index] = trim(value);
			
			// Check if friend exists
			if (trim(value.toLowerCase()) == friend.toLowerCase()) {
				friend_exists = true;
				friend_index = index;
			}

			// Remove any empty elements (if user ended list w/ a semi-colon)
			if (value === '') {
				friend_list.splice(index, 1);
			}
		});
	} else {
		friend_list = [];
	}

	if (action == 'add') {
		if (friend_exists) {
			return false;
		} else {
			// Add Friend to list
			friend_list.push(friend);
			
			// Insert updated list 
			$('#to').val(friend_list.join('; '));
	
			// Toggle Link (show Remove)
			$friend_block.find('a.toggle-link-add').hide();
			$friend_block.find('a.toggle-link-remove').show();
		}
	} else if (action == 'remove') {
		if (friend_exists) {
			// Remove Friend from list
			friend_list.splice(friend_index, 1);
			
			// Insert updated list 
			$('#to').val(friend_list.join('; '));
			
			// Toggle Link (show Add)
			$friend_block.find('a.toggle-link-remove').hide();
			$friend_block.find('a.toggle-link-add').show();
		} else {
			return false;
		}
	}
}

/* FORUM */
function trackView(contentobject_id, max_child) {
	if (typeof(max_child) == 'undefined') { max_child = 0; }
	if (uni.deactivate) { return false; }
	$.ajax({
		type: "POST",
		url: '/viewtracker/track',
		data: { 
			user_id: uni.user_id,
			contentobject_id: contentobject_id,
			max_child: max_child
		},
		beforeSend: function() {
		},
		success: function(data){
		}
	});
}

function markNewPosts (contentobject_id, max_child) {
	if (typeof(max_child) == 'undefined') { max_child = 0; }
	if (uni.deactivate) { return false; }
	$.ajax({
		type: "POST",
		url: '/viewtracker/action',
		data: { 
			action: 'get_last_viewed_child',
			user_id: uni.user_id,
			contentobject_id: contentobject_id
		},
		beforeSend: function() {
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
		},
		success: function(data){
			$('div.forum-item').each(function() { // was .post
				id = $(this).attr('id').split('_')[1];
				if (id > data) {
					$(this).find('.new-icon').show();
				}
			});
			trackView(contentobject_id, max_child);
		}
	});
}

function hasNewPostsList (contentobject_ids) {
	if (uni.deactivate) { return false; }
	$.ajax({
		type: "POST",
		url: '/viewtracker/action',
		data: { 
			action: 'has_new',
			user_id: uni.user_id,
			contentobject_ids: contentobject_ids.toString()
		},
		beforeSend: function() {
		},
		success: function(data){
			results = jQuery(data.split(','));
			contentobject_ids = jQuery(contentobject_ids);
			//contentobject_ids = contentobject_ids.split(',');
			threads = new Array;
			if (results.length > 0) {
				results.each(function() {
					if (this.length > 0) {
						result_array = this.split('=');
						thread_id = result_array[0];
						threads.push(thread_id);
						last_view = result_array[1];
						last_post = $('#thread_'+thread_id+' .last-post').attr('id').split('_')[1];
						if (last_view < last_post) {
							$('#thread_'+thread_id+' .new-icon').show();
						}
					}
				});
			};
			contentobject_ids.each(function() {
				if (jQuery.inArray(this.toString(), threads) == -1) {
					$('#thread_'+this+' .new-icon').show();
				}
			});
		}
	});
}

/* SAVE */
function ajaxSave (contentobject_id) {
	if (uni.deactivate) { return false; }
	if ($('#save_button_'+contentobject_id).attr("status") != 'disabled') {
		$.ajax({
			type: "POST",
			url: '/save/toggle',
			data: { 
				user_id: uni.user_id,
				contentobject_id: contentobject_id
			},
			beforeSend: function() {
				$('#save_button_'+contentobject_id).addClass('active');
				$('#save_button_'+contentobject_id).attr("status","disabled");
			},
			success: function(data){
				if (data == 'access_denied') {
					window.location = '/user/login';
				} else {
					$('#save_button_'+contentobject_id).attr("status","enabled");
					if (data.split(',')[0] == true) {
						$('#save_button_'+contentobject_id).html('Unfavorite');
						if ($('#save_count')) {
							$('#save_count').html(data.split(',')[1]);
						}
					} else {
						$('#save_button_'+contentobject_id).html('Favorite');
						if ($('#save_count')) {
							$('#save_count').html(data.split(',')[1]);
						}
					}
				}
			}
		});
	}
}

function isSaved (contentobject_id) {
	if (uni.deactivate) { return false; }
	$.ajax({
		type: "POST",
		url: '/save/action',
		data: { 
			action: 'is_saved',
			user_id: uni.user_id,
			contentobject_id: contentobject_id
		},
		beforeSend: function() {
			$('#save_button_'+contentobject_id).attr("status","disabled");
		},
		success: function(data){
			if (data == 'true') {
				$('#save_button_'+contentobject_id).html('Unfavorite');
			} else {
				$('#save_button_'+contentobject_id).html('Favorite');
			}
			$('#save_button_'+contentobject_id).attr("status","enabled");
		}
	});
}

function isSavedList (contentobject_ids) {
	if (uni.deactivate) { return false; }
	$.ajax({
		type: "POST",
		url: '/save/action',
		data: { 
			action: 'is_saved_list',
			user_id: uni.user_id,
			contentobject_ids: contentobject_ids.toString()
		},
		beforeSend: function() {
			contentobject_ids = jQuery(contentobject_ids);
			contentobject_ids.each(function() {
				$('#save_button_'+this).attr("status","disabled");
			});
		},
		success: function(data){
			savedIDs = jQuery(data.split(','));
			savedIDs.each(function() {
				$('#save_button_'+this).html('Unfavorite');
			});
			contentobject_ids.each(function() {
				$('#save_button_'+this).attr("status","enabled");
			});
		}
	});
}

function numSaves (contentobject_id) {
	$.ajax({
		type: "POST",
		url: '/save/action',
		data: { 
			action: 'num_saves',
			contentobject_id: contentobject_id
		},
		success: function(data){
			$('#save_count').html(data);
		}
	});
}

function ajaxSavedList (contentobject_id) {
	$.ajax({
		type: "POST",
		url: '/save/listusers',
		data: { 
			contentobject_id: contentobject_id
		},
		success: function(data){
			$('#saved-inject').html(data);
		}
	});
}

/* GROUP FUNCTIONS */
function checkGroupMember (group_id) {
	$.ajax({
		type: "POST",
		url: '/group/action',
		data: { 
			action: 'check_group_member',
			user_id: uni.user_id,
			group_id: group_id
		},
		success: function(data){
			if (data == 'true') {
				$('.group-member').show();
				$('.not-group-member').hide();
			} else {
				$('.group-member').hide();
				$('.not-group-member').show();
			}
		}
	});
}

function groupInvite (group_id) {
	if (uni.deactivate) { return false; }
	url = '/group/invite?height=' + uni.listHeight + '&width=' + uni.listWidth + '&group_invite=1&user_id=' + uni.user_id + '&group_id=' + group_id;
	tb_show('Invite your friends', url);
}

function groupInviteFriend (group_id, friend_id) {
	$.ajax({
		type: "POST",
		url: '/group/invite',
		data: { 
			invite_friend: 1,
			user_id: uni.user_id,
			group_id: group_id,
			friend_id: friend_id
		},
		success: function(data){
			$('#friend_' + friend_id + ' .invite-link').html('Invited');
		}
	});
}

function groupJoin (group_id, redirect_url) {
	$.ajax({
		type: "POST",
		url: '/group/join',
		data: { 
			user_id: uni.user_id,
			group_id: group_id
		},
		success: function(data){
			document.location = redirect_url;
		}
	});
}

function groupLeave (group_id, redirect_url) {
	$.ajax({
		type: "POST",
		url: '/group/leave',
		data: { 
			user_id: uni.user_id,
			group_id: group_id
		},
		success: function(data){
			document.location = redirect_url;
		}
	});
}

function declineGroupInvite (group_id) {
	$.ajax({
		type: "POST",
		url: '/group/decline',
		data: { 
			user_id: uni.user_id,
			group_id: group_id
		},
		success: function(data){
			$('#group-invite_' + group_id).hide();
		}
	});
}

function ajaxGroupMembers (node_id) {
	$.ajax({
		type: "POST",
		url: '/content/view/ajax/' + node_id + '/(view)/members',
		success: function(data){
			$('#group-members-inject').html(data);
		}
	});
}

/* FOOD LOG FUNCTIONS */
function foodLogEdit () {
	if (uni.deactivate) { return false; }
	url = '/foodlog/edit?height=' + uni.pHeight + '&width=' + uni.pWidth + '&user_id=' + uni.user_id;
	tb_show('My Saved Foods', url);
}

function removeFood (contentobject_id) {
	$.ajax({
		type: "POST",
		url: '/foodlog/remove',
		data: { 
			user_id: uni.user_id,
			contentobject_id: contentobject_id
		},
		success: function(data){
			$('#saved-food_' + contentobject_id).remove();
		}
	});
}

/* EVENTS */

function eventInvite (event_id) {
	if (uni.deactivate) { return false; }
	url = '/event/invite?height=' + uni.listHeight + '&width=' + uni.listWidth + '&event_invite=1&user_id=' + uni.user_id + '&event_id=' + event_id;
	tb_show('Invite your friends', url);
}

function eventInviteFriend (event_id, friend_id) {
	$.ajax({
		type: "POST",
		url: '/event/invite',
		data: { 
			invite_friend: 1,
			user_id: uni.user_id,
			event_id: event_id,
			friend_id: friend_id
		},
		success: function(data){
			$('#friend_' + friend_id + ' .invite-link').html('Invited');
		}
	});
}

function eventInviteFriendsUpdateDisplay(){

	$('#friends_id_list').css("display",  (window._UniversalInviteAll.length > 0) ? 'inline' : 'none');

}

function eventInviteFriends (event_id) {

	var friend_ids = window._UniversalInviteAll;

	$.ajax({
		type: "POST",
		url: '/event/invite',
		data: {
			invite_friends: 1,
			user_id: uni.user_id,
			event_id: event_id,
			friend_ids: friend_ids
		},
		success: function(data){
			window._UniversalInviteAll = [];
			eventInviteFriendsUpdateDisplay();
			for(var i=0; i<friend_ids.length; i++){
				$('#friend_' + friend_ids[i] + ' .invite-link').html('Invited');
			}			
		}
	});
}

function eventJoin (event_id) {
	$.ajax({
		type: "POST",
		url: '/event/accept',
		data: { 
			user_id: uni.user_id,
			event_id: event_id
		},
		success: function(data){
			$('#event-invite_' + event_id).hide();
		}
	});
}

function declineEventInvite (event_id) {
	$.ajax({
		type: "POST",
		url: '/event/decline',
		data: { 
			user_id: uni.user_id,
			event_id: event_id
		},
		success: function(data){
			$('#event-invite_' + event_id).hide();
		}
	});
}

/* USER PROFILE FUNCTIONS */
function ajaxUserNotifications(contentobject_id, node_id) {
	if (uni.deactivate) { return false; }
	if (uni.user_id != contentobject_id) { 
		$('#notifications-inject .loading').remove(); return false; 
	}
	$.ajax({
		type: "POST",
		url: '/content/view/ajax/' + node_id + '/(view)/cpnotification',
		data: { 
			user_id: uni.user_id
		},
		success: function(data){
			$('#notifications-inject').html(data);
		}
	});
}

function ajaxUserMessages(contentobject_id, node_id) {
	if (uni.deactivate) { return false; }
	if (uni.user_id != contentobject_id) { 
		$('#messages-inject .loading').remove(); return false; 
	}
	$.ajax({
		type: "POST",
		url: '/content/view/ajax/' + node_id + '/(view)/messages',
		data: { 
			user_id: uni.user_id
		},
		success: function(data){
			$('#messages-inject').html(data);
		}
	});
}
function ajaxUserFeed(contentobject_id, node_id) {
	if (uni.deactivate) { return false; }
	$.ajax({
		type: "POST",
		url: '/content/view/ajax/' + node_id + '/(view)/feed',
		data: { 
			user_id: uni.user_id
		},
		success: function(data){
			$('#feed-inject').html(data);
		}
	});
}
function ajaxUserFriends(contentobject_id, node_id) {
	if (uni.deactivate) { return false; }
	$.ajax({
		type: "POST",
		url: '/content/view/ajax/' + node_id + '/(view)/friends',
		data: { 
			user_id: uni.user_id
		},
		success: function(data){
			$('#friends-inject').html(data);
		}
	});
}
function ajaxUserGroups(contentobject_id, node_id) {
	if (uni.deactivate) { return false; }
	$.ajax({
		type: "POST",
		url: '/content/view/ajax/' + node_id + '/(view)/groups',
		data: { 
			user_id: uni.user_id
		},
		success: function(data){
			$('#groups-inject').html(data);
		}
	});
}
function ajaxUserForumPosts(contentobject_id, node_id) {
	if (uni.deactivate) { return false; }
	$.ajax({
		type: "POST",
		url: '/content/view/ajax/' + node_id + '/(view)/forum-posts',
		data: { 
			user_id: uni.user_id
		},
		success: function(data){
			$('#forum-posts-inject').html(data);
			$('#forum-posts-inject span.timespan').each(function() {
				ts = $(this).attr('title');
				timespan = calculateTime(ts);
				if (timespan != false) {
					$(this).text(timespan);
				}
			});
		}
	});
}
function ajaxUserSavedArticles(contentobject_id, node_id) {
	if (uni.deactivate) { return false; }
	$.ajax({
		type: "POST",
		url: '/content/view/ajax/' + node_id + '/(view)/saved-articles',
		data: { 
			user_id: uni.user_id
		},
		success: function(data){
			$('#saved-articles-inject').html(data);
			$('#saved-articles-inject span.timespan').each(function() {
				ts = $(this).attr('title');
				timespan = calculateTime(ts);
				if (timespan != false) {
					$(this).text(timespan);
				}
			});
			var block_ids = new Array();
			$('#saved-articles-inject div.function-save').each(function() {
				block_ids.push($(this).attr('id').split('_')[1]);
			});
			if (block_ids.length > 0) {
				isSavedList(block_ids);
			}
		}
	});
}
function ajaxUserSavedSupplements(contentobject_id, node_id) {
	if (uni.deactivate) { return false; }
	$.ajax({
		type: "POST",
		url: '/content/view/ajax/' + node_id + '/(view)/supplements',
		data: { 
			user_id: uni.user_id
		},
		success: function(data){
			$('#saved-supplements-inject').html(data);
			var block_ids = new Array();
			$('#saved-supplements-inject div.function-save').each(function() {
				block_ids.push($(this).attr('id').split('_')[1]);
			});
			if (block_ids.length > 0) {
				isSavedList(block_ids);
			}
		}
	});
}
function ajaxUserSavedVideos(contentobject_id, node_id) {
	if (uni.deactivate) { return false; }
	$.ajax({
		type: "POST",
		url: '/content/view/ajax/' + node_id + '/(view)/saved-videos',
		data: { 
			user_id: uni.user_id
		},
		success: function(data){
			$('#saved-videos-inject').html(data);
		}
	});
}

function addStat(node_id) {
	if (uni.deactivate) { return false; }
	contentobject_id = $('#stat').val();
	url = '/usercontent/post?height=' + uni.pHeight + '&width=' + uni.pWidth + '&content_type=user_stat&action=post&ajax=1&contentobject_id=' + contentobject_id + '&location=' + node_id;
	tb_show('Add User Stat', url);
}

