var update_follow_view = true;
var update_unfollow_view = true;

function updateFriendCount(){
	$.post(siteUrl("friends/ajax_friend_count"), {}, 
			function(data){
		
				if (data.followers != undefined)
					$("#followers-count").html('<strong>' + data.followers + '</strong> Followers');
				else
				{	$("#followers-count").empty();

					if (active_view == 'followers')
						window.location = siteUrl('dashboard/friends');

				}

				
				if (data.following != undefined)
					$("#following-count").html('<strong>' + data.following + '</strong> Following');
				
				else{
					$("#following-count").empty();

					if (active_view == 'following')
						window.location = siteUrl('dashboard/friends');

				}
					
				/*
				 * if (data.mutual != undefined)
				 * $("#mutual-count").html('<strong>' + data.mutual + '</strong>
				 * Two-way'); else{
				 * 
				 * $("#mutual-count").empty();
				 * 
				 * if (active_view == 'mutual') window.location =
				 * siteUrl('dashboard/friends'); }
				 */
				
				if (data.blocked != undefined)
					$("#blocked-count").html('<strong>' + data.blocked + '</strong> Blocked');
				else{
					$("#blocked-count").empty();

					if (active_view == 'blocked')
						window.location = siteUrl('dashboard/friends');
				}
	}, 'json');
}
$(".follow-friend").live('click', function(e)
{
	e.preventDefault();

	var member_id = $(this).attr('id').split('-');

	member_id = member_id[1];

	$.post($(this).attr('href'), {}, function(data)
	{
		var injection = '<li><a id="unfollow-' + member_id + '" href="' + siteUrl('friends/unfollow/')  + member_id + '" class="unfollow-friend">Unfollow</a></li>';
		$("#friend-actions-" + member_id).empty();
		$("#friend-actions-" + member_id).append(injection);
		if (update_follow_view){
			var friend_status = 'following';
			$("#friend-status-" + member_id).text(friend_status);
						
			injection = '<li><a id="block-' + member_id + '" href="' + siteUrl('friends/block/') + member_id + '" class="block-friend">Block</a></li>';

			$("#friend-actions-" + member_id).append(injection);
								
			updateFriendCount();
		}
		$("#message-text").html('<div class="success">You are now following this person.</div>');

//		$.scrollTo(0, 800, {easing:"swing", onAfter: function(){initMessage();}});
		initMessage();
	}, 'json');				
});

$(".unfollow-friend").live('click', function(e)
{
	e.preventDefault();
	var member_id = $(this).attr('id').split('-');
	member_id = member_id[1];
	$.post($(this).attr('href'), {}, function(data)
	{
		var injection = '<li><a id="follow-' + member_id + '" href="' + siteUrl('friends/follow/') + member_id + '" class="follow-friend">Follow</a></li>';
		$("#friend-actions-" + member_id).empty();
		$("#friend-actions-" + member_id).append(injection);
		if (update_unfollow_view){
			var remove_friend = false;
			
			if (active_view == 'mutual' || active_view == 'following' || active_view == 'groups')
				remove_friend = true;
			
			if (remove_friend)
			{
				$("#friend-" + member_id).remove();
			}
			else
			{
				if (data.follower)
				{	
					$("#friend-status-" + member_id).text('follower');
					injection = '<li><a id="block-' + member_id + '" href="' + siteUrl('friends/block/')+ member_id + '" class="block-friend">Block</a></li>';
					$("#friend-actions-" + member_id).append(injection);
				}
				else
				{
					$("#friend-" + member_id).remove();
				}
			}
			updateFriendCount();
		}
		$("#message-text").html('<div class="error">You have unfollowed this person.</div>');
//		$.scrollTo(0, 800, {easing:"swing", onAfter: function(){initMessage();}});
		initMessage();
	}, 'json');
});

$(".unblock-friend").live('click', function(e){
	e.preventDefault();
	e.stopImmediatePropagation();
	$.post($(this).attr('href'), {}, function(data)
	{
		$("#friend-" + data.member_id).remove();
		$("#message-text").html('<div class="success">You have unblocked this person.</div>');
//		$.scrollTo(0, 800, {easing:"swing", onAfter: function(){initMessage();}});
		initMessage();
		updateFriendCount();
	}, 'json');
});

$(".block-friend").click(function(e){
	e.preventDefault();
	e.stopImmediatePropagation();
	var target_obj = this;

	if (confirmation('Are you sure you want to block this user?'))
	{				
		$.post($(this).attr('href'), {}, function(data)
		{
			$("#friend-" + data.member_id).remove();

			updateFriendCount();
								
			$("#message-text").html('<div class="error">You have blocked this person.</div>');
//			$.scrollTo(0, 800, {easing:"swing", onAfter: function(){initMessage();}});
			initMessage();
		}, 'json');						
	}
});

$(".quick-follow").live('click', function(e)
{
	e.preventDefault();
	e.stopImmediatePropagation();
	if(!Login.valid(arguments.callee, this, arguments))
		return false;
	var $this = $(this);
	$.post($this.attr('href'), {}, function(data){
		var msg = '';
		if(data.act == 'error')
		{
			msg = '<div class="error">'+data.error+'</div>';
		}
		else if(data.act == 'followed')
		{
			if($this.parents(".response-user-nav").length != 0){
				$this.html("Following").addClass("following");
			} else {
				$this.html("&#x2713; Following").addClass("following");
			}
			msg = '<div class="success">You are now following '+data.username+'.</div>';
		}
		else if(data.act == 'unfollowed')
		{	
			msg = '<div class="error">You stopped following '+data.username+'.</div>';
			$this.html("Follow").removeClass("following unfollow");
		}

		$("#message-text").html(msg);

//		$.scrollTo(0, 800, {easing:"swing", onAfter: function(){initMessage();}});
		initMessage();
	}, 'json');
});

//$(".following.friendship").mouseenter(function(){
//	$(this).html("Unfollow");
//}).mouseleave(function(){
//	$(this).html("Following");
//});

//$(".following").live({
//	mouseenter: function(){
//		$(this).html("Unfollow  <span class='undo'>&times;</span>").addClass("unfollow");
//	},
//	mouseleave: function(){
//		$(this).html("Following").removeClass("unfollow");
//	}
//});

$(".friendship").live('click', function(e)
{
	e.preventDefault();
	e.stopImmediatePropagation();
	if(!Login.valid(arguments.callee, this, arguments))
		return false;
	$.post($(this).attr('href'), {}, function(data){
		var msg = '';
		if (data.unfollowed){
			msg = '<div class="error">You have unfollowed this person.</div>';
			$("#friendship-toggle").attr('href', siteUrl('friends/follow/' + focused_member_id));
			$('.utility-block a:first-child').html("Follow").removeClass("following unfollow");			
		}
		if (data.following){
			msg = '<div class="success">You are now following this person.</div>';
			$("#friendship-toggle").attr('href', siteUrl('friends/unfollow/' + focused_member_id));
			$('.utility-block a:first-child').html("&#x2713; Following").addClass("following");
		}
		if (data.blocked){
			msg = '<div class="error">You have blocked this person.</div>';
			$("#friendship-toggle").attr('href', siteUrl('friends/unblock/' + focused_member_id ));
			$('#block-friend').html("(×) Unblock " + data.username).attr('href', siteUrl('friends/unblock/' + focused_member_id ));
			$('.utility-block a:first-child').html("Blocked").addClass("blocked").removeClass("following");
		}
		if (data.unblocked){
			$('#friend-'+data.member_id).hide();
			msg = '<div class="success">You have unblocked this person.</div>';
			$("#friendship-toggle").attr('href', siteUrl('friends/follow/' + focused_member_id));
			$('#block-friend').html("(×) Block " + data.username).attr('href', siteUrl('friends/block/' + focused_member_id ));
			$('.utility-block a:first-child').html("Follow").removeClass("blocked");
		}
							
		$("#message-text").html(msg);			
		initMessage();
	}, 'json');					
});
