function addToFavourite(id){
	//Start the image swap for adding to favourites
	switchImage(id,"wait");

	//Check cookie
	$.ajax({
		url: '/ajax.php?mode=cookie',
		type: 'get',
		success: function(t) {
			sess_id = t;
			$.ajax({
				url: '/ajax.php?mode=favourite&id=' + id + '&sid=' + sess_id,
				type: 'post',
				success: function(t) {
					switchImage(id,t);
					updateYourFavourites();
					removeFavouriteList(id);
				}
			});
		}
	});
}

function updateYourFavourites(){
	var updatable = $('#your-favourites > p > strong');
	$.ajax({
		url: '/ajax.php?mode=count&sid=' + sess_id,
		type: 'post',
		success: function(t) {
			updatable.fadeOut('slow', function(){
				updatable.html(t);
				updatable.fadeIn('slow');
			});
		}
	});
}

function removeFavouriteList(id){
	if($('#list-favourite-' + id).length > 0){
		$('#list-favourite-' + id).fadeOut('slow', function(){
			//Need to check if there's any more entries
			$.ajax({
				url: '/ajax.php?mode=count&sid=' + sess_id,
				type: 'post',
				success: function(t) {
					if(t == "no attractions"){
						$('h3.search').after("<p>You have no attractions saved as your favourites. Please use the map or the search to the left to find your attractions</p>");
						$('#emailsave').fadeOut("slow");
					}
				}
			});
		});
	}
}

function switchImage(id,mode){
	var src = "";
	var clss = "";
	switch(mode){
	case "add":
		src = "Add to Favourites";
		clss = "add";
		break;
	case "wait":
		src = "wait";
		clss = "wait";
		break;
	case "remove":
		src = "Remove from Favourites";
		clss = "remove";
		break;
	default:
		src = "error";
		break;
	}
	$("#favourite-" + id).html(src);
	$("#favourite-" + id).attr("class",clss);
}