﻿/// <reference path="/Scripts/jquery-1.4.4.min.js" />
/// <reference path="/Scripts/jquery-ui-1.8.7.custom.min.js" />
/// <reference path="/Scripts/MicrosoftAjax.js" />
/// <reference path="/Scripts/MicrosoftMvcAjax.js" />
/// <reference path="/Scripts/MicrosoftMvcValidation.js" />
/// <reference path="/Scripts/HouseTrevethan.Math.js" />

// Set the document ready function...
$(document).ready(function ()
{
	imagePreload();
});

// Image preload function
imagePreload = function()
{
	var i = 0;
	images = new Array();
	imageObjArray = new Array();
	images[0]="/Content/Images/mainNavActive_c.png";
	images[1]="/Content/Images/mainNavActive_l.png";
	images[2]="/Content/Images/mainNavActive_r.png";
	images[3]="/Content/Images/mainNavHover_c.png";
	images[4]="/Content/Images/mainNavHover_l.png";
	images[5]="/Content/Images/mainNavHover_r.png";
	images[6]="/Content/Images/mainNavNeutral_c.png";
	images[7]="/Content/Images/mainNavNeutral_l.png";
	images[8]="/Content/Images/mainNavNeutral_r.png";
	for(i=0; i<=8; i++)
	{
		imageObjArray[i] = new Image();
		imageObjArray[i].src=images[i];
	}
}

// Initializes the main login control.
function initMainLogin()
{
	$("#mainLogonButton").button({ text: true,
								label: "Login"
	});
	$("#RememberMe").val(true);
}

// Initializes the main logout control.
function initMainLogout()
{
	$("#mainLogoutLink").click(function()
	{
		$("#mainLogoutButton").click();
	});
}

// Fires during login attempt
function mainLogonBegin()
{
	// Disable controls.
	$("#mainLogonControl * input").attr("disabled", true);

	// Display feedback for loading.
	$("#mainLogonControl .logonFeedback").html("<span style=\"color: #00ff00; font-weight: bold;\">Logging in... Please wait...</span>");
}

// Fires on login success
function mainLogonSuccess(e)
{
	// Get the response
	var response = e.get_response().get_object();

	// If login was successful
	if (response.loggedIn == true)
	{
		// Erase the control container
		$("#mainLogonControlContainer").hide("drop", 200, function()
		{
			$("#mainLogonControlContainer").empty();

			// Reload the logged in control
			$("#mainLogonControlContainer").load("/Account/GenerateMainLoggedInControl");
			$("#mainLogonControlContainer").show("bounce", 400);
		});
		
	}
	else // Login failed
	{
		// Enable the controls.
		$("#mainLogonControl * input").attr("disabled", false);

		// Display feedback for error.
		$("#mainLogonControl .logonFeedback").html("<span style=\"color: #ff0000; font-weight: bold;\">" + response.errorMsg + "</span>");
	}
}

// Fires on login failure
function mainLogonFailure()
{
	// Enable controls.
	$("#mainLogonControl * input").attr("disabled", false);

	// Display feedback for error.
	$("#mainLogonControl .logonFeedback").html("<span style=\"color: #ff0000; font-weight: bold;\">There was a problem communicating with the server...</span>");
}

// Fires during logout attempt
function mainLogoutBegin()
{
	// Display feedback for loading.
	$("#mainLogonControl .logonFeedback").html("<span style=\"color: #00ff00; font-weight: bold;\">Logging out... Please wait...</span>");
}

// Fires on logout success
function mainLogoutSuccess(e)
{
	// Get the response
	var response = e.get_response().get_object();

	// If login was successful
	if (response.loggedIn == false)
	{
		// Erase the control container
		$("#mainLogonControlContainer").hide("drop", 200, function()
		{
			$("#mainLogonControlContainer").empty();

			// Reload the logged in control
			$("#mainLogonControlContainer").load("/Account/GenerateMainLogonControl");
			$("#mainLogonControlContainer").show("bounce", 400);
		});
	}
	else // Login failed
	{
		// Display feedback for error.
		$("#mainLogonControl .logonFeedback").html("<span style=\"color: #ff0000; font-weight: bold;\">There was a problem communicating with the server...</span>");
	}
}

// Fires on logout failure
function mainLogoutFailure()
{
	// Display feedback for error.
	$("#mainLogonControl .logonFeedback").html("<span style=\"color: #ff0000; font-weight: bold;\">There was a problem communicating with the server...</span>");
}
