$(document).ready(function(){
	
	$('.camera').lightBox({fixedNavigation:true});
	
	$('#signin_dialog').dialog({
		width: 250,
		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;
	})

	$("#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':	 $('#filename').val(),
		'uploader':	 '/databases/common/public/swf/uploadify.swf', 
		'script':	 "/databases/common/helpers/uploadify.php", 
		'folder':	 '/databases/common/public/images/' + $("#site_mod").val(),
		'cancelImg' : '/databases/common/public/img/uploadify_cancel.png',
		'fileExt'     : '*.jpg;',
		'fileDesc'    : 'JPEG Image Files (.JPG)',
		'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('');
		}
	});

	$('#delete_pdf').click(function() {
		if (confirm('Are you sure you want to delete this file?'))
		{
			$.post("/databases/common/helpers/pdf.php", 
				{ 
				  action: 'delete_file', 
				  filename: $('#filename').val() ,
				  site_mod: $('#site_mod').val()
				},
				function(data) {
					console.log(data);
					$("#delete_pdf_wrap").hide();
					$("#delete_pdf_status").html(data);
					$("#pdf_uploadQueue").html('');
				}
			);
		}
		else
		{
			return false;
		}
	})


});

function signIn()
{
	$.post("/databases/common/helpers/signin.php", { email: $('#email').val(), password: $('#password').val() },
		function(data) {
			//alert (data);
			if (data == 1) {
				if ($('#page').val() == '/databases/common/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="/databases/common/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 if (data == 4) {
				location.href = '/databases/common/verify.php';
			} 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.');
			}
		}
	);
}

