/*
** superLogin.js
** Copyright Ronningen Design, LLC (http://www.websitesthatdostuff.com)
** Subcontracted to Substratal Code LLC (http://www.substratalcode.com)
** Tampered with by Way Short of Heaven (http://www.wayshortofheaven.com)
**
*/


/*
** Launch the main (top right of page) login modal
*/

$(document).ready(function() {
   $("#superLoginModal").dialog({
	   autoOpen: false,
	   modal: false,
	   height: 272,
	   width: 240,
	   position: [0, 0],
	   dialogClass: 'superLoginModal'
	   });

	$(".logInOutLink").click(toggleLogin);

	$("#superLoginModal .cancel").click(toggleLogin);

	$("#superLoginModal .superLoginBtn").click(loginSuper);

	// Set up the background iframe to work around IE8:
	$('.fix-z-index').bgiframe();

	// Check for the login.
	$("#superLoginModal .loginForm").keypress(function(e) {
		if(e.keyCode == 13) {
			// Click the submit button:
			$('.formLogInBtn.superLoginBtn').click();
			return false;
		}
	});

});

/*
* Function to toggle the visibility of the global login modal.
*
*/
function toggleLogin() {
	if( $(".logInOutLink").hasClass('logInVisible') ) {
		closeLogin();
	} else {
		openLogin();
	}

	return false;
}

/*
* Function to open the login modal and move it to the correct position.
*
*/
function openLogin() {
	// Open the dialog:
	$("#superLoginModal").dialog('open');
	// Move to the correct place:
	$("#superLoginModal").parent().appendTo("#superLoginModalInner");
	// Fix the CSS:
	$("#superLoginModal").parent().css({'top' : '0', 'left' : '197px', 'z-index' : '2000', 'position':'absolute'});

	// Increase the x-index of the link so it will be visible:
	$(".logInOutLink").addClass('logInVisible');

	$("#superLoginEmail").css("z-index","2000");

	return false;
}

/*
* Function to close the login modal.
*
*/
function closeLogin() {
	$("#superLoginModal").dialog('close');
	$(".logInOutLink").removeClass('logInVisible');
	return false;
}


/*
** Function to log the user in (from the top right):
*/
function loginSuper() {
	var data = $("#superLoginModal form.loginForm").serialize();

	if( $(".superLoginBtn").hasClass('loginLoader') ) {
		return false;
	}

	$.ajax({
		type: "POST",
		url: "/users/login/",
		data: data,
		dataType: "json",
		beforeSend: function() {
			$("#superLoginModal .formLogInBtn").toggleClass("loginLoader");
			$("#superLoginModal .cancel").toggleClass("btnDisabled");
		},
		complete: function() {
			$("#superLoginModal .formLogInBtn").toggleClass("loginLoader");
			$("#superLoginModal .cancel").toggleClass("btnDisabled");
		},
		success: function (response) {
			if (response.success == true) {
				// Remove the click listener:
				$(".logInOutLink").unbind('click', toggleLogin);

				if($('body').hasClass('install')) {
					location.reload();
				}

				// if - Add Profile (Alumni and People)
				if($('#notLoggedIn').length != 0) {
					/*
					$('#notLoggedIn').toggle();
					$('#addProfileForm').toggle();
					$('#idMe').val(response.data.id);

					$('#ProfileFirstName').val(response.data.firstName);
					$('#ProfileLastName').val(response.data.lastName);
					*/
					
					location.reload();
					
				}

				// if - Alumni Registration
				if($('#notLoggedIn_AlumReg').length != 0) {
					$('#notLoggedIn_AlumReg').toggle();
					$('#updateFormToggleWrap').toggle();
				}

				// Close the login.
				closeLogin();

				superUpdateLoggedInNotifications(response.data.html);

			} else {
				$("#superLoginModal .loginError").html(response.data).show().animate({backgroundColor: "#c01a1a", color:"white"}, 500);
			}
		}
	});

	return false;
}

function superUpdateLoggedInNotifications(data) {
	$(".loggedInAs").html(data);
	$("#superLoginModalInner").html( data + "<span>&nbsp;/&nbsp;</span>" + $("#superLoginModalInner").html() );

	// Change the Log In link to Log Out.
	$(".logInOutLink").attr('href', '/users/logout').html('Log Out');

	// Check for other places to update:
	if(typeof updateModals == 'function' ) {
		updateModals();
	}
}

// We use JS to hide the image initially because otherwise if we just
// display:none in the image, ad JS is disabled, you never get the image.
$('#feature img').hide();

// Image Preloader
$(window).bind("load", function() {
	$('#feature img:hidden').fadeIn(500);
});


