//============================================================================================
/* 	FUNCTION - FavToggle ----------------------------------------------------------------------
 this function sends an ajax request to a server side script with an ImageID, and a 
 boolean value indicating whether the image should be added or removed from the favourites
 collection.

 the function expects a json encoded response from the server with essentially the same
 data that was sent to the server. The imageID and a boolean value indicating whether the
 image was added (true) or removed (false) from favourites.

 if an error occurs and the server cannot be reached and alert message is displayed				
 ---------------------------------------------------------------------------------------------- */
function favToggle(imageID) {

	// detect whether the image is being faved or unfaved
	if (pageData['favesList'][imageID]) {
		var faved = 0; // image is being faved
	} else {
		var faved = 1; // image is being unfaved
	}

	// send the ajax request See http://api.jquery.com/jQuery.ajax/ for more
	// details
	$
			.ajax( {
				type : "GET",
				url : "/gallery",
				data : "blockID=userStateBC&op=fav" + "&SkinID=" + imageID
						+ "&favStatus=" + faved,
				dataType : 'json',

				success : function(returnData) {
					var image = $('#' + returnData.setData.changedImage);
					ajaxUpdate(returnData);
					// add favourite badge to image
					if (pageData['favesList'][imageID]) {
						image
								.append("<div class=\"favourite\">Favourite</div>");
					}
					// remove favourite badge from image
					else {
						image.find('.favourite').remove();
					}
				},

				error : function(XHR, msg, error) {
					alert('There was a problem communicating with the server, please try again.');
				}
			});
}

$(document).ready(function() {
	var path = "/resources/images/icons/";
	$('.fav').hover(function() {
		$(this).attr('src', path + 'favourite_add_over.gif');
	}, function() {
		$(this).attr('src', path + 'favourite_add.gif');
	});
});
