// JavaScript Document
jQuery(document).ready(function(){

	//FANCYBOX SIGNUP
	jQuery("#signup_link").fancybox({
		'scrolling' : 'no',
		'titleShow'	: false,
		'onClosed'	: function() {
		    jQuery("#signup_error").hide();
		}
	});
	
	//FANCYBOX LOGIN
	jQuery("#login_link").fancybox({
		'scrolling' : 'no',
		'titleShow'	: false,
		'onClosed'	: function() {
		    jQuery("#login_error").hide();
			jQuery("#login_form").show();
			jQuery("#forgotpassword").hide();
		}
	});
	
	//SIGNUP FORM
	jQuery('#signupform').submit(function(event) {
		event.preventDefault();
		jQuery.ajax({
			type: "POST",
			url: "wp-content/plugins/ajax-login/register.php",
			data: jQuery(this).serialize(),
			success: function(response_html){
				if(response_html!=1) {
					var error_array=response_html.split("\n",2);
					jQuery("#signup_success").hide();
					jQuery("#signup_error").show();
					jQuery("#signup_error").html(error_array[1]);
				} else {
					jQuery('#signupform').get(0).reset()
					jQuery("#signup_error").hide();
					jQuery("#signup_success").html('Registration successfull. Please check your email for activation.');
					jQuery("#signup_success").show();
				}
				jQuery.fancybox.resize();
			}
		});
	});
	
	//LOGIN FORM
	jQuery('#form1').submit(function(event) {
		event.preventDefault();
		jQuery.ajax({
			type: "POST",
			url: "wp-content/plugins/ajax-login/login.php",
			data: jQuery(this).serialize(),
			success: function(response_html){
				if(response_html!=1) {

					var error_array=response_html.split("\n",2);
					jQuery("#login_success").hide();
					jQuery("#login_error").show();
					jQuery("#login_error").html(error_array[1]);
					//FORGOT PASSWORD LINK
					jQuery("#login_error a").click(function(event){
						event.preventDefault();
						jQuery("#login_form").hide();
						jQuery("#forgotpassword").show();
						jQuery.fancybox.resize();
					});
				} else {
					jQuery("#login_error").hide();
					jQuery("#login_success").html('Login Successful.');
					jQuery("#login_success").show();
					jQuery('#login_form').fadeOut('slow', function() {
					    jQuery.fancybox.resize();
						location.reload();
						/*jQuery.post("wp-content/plugins/ajax-login/login.php", { get_logout: 1 },
						   function(logout_link){
							 jQuery("#login_li").html(logout_link);
							 jQuery("#signup_li").remove();
					   });*/
					});
				}
				jQuery.fancybox.resize();
			}
		});
	});
	
	//FORGOT PASSWORD FORM
	jQuery('#lostpasswordform').submit(function(event) {
		event.preventDefault();
		jQuery.ajax({
			type: "POST",
			url: "wp-content/plugins/ajax-login/lostpassword.php",
			data: jQuery(this).serialize(),
			success: function(response_html){
				if(response_html!=1) {
					var error_array=response_html.split("\n",2);
					jQuery("#login_success").hide();
					jQuery("#login_error").show();
					jQuery("#login_error").html(error_array[1]);
				} else {
					jQuery("#login_error").hide();
					jQuery("#login_success").html('Reset Password request has been sent to your email address successfully.');
					jQuery("#login_success").show();
				}
				jQuery.fancybox.resize();
			}
		});
	});
	
	//BACK TO LOGIN LINK
	jQuery('#login_form_link').click(function(event) {
		event.preventDefault();
		jQuery("#login_error").hide();
		jQuery("#login_form").show();
		jQuery("#forgotpassword").hide();
		jQuery.fancybox.resize();
	});

	return false;
});