$(document).ready(function()
{
	$(document).ready(function(){
		$("a[href^='http']").attr('target','_blank');
	});  

	$("#menu ul").each(function(i)
	{
		$(this).hover(function() {
			$(this).parent().find("a").slice(0,1).addClass("active");
		},function() {
			$(this).parent().find("a").slice(0,1).removeClass("active");
		});
	});


	Cufon.replace('.box h1, #copy h1, #client-form h2, #agent-form h2', {
		textShadow: '0px 1px 0px #004970',
		fontFamily: 'LocatorDisplay'
	});
	
	Cufon.replace('#sub-menu ul li a, #menu li ul li a', {
		textShadow: '0px 1px 0px #061B31',
		fontFamily: 'Locator'
	});
	
	Cufon.replace('#header #login.hasloggedin a', {
		textShadow: '0px 1px 0px #68B005',
		fontFamily: 'Bryant'
	});
	
	$("p.success").click(function() {
		$(this).slideUp();
	});
	
	$("a[href=\"#client-login\"]").click(function() {
		$("#overlay, #client-form").fadeIn();
		$('html, body').animate({scrollTop:0}, 'fast');
	});
	
	$("#overlay, #client-form a.close").click(function() {
		$("#overlay, #client-form").fadeOut();
	});
	
	
	$("a[href=\"#agent-login\"]").click(function() {
		$("#overlay, #agent-form").fadeIn();
		$('html, body').animate({scrollTop:0}, 'fast');
	});
	
	$("#overlay, #agent-form a.close").click(function() {
		$("#overlay, #agent-form").fadeOut('fast', function() {
			
			// reset the form, error and login messages when we close the lightbox.
			
			$('.loading').hide();
			$('.error').hide();
			
			$("#agent-form input[type=\"text\"], #client-form input[type=\"text\"]").attr('value', '');
			$("#agent-form input[type=\"password\"], #client-form input[type=\"password\"]").attr('value', '');
			
		});
	});
	
	// client login
	
	$("#client-form input.submit").click(function() {
		
		var username = $("#client-form input[type=\"text\"]").attr('value');
		var password = $("#client-form input[type=\"password\"]").attr('value');
		
		if(username && password != "")
		{
		
			if($("#client-form .error").is(":visible")) {
					
				$('#client-form .error').fadeOut('fast', function()
				{
					$('#client-form .loading').fadeIn('fast', function() {
						performAjaxLogin(username, password, "client");
						
					});
				});
			} else {
				$('#client-form .loading').fadeIn('fast', function() {
					performAjaxLogin(username, password, "client");
				});
			}
			
		} else {
			$('#client-form .loading').fadeOut('fast', function()
			{
				$('#client-form .error').fadeIn();
			});
		}
	});
	
	
	// agent login
	
	$("#agent-form input.submit").click(function() {
		
		var username = $("#agent-form input[type=\"text\"]").attr('value');
		var password = $("#agent-form input[type=\"password\"]").attr('value');
		
		if(username && password != "")
		{
		
			if($("#agent-form .error").is(":visible")) {
					
				$('#agent-form .error').fadeOut('fast', function()
				{
					$('#agent-form .loading').fadeIn('fast', function() {
						performAjaxLogin(username, password, "agent");
						
					});
				});
			} else {
				$('#agent-form .loading').fadeIn('fast', function() {
					performAjaxLogin(username, password, "agent");
				});
			}
			
		} else {
			$('#agent-form .loading').fadeOut('fast', function()
			{
				$('#agent-form .error').fadeIn();
			});
		}
	});
	
});

function performAjaxLogin(username, password, type) {
	$.ajax({
		type: "POST",
		url: "/controllers/ajax.php",
		data: 	"username=" + username + 
				"&password=" + password + "&type=" + type,
		success: function(data)
		{
			// if the data returned 1, it means that the user has been logged in.
			// else if it returns 0, the user has not supplied the correct username/password
			if(data == '1client') {
				$('#'+type+'-form .loading').html('Logging you in, please wait...');
				$('#'+type+'-form .loading').fadeIn('fast', function()
				{
					window.location = "/client";
				});
			} else if(data == '1agent') {
				$('#'+type+'-form .loading').html('Logging you in, please wait...');
				$('#'+type+'-form .loading').fadeIn('fast', function()
				{
					window.location = "/agent";
				});
			}
			
			else {
				$('#'+type+'-form .loading').fadeOut('fast', function()
				{
					$('#'+type+'-form .error').fadeIn();
				});
			}
			
		},
		error: function()
		{
			$('#'+type+'-form .loading').fadeOut('fast', function()
			{
				$('#'+type+'-form .error').fadeIn();
			});
		}
	});
}

