function friend(ele){
	$.ajax({
		"type" : "post",
		"url" : "/friend",
		"data" : "fu=" + $(ele).attr("id").substring(7) + "&action=add",
		"success" : function(data){updateFriend(data);},
		"error" : "showFriendError",
		"dataType" : "xml"
	});
}

function unfriend(ele){
	$.ajax({
		"type" : "post",
		"url" : "/friend",
		"data" : "fu=" + $(ele).attr("id").substring(7) + "&action=remove",
		"success" : function(data){updateUnfriend(data);},
		"error" : "showFriendError",
		"dataType" : "xml"
	});
}

function updateFriend(data){
	var msgType = $(data).find("messageType").text();	
	if(msgType == "1"){
		$(".add_as_friend").css("display", "none");
		$(".remove_friend").css("display", "block");
	}else if(msgType == "0"){
		alert($(data).find("message").text());
	}
}

function updateUnfriend(data){
	var msgType = $(data).find("messageType").text();
	if(msgType == "1"){
		$(".remove_friend").css("display", "none");
		$(".add_as_friend").css("display", "block");
	}else if(msgType == "0"){
		alert($(data).find("message").text());
	}
}

function showFriendError(){
	alert("Oops, something messed up and we were unable to complete your request. Please try again in a few minutes");
}

$(document).ready(function(){
	$(".add_as_friend").click(function(e){friend($(this)); e.preventDefault();});
	$(".remove_friend").click(function(e){unfriend($(this)); e.preventDefault();});
});