function FriendshipManager (itsMe, friendsUrl, coachId) 
{
    this.itsMe = itsMe;
    this.friendsUrl = friendsUrl;
    this.coachId = coachId;
    this.addFriendBut = $("#addFriend");
    this.removeFriendBut = $("#removeFriend");

    this.initButtons();
    this.updateStatus();
}

FriendshipManager.prototype.initButtons = function ()
{
    this.addFriendBut.click (function() {friendshipMgr.becomeFriends()});
    this.removeFriendBut.click (function() {friendshipMgr.finishFriendship()});
}

FriendshipManager.prototype.updateStatus = function ()
{
    if(!this.itsMe){
		$.ajax({
	        type: "GET",
	        url: this.friendsUrl,
	        data: {"op": "checkIsFriend", "id": this.coachId},
	        dataType: "json",
	        cache: false,
	        success: function (data) {
	            friendshipMgr.updateFriendButton(data);
		    },
	        error: function (request, errorMsg, errorObj)
	        {
	            alert(errorMsg);
	        }
	    });
    }
}

FriendshipManager.prototype.updateFriendButton = function (data)
{
	if(data.isFriend)
    {
        this.addFriendBut.css("display", "none");
        this.removeFriendBut.css("display", "block");
    }
	else
    {
        this.addFriendBut.css("display", "block");
        this.removeFriendBut.css("display", "none");
    }
}

FriendshipManager.prototype.becomeFriends = function(){
	$.ajax({
        type: "POST",
        url: this.friendsUrl,
        data: {"op": "becomeFriends", "id": this.coachId},
        dataType: "json",
        cache: false,
        success: function (data) {
            friendshipMgr.updateStatus();
	    },
        error: function (request, errorMsg, errorObj)
        {
            alert(errorMsg);
        }
    });
}

FriendshipManager.prototype.finishFriendship = function(){
	$.ajax({
        type: "POST",
        url: this.friendsUrl,
        data: {"op": "finishFriendship", "id": this.coachId},
        dataType: "json",
        cache: false,
        success: function (data) {
            friendshipMgr.updateStatus();
	    },
        error: function (request, errorMsg, errorObj)
        {
            alert(errorMsg);
        }
    });
}

var friendshipMgr;
$(document).ready(function(){
	if(loggedIn)
		friendshipMgr = new FriendshipManager(itsMe, friendsUrl, coachId);
});