//  Interested in looking at our code?  Maybe you should work for us !!!

//  If you understand most of what is going on below this message and you have a real passion 
//  for working somewhere which breaks into new opportunites and CAN'T STAND red tape you may 
//  be right for the job.  Drop us an email: info@puntersparadise.com.au

  function RelationshipHandler()
  {

  }

  RelationshipHandler.prototype.addFavourite = function(userID,username,avatar)
  {
    showFacebox('Add Favourite',
      '<div class="question">Adding this user to your favourites helps you track their performance, easily purchase their tips and compare them to your other favourite tipsters.<br/><br/><div class="questionImage"><div class="AvatarOuter"><div class="avatarBackdrop"><div class="avatarBackdropInner"><div style="width: 48px; height: 48px; background-image: url(\'' + unescape(avatar) + '\');"></div></div></div></div></div><div class="questionMessage">Are you sure you would like to add ' + username + ' to your favourites?</div><div style="clear: both;"></div></div>',
      '<a class="button button-green" href="javascript:void(null);" onclick="relationshipPageHandler.confirmedAddFavourite(' + userID + ',\'' + username +'\',\'' + avatar + '\');"><span>Add Favourite</span></a>',
      false);
  }

  RelationshipHandler.prototype.confirmedAddFavourite = function(userID,username,avatar)
  {
    $.ajax({ 
        url: '/kingtipping/Relationship_AddFavourite.php', 
        cache: false, 
        data: "u=" + userID,
        success: function() { 
          //Update to the F symbol on the page (if the page has the user somewhere on it)
          $('#addFavourite_'+userID).html('<a href="javascript:relationshipPageHandler.removeFavourite('+userID+",'"+username+"','" + avatar +'\');">Remove favourite</a>');
          $('#addFavourite_'+userID).addClass('remove');
          //Clear the cache so the drop-downs will reload
          mainPageHandler.cacheContainer = new Array();
          //showBasicPopup('<div class="success">'+username+' has been successfully added to your favourites.</div>');
          $.facebox.close();
        },
        error: function() { 
          showFacebox('Error',
            '<div class="error">An error occurred while attempting to add this favourite.<br/><br/>Please refresh your page and try again.</div>',
            '',
            false);
        }
    }); 
  }


  RelationshipHandler.prototype.addMate = function(userID,username,avatar)
  {
    showFacebox('Add Mate',
      '<div class="question">Adding this user as a mate will allow you to stay in touch with their comments, tipping information, blackbook entries and status updates.<br/><br/><div class="questionImage"><div class="AvatarOuter"><div class="avatarBackdrop"><div class="avatarBackdropInner"><div style="width: 48px; height: 48px; background-image: url(\'' + unescape(avatar) + '\');"></div></div></div></div></div><div class="questionMessage"><span class="strongText">Personal message: </span><br/><textarea id="txtMessage" rows="3" style="width: 300px;"></textarea><br/><br/></div></div><div style="clear: both;"></div></div>',
      '<a class="button button-green" href="javascript:void(null);" onclick="relationshipPageHandler.confirmedAddMate(' + userID + ',\'' + username +'\',\'' + avatar + '\');"><span>Send Request</span></a>',
      false);
  }

  RelationshipHandler.prototype.confirmedAddMate = function(userID,username,avatar)
  {
    //Get the information entered before sending up the request:
    var personalMessage = $("#txtMessage").val();
    var relation = $("#txtRelation").val();

    $.ajax({ 
        url: '/kingtipping/Relationship_AddMate.php', 
        cache: false, 
        data: "u=" + userID + "&m=" + escape(personalMessage) + "&r=" + escape(relation),
        success: function(data,textStatus) { 
          var splitResponse = data.split(',');
          if (splitResponse.length > 0 && splitResponse[0] == 1)
          {
            showFacebox('Request Sent',
              '<div class="success">A request has been sent out for '+username+' to be your mate.</div>',
              '',
              false);
          }
          else
          {
            //Failed
            var message = 'An unknown error occurred while attempting to request a mate.<br/><br/>Please refresh the page and try again.';
            if (splitResponse.length > 1)
            {
              message = data.substring(splitResponse[0].length+1);
            }
            showFacebox('Error',
              '<div class="error">' + message + '</div>',
              '',
              false);
          }
        },
        error: function() { 
          showFacebox('Error',
              '<div class="error">An error occurred while attempting to add ' + username + ' to your mates.<br/><br/>Please refresh your page and try again.</div>',
              '',
              false);
        }
    }); 
  }

  RelationshipHandler.prototype.removeFavourite = function(userID,username,avatar)
  {
    showFacebox('Remove Favourite',
      '<div class="question"><div class="questionImage"><div class="AvatarOuter"><div class="avatarBackdrop"><div class="avatarBackdropInner"><div style="width: 48px; height: 48px; background-image: url(\'' + unescape(avatar) + '\');"></div></div></div></div></div><div class="questionMessage">Are you sure you would like to remove ' + username + ' from your favourites?</div><div style="clear: both;"></div></div>',
      '<a class="button button-green" href="javascript:void(null);" onclick="relationshipPageHandler.confirmedRemoveFavourite(' + userID + ',\'' + username +'\',\'' + avatar + '\');"><span>Remove Favourite</span></a>',
      false);
  }

  RelationshipHandler.prototype.confirmedRemoveFavourite = function(userID,username,avatar)
  {
    $.ajax({ 
        url: '/kingtipping/Relationship_RemoveFavourite.php', 
        cache: false, 
        data: "u=" + userID,
        success: function() { 
          $('#addFavourite_'+userID).html('<a href="javascript:relationshipPageHandler.addFavourite('+userID+",'"+username+"','" + avatar + '\');">Add favourite</a>');
          $('#addFavourite_'+userID).removeClass('remove');
          $('#userFavIcon_' + userID).remove();
          $('#FavouritesUser_'+userID).remove();
          mainPageHandler.cacheContainer = new Array();
          $.facebox.close();
        },
        error: function() { 
          showFacebox('Error',
            '<div class="error">An error occurred while attempting to remove this favourite.<br/><br/>Please refresh your page and try again.</div>',
            '',
            false);
        }
    }); 
  }

  RelationshipHandler.prototype.removeMate = function(userID,username,avatar)
  {
    showFacebox('Remove Mate',
      '<div class="question"><div class="questionImage"><div class="AvatarOuter"><div class="avatarBackdrop"><div class="avatarBackdropInner"><div style="width: 48px; height: 48px; background-image: url(\'' + unescape(avatar) + '\');"></div></div></div></div></div><div class="questionMessage">Are you sure you would like to remove ' + username + ' from your mates?</div><div style="clear: both;"></div></div>',
      '<input type="button" id="yes" value="Yes" onclick="relationshipPageHandler.confirmedRemoveMate(' + userID + ',\'' + username +'\',\'' + avatar + '\');" class="button" />',
      false);
  }

  RelationshipHandler.prototype.confirmedRemoveMate = function(userID,username,avatar)
  {
    $.ajax({ 
        url: '/kingtipping/Relationship_RemoveMate.php', 
        cache: false, 
        data: "u=" + userID,
        success: function(data,textStatus) {
          
          var splitResponse = data.split(',');
          if (splitResponse.length > 0 && splitResponse[0] == 1)
          {
            $('#addMenu_'+userID).html('<a href="javascript:relationshipPageHandler.addMate('+userID+",'"+username+"','" + avatar + '\');">Add as a mate</a>');
            $('#addMate_'+userID).removeClass('remove');
            $('#MatesUser_'+userID).remove();
            mainPageHandler.cacheContainer = new Array();
            $.facebox.close();
          }
          else
          {
            var message = 'An unknown error occurred while attempting to remove this mate.<br/><br/>Please refresh the page and try again.';
            if (splitResponse.length > 1)
            {
              message = data.substring(splitResponse[0].length+1);
            }
            showFacebox('Error',
              '<div class="error">' + message + '</div>',
              '',
              false);
          }
        },
        error: function() { 
          showFacebox('Error',
            '<div class="error">An error occurred while attempting to remove this favourite.<br/><br/>Please refresh your page and try again.</div>',
            '',
            false);
        }
    }); 
  }

  var relationshipPageHandler = new RelationshipHandler();