﻿if (!gotEntryPoint)
{
	gotEntryPoint = true;

	$(document).ready(function() {
		initMain(true);

		$('#tagSearchFor').keyup(function(event) {
			if (event.keyCode == 13)
				tagSearchButtonClick(event);
		});

		if (document.location.search.length > 0)
		{
			var parts = document.location.search.substr(1, document.location.search.length - 1).split('&');

			for (var i = 0; i < parts.length; i++)
			{
				var keyValue = parts[i].split('=');

				if (!keyValue.length)
					continue;

				switch (keyValue[0])
				{
					case 'tag':
						if (keyValue.length > 1)
							$('#tagSearchFor').val(decodeURIComponent(keyValue[1]));

						break;
				};
			}
		}

		if ($('#tagSearchFor').val().length > 0)
			tagSearchButtonClick(null);
	});
}

function addTagButtonClick(event)
{
	if (event && typeof(event.preventDefault) != 'undefined')
		event.preventDefault();

	$('#myTags').addClass('invisible');
	$('#myTagsUpdateProgress').text('Adding tag, please wait...');
	$('#myTagsUpdateProgress').removeClass('invisible');

	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:		tagHandlerUrl,

		data: {
			Action:		'Save',
			ItemID:		$('#itemID').val(),
			Value:		$('#newTagValue').val(),
			IsOfficial:	false,
			IsBanned:	false,

			ItemName:			document.title,
			ItemDescription:	itemDescription,
			ItemUrl:			document.location.href,
			ItemImageUrl:		itemImage
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'Tag.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#myTagsUpdateProgress'
			});
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			$('#myTagsUpdateProgress').text('Your tag could not be added at this time, please try again later.');

			setTimeout(function() {
				$('#myTagsUpdateProgress').addClass('invisible');
				$('#myTags').removeClass('invisible');
			}, 10000);
		}
	});
}

function onAddTagCallback(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;

		$('#myTagsUpdateProgress').text('Your tag could not be added at this time, please try again later.');

		setTimeout(function() {
			$('#myTagsUpdateProgress').addClass('invisible');
			$('#myTags').removeClass('invisible');
		}, 10000);

		return;
	}

	$('#myTagsUpdateProgress').addClass('invisible');
	$('#myTags').removeClass('invisible');

	updateTagsList();
}

function tagLinkClick(link)
{
	if (navigator.appName == 'Microsoft Internet Explorer')
		event.returnValue = false;

	document.location.href = searchTagsUrl + encodeURIComponent($(link).text());
}

function removeTagLinkClick(event, tagId)
{
	if (event && typeof(event.preventDefault) != 'undefined')
		event.preventDefault();

	$('#myTags').addClass('invisible');
	$('#myTagsUpdateProgress').text('Removing tag, please wait...');
	$('#myTagsUpdateProgress').removeClass('invisible');

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		tagHandlerUrl,

		data: {
			Action:		'Remove',
			IDs:		tagId
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'Tag.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#myTagsUpdateProgress'
			});
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			$('#myTagsUpdateProgress').text('Your tag could not be removed at this time, please try again later.');

			setTimeout(function() {
				$('#myTagsUpdateProgress').addClass('invisible');
				$('#myTags').removeClass('invisible');
			}, 10000);
		}
	});
}

function onRemoveTagCallback(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;

		$('#myTagsUpdateProgress').text('Your tag could not be removed at this time, please try again later.');

		setTimeout(function() {
			$('#myTagsUpdateProgress').addClass('invisible');
			$('#myTags').removeClass('invisible');
		}, 10000);

		return;
	}

	$('#myTagsUpdateProgress').addClass('invisible');
	$('#myTags').removeClass('invisible');

	updateTagsList();
}

function reportOffensiveTagLinkClick(event, tagID)
{
	if (event && typeof(event.preventDefault) != 'undefined')
		event.preventDefault();

	$('#tagOffensive' + tagID + ' ~ a').css('margin-left', '16px'); // <img> size (14px) plus 2px margin
	$('#tagOffensive' + tagID).addClass('invisible');

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		tagHandlerUrl,

		data: {
			Action:	'ReportOffensive',
			IDs:	tagID,

			Tag:	tagID
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'Tag.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#myTagsUpdateProgress'
			});
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			alert("There was an error communicating with the server and the tag could\nnot be reported at this time, please try again later.");

			$('#tagOffensive' + tagID).removeClass('invisible');
		}
	});
}

function onReportOffensiveTagCallback(success, tagID)
{
	if (!success)
	{
		alert("There was an error communicating with the server and the tag could\nnot be reported at this time, please try again later.");
		$('#tagOffensive' + tagID).removeClass('invisible');

		return;
	}
}

function updateTagsList()
{
	updateRecentTagsList();
	updateUserTagsList();
}

function updateRecentTagsList()
{
	var recentTagsList = $('#recentTagsList');

	if (!recentTagsList || !recentTagsList.length)
		return;

	recentTagsList.text('Retrieving recent tags, please wait...');

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		tagHandlerUrl,

		data: {
			Action:		'GetList',
			ItemIDs:	$('#itemID').val(),
			IsBanned:	false,

			SortBy:			'CreatedDate',
			SortDirection:	'Descending',
			PageNumber:		1,
			PageSize:		5,

			Tag:		'<label>Recent</label><userid>' + myUserId + '</userid><searchtype>regular</searchtype>'
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'Tag.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#recentTagsList'
			});
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			recentTagsList.text('The recent tags list could not be retrieved at this time, please try again later.');
		}
	});
}

function updateUserTagsList()
{
	var userTagsList = $('#userTagsList');

	if (!userTagsList || !userTagsList.length)
		return;

	userTagsList.text('Retrieving user tags, please wait...');

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		tagHandlerUrl,

		data: {
			Action:		'GetList',
			UserIDs:	myUserId,
			ItemIDs:	$('#itemID').val(),
			IsBanned:	false,

			Tag:		'<label>My</label><userid>' + myUserId + '</userid><searchtype>regular</searchtype>'
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'Tag.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#userTagsList'
			});
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			userTagsList.text('The user tags list could not be retrieved at this time, please try again later.');
		}
	});
}

function renderMyTags()
{
	var myTags = $('#myTags');

	var cookie = $.cookie('blLogin');

	if (cookie)
	{
		if (!myTags || !myTags.length)
		{
			myTags = $('#tagList');

			if (myTags && myTags.length > 0)
				myTags.removeClass('invisible');
		}
		else
		{
			myTags.removeClass('invisible');
		}
	}
	else
	{
		if (!myTags || !myTags.length)
		{
			myTags = $('#tagList');

			if (myTags && myTags.length > 0)
				myTags.addClass('invisible');
		}
		else
			myTags.addClass('invisible');
	}
}

function tagSearchButtonClick(event)
{
	if (event && typeof(event.preventDefault) != 'undefined')
		event.preventDefault();

	if ($('#tagSearchFor').val().length < 1)
	{
		$('#itemList').text('Please enter a tag, or list of tags, before attempting to search.');
		return;
	}

	$('#itemList').text('Retrieving tagged items, please wait...');

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		itemHandlerUrl,

		data: {
			Action:			'GetList',
			Tags:			$('#tagSearchFor').val(),
			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 updateTagCloud()
{
	var tagCloud = $('#tagCloud');

	if (!tagCloud || !tagCloud.length)
		return;

	tagCloud.html('Retrieving tag cloud, please wait...');

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		tagHandlerUrl,

		data: {
			Action:		'GetCloud',
			Tag:		''
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'Tag.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#tagCloud'
			});
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			tagCloud.text('The tag cloud could not be retrieved at this time, please try again later.');
		}
	});
}

