$(window).load(function() {
	$('.favourite').click(function(e) {
		e.preventDefault();
		var $this = $(this);
		var $params = $this.attr('rel').split(':');
		var $id = $params[0];
		var $type = $params[1];
		var $member = $params[2];
		var $direction = $params[3];
		var $return = ($direction == 'add') ? 'remove' : 'add';
		$.ajax({
			type: 'POST',
			url: siteurl + 'application/favourite.php',
			data: { 
				'id': $id,
				'type': $type,
				'member': $member,
				'direction': $direction
			},
			success: function() {
				$this.html(($direction == 'add') ? 'remove from favourites' : 'add to favourites');
				$this.attr('rel', $this.attr('rel').replace($direction, $return));
			},
			error: function() {
				alert('We\'re sorry but there was an error when adding your favourite. Please try again');
			}
		});
	});
});

