function showSection(source, target) {
	$('#form-success').hide();
	if(curOpen == target) return;
	$('#'+ curOpen).hide();
	curOpen = target;
	$('#' + curOpen).fadeIn();
	//$(source).toggleClass('selected');
}

// Add a user while browsing any part of the site
function addFriendInline(user) {
	$(document).ready(function() {
		$.getJSON("./?c=api&m=addFriendFeed", { email: user, callback: 'silent' }, 
			function(json){				
			}
		);
	});	
}


function addFriendFeed(user) {
	$(document).ready(function(){
	    // Check if the user already has the friend
	    var exists = document.getElementById(user);

		$.getJSON("./?c=api&m=addFriendFeed", { email: user }, function(json){
		    if(json.error) {
		        toggleAddMessage("<div class='myaccount-friend-add-error'>"+json.error_msg+"</div>", "Friend");
		    } else {
		        // Clear the input field
		        $("#addFriend").attr("value","");
		        
		        // Slide the friend row into the list
		        $("#friendList").prepend(json.html);
		        $("#friend_"+json.uid).slideDown('slow'); 
		        
		        toggleAddMessage("[Enter your friend's email address]", "Friend");
		        
		    }
		});	  
	
	});	
}

// If following is set to 1, it actually removes someone from following you
function removeFriendFeed(uid, following) {
	$(document).ready(function(){	
		$.getJSON("./?c=api&m=removeFriendFeed", { uid: uid, following: following }, function(json){
		    if(json.error) {
		        toggleAddMessage("<div class='myaccount-friend-remover-error'>"+json.error_msg+"</div>", "Friend");
		    }else{
		        $("#remove"+uid).parent().slideUp('slow');	  
		        // Remove the email is from parent so we can add it again
		        // We don't just use remove() because otherwise it won't slide
		        $("#remove"+uid).parent().attr("id","");
		    }
		});		
	});	
}



// Used for the story view version of adding and removing feeds
function updateFavFeed(fid) {
	$(document).ready(function(){
	
		// Replace the star with a yellow star
	    var starImg = $("div[id='fav_"+ fid + "'] > img");
		var src = starImg.attr("src");
		var starred = src.search(/full/);
		
		// Remove feed from the user's fave list if the star is already marked
		if(starred > 0) {
			$.getJSON("./?c=api&m=removeFaveFeed", { fid: fid }, function(json){
			    if(json.error) {
			        // No errors yet
			    }else{
					// Replace the yellow star with an empty star
					var newSrc = src.replace(/full/, "empty");
					starImg.attr("src", newSrc);
					starImg.attr("title", "Add to favorites");
			    }
			});			
		}else{
		// Add the feed to the user's fave list
			$.getJSON("./?c=api&m=addFaveFeed", { fid: fid }, function(json){
			    if(json.error) {
			        // No errors yet
			    }else{
					// Replace the empty star with a yellow star
					var newSrc = src.replace(/empty/, "full");
					starImg.attr("src", newSrc);
					starImg.attr("title", "Remove from favorites");
			    }
			});			
		}
		
		
	});
}

// Used for the my profile page version of adding feeds
function addFavFeed(fid) {
	$(document).ready(function(){
	    // Check if the user already has the friend
	    var exists = document.getElementById("fav_"+fid);
	
		if(exists){
		    toggleAddMessage("<div class='myaccount-form-error'>You have already added "+fid+" as a feed.</div>", "Feed");
		}else{
			// Add the feed to the user's fave list
			$.getJSON("./?c=api&m=addFaveFeed", { fid: fid, returnHTML: 1 }, function(json){
			    if(json.error) {
			        toggleAddMessage("<div class='myaccount-form-error'>"+json.error_msg+"</div>", "Feed");
			    }else{
			        // Clear the add feed text box
			        $("#addFeed").attr("value","");
			        $("#feedList").prepend(json.html);		
			        $("#fav_"+fid).slideDown('slow'); 
			        
			        toggleAddMessage("[Enter the url of your feed]", "Feed");
			    }
			});	
		}
	});	
}

// Used for the my profile page version of removing feeds
function removeFavFeed(fid) {
	$(document).ready(function(){	
		$.getJSON("./?c=api&m=removeFaveFeed", { fid: fid }, function(json){
		    if(json.error) {
		        toggleAddMessage("<div class='myaccount-form-error'>"+json.error_msg+"</div>", "Feed");
		    }else{
		        $("#fav_"+fid).slideUp('slow');	  
		        // Remove the fid so we can add it again
		        // We don't just use remove() because otherwise it won't slide
		        $("#fav_"+fid).attr("id","");
		    }
		});		
	});	
}

//// Used to delete the user's profile pic
function removeProfilePic() {
	$(document).ready(function(){
		$("#myaccount-profile-pic2").slideUp('slow');
		$("#myaccount-profile-pic").animate({width:'toggle'},1000);
		
		$.getJSON("./?c=api&m=removeProfilePic", { }, function(json){
		});
	});
}


//***********************
// Private functions
//***********************
function toggleAddMessage(msg, type) {
	$(document).ready(function(){
        $("#add"+ type + "Message").hide();
        $("#add"+ type + "Message").html(msg);
        $("#add"+ type + "Message").fadeIn("slow");	
	});	
}