﻿var userPageSize   = 10;
var userPageNumber = 1;
var userNumResults = 0;

if (!gotEntryPoint)
{
	gotEntryPoint = true;

	$(document).ready(function() {
		initMain(true);
	});
}

function loginLinkClick(event, args)
{
	if (event && typeof(event.preventDefault) != 'undefined')
		event.preventDefault();

	if (typeof(args) == 'undefined')
	{
		args = {goBackOnSuccess: false};
	}

	$('#newLoginBanner').addClass('invisible');
	$('#authLoginBanner').addClass('invisible');
	$('#loginOrRegisterBanner').addClass('invisible');
	$('#forgottenPasswordBanner').addClass('invisible');

	$('#loginResult').text('Logging in, please wait...');
	$('#loginResult').removeClass('invisible');

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		userHandlerUrl,

		data: {
			Action:		'Login',
			User:		$('#userName').val(),
			Password:	$('#password').val(),

			Tag:		'<flags><gobackonsuccess value="' + args.goBackOnSuccess + '" /></flags>'
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'User.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#loginResult'
			});
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			$('#loginResult').text('The system could not log you in at this time, please try again later.');

			setTimeout(function() {
				$('#loginResult').addClass('invisible');
				$('#newLoginBanner').removeClass('invisible');
				$('#loginOrRegisterBanner').removeClass('invisible');
				$('#forgottenPasswordBanner').removeClass('invisible');
			}, 10000);
		}
	});

	$('#password').val('');
}

function logoutLinkClick(event)
{
	$.cookie('blLogin', null, {path: '/'});

	myUserId      = -1;
	myPermissions = 0;
	myCommentId   = -1;

	if (event && typeof(event.preventDefault) != 'undefined')
		event.preventDefault();

	$('#newLoginBanner').addClass('invisible');
	$('#authLoginBanner').addClass('invisible');
	$('#loginOrRegisterBanner').addClass('invisible');
	$('#forgottenPasswordBanner').addClass('invisible');

	$('#welcomeOnlyBanner').addClass('invisible');
	$('#loginOnlyBanner').removeClass('invisible');

	$('#loginResult').text('Logging out, please wait...');
	$('#loginResult').removeClass('invisible');

	$('#loginLink').text('Login');
	$('#loginResult').addClass('invisible');
	$('#newLoginBanner').removeClass('invisible');
	$('#loginOrRegisterBanner').removeClass('invisible');
	$('#forgottenPasswordBanner').removeClass('invisible');

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		userHandlerUrl,

		data:		{Action: 'Logout'},

		dataType:	'text',

		complete:	function()
		{
			if (typeof(renderMyTags)           != 'undefined')	renderMyTags();
			if (typeof(renderMyComments)       != 'undefined')	renderMyComments();
			if (typeof(renderFavouriteLink)    != 'undefined')	renderFavouriteLink();
			if (typeof(renderMyGalleriesLinks) != 'undefined')	renderMyGalleriesLinks();
			if (typeof(renderWeb2Panels)       != 'undefined')	renderWeb2Panels();

			if (typeof(updateTagsList)	       != 'undefined')	updateTagsList();
			if (typeof(updateCommentsList)     != 'undefined')	updateCommentsList();
			if (typeof(updateFavouriteLink)    != 'undefined')	updateFavouriteLink();
			if (typeof(updateMyGalleriesLinks) != 'undefined')	updateMyGalleriesLinks();
		}
	});
}

function onLoginCallback(success, id, firstName, lastName, permissions, goBackOnSuccess)
{
	if (success)
	{
		$.cookie('blLogin', id + ':' + permissions + ':' + firstName + ':' + lastName, {path: '/'});
		$('.userFullName').text(firstName + ' ' + lastName);
		$('#loginLink').text('Logout');
		$('#loginOnlyBanner').addClass('invisible');
		$('#welcomeOnlyBanner').removeClass('invisible');

		myUserId      = id;
		myPermissions = permissions;
		myCommentId   = -1;

		if (goBackOnSuccess)
			window.history.go(-1);

		if (typeof(updateTagsList)	       != 'undefined')	updateTagsList();
		if (typeof(updateCommentsList)     != 'undefined')	updateCommentsList();
		if (typeof(updateFavouriteLink)    != 'undefined')	updateFavouriteLink();
		if (typeof(updateMyGalleriesLinks) != 'undefined')	updateMyGalleriesLinks();
	}
	else
		$.cookie('blLogin', null);

	if (typeof(renderMyTags)           != 'undefined')	renderMyTags();
	if (typeof(renderMyComments)       != 'undefined')	renderMyComments();
	if (typeof(renderFavouriteLink)    != 'undefined')	renderFavouriteLink();
	if (typeof(renderMyGalleriesLinks) != 'undefined')	renderMyGalleriesLinks();
	if (typeof(renderWeb2Panels)       != 'undefined')	renderWeb2Panels();

	setTimeout(function() {
		$('#loginResult').addClass('invisible');

		var cookie = $.cookie('blLogin');

		if (cookie)
		{
			$('#authLoginBanner').removeClass('invisible');
			$('#forgottenPasswordBanner').addClass('invisible');
		}
		else
		{
			$('#newLoginBanner').removeClass('invisible');
			$('#forgottenPasswordBanner').removeClass('invisible');
		}

		$('#loginOrRegisterBanner').removeClass('invisible');
	}, 5000);
}

function onIdleLogout()
{
	logoutLinkClick();
}

function userSearchButtonClick(event, pageNumber)
{
	if (event && typeof(event.preventDefault) != 'undefined')
		event.preventDefault();

	if (!(myPermissions & permGetAnyUserDetails))
	{
		$('#userList').text('You are not authorised to retrieve user details.');
		return;
	}

	$('#userList').text('Searching, please wait...');

	var request = getSearchCriteria();

	request['Action']     = 'GetList';
	request['Tag']        = '';
	request['PageSize']   = userPageSize;
	request['PageNumber'] = userPageNumber = pageNumber;

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		userHandlerUrl,

		data:		request,

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'User.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#userList'
			});
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			$('#userList').text('The system could not retrieve any results at this time, please try again later.');
		}
	});
}

function onUserSearchCallback(numResults)
{
	userNumResults = numResults;
}

function saveUserDetailsClick(event, userId)
{
	if (event && typeof(event.preventDefault) != 'undefined')
		event.preventDefault();

	var userDetailsNode = $('#user' + userId + 'Details');

	var request = new Object();

	request['Action'] = 'Save';
	request['ID']     = userId;

	$(':input', userDetailsNode).each(function(index) {
		var node = $(this);

		request[node.attr('class')] = node.val();
	});

	$('.updateProgress', userDetailsNode).text('Saving user details, please wait...');
	$('.updateProgress', userDetailsNode).removeClass('invisible');

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		userHandlerUrl,

		data:		request,

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'User.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#user' + userId + 'Details .updateProgress'
			});

			$('.updateProgress', userDetailsNode).addClass('invisible');
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			$('.updateProgress', userDetailsNode).text('User details could not be saved at this time, please try again later.');
			setTimeout(function() {
				$('.updateProgress', userDetailsNode).addClass('invisible');
			}, 10000);
		}
	});
}

