(function($) {

	// Global functions that need to run on every page to setup various interface elements
	$(function() {
	
		/* Setup the main navigation
		----------------------------------------------------*/
		(function(){
		
			var subNavTimer;
			
			var globalNav = $("#navGlobal");
		
			$("#navGlobal > li").bind("mouseover", function() {
			
				$(this).addClass("over");			
				
			}).bind("mouseout", function() {
			
				$(this).removeClass("over");
				
			});
			
			globalNav.find("ul.subNav li").bind("mouseover", function() {
				$(this).addClass("over");
				$(this).siblings().removeClass("over");
				$(this).find(".promo").addShim();
				clearTimeout(subNavTimer);
				
				//remove any shims on other promo items (not the current one)
				$(this).siblings().find(".promo").removeShim();
				
			}).bind("mouseout", function() {
				var subNavItem = $(this);
				subNavTimer = setTimeout(function(){
					subNavItem.removeClass("over");
					subNavItem.find(".promo").removeShim();
				}, 100);
			});
			
			//Add focus handling to aid in keyboard navigation
			var navTimer;
			
			globalNav.find("li a").bind("focus", function() {
			
				//If it's a sub-menu link, make sure the sub-menu stays open and the main nav item still shows as "over"
				if ($(this).parents(".subNav").length > 0) {
					$(this).parent().trigger("mouseover");
					clearTimeout(navTimer);
				}

				$(this).parent().trigger("mouseover");			
				
			}).bind("blur", function() {
			
				var navItem = $(this);
				//Set a slight delay before firing the mouseout event for this nav item (which will remove the "over" class and cause the sub menu to be hidden again). This delay will be cancelled out if the next item to get focus is still inside the currently visible sub menu.
				navTimer = setTimeout(function() {
					navItem.parent().trigger("mouseout");
				}, 50);
				
			});			
	
		})();
		
		
		/* Setup Chat Offline Tooltip for Global Header
		----------------------------------------------------*/
		(function() {

			// Configure tooltip
			$("#chatOffline").setupComponents();
			$("#chatTooltip").tooltip({belowAnchor: true});
			setTimeout(function() {
					$("#chatOffline").hide();
				}, 1);
			
			// Holiday hours
			$(".chatHolidayHours").hide();
			$("#showChatHolidayHours").live("click", function(event) {
				event.preventDefault();
				$(".chatHolidayHours").toggle("fast");												  
			});
			
		})();
		
		/* Setup the Search form in the nav
		----------------------------------------------------*/
		(function() {
			
			var searchLabel = $("#frmSearch label");
			var searchField = $("#frmSearch #txtSearch");
			
			searchLabel.hide();
			searchField.val(searchLabel.text());
	
			//Event handlers
			searchField.bind("focus", function() {
				if ($(this).val() == searchLabel.text()) {
					//Clear the text field
					searchField.val("");
				}
			});
			searchField.bind("blur", function() {
				if ($.trim($(this).val()) == "") {
					searchField.val(searchLabel.text());
				}
			});
			
		})();
		
	
		
		/* Hide the Site Map and setup the events for it
		----------------------------------------------------*/
		(function() {
			var siteMap = $("#siteMap");

			siteMap.hide();
			
			//setup event handling for the sitemap link
			$("#siteMapLink").bind("click", function() {
			
				var siteMapLink = $(this);
				
				if(siteMap.is(".open")) {
					siteMap.slideUp(1000, function() {
		
						siteMapLink.removeClass("open");
					
					}).removeClass("open");
				}
				else {
					siteMap.slideDown(50, function() {
					
						//scroll to the siteMapLink (which will make sure that the sitemap is in view)
						siteMapLink.scrollTo({
							speed: 1000,
							afterScroll: function() {
								siteMapLink.addClass("open");
							}
						});
					
					}).addClass("open");
				}
				
				return false;
			
			});
		})();
		
		
		/* Convert low-res buttons to high-res buttons
		----------------------------------------------------*/
		$(".sprint input[type='submit'], .sprint input[type='button'], .sprint a.button1, .sprint a.button2, .sprint a.button3, .sprint a.button4, .sprint a.flyout").createHighResButtons();


		/* Setup all default components with default values except for buttons
		----------------------------------------------------*/
		$().setupComponents({
			modals: true,
			buttons: false
		});

		
		
		/* Adds Print button to container
		----------------------------------------------------*/
		/* $(".printButtonContainer").each(function() {
			$(this).append("<div class=\"button4AltIcon\"><a href=\"#\" onclick=\"window.print(); return false;\" id=\"btnPrint\" class=\"functionPrint\"><span>Print</span></a></div>");
		}); */
		$(".printButtonContainer").show();
		
		

		/* Default Error Messaging Behaviour
		----------------------------------------------------*/
		$("ul.formErrors li a").live("click", function() {
		
			//Find the anchor target that this error links to
			var anchorTarget = $(this).attr("href");
			
			//strip off everything before the "#"
			anchorTarget = anchorTarget.substr(anchorTarget.indexOf("#"));
			
			$(anchorTarget).scrollTo({speed: "slow"});
			
			//Set focus to the field that the label represents and select any text that may be inside
			$("#"+$(anchorTarget).attr("for")).trigger("focus").trigger("select");
			
			return false;
		
		});
		
		
		
		/* Sign-In
		-------------------------------------------------*/
		(function() {
		
			var submitLoginForm = false;
			var frmUserLogin = $("#frmUserLogin");
		
			//Make sure form submission goes through the Sign In button.
			frmUserLogin.bind("submit", function(event) {

				if (!submitLoginForm) {
					event.preventDefault();
					
					frmUserLogin.find("#btnLoginSubmit").trigger("click");
				}
				
			});
			
			frmUserLogin.find("#btnLoginSubmit").unbind("click").bind("click", function(event) {
				event.preventDefault();
				
				//Submit the form using an AJAX request.
				var formPath = frmUserLogin.attr("action");
				
				$.ajax({
					
					data: frmUserLogin.serialize(),
					type: "POST",
					url: formPath,
					dataType: "json",
					
					success: function(data) {
						
						//Remove any previous error messages
						frmUserLogin.find("ul.formErrors").remove();
						frmUserLogin.find(".error").removeClass("error");

						if (data.validated) {										
							//Submit the registration form.
							window.location = data.nextPage;
						}
						else if (data.accountLocked) {
							//Account locked, show account locked message.
							var errorList = $("<ul class=\"formErrors\"><li>"+data.accountLockedErrorHeader+"</li></ul><p>"+data.accountLockedErrorMessage+"</p>");
							
							//Empty the form, change it's action, put in the new fields
							frmUserLogin.attr("action", data.accountResetPath);
							frmUserLogin.find("fieldset").html(errorList);
							
							//Reset checkbox
							var resetCheck = $("<div><label for=\"chkLoginAccountReset\" id=\"lblLoginAccountReset\"><input type=\"checkbox\" class=\"check\" name=\"chkLoginAccountReset\" id=\"chkLoginAccountReset\" /> "+data.accountLockedResetLabel+"</label></div>");
							
							resetCheck.appendTo(frmUserLogin.find("fieldset"));							
							
							//Put in the new submit button.
							var newSubmitButton = $("<input type=\"submit\" class=\"button1\" id=\"btnLoginSubmit\" value=\""+data.accountResetButtonLabel+"\" />");
							
							newSubmitButton.appendTo(frmUserLogin.find("fieldset"));
							
							newSubmitButton.wrap("<div class=\"buttons\"></div>");
							
							newSubmitButton.createHighResButtons();
							
							$("#btnLoginSubmit").unbind("click").bind("click", function(event) {
								
								event.preventDefault();
								
								//if the reset account checkbox is checked, submit the form. If not, do nothing.
								if ($("#chkLoginAccountReset").is(":checked")) {
									submitLoginForm = true;
									
									frmUserLogin.eq(0).submit();
								}
								
							});
						}
						else {
							
							//Build the error message list
							var errorList = $("<ul class=\"formErrors\"></ul>");
							
							for (i = 0; i < data.errors.length; i++) {
								var errorItem = $("<li><a href=\"#"+data.errors[i].field+"\">"+data.errors[i].errorMessage+"</a></li>");
								errorItem.appendTo(errorList);
								
								$("#"+data.errors[i].field).addClass("error");
							}
							
							errorList.find("a").bind("click", function() {
								//Find the anchor target that this error links to
								var anchorTarget = $(this).attr("href");
								
								//strip off everything before the "#"
								anchorTarget = anchorTarget.substr(anchorTarget.indexOf("#"));
								
								//Set focus to the field that the label represents and select any text that may be inside
								$(anchorTarget).trigger("focus").trigger("select");
								
								return false;
							});
							
							//Show the error message list.
							frmUserLogin.find("fieldset").prepend(errorList);
						}
						
					},
					
					error: function(event) {
						alert("error communicating with server\n\nStatus: "+event.status);
					}
				
				});
				
			});
		
			$("#userLoginContent").addRoundedCorners();
				
			var userLogin = $("#userLogin");
			var signInLink = $("#signInLink");
			var signInLinkOriginalWidth = signInLink.width();
		
			userLogin.disclosure({
				startClosed:     true,
				titleClickable:  false,
				speed:           "fast",
				openedText:      Sprint.content.userLoginDisclosure.openedText[Sprint.currentLanguage],
				closedText:      Sprint.content.userLoginDisclosure.closedText[Sprint.currentLanguage],
				
				openCallback:    function() {
					userLogin.addShim(); //Add an iframe shim (for IE6)
					
					function removeShim() {
						userLogin.removeShim(); //Remove iframe shim (for IE6)
						
						//Once the shim has been removed, get rid of the click function
						userLogin.find(".disclosureToggle").unbind("click", removeShim);
					}
					
					userLogin.find(".disclosureToggle").bind("click", removeShim);
				},
				
				closeCallback:   function() {
					var navButton = signInLink.parent().parent();
					userLogin.hide();
					navButton.removeClass("expanded");
					signInLink.animate({
						width: signInLinkOriginalWidth
					}, function() {
						/*signInLink.fadeIn("fast");*/
					});
				}
				
			}).hide();
		
			signInLink.bind("click", function() {
											  
									  
											  
				//$(this).parent().width($(this).outerWidth());
				
				var navButton = signInLink.parent().parent();
	
				signInLink.animate({width: "226px"}, function() {
					navButton.addClass("expanded");
					userLogin.show();
					userLogin.find(".disclosureToggle").trigger("click");
				});
				
				return false;
			});
			
			$("#signInCookied").bind("click", function(event) {
				event.preventDefault();
				// Can we get width of hidden element (dropdown)
				//var navButton = signInLink.parent().parent();
				
				userLogin.show();
				userLogin.find(".disclosureToggle").trigger("click");						 
			});
		
		})();



		/* Signed In User Nav
		-------------------------------------------------*/
		(function() {
		
			// TODO: Do we really need navUser object?
			var userLoggedIn = $("#userLoggedIn");
			var signOutLink = $("#signOutLink");
			var navUser = $("#navUser");
			var loggedInUserLink = navUser.find("li.user a");
		
			$("#userLoggedInContent").addRoundedCorners();
			userLoggedIn.hide();
			
  			
			// 
			loggedInUserLink.bind("click", function(event) {
				
				event.preventDefault();
				
				var navButton = loggedInUserLink.parent().parent();
				var hasExpandedClass = navButton.hasClass("expanded");
				
				// expanded class should always be added first and removed last
				if (!hasExpandedClass) {
					navButton.addClass("expanded");
				}
				userLoggedIn.slideToggle("fast", function() {
					if (hasExpandedClass) {
						navButton.removeClass("expanded");	
					}
				});
			});
			
		
		})();
		
		
		/* MyTools Tabs in Global Fred
		-------------------------------------------------------*/
		$(function() {
			
			// Initalize
			var tabLinks = $("#navUser ul.myToolsTabs li a");
			
			
			// Hide content for all tabs except the selected one
			tabLinks.each(function() {
				
				var tabLink = $(this);
								   
				if (!tabLink.parent().hasClass("selected")) {
					$(tabLink.attr("href")).hide();
				}
			});
			
				   
			// Add click event handler to all tabs
			tabLinks.bind("click", function(event) {
				
				event.preventDefault();
				
				var tabLink = $(this);
				var tab = tabLink.parent();
				
				// Don't process a tab that's already selected
				if (tab.hasClass("selected")) {
					return false;	
				}
				
				// Remove selected class from other tabs
				tab.parent().find("li.selected").removeClass("selected");
				
				// Add class to the selected tab
				tab.addClass("selected");
				
				// Hide content for other tabs
				$("#navUser #myTools .myToolsTabContent").hide();
				
				// Show content for selected tab
				$(tabLink.attr("href")).show();
			
			});
		});
		
		
		/* View Contract Modal 
		-------------------------------------------------------*/
		$(function() {
			
			var onClick = function(event) {
				
				event.preventDefault();
				
				var viewContractModal = $("#viewYourContractModal");
						
						viewContractModal.openModal({
							width: 400,
							openCallback: function() {
								// Make sure sIFR is properly applied
								viewContractModal.setupComponents({buttons:false});
							}
						});
			};
			
			// Display modal dialog
			$("a.viewYourContractModal").bind("click", onClick);
			
		});
		
		/* Change Location Modal 
		-------------------------------------------------------*/
		$(function() {
			
			// Display modal dialog
			$("a.showModalChangeLocation").bind("click", function(event) {
				
				event.preventDefault();
				
				var locationModal = $("#changeLocationModal");
				
				locationModal.openModal({
					width:275,
					openCallback: function() {
						// Make sure sIFR is properly applied
						locationModal.setupComponents({buttons:false});
					}
				});
			});
			
			
			// Change location submit handling
			$("#frmChangeLocation").bind("submit", function(event) {
															
				event.preventDefault();
				$("#btnChangeLocation").trigger("click", event);
			
			});
			
			
			$("#btnChangeLocation").unbind("click").bind("click", function(event) {
				event.preventDefault();
				
				// When we change location we need to submit the new location to the store locator
				$("#txtFindStoreAddress").val($("#txtNewLocation").val());
				$("#btnFindStore").trigger("click");
				
				// Once submitted we can close the modal
				$("#changeLocationModal").closeModal();
				
			});
		});


		/* Find a Store Form (in the Contact Us module in the side bar)
		-------------------------------------------------*/
		(function() {
		
			var frmFindStore = $("#frmFindStore");
			
			if (frmFindStore.length > 0) {
			
				var locationDisplay = $("#locationDisplay");
				
				locationDisplay.hide();
				
				locationDisplay.find("ul.storeLocatorLink li a").bind("click", function(event) {
				
					event.preventDefault();
					
					if ($(this).is(".changeLocation")) {
						locationDisplay.hide();
						frmFindStore.parent().show();
						
						frmFindStore.find("#btnFindStore").removeClass("disabled");
						
						frmFindStore.find("#txtFindStoreAddress").focus().select();
					}
					else if ($(this).is(".seeAll")) {
						
						/*
						$("#frmViewAllStores").find("input[name='r']").val(frmFindStore.find("#hidFindStoreRadius").val());
						$("#frmViewAllStores").find("input[name='addr']").val($.trim(frmFindStore.find("#txtFindStoreAddress").val()));						
						
						$("#frmViewAllStores").trigger("submit");						
						*/
					}
				
				});
				
				frmFindStore.bind("submit", function(event) {
				
					event.preventDefault();
					
					frmFindStore.find("#btnFindStore").trigger("click");
				
				});
			
				function findStores(event, supressErrors){
					if(event){	event.preventDefault();	}
					
					if( supressErrors==null ){ supressErrors=false;}
					
					if ($(this).is(".disabled")) {
						return false;
					}
					
					var searchString = $.trim(frmFindStore.find("#txtFindStoreAddress").val());
					
					$(this).addClass("disabled");
				
					
					var errorFunction = Sprint.fn.ajaxError;
					if(supressErrors){
						errorFunction = function(){return true};
					}
					
					//Send an AJAX request to get the store data for the address entered.
					$.ajax({
					
						url: frmFindStore.attr("action"),
						
						data: {
							addr: searchString,
							r: frmFindStore.find("#hidFindStoreRadius").val()
						},
						
						type: "GET",
						
						dataType: "json",
						
						success: function(data) {
						
							//Remove any previous results
							locationDisplay.find(".storeLocation").remove();
						
							if (data.error) {
								locationDisplay.prepend("<div class=\"storeLocation\"><h5>"+data.error+"</h5></div>");
							}
							else if (data.stores) {
							
								var outputString = "";
							
								//No errors, output the first 2 results
								$.each(data.stores, function(i) {
								
									if (i >= 2) {
										return;
									}
									
									outputString = outputString+"<div class=\"storeLocation\"><h5>"+data.stores[i].name+"</h5><div class=\"locationTelephone\"><em>"+data.stores[i].phone+"</em></div><div class=\"locationAddress01\">"+data.stores[i].address+"</div></div>";
									
								});
							
								locationDisplay.prepend(outputString);
							
							}
							
							frmFindStore.parent().hide();
							
							locationDisplay.show();
						
						},
						
						error: errorFunction
					});
						
				}//end of findStores
			
				frmFindStore.find("#btnFindStore").unbind("click").bind("click", function(event) {
					var checkNullVal = $.trim(frmFindStore.find("#txtFindStoreAddress").val());
					if(checkNullVal != "") {
						findStores(event);
					}
				});
			
				$(window).load( function(){
					//initialize the store locator, but supress error messages
					findStores(null, true);
				});
			}
			

		
		})();
		
		
		
		/* Form Enter-key support for Internet Explorer (because buttons are hidden, enter doesn't submit the form, this fixes that) */
		if ($.browser.msie) {
			$(document).bind("keydown", function(event) {
				if (event.keyCode == 13) {
				
					sourceElement = $(event.srcElement);
					parentForm = sourceElement.parents("form");
				
					if (parentForm.length > 0) {
					
						event.preventDefault();
						
						//Submit this item's parent form.
						parentForm.trigger("submit");
					
					}

				}
			});			
		}



		//Setup link handling for any "forgot password" links
		(function() {
		
			var forgotPasswordFields = {
				txtForgotPasswordUsername: {
					name: "txtForgotPasswordUsername",
					type: "username",
					required: true,
					emptyErrorMessage: Sprint.content.formFieldErrors.usernameEmpty[Sprint.currentLanguage],
					invalidErrorMessage: Sprint.content.formFieldErrors.usernameInvalid[Sprint.currentLanguage]
				},
				radForgotPasswordDestinationEmail: {
					name: "radForgotPasswordDestinationEmail",
					type: "radioButton",
					required: true,
					emptyErrorMessage: Sprint.content.forgotPasswordErrors.passwordDestinationEmpty[Sprint.currentLanguage]
				},
				radForgotPasswordDestinationEmail: {
					name: "radForgotPasswordDestinationText",
					type: "radioButton",
					required: true,
					emptyErrorMessage: Sprint.content.forgotPasswordErrors.passwordDestinationEmpty[Sprint.currentLanguage]
				},
				txtForgotPasswordCode: {
					name: "txtForgotPasswordCode",
					type: "ESN,MEID,BAN,phoneNumber",
					required: true,
					emptyErrorMessage: Sprint.content.forgotPasswordErrors.passwordCodeEmpty[Sprint.currentLanguage],
					invalidErrorMessage: Sprint.content.forgotPasswordErrors.passwordCodeInvalid[Sprint.currentLanguage]
				},
				txtForgotPasswordNewPassword: {
					name: "txtForgotPasswordNewPassword",
					type: "password",
					required: true,
					emptyErrorMessage: Sprint.content.forgotPasswordErrors.newPasswordEmpty[Sprint.currentLanguage],
					invalidErrorMessage: Sprint.content.forgotPasswordErrors.newPasswordInvalid[Sprint.currentLanguage]
				},
				txtForgotPasswordConfirmPassword: {
					name: "txtForgotPasswordConfirmPassword",
					type: "match",
					required: true,
					mustMatch: "txtForgotPasswordNewPassword",
					emptyErrorMessage: Sprint.content.forgotPasswordErrors.newPasswordConfirmEmpty[Sprint.currentLanguage],
					invalidErrorMessage: Sprint.content.forgotPasswordErrors.newPasswordConfirmInvalid[Sprint.currentLanguage]
				},
				txtForgotPasswordEmail: {
					name: "txtForgotPasswordEmail",
					type: "emailAddress",
					required: false,
					invalidErrorMessage: Sprint.content.formFieldErrors.emailAddressInvalid[Sprint.currentLanguage],
					customValidationRule: function(field) {
						var fieldValue = field.val();
						var secondaryFieldValue = $("#txtForgotPasswordPhone").val();
						
						if (fieldValue != "" && secondaryFieldValue == "") {
							forgotPasswordFields.txtForgotPasswordPhone.required = false;
							return Sprint.formFieldTypes.emailAddress.test(fieldValue);
						} else {
							return true;
						}
					}
				},
				txtForgotPasswordPhone: {
					name: "txtForgotPasswordPhone",
					type: "phoneNumber",
					required: true,
					emptyErrorMessage: Sprint.content.forgotPasswordErrors.phoneNumberAndEmailEmpty[Sprint.currentLanguage],
					invalidErrorMessage: Sprint.content.formFieldErrors.phoneNumberInvalid[Sprint.currentLanguage]
				}
			};
			
			// Bind the cancel button to trigger the click event on the modal chrome close button.
			function bindCancelButton() {
				$("#forgotPasswordModal").find("#btnForgotPasswordCancel").bind("click", function(event) {
					event.preventDefault();
					Sprint.modal.elem.find("a.modalChromeCloseButton").trigger("click");
				});
			}
			
			// Bind the submit button to do client-side validation on the form fields, if that passes then
			// call ajax query to get the next page in the flow.
			function bindSubmitButton() {
				var forgotPasswordModal = $("#forgotPasswordModal");
				var frmForgotPassword   = forgotPasswordModal.find("#frmForgotPassword");
								
				forgotPasswordModal.find("#btnForgotPasswordSubmit").unbind("click").bind("click", function(event) {
					event.preventDefault();
					
					// Client-side validation
					var validationRules = Sprint.fn.cloneObject(forgotPasswordFields);
					var validForm = Sprint.fn.validateForm(forgotPasswordModal.find("#frmForgotPassword"), forgotPasswordFields);
					if (validForm == true) {
						$.ajax({
							data: "ajax=true&" + frmForgotPassword.serialize(),
							url: frmForgotPassword.attr("action"),
							type: "GET",								
							async: false,
							dataType: "json",
							
							success: function(data) {
								if (data.validated) {
									Sprint.modal.elem.loadModalContent(data.responseUrl, forgotPasswordModal);
									bindCancelButton();
									bindSubmitButton();
									bindEnterToSubmitEvent();
									setFocusToFirstField();
									if (data.securityQuestion) {
										forgotPasswordModal.find("span.securityQuestion").text(data.securityQuestion);
									}
									if (data.emailAddress) {
										forgotPasswordModal.find("span.sendToEmail").text(data.emailAddress);
									}
									if (data.phoneNumber) {
										forgotPasswordModal.find("span.sendToPhone").text(data.phoneNumber);
									}
								} else {
									//Remove previous error messages.
									forgotPasswordModal.find(".headerWrapperPrimary").find(".formErrors").remove();
									forgotPasswordModal.find(".error").removeClass("error");
				
									//Form has errors, show messages.
									forgotPasswordModal.find("#frmForgotPassword").showFormErrors({
										errorData: {
											txtValidationCode: {
												name: data.errorField,
												errorMessage: data.errorMessage
											}
										},
										summaryAnchor: forgotPasswordModal.find(".headerWrapperPrimary"),
										showInline: false,
										scrollToSummary: false
									});
								}
							},
														
							error: Sprint.fn.ajaxError	
						});
					} else {
						//Remove previous error messages.
						forgotPasswordModal.find(".headerWrapperPrimary").find(".formErrors").remove();
						forgotPasswordModal.find(".error").removeClass("error");
				
						//Form has errors, show messages.
						forgotPasswordModal.find("#frmForgotPassword").showFormErrors({
							errorData: validForm,
							summaryAnchor: forgotPasswordModal.find(".headerWrapperPrimary"),
							showInline: false,
							scrollToSummary: false
						});
					}
					
					// Reset the validation rules
					forgotPasswordFields = validationRules;
				});
			}
			
			//Make sure that the form submit goes through the submit button click event
			function bindEnterToSubmitEvent() {
				$("#frmForgotPassword").bind("submit", function(event) {
					event.preventDefault();
					$("#forgotPasswordModal").find("#btnForgotPasswordSubmit").trigger("click");
				});
			}
			
			// Bind the forgot username link to an event that will load content into the modal.
			function bindForgotUsernameLink() {
				$("#forgotPasswordModal").find("a.forgotUsername").unbind("click").bind("click", function(event) {
					event.preventDefault();
					Sprint.modal.elem.loadModalContent("modals/forgot_username.php", $("#forgotPasswordModal"));
					bindCancelButton();
					bindSubmitButton();
					bindEnterToSubmitEvent();
					setFocusToFirstField();
				});
			}
			
			// Set the focus on the first input field, not the first item which would be the close button.
			function setFocusToFirstField() {
				Sprint.modal.elem.focusFirstElement({
					focusableElements: "input[type!='hidden'], submit, button",
					containFocus: false
				});
			}
			
			// Bind the forgot password link to open the modal and display the contents.
			$("a.forgotPassword").unbind("click").bind("click", function(event) {
				event.preventDefault();
				
				var forgotPasswordModal = $("#forgotPasswordModal");
				
				if (forgotPasswordModal.length < 1) {
					var newModal = $("<div class=\"modal\"></div>");
					forgotPasswordModal = $("<div id=\"forgotPasswordModal\"></div>");
					forgotPasswordModal.appendTo(newModal);
					newModal.appendTo("div.sprint div.body").hide();
				}
				
				// Open the modal from the initial forgot password state
				forgotPasswordModal.openModal({
					ajaxContent: true,
					ajaxPath: "/global/modals/forgot_password.php",
					
					openCallback: function() {
						bindCancelButton();
						bindSubmitButton();
						bindEnterToSubmitEvent();
						bindForgotUsernameLink();
						setFocusToFirstField();
					}
				});
			});
			
			// Bind the forgot username link to open the modal and display the contents.
			$("a.forgotUsername").unbind("click").bind("click", function(event) {
				event.preventDefault();
				
				var forgotPasswordModal = $("#forgotPasswordModal");
				
				if (forgotPasswordModal.length < 1) {
					var newModal = $("<div class=\"modal\"></div>");
					forgotPasswordModal = $("<div id=\"forgotPasswordModal\"></div>");
					forgotPasswordModal.appendTo(newModal);
					newModal.appendTo("div.sprint div.body").hide();
				}
				
				// Open the modal from the initial forgot password state
				forgotPasswordModal.openModal({
					ajaxContent: true,
					ajaxPath: "/global/modals/forgot_username.php",
					
					openCallback: function() {
						bindCancelButton();
						bindSubmitButton();
						bindEnterToSubmitEvent();
						setFocusToFirstField();
					}
				});
			});
		
		})();

	});
	
})(jQuery);