﻿gotEntryPoint = true;

$(document).ready(function() {
	initMain(true);

	$('#addTagButton').click(addTagButtonClick);
	$('#addCommentButton').click(addCommentButtonClick);
	$('#removeCommentButton').click(removeCommentButtonClick);

	$('#newTagValue').focus(function(event) {
		this.select();
	});

	$('.offensiveItemLink').click(reportOffensiveItemLinkClick);

	$('#reportOffensiveItemCancelButton').click(function(event) {
		$('#offensiveItemNotes').hide();
	});

	$('#reportOffensiveItemSubmitButton').click(function(event) {
		var cookie = $.cookie('blLogin');

		if (!cookie)
			reportOffensiveItem($('#offensiveItemNotes textarea').val (), $('#reportOffensiveItemName input').val(), $('#reportOffensiveItemEmail input').val());
		else
			reportOffensiveItem($('#offensiveItemNotes textarea').val());
	});

	$('#reportOffensiveCommentCancelButton').click(function(event) {
		$('#offensiveCommentNotes').hide();
	});

	$('#reportOffensiveCommentSubmitButton').click(function(event) {
		var cookie = $.cookie('blLogin');

		if (!cookie)
			reportOffensiveComment($('#offensiveCommentNotes textarea').val(), $('#reportOffensiveCommentName input').val(), $('#reportOffensiveCommentEmail input').val());
		else
			reportOffensiveComment($('#offensiveCommentNotes textarea').val());
	});

	var cookie = $.cookie('blLogin');

	if (!cookie)
	{
		$('.emailItemLink').addClass('invisible');
	}

	$('.emailItemLink').click(emailItemLinkClick);

	$('#emailItemCancelButton').click(function(event) {
		$('#emailItemNotes').hide();
	});

	$('#emailItemSubmitButton').click(function(event) {
		var cookie = $.cookie('blLogin');

		if (!cookie)
			return;

		emailItem($('#emailItemNotes textarea').val(), $('#emailItemToEmail input').val());
	});

	$('#seeMyGalleriesLink').click(function(event) {
		if (event && typeof(event.preventDefault) != 'undefined')
			event.preventDefault();

		document.location.href = viewMyGalleriesUrl;
	});

	renderMyTags();
	renderMyComments();
	renderFavouriteLink();
	renderMyGalleriesLinks();
	renderWeb2Panels();

	updateTagsList();
	updateCommentsList();
	updateFavouriteLink();
	updateMyGalleriesLinks();
});

function reportOffensiveItemLinkClick(event)
{
	var offensiveDiv = $('#offensiveItemNotes');

	var x = event.pageX, y = event.pageY, $window = $(window);

	var maxX = $window.width(), maxY = $window.height();

	if ((event.clientX + offensiveDiv.width()) >= maxX)
		x -= offensiveDiv.width();

	if ((event.clientY + offensiveDiv.height()) >= maxY)
		y -= offensiveDiv.height();

	offensiveDiv.css('top',  y + 'px');
	offensiveDiv.css('left', x + 'px');

	var cookie = $.cookie('blLogin');

	if (cookie)
	{
		$('#reportOffensiveItemName').addClass('invisible');
		$('#reportOffensiveItemEmail').addClass('invisible');
	}
	else
	{
		$('#reportOffensiveItemName').removeClass('invisible');
		$('#reportOffensiveItemEmail').removeClass('invisible');
	}

	offensiveDiv.show();
}

function reportOffensiveItem(comments, name, email)
{
	$('#reportOffensiveItemButtons').addClass('invisible');
	$('#reportOffensiveItemStatus').text('Reporting your concern, please wait...');

	if (typeof(name) != 'undefined')
	{
		comments = 'From: ' + name + ' <' + email + ">\n\n" + comments;
	}

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		itemHandlerUrl,

		data: {
			Action:		'ReportOffensive',
			IDs:		$('#itemID').val(),
			Comments:	comments,

			Tag:		''
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'Item.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#reportOffensiveItemStatus'
			});

			$('#reportOffensiveItemButtons').removeClass('invisible');
			$('#reportOffensiveItemStatus').addClass('invisible');
			$('#offensiveItemNotes').hide();

			$('#offensiveItemNotes textarea').val('');
			$('#reportOffensiveItemName input').val('');
			$('#reportOffensiveItemEmail input').val('');
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			$('#reportOffensiveItemStatus').text('The item could not be reported at this time, please try again later...');

			setTimeout(function() {
				$('#reportOffensiveItemStatus').addClass('invisible');
				$('#reportOffensiveItemButtons').removeClass('invisible');
			}, 10000);
		}
	});
}

function emailItemLinkClick(event)
{
	var emailDiv = $('#emailItemNotes');

	var x = event.pageX, y = event.pageY, $window = $(window);

	var maxX = $window.width(), maxY = $window.height();

	if ((event.clientX + emailDiv.width()) >= maxX)
		x -= emailDiv.width();

	if ((event.clientY + emailDiv.height()) >= maxY)
		y -= emailDiv.height();

	emailDiv.css('top',  y + 'px');
	emailDiv.css('left', x + 'px');

	emailDiv.show();
}

function emailItem(comments, emailTo)
{
	$('#emailItemButtons').addClass('invisible');
	$('#emailItemStatus').text('Emailing item to your friend, please wait...');

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		itemHandlerUrl,

		data: {
			Action:		'EmailToFriend',
			IDs:		$('#itemID').val(),
			Comments:	comments,
			EmailTo:	emailTo,

			Tag:		''
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'Item.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#emailItemStatus'
			});

			$('#emailItemButtons').removeClass('invisible');
			$('#emailItemStatus').addClass('invisible');
			$('#emailItemNotes').hide();

			$('#emailItemNotes textarea').val('');
			$('#emailItemToEmail input').val('');
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			$('#emailItemStatus').text('The item could not be emailed at this time, please try again later...');

			setTimeout(function() {
				$('#emailItemStatus').addClass('invisible');
				$('#emailItemButtons').removeClass('invisible');
			}, 10000);
		}
	});
}

