$().ready(function() {


$.extend($.modal.defaults, {
	onOpen: function (dialog) {
		dialog.overlay.fadeIn('slow', function () {
			dialog.data.hide();
			dialog.container.fadeIn('slow', function () {
			dialog.data.fadeIn('slow');
			});
		});
	},
	onClose: function (dialog) {
		dialog.data.fadeOut('slow', function () {
			dialog.container.hide('slow', function () {
				dialog.overlay.fadeOut('slow', function () {
					$.modal.close();
				});
			});
		});
	}

});



function loginDo() {
	$("#login-msg").css({"color":"#FFF"}).text('Validating....').fadeIn(1000);        
	var username = $('#lusername').val();
	var password = $('#lpassword').val();
	var redirect = $('#redirect').val();       	 
	
	$.ajax({
		type: "POST",
		url: "http://www.adventistyouth.org/ajax/login-exec.php",
		data: "username="+username+"&password="+password+"&redirect="+redirect,
		success: function(response){       		      
			var sresponse = response.substr(0,5);       		     
			if(sresponse == 'Error') {
				$("#login-msg").fadeTo(200,0.1,function() {
				       	$(this).html(response).css({"color":"red"}).fadeTo(900,1);  
				 });
			} else {
				$("#login-msg").css({"color":"green","background":"url(http://www.adventistyouth.org/ff2014/images/ajax-loader.gif) no-repeat 100px center"}).fadeTo(200,0.1,function() {
				       	$(this).text('logging in');		    		                  
				       	$("#login-msg").fadeTo(900,1,function() {
				       		document.location= response;
				       	});
				});  
				       		              
			}
		}
	});
	return false;
}


function loginModal() {	

	var loginHTML = '<div class="header">Log in to myCYE</div><form action="#" method="post" style="margin:0px" id="loginForm"><input type="hidden" name="redirect" id="redirect" value="mycye"><div class="text">Please enter your password and username</div><div id="login-msg"></div><p><label style="width:80px;">Username</label><input name="lusername" id="lusername" type="text" style="width:170px;margin:0;"></p><p><label style="width:80px;">Password</label><input name="lpassword" id="lpassword" type="password" style="width:170px;margin:0;"></p><p style="padding-left:150px;"><a href="#" id="forgot-link">Forgotten your password?</a></p><div class="buttons"><div class="forgot"><a href="http://www.adventistyouth.org/register.php">Register a myCYE Account!</a></div><button name="login-button" id="login-button" type="submit">Log in</button> <button type="button" name="cancel-button" id="cancel-button">Cancel</button></div></form>';

	$.modal(loginHTML, {
		overlayClose: true,
		overlayId: 'login-overlay',
		containerId: 'login-container',
		minHeight:230,		
		onShow: function() {
			$("#cancel-button").bind("click", function() {
				$.modal.close();
			});
			$("#forgot-link").bind("click", function () {
				$.modal.close();
				setTimeout(function() {
					passwordReset();
       				},1000);
				
				
				
			});		
			$("#loginForm").bind("submit",loginDo);
		}

	});
}

$(".mycye-login").bind("click",loginModal);



});