//settings for the colorbox
var loginColorboxSettings = {
	innerWidth: 730,
	inline: true,
	href: '#loginwrapper',
	scrolling: false,
	speed: 0,
	onComplete: function () {
		$('#tbUsername').focus();
	},
	onClosed: function () {
		//Hide all containers, then show client login container
		$('#loginwrapper').find('.container').hide();
		$('#clientlogincontainer').show();
		window.location.hash = '#nologin';
	}
};

var showLogin = window.location.hash.indexOf('clientlogin') >= 0;
var disableSlide = window.location.hash.indexOf('noslide') >= 0;
var showResetPassword = window.location.hash.indexOf('resetpassword') >= 0;

$(function () {
	if (!disableSlide) {
		$('#login-slide').cycle({
			timeout: 8000,  // milliseconds between slide transitions (0 to disable auto advance)
			fx: 'scrollDown', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			pause: true,   // true to enable "pause on hover"
			cleartypeNoBg: true // set to true to disable extra cleartype fixing (leave false to force background color setting on slides)
		});
	}

	//set up colorbox
	$('#btnShowLogin').colorbox(loginColorboxSettings);

	$('#btnSetPassword').click(function () {
		return ValidateSetPassword();
	});

	var loginContainers = $('#loginwrapper').find('.container');
	loginContainers.hide();

	//set up the login/forgotpassword/reset password dialog
	//always show the login box when this hidden field is set to true. Use case: user clicks reset password email then tries to login, should show the login dialog
	if (showLogin || $('#hShowLogin').val() == 'true') {
		$('#clientlogincontainer').show();
		$('#btnShowLogin').click();
	}
	else if (showResetPassword) {
		$('#resetpasswordcontainer').show();
		$('#btnShowLogin').click();
	}
	else { //if not showing the dialog box then make sure the login container is the one visible when user chooses to show
		$('#clientlogincontainer').show();
	}

	//click handler for forgot pass and back buttons
	$('.loginnav').click(function () {
		$('#clientlogincontainer').toggle();
		$('#forgotpasswordcontainer').toggle();
	});

	//click handler for CLient Login button
	$('#btnSignIn').live('click', function () {
		return ValidateLogin();
	});
	//handler enter for user/pass textboxes
	$('#tbUsername, #tbPassword').live('keypress', function (e) {
		var code = (e.keyCode ? e.keyCode : e.which);
		if (code == 13) { //Enter keycode
			if (ValidateLogin())
				eval($('#btnSignIn').attr('href')); //can't use .click() as the js is actually in the href
		}
	});

	//click handler for Forgot Password button
	$('#btnForgotPassword').live('click', function () {
		//validate

		//hide success/error messages
		$('#forgotSuccess').hide();
		$('#forgotError').hide();
		//show ajax progress indicator
		$('#forgotAjaxMsg').show();
		//actual ajax call
		$.ajax({
			type: "POST",
			url: "/Info/InfoAjaxMethods.asmx/ForgotPassword",
			contentType: "application/json; charset=utf-8",
			data: '{resetstring: "' + $('#tbForgotPassword').val() + '"}',
			dataType: "json",
			success: function (data) {
				//show success msg
				$('#forgotSuccess').text(data.d).show();
			},
			error: function (xhr, status) {
				var err = jQuery.parseJSON(xhr.responseText);
				if (err.ExceptionType == 'System.Exception')
					$('#forgotError').text(err.Message).show();
				else
					$('#forgotError').text('There was and error trying to reset your password. Please refresh the page and try again.').show();
			},
			complete: function () {
				//hide loading msg
				$('#forgotAjaxMsg').hide();
				//resize the colorbox
				$.colorbox.resize(loginColorboxSettings);
			}
		});
	});
});

//make sure the username and password have been entered
function ValidateLogin() {
	var usr = $('#tbUsername'); var pass = $('#tbPassword');
	if (usr.val() == '') {
		$('#pError').text('You must enter your username to login.').show();
		$.colorbox.resize(loginColorboxSettings);
		usr.focus();
		return false;
	}
	else if (pass.val() == '') {
		$('#pError').text('You must enter your password to login.').show();
		$.colorbox.resize(loginColorboxSettings);
		pass.focus();
		return false;
	}
	return true;
}

function ValidateSetPassword() {
	var pswrd = $('#tbPasswordReset');
	var pswrdConfirm = $('#tbConfirmPasswordReset');
	if (pswrd.val() != pswrdConfirm.val()) {
		$('#pResetPasswordError').text('Passwords don\'t match').show();
		$.colorbox.resize(loginColorboxSettings);
		return false;
	}

	if (pswrd.val().length < 6) {
		$('#pResetPasswordError').text('Password must be 6 or more characters.').show();
		$.colorbox.resize(loginColorboxSettings);
		return false;
	}
	return true;
}
