﻿$(document).ready(function() {
	$('#nojs').hide();
	$('#pr_email').focus();
	var dirpreface = '';
	var homedir = '';
	var plan_room = 'http://www.aecci.com/planroom/index.php';
	var pr_login = 'http://www.aecci.com/planroom/pr_login.php';
	var pr_register = 'http://www.aecci.com/planroom/pr_register.php';
	var pr_processing = 'http://www.aecci.com/planroom/pr_processing.php';
	var pr_logout = 'http://www.aecci.com/planroom/pr_logout.php';
	var prpass_reset = 'http://www.aecci.com/planroom/pr_reset.php';
	var prset_pass = 'http://www.aecci.com/planroom/prset_pass.php';
	var numfiles = 0;
	$('#select_all').attr('checked', false);
	$('.pr_cb').attr('checked', false);
	$('#pr_download').attr('disabled', true);
	function processData (data) {
		$('#pr_ajax_container').fadeOut(200, function() {
			$('#loading_container').hide();
			$('#pr_ajax_container').html(data);
			$('#pr_ajax_container').fadeIn(200);
			$('#pr_download').attr('disabled', true);
		});
	} //end processData function
	
	$('#pr_regbutton').click(function() {
		var email = $('#pr_email').val();
		$('#pr_login').fadeOut(200, function() {
			$('.pr_error').html('');
			$('#prr_email').val(email);
			$('.pr_register').fadeIn(200);
			$('#prr_fn').focus();
		});
	});
	
	$('#prr_cancel').click(function() {
		$('.pr_register').fadeOut(200, function() {
			$('.pr_error').html('');
			$('#pr_login').fadeIn(200);
			$('#pr_email').focus();
		});
	});
	
	$('#prsetpass_cancel').click(function() {
		$('#prset_pass').fadeOut(200, function() {
			$('.pr_error').html('');
			$('#pr_login').fadeIn(200);
			$('#pr_email').focus();
		});
	});
	
	$('#pr_resetbutton').click(function(){
		var email = $('#pr_email').val();
		$('#pr_login').fadeOut(200, function() {
			$('.pr_error').html('');
			$('#prpass_reset').fadeIn(200);
			$('#prreset_email').val(email).focus();
		});
	});
	
	$('#prreset_cancel').click(function() {
		$('#prpass_reset').fadeOut(200, function() {
			$('.pr_error').html('');
			$('#pr_login').fadeIn(200);
			$('#pr_email').focus();
		});
	});
	
	$('#pr_register').submit(function(rg) {
		rg.preventDefault();
		regdata = $(this).serialize();
		$.post(pr_register, regdata, processReg);
		function processReg(data) {
			if (data == 'success') {
				$('.pr_error').fadeOut(200);
				$('#pr_register').fadeOut(200, function() {
					$('#success').load('sucreg.htm', function() {
						$('#success').fadeIn(200);
					});
				});
			} else if (data == 'incomplete') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('All fields are required.  Please try again.');
					$('.pr_error').fadeIn(300);
				}); //end incomplete
			} else if (data == 'e') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('That is not a valid email address.  Please try again.');
					$('.pr_error').fadeIn(300);
				}); //end validate email
			} else if (data == 'x') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('Your password and confirmed password do not match.  Please try again.');
					$('.pr_error').fadeIn(300);
				}); //end passwords don't match
			} else if (data == 'duplicate') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('That email address has already been registered.  To reset your password, <a href="" id="pr_reset">click here.</a>');
					$('.pr_error').fadeIn(300);
				}); // end already registered
			}; // end multiple data if
		}; //end processReg function
	}); //end register click function
	
	//start successful login function
	$('#pr_login').submit(function(li) {
		li.preventDefault();
		var lidata = $(this).serialize();
		$.post(pr_login, lidata, processLogin);
		function processLogin(data) {
			if (data == 'e') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('You must enter an email address.');
					$('.pr_error').fadeIn(300);
				});
			} else if (data == 'p') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('You must enter a password.');
					$('.pr_error').fadeIn(300);
				});
			} else if (data == 'b') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('You must enter an email address and password.');
					$('.pr_error').fadeIn(300);
				});
			} else if (data == 'f') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('That email and password does not match our records.  Please try again.');
					$('.pr_error').fadeIn(300);
				});
			} else if (data == 'a') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('That account has not been activated.  In order to activate your account, you must click on the link in the Registration Confirmation email you should have received.  If you did not receive this email, please contact the <a href="mailto:abaker@aecci.com">contact the Webmaster.</a>');
					$('.pr_error').fadeIn(300);
				});			
			} else if (data == 'r') {
				$('#pr_login').fadeOut(200, function() {
					$('#prset_pass').fadeIn(200);
					$('#prset_passn').focus();
				});
			} else if (data == 's') {
				navdata = {directory: homedir};
				$.post(pr_processing, navdata, processData);
				$('#pr_login').fadeOut(200, function() {
					$('#plan_room_box').fadeIn(200);
				});
				$('.pr_error').hide();
				$.cookie('pr', 'true');
				dirpreface = '';
				$('#dirpreface').val(dirpreface);
			};
		}; //end processLogin function
	});
	//end successful login function
	
	$('#prpass_reset').submit(function(reset) {
		reset.preventDefault();
		var resetdata = $(this).serialize();
		$.post(prpass_reset, resetdata, processReset);
		function processReset(data) {
			if (data == 'success') {
				$('.pr_error').fadeOut(200);
				$('#prpass_reset').fadeOut(200, function() {
					$('#success').load('sucreset.htm', function() {
						$('#success').fadeIn(200);
					});
				});
			} else if (data == 'incomplete') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('You must enter an email address.  Please try again.');
					$('.pr_error').fadeIn(300);
				}); //end incomplete
			} else if (data == 'e') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('That is not a valid email address.  Please try again.');
					$('.pr_error').fadeIn(300);
				}); //end validate email
			} else if (data == 'duplicate') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('That email address is not registered.  Please try again.');
					$('.pr_error').fadeIn(300);
				}); // end not registered
			}; // end multiple data if
		};
	});
	
	$('#prset_pass').submit(function(np) {
		np.preventDefault();	
		var e = $('#pr_email').val();
		var p = $('#pr_pass').val();
		var np = $('#prset_passn').val();
		var cp = $('#prset_passc').val();
		var setdata = {
			pr_email: e,
			pr_pass: p,
			prset_passn: np,
			prset_passc: cp
		}
		$.post(prset_pass, setdata, processSet);
		function processSet (data) {		
			if (data == 'np') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('You must enter a new password.  Please try again.');
					$('.pr_error').fadeIn(300);
				}); // end not registered
			} else if (data == 'cp') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('You must enter a confirmed password.  Please try again.');
					$('.pr_error').fadeIn(300);
				}); // end not registered
			} else if (data == 'both') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('You must enter a new password and a confirmed password.  Please try again.');
					$('.pr_error').fadeIn(300);
				}); // end not registered
			} else if (data == 'e') {
				$('.pr_error').fadeOut(300, function() {
					$('.pr_error').html('Your new password and your confirmed password do not match.  Please try again.');
					$('.pr_error').fadeIn(300);
				}); // end not registered
			} else if (data == 'reset') {
			navdata = {directory: homedir};
				$.post(pr_processing, navdata, processData);
				$('#prset_pass').fadeOut(200, function() {
					$('#plan_room_box').fadeIn(200);
				});
				$('.pr_error').hide();
				$.cookie('pr', 'true');
				dirpreface = '';
				$('#dirpreface').val(dirpreface);	
			}
		};
	});
	
	//start nav click
    $('#plan_room_table a').live("click", function(evt){
    	evt.preventDefault();
		$('#loading_container').fadeIn(200);
    	$('#pr_ajax_container').fadeOut(200);
    	var dir = $(this).attr('href');
    	dirtest = dir.slice(0,4);
		if (dirtest == 'http') {
			dir = dir.slice(30);
			dirslashtest = dir.slice(0, 1);
			if (dirslashtest == '/') {
				dir = dir.replace(/%20/g, ' ');
			} else {
				dir = "/" + dir.replace(/%20/g, ' ');
			}
		} else {
			dirslashtest = dir.slice(0, 1);
			if (dirslashtest == '/') {
				dir = dir.replace(/%20/g, ' ');
			} else {
				dir = "/" + dir.replace(/%20/g, ' ');
			}
		};
		var fulldir = dirpreface + dir;
		navdata = 	{directory: fulldir};
		$.post(pr_processing, navdata, processData);
		dirpreface += dir;
		$('#dirpreface').val(dirpreface);
	}); 
	//end nav click function
	
	//start home click
	$('#pr_home').live("click", function(home) {
		if (dirpreface == '') {
		return false;
		};
		home.preventDefault();
		$('#pr_ajax_container').fadeOut(200);
		$('#loading_container').fadeIn(200);
		navdata = {directory: homedir};
		$.post(pr_processing, navdata, processData);
		dirpreface = '';
		$('#dirpreface').val(dirpreface);
	});
	//end home click
	
	//start up click
	$('#pr_up').live("click", function(up) {
		if (dirpreface == '') {
			return false;
		};
		up.preventDefault();
    	$('#pr_ajax_container').fadeOut(200);
		$('#loading_container').fadeIn(200);
		slashindex = dirpreface.lastIndexOf("/");
		if (slashindex == -1) {
			var updir = '';
		} else {
			var updir = dirpreface.slice(0, slashindex);
		};
		navdata = {directory: updir};
		$.post(pr_processing, navdata, processData);
		dirpreface = updir;
		$('#dirpreface').val(dirpreface);
	});
	//end up click
	
	//start download click
	$('#pr_dowload').live("click", function(dl) {
		dl.preventDefault();
		var filename = $(':checked').val();
		dldata = {file: filename};
		$.post('dl_processing.php', dldata, processDLData);
		function processDLData (data) {
			
		};
	});
	//end download click
	
	//start select-all click
	$('#select_all').live("click", function() {
		var checked_status = this.checked;
		$('.results :checkboxes').each(function() {
		    this.checked = checked_status;
		});
		var boxstatus = '';
		$('.pr_cb').each(function() {
			var thisboxstatus = $(this).attr('checked');
			if (thisboxstatus == true) {
				boxstatus += thisboxstatus;
			}; //end if
		}); //end each function
		if (boxstatus == '') {
			$('#pr_download').attr('disabled', true);
		} else {
			$('#pr_download').attr('disabled', false);
		};
	});
	//end select-all click
	
	//start checkboxes click
	$('.pr_cb').live("click", function(av) {
		var boxstatus = '';
		$('.pr_cb').each(function() {
			var thisboxstatus = $(this).attr('checked');
			if (thisboxstatus == true) {
				boxstatus += thisboxstatus;
			};
		});
		if (boxstatus == '') {
			$('#pr_download').attr('disabled', true);
			$('#select_all').attr('checked', false);
		} else {
			$('#pr_download').attr('disabled', false);
		};
	});
	// end checkboxes click
		
	//start logout click
	$('#pr_logout').live("click", function() {
		var lo_data = '';
		$.post(pr_logout, lo_data, process_LOData);
		function process_LOData(data) {
			if (data == 'lo') {
				$.cookie('pr', 'logout');
				location = plan_room;
			};
		};
	});
	//end logout click
	
	if ($.cookie('pr') == 'true') {
		navdata = {directory: homedir};
		$.post(pr_processing, navdata, processData);
		$('#pr_login').hide();
		$('#pr_error').hide();
		$('#plan_room_box').fadeIn(200);
		dirpreface = '';
	}
	
	if ($.cookie('epr') == 'true') {
		navdata = {directory: homedir};
		$.post(pr_processing, navdata, processData);
		$('#pr_logout').hide();
		$('#plan_room_box').fadeIn(200);
		dirpreface = '';
	};
	
// HELP SECTION // HELP SECTION // HELP SECTION // HELP SECTION // HELP SECTION // HELP SECTION
	
	var helpbutton = $('#helpbutton');
	var ol = $('#prhelp_overlay');
	var win = $('#prhelp_window');
	var con = $('#container');
	var help_content = 'http://www.aecci.com/planroom/help_cont.htm';
	
	var close_button = $('#prhelp_closebutton');
	var close = function(){
		$(win).fadeOut(1000, function() {
			$(ol).fadeOut(1000);
		});
	};


	
	$(helpbutton).click(function(help) {
		help.preventDefault();
		$(win).load(help_content);
		var cw = $(document).width();
		var ch = $(document).height();
		var ww = $(win).width();
		var wh = $(win).height();
		if (wh >= ch) {
			var tm = '0px';
		} else {
			var tm = (ch-wh)/2 + 'px';
		};
		var hdr = $('#prhelp_header');
		var hcon = $('#prhelp_container');
		var ftr = $('#prhelp_footer');
		var hdrh = $(hdr).height();
		var ftrh = $(ftr).height();
		var ftrw = $(ftr).width();
		var prstatic = $('.static');
		var closew = $(close_button).width();
		$(win).css({
			left: (cw - ww)/2 + 'px',
			top: tm
		});
		$(close_button).css({
			
		});
		$(ol).fadeIn(1000, function() {
			$(win).fadeIn(1000);
		});
	});
 	$(ol).click(close);
	$(close_button).live("click", close);

}); //end ready