//----------------------------------------------------------------------------------//
// AM Design - Prestonwood Baptist Church - Media & Media Archive Pages
//----------------------------------------------------------------------------------//


/*	Apply 'clearClick' functionality to the "Take Notes" 
/*	section
/*-----------------------------------------------------------*/
function clearClickNotes(){
	$("#input_notes").focus(function(){
		if($(this).val() == "Enter your notes here..."){
			$(this).val("");	
		}
	});
	$("#input_notes").blur(function(){
		if($(this).val().trim() == ""){
			$(this).val("Enter your notes here...");	
		}
	});
}

/*	Display the form for sending the notes by email
/*-----------------------------------------------------------*/
function doEmail(){
	if($("#input_notes").val().trim() != "" && $("#input_notes").val().trim() != "Enter your notes here..."){
		$("#div_notes").hide();
		$("#options").hide();
		$("#div_email").show();
	}else{
		alert("Please fill in any notes you would like to email in the available text area before continuing.");	
	}
}

/*	Call the AJAX request for sending the email/Hide the form 
/*	when finished
/*-----------------------------------------------------------*/
function sendEmail(){
	var ea = $("#email").val().trim();
	if(ea != ""){
		var msg = $("#message").val().trim();
		var title = $("#sermon").val().trim();
		var content = $("#input_notes").val().trim();
		
		$("#email").attr("disabled","disabled");
		$("#message").attr("disabled","disabled");
		$("#loader").fadeIn("fast");
		$("#response").load("/includes/sermons/email_notes.php", {email:ea,message:msg,sermon:title,notes:content}, function(response){
			if(eval(response) == 1){
				$("#response").attr("class","success_message");
				$("#email_form").fadeOut("fast");
				$("#response").html("<p class='success'>Your email was successfully sent!</p>");
				$("#loader").fadeOut("fast", function(){
					$("#response").fadeIn("fast");
					setTimeout(emailSuccess, 2000);
				});
			}else{
				$("#email").attr("disabled","");
				$("#message").attr("disabled","");
				$("#loader").fadeOut("fast");
				alert("An error occurred while trying to send your email. Please check your information and try again.");
			}
		});
	}else{
		alert("Please enter the email address to which you would like to send your notes before continuing.");
	}
}

function emailSuccess(){
	$("#response").fadeOut("slow", function(){
		$("#div_email").hide();
		$("#email").attr("disabled","");
		$("#message").attr("disabled","");
		$("#email_form").show();
		$("#div_notes").show();
		$("#options").show();
	});	
}

/*	Hide the email form and bring up the notes form again
/*-----------------------------------------------------------*/
function cancelEmail(){
	$("#div_email").hide();
	$("#div_notes").show();
	$("#options").show();
	$("#email").val("");
	$("#message").val("");
}

/*	Generate a download-able plain text version of the notes
/*-----------------------------------------------------------*/
function doSave(){
	if($("#input_notes").val().trim() != "" && $("#input_notes").val().trim() != "Enter your notes here..."){
		$("#form_notes").attr("action", "/includes/sermons/save_notes.php");
		$("#form_notes").submit();
	}else{
		alert("Please fill in any notes you would like to print in the available text area before continuing.");	
	}
}

/*	Print the content in the "Take Notes" section
/*-----------------------------------------------------------*/
function doPrint(){
	if($("#input_notes").val().trim() != "" && $("#input_notes").val().trim() != "Enter your notes here..."){
		$("#form_notes").attr("action", "/includes/sermons/print_notes.php");
		$("#form_notes").submit();
	}else{
		alert("Please fill in any notes you would like to print in the available text area before continuing.");	
	}
}



/*	Media Archive functions
/*-----------------------------------------------------------*/
function getSermons(pagenum){
	var campus = $("#tf_campus").val();
	var service = $("#tf_service").val();
	var date = $("#tf_month").val();
	$("#loader").fadeIn();
	$("#ajax_sermons").fadeOut("fast");
	$("#ajax_sermons").load("/includes/sermons/sermon_archive.php", {page:pagenum, group:campus, category:service, month:date}, function(){
		$("#loader").fadeOut();
		$("#ajax_sermons").fadeIn("slow");
	});
}

function initMenus(){
	/*-------------------------------------------------------*/
	/*	PLEASE NOTE:
	/*	The focus() and blur() functions do not currently
	/*	work in WebKit-enabled browsers like Google Chrome and
	/*	Safari, so the click() event is being used in their
	/*	place. If the browser successfully interprets the 
	/*	blur() and focus() events, a custom DOM attribute of
	/*	"focusEnabled" is set and therefore overrides the 
	/*	click() event functionality
	/*-------------------------------------------------------*/
	/*--- Select Campus ---*/
	$("#menu_campus a.selection").click(function(){
		if($("#menu_campus ul").attr("focusEnabled") != "true"){
			$("#menu_campus ul").toggle();
		}
	});
	$("#menu_campus a.selection").focus(function(){
		$("#menu_campus ul").attr("focusEnabled", "true");
		$("#menu_campus ul").show();
	});
	$("#menu_campus a.selection").blur(function(){
		if($("#menu_campus ul").attr("onMenu") == "true"){
			// do nothing
		}else{
			$("#menu_campus ul").hide();
		}
	});
	$("#menu_campus ul").mouseover(function(){
		$(this).attr("onMenu","true");
	});
	$("#menu_campus ul").mouseout(function(){
		$(this).attr("onMenu","false");
	});
	
	
	/*--- Select Service ---*/
	$("#menu_service a.selection").click(function(){
		if($("#menu_service ul").attr("focusEnabled") != "true"){
			$("#menu_service ul").toggle();
		}
	});
	$("#menu_service a.selection").focus(function(){
		$("#menu_service ul").attr("focusEnabled", "true");
		$("#menu_service ul").show();
	});
	$("#menu_service a.selection").blur(function(){
		if($("#menu_service ul").attr("onMenu") == "true"){
			// do nothing
		}else{
			$("#menu_service ul").hide();
		}
	});
	$("#menu_service ul").mouseover(function(){
		$(this).attr("onMenu","true");
	});
	$("#menu_service ul").mouseout(function(){
		$(this).attr("onMenu","false");
	});
	
	
	/*--- Select Month ---*/
	$("#menu_month a.selection").click(function(){
		if($("#menu_month ul").attr("focusEnabled") != "true"){
			$("#menu_month ul").toggle();
		}
	});
	$("#menu_month a.selection").focus(function(){
		$("#menu_month ul").attr("focusEnabled", "true");
		$("#menu_month ul").show();
	});
	$("#menu_month a.selection").blur(function(){
		if($("#menu_month ul").attr("onMenu") == "true"){
			// do nothing
		}else{
			$("#menu_month ul").hide();
		}
	});
	$("#menu_month ul").mouseover(function(){
		$(this).attr("onMenu","true");
	});
	$("#menu_month ul").mouseout(function(){
		$(this).attr("onMenu","false");
	});
}

function findServices(){
	var campus = $("#tf_campus").val();
	$("#menu_service li ul").load("/includes/sermons/sermon_archive.php", {view:"categories", group:campus}, null);	
}

function findMonths(){
	var campus = $("#tf_campus").val();
	var service = $("#tf_service").val();
	$("#menu_month li ul").load("/includes/sermons/sermon_archive.php", {view:"months", group:campus, category:service}, null);	
}

function changeCampus(value, display){
	$("#tf_campus").val(value);
	$("#sel_campus").html(display);
	findServices();
	findMonths();
	getSermons(1);
	$("#menu_campus ul").hide();
}

function changeService(value, display){
	$("#tf_service").val(value);
	$("#sel_service").html(display);
	findMonths();
	getSermons(1);
	$("#menu_service ul").hide();
}

function changeMonth(value, display){
	$("#tf_month").val(value);
	$("#sel_month").html(display);
	getSermons(1);
	$("#menu_month ul").hide();
}
