$(document).ready(function()
{
	/* REGISTRATION */
	$('#register-btn').click(function()
	{
		showRegistrationDialog();
		return false;
	});
	
	$('#register-submit-btn').live('click', function()
	{
		submitRegsitration();
		return false;
	});
	
	$('#location-tb').live('keyup', function() {
		suggestRegLocation($(this).val());
	});
	
	$('#suggestionsList_locations li').live('click', function()
	{
		fillRegLocation($(this).html());
		return false;
	});
	
	/* LOGIN */
	$('#login-btn').live('click', function()
	{
		showLoginDialog();
		return false;
	});
	
	$('#login-submit-btn').live('click', function()
	{
		submitLogin();
		return false;
	});
	
	/* FORGOT PASSWORD */
	$('#forgot-password-btn').live('click', function()
	{
		displayForgotPassword();
		return false;
	});
	
	$('#forgotpw-submit-btn').live('click', function()
	{
		sendForgotPassword();
		return false;
	});
	
	$('#resetnewpw-submit-btn').click(function()
	{
		validateNewPassword();
		return false;
	});
});

/* REGISTRATION */
function showRegistrationDialog()
{
	if( $('#register-dialog').length == 0 )
	{
		$('body').append('<div id="register-dialog" style="display: none; width: 500px; height: 400px;"></div>');
		
		$('#register-dialog').dialog({
			autoOpen: false,
			bgiframe: true,
			resizable: false,
			autoResize: true,
			width: 500,
			height: 475,
			modal: true,
			overlay: {
				backgroundColor: '#000000',
				opacity: 1
			},
			title: 'Register for Your Adventure Page!',
			show: 'blind',
			hide: 'blind',
			close: function(){ $('#register-dialog').remove(); }
		});
	}
	
	$.ajax({
		type: "GET",
		url: '/registration/register.php',
		data: '',
		success: function(msg)
		{
			$('#register-dialog').html(msg);
			$('#register-dialog').dialog('open');
		}
	});
	return false;
}

function submitRegsitration()
{
	$.ajax({
		type: "POST",
		url: '/registration/register.php',
		data: $('#registration-form').serialize(),
		success: function(msg)
		{
			$('#register-dialog').html(msg);
		}
	});
}

// location suggest
function suggestRegLocation(inputString)
{
	if(inputString.length == 0)
	{
		$('#suggestions-location').fadeOut();
	}
	else
	{
		$.ajax({
			type: "POST",
			url: '/autoSuggestCityLocation.php',
			data: 'queryString=' + inputString,
			success: function(msg)
			{
				$('#suggestions-location').fadeIn();
				$('#suggestionsList_locations').html(msg);
			}
		});
	}
}

function fillRegLocation(thisValue)
{
	$('#location-tb').val(thisValue);
	setTimeout("$('#suggestions-location').fadeOut();", 200);
}

function showLoginForm(the_div)
{
	$(the_div).after('<div id="login-dialog"></div>');
	$.ajax({
		type: "GET",
		url: '/registration/login.php',
		data: '',
		success: function(msg)
		{
			$('#login-dialog').html(msg);
		}
	});
}


/* LOGIN */
function showLoginDialog()
{
	if( $('#login-dialog').length == 0 )
	{
		$('body').append('<div id="login-dialog" style="display: none; width: 500px; height: 400px;"></div>');
		
		$('#login-dialog').dialog({
			autoOpen: false,
			bgiframe: true,
			resizable: false,
			autoResize: true,
			width: 500,
			height: 350,
			modal: true,
			overlay: {
				backgroundColor: '#000000',
				opacity: 1
			},
			title: 'Login to Your Adventure Page!',
			show: 'blind',
			hide: 'blind',
			close: function(){ $('#login-dialog').remove(); }
		});
	}
	
	$.ajax({
		type: "GET",
		url: '/registration/login.php',
		data: '',
		success: function(msg)
		{
			$('#login-dialog').html(msg);
			$('#login-dialog').dialog('open');
		}
	});
	return false;
}

function submitLogin()
{
	$.ajax({
		type: "POST",
		url: '/registration/login.php?r=' + Math.random(),
		data: $('#login-form').serialize(),
		success: function(msg)
		{
			$('#login-dialog').html(msg);
		}
	});
}


/* FORGOT PASSWORD */
function displayForgotPassword()
{
	$.ajax({
		type: "GET",
		url: '/registration/forgot_password.php',
		data: '',
		success: function(msg)
		{
			$('#login-dialog').html(msg);
		}
	});
}

function sendForgotPassword()
{
	$.ajax({
		type: "POST",
		url: '/registration/forgot_password.php',
		data: $('#forgotpw-form').serialize(),
		success: function(msg)
		{
			$('#login-dialog').html(msg);
		}
	});
}

function validateNewPassword()
{
	var p1 = $('#password').val();
	var p2 = $('#password2').val();
	
	if(p1 != p2)
	{
		alert('Your passwords do not match!');
	}
	else
	{
		$.ajax({
			type: "POST",
			url: '/registration/reset_password.php',
			data: $('#resetpw-form').serialize(),
			success: function(msg)
			{
				$('#body-content').html('Your password was reset!<br /><br /><a href="#" id="login-btn">Click here to login</a>');
			}
		});
	}
}
