﻿if (!gotEntryPoint)
{
	gotEntryPoint = true;

	$(document).ready(function() {
		initMain(true);

		renderFavouriteLink();
		updateFavouriteLink();
	});
}

function addFavouriteLinkClick(event)
{
	if (event && typeof(event.preventDefault) != 'undefined')
		event.preventDefault();

	$('#favouriteLink').text('Adding to favourites, please wait...');

	var itemDescription = $("meta[name='description']").attr('content');
	if (!itemDescription)
		itemDescription = document.title;

	var itemImage = $("meta[name='object-image']").attr('content');
	if (!itemImage)
		itemImage = defaultItemImage;




	$.ajax({
		cache:		false,

		type:		'POST',
		url:		favouriteHandlerUrl,

		data: {
			Action:		'Save',
			ItemID:		$('#itemID').val(),
			
			ItemName:			document.title,
			ItemDescription:	itemDescription,
			ItemUrl:			document.location.href,
			ItemImageUrl:		itemImage
			
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'Favourite.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#favouriteLink'
			});
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			$('#favouriteLink').text('Your favourites could not be updated at this time, please try again later.');
		}
	});
}

function onAddFavouriteCallback(success)
{
	if (!success)
	{
		// If the call logged us out, don't try showing/hiding anything since it's already been done

		var cookie = $.cookie('blLogin');

		if (!cookie)
			return;

		$('#favouriteLink').text('Your favourites could not be updated at this time, please try again later.');

		return;
	}
}

function removeFavouriteLinkClick(event)
{
	if (event && typeof(event.preventDefault) != 'undefined')
		event.preventDefault();

	$('#favouriteLink').text('Removing from favourites, please wait...');

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		favouriteHandlerUrl,

		data: {
			Action:		'Remove',
			UserIDs:	myUserId,
			ItemIDs:	$('#itemID').val()
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'Favourite.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#favouriteLink'
			});
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			$('#favouriteLink').text('Your favourites could not be updated at this time, please try again later.');
		}
	});
}

function onRemoveFavouriteCallback(success)
{
	if (!success)
	{
		// If the call logged us out, don't try showing/hiding anything since it's already been done

		var cookie = $.cookie('blLogin');

		if (!cookie)
			return;

		$('#favouriteLink').text('Your favourites could not be updated at this time, please try again later.');

		return;
	}
}

function updateFavouriteLink()
{
	var link = $('#favouriteLink');

	if (!link)
		return;

	var cookie = $.cookie('blLogin');

	if (!cookie)
		return;

	if (link.length > 0)
	{
		link.text('Retrieving favourite items, please wait...');

		$.ajax({
			cache:		false,

			type:		'POST',
			url:		favouriteHandlerUrl,

			data: {
				Action:		'GetList',
				UserIDs:	myUserId,
				ItemIDs:	$('#itemID').val()
			},

			dataType:	'text',

			success:	function(data, textStatus)
			{
				$.xslt({
					xml:			data,
					xslUrl:			xslBaseUrl + 'Favourite.xsl',
					xslCache:		isLive,		/* Don't cache if debugging */
					dataTypeXML:	true,
					target:			'#favouriteLink'
				});
			},

			error:		function(XMLHttpRequest, textStatus, error)
			{
				link.text('Your favourites could not be retrieved at this time, please try again later.');
			}
		});
	}
	else
	{
		var itemList = $('#itemList');

		if (!itemList || !itemList.length)
			return;

		itemList.text('Retrieving favourites, please wait...');

		$.ajax({
			cache:		false,

			type:		'POST',
			url:		itemHandlerUrl,

			data: {
				Action:			'GetList',
				IsFavourite:	true,
				Name:			'',
				Description:	'',
				Url:			'',
				ImageUrl:		'',

				Tag:			''
			},

			dataType:	'text',

			success:	function(data, textStatus)
			{
				$.xslt({
					xml:			data,
					xslUrl:			xslBaseUrl + 'Item.xsl',
					xslCache:		isLive,		/* Don't cache if debugging */
					dataTypeXML:	true,
					target:			'#itemList'
				});
			},

			error:		function(XMLHttpRequest, textStatus, error)
			{
				itemList.text('There was an error communicating with the server and the items could not be retrieved at this time, please try again later.');
			}
		});
	}
}

function renderFavouriteLink()
{
	var cookie = $.cookie('blLogin');

	$('#favouriteContainer').removeClass('invisible');

	if (cookie)
	{
		$('#favouriteLink').removeClass('invisible');
		$('#itemList').removeClass('invisible');
		$('#seeMyFavouritesLink').removeClass('invisible');
		$('#favouriteLinkLogin').addClass('invisible');
	}
	else
	{
		$('#favouriteLink').addClass('invisible');
		$('#itemList').addClass('invisible');
		$('#seeMyFavouritesLink').addClass('invisible');
		$('#favouriteLinkLogin').removeClass('invisible');
	}
}

