// Preload Images
img1 = new Image(16, 16);  
img1.src="/images/login_images/spinner.gif";

img2 = new Image(220, 19);  
img2.src="/images/login_images/ajax-loader.gif";

// When DOM is ready
$(document).ready(function(){
	// Launch MODAL BOX if the Login Link is clicked
	$('#login_link').click(function(){
		$('#login_div').modal();
		});
	// When the form is submitted
	$('form#login').submit(function(){  
		// Hide "Submit" Button
		$('#submit').hide();
		// Show Gif Spinning Rotator
		$('#ajax_loading').show();
		// -- Start AJAX Call --
		$.ajax({
			url: '/login/do-login.php',  // Send the login info to this page
			data: $('form#login').serialize(),
			type: 'post',
			cache: false,
			dataType: 'data',
			success: function(msg) {
			$('#status').ajaxComplete(function(event, request, settings){  
				 // Show "Submit" Button
				$('#submit').show();
				// Hide Gif Spinning Rotator
				$('#ajax_loading').hide();
				if(($.trim(msg))=='OK') {  // LOGIN OK?
					var login_response = '<div id="logged_in">' +
					'<div style="width: 250px; float: left; margin-left: 30px;">' + 
					'<div style="width: 40px; float: left;">' +
					'<img style="margin: 6px 0px;" align="absmiddle" ' + 
					'src="/images/login_images/ajax-loader.gif" />' +
					'</div>' +
					'<div style="margin: 15px 0px 0px 15px; float: left; ' + 
					'width: 170px;">'+ 
					'<strong>You are successfully logged in!</strong></div></div>';  
					$('a.modalCloseImg').hide();  
					$('#simplemodal-container').css("height","100px");
					$(this).html(login_response); // Refers to "status"
					window.setTimeout('location.reload()', 2000); //reloads after 2 seconds
					}  
					else 
					{  // ERROR?
					 var login_response = msg;
					$('#login_response').html(login_response);
					} // end if msg
				});  //end status
			} // end msg
		});  // -- End AJAX Call --
	return false;
	}); // end submit event
}); // end click function

$(document).ready(function(){
	// Launch MODAL BOX if the Login Link is clicked
	$('#logout_link').click(function(){
		// -- Start AJAX Call --
		$.ajax({  
			type: 'post',
			url: '/login/logout.php',
			success: function() {
			window.location = '/index.php?'; // go home to refresh the page
		    // window.setTimeout('location.reload()', 1000);
			}
			 // Send the logout command to this page
		   });  // -- End AJAX Call --
		return false;
	 });
});
