$(document).ready(function(){

	$('#signin_dialog').dialog({
		width: 206,
		autoOpen: false,
		buttons: {
			"Sign In": function() { 
				signIn();
			},
			"Cancel": function() { 
				$(this).dialog("close"); 
				$("#signin_error").html('');
			}
		}
	});
	
	$('#signin_link').click(function() {
		$("#signin_dialog").dialog('open');
		return false;
	})
	
	$('#delete_pdf').click(function() {
		if (confirm('Are you sure you want to delete this file?'))
		{
			$.post("/helpers/pdf.php", { action: 'delete_file', filename: $('#filename').val() },
				function(data) {
					$("#delete_pdf_wrap").hide();
					$("#delete_pdf_status").html(data);
					$("#pdf_uploadQueue").html('');
				}
			);
		}
		else
		{
			return false;
		}
	})
	
	$("#password").keyup(function(e) {
		if(e.keyCode == 13)
		{
			signIn();
		}
	});
	
	$("#signup").validate({
		errorContainer: $("#signup_error"),
		rules: {
			password_again: {
				equalTo: "#password_signup"
			},
			email: {
				email: true
			}
		}
	});

	$("#forgot").validate({
		errorContainer: $("#forgot_error"),
		rules: {
			email: {
				email: true
			}
		}
	});

	$("#search_form").validate({
		messages: {
			query: {
				required: "Please enter a search query."
			}
		}
	});

	$("#member_search_results").tablesorter({widgets: ['zebra']});
	$("#member_pending_memberships").tablesorter({widgets: ['zebra']});
	$("#member_expired_memberships").tablesorter({widgets: ['zebra']});
	$("#obits_search_results").tablesorter({widgets: ['zebra']});
	
	$('#pdf_upload').fileUpload({ 
		'filename':	 $('#id').val(),
		'uploader':	 '../public/swf/uploadify.swf', 
		'script':	 "../helpers/uploadify.php", 
		'folder':	 '../public/pdf',
		'fileDesc':  'Only *.pdf files are permitted.',
		'fileExt':   '*.pdf;',
		'auto': true,
		onSelect : function() {
			$("#delete_pdf_status").html('');
		},
		onComplete: function(event, queueID, fileObj, reposnse, data) {
			// show view delete options
			$("#delete_pdf_wrap").show();
			$("#delete_pdf_status").html('');
		}
	});
	
});

function signIn()
{
	$.post("helpers/signin.php", { email: $('#email').val(), password: $('#password').val() },
		function(data) {
			//alert (data);
			if (data == 1) {
				if ($('#page').val() == '/signout.php') {
					location.href = $('#site').val();
				} else {
					history.go(0);
				}
			} else if (data == 2) {
			    $("#signin_error").html('<span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"></span> Your account has expired.<br /><a href="renew.php">Renew your account</a>.');
		    } else if (data == 3) {
		        $("#signin_error").html('<span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"></span> Your account is awaiting approval.<br /><br />You will be notified by email when your account has been approved.');
			} else {
				$("#signin_error").html('<span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"></span> The email or password you entered is incorrect.');
			}
		}
	);
}