var LoginPopupWidth = 0;
var LoginPopupLeft = 0;
var LoginPopupRight = 0;

//-----------------------------------------------------------------------------
function InitPopup() {
	// Set position of LoginPopup div
	if ( LoginPopupWidth == 0 ) // after it's shrunk, don't reget width
		LoginPopupWidth = $("#LoginPopup").width();

	var os = $("#LoginShow").offset();
	var top = os.top;
	LoginPopupRight = os.left + $("#LoginShow").width();
	LoginPopupLeft = LoginPopupRight - LoginPopupWidth;
	// LoginPopup is initially hidden
	//$("#LoginPopup :input:first").attr("tabindex","1");
	$("[tabIndex]").removeAttr("tabIndex");
	$("#LoginPopup :input:eq(0)").attr("tabIndex","1");
	$("#LoginPopup :input:eq(1)").attr("tabIndex","2");
	$("#LoginPopup :input:eq(2)").attr("tabIndex","3");
	$("#LoginPopup :input:eq(3)").attr("tabIndex","4");
	$("#LoginPopup").css({"width" : "0px", "left" : LoginPopupRight + "px", "display" : "none"});
	$("#LoginErrorPopup").css({"display" : "none", "top" : (top + 10) + "px", "left" : (LoginPopupRight - $("#LoginErrorPopup").width()) + "px"});
} // InitPopup

//-----------------------------------------------------------------------------
// Document ready function
//-----------------------------------------------------------------------------
$(document).ready(function() {

	$("#LoginShow").hover(
		function() {
			$(this).css("textDecoration","underline");
		},
		function() {
			$(this).css("textDecoration","none");
		}
	);

	// LoginPopup Initialization
	InitPopup();

	// Set up the show animation for the LoginPopup
	$("#LoginShow").click(function() {
		$("#LoginPopup").css("display","block");
		$("#LoginPopup").animate({
			"left" : LoginPopupLeft + "px",
			"width" : LoginPopupWidth + "px"
		}, 1500);
		$("#LoginPopup").queue(function() {
			$("#LoginPopup :input:first").focus();
			$(this).dequeue();
		});
	});

	// Set up the hide animation for the LoginPopup
	$("#LoginHide").click(function() {
		if ( $("#LoginErrorPopup").css("display") == "block" )
			$("#LoginErrorPopup").css("display","none");
		$("#LoginPopup").animate({
			"left" : LoginPopupRight + "px",
			"width" : "0px"
		}, 1500);
		$("#LoginPopup").queue(function() {
			$("#LoginPopup").css("display","none");
			$(this).dequeue();
		});
	});

	// Set up the close event for the LoginErrorPopup
	$("#LoginErrorHide").click(function() {
		$("#LoginErrorPopup").css("display","none");
	});

	// Remove hard-coded style width from input boxes
	$("#LoginPopup :input:not(:submit)").css("width","");

	// Look for login error field (indicates logged in or not)
	if ( $("#LoginError").length > 0 ) {
		// Check if there is a login error
		if ( $("#LoginError").html() != "" ) {
			// Move error message to LoginErrorPopup
			$("#LoginErrorPopupMessage").html($("#LoginError").html());
			$("#LoginError").html("");
			// Show the LoginPopup (don't bother with animation)
			$("#LoginPopup").css({"width" : LoginPopupWidth + "px", "left" : LoginPopupLeft + "px", "display" : "block"});
			// Show the LoginErrorPopup
			$("#LoginErrorPopup").css("display","block");
		}
	} else { // Logged in, change login to logout and profile
		$("#LoginShow").unbind("click");
		$("#ProfileLink a").text("Profile");
		var sTmp = $("#ProfileLink").html() + "<br />" + $("#LogoutLink").html();
		$("#LoginShow").html(sTmp);
	}

	// Need to call InitPopup when window is resized
	$(window).resize(InitPopup);

}); // $(document).ready(function()

