// JavaScript Document

/*-----------------------------------------------------------------------------*/
/*	DETAILS
/*-----------------------------------------------------------------------------*/
function loadMedia(type){
	if(type == 'video'){
		thisMovie("JCC_sermon_player").playVideo();
	}else{
		thisMovie("JCC_sermon_player").playAudio();
	}
}


/*	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("Your email was successfully sent!");
				$("#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..."){
		$("#notes_form").attr("action", "/includes/sermons/save_notes.php");
		$("#notes_form").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..."){
		$("#notes_form").attr("action", "/includes/sermons/print_notes.php");
		$("#notes_form").submit();
	}else{
		alert("Please fill in any notes you would like to print in the available text area before continuing.");	
	}
}

/*-----------------------------------------------------------------------------*/
/*	SERMONS
/*-----------------------------------------------------------------------------*/
function getSermons(page, asfeatures){
	//var category = $('#tf_ctg').val().trim();
	//var month = $('#tf_month').val().trim();
	var category = "";
	var month = "";
	page = (page != null && isNumeric(page)) ? page : 1;
	asfeatures = (asfeatures != null ) ? asfeatures : "";
	$('#sermon_loader').fadeIn();
	$('#ajax_sermons').load('/includes/sermons/sermon.php?page='+page, {"category":category, "month":month, "find_features":asfeatures}, function(){
		if($('#pagination a').length > 0){
			$('#pagination a').each(function(i,elem){
				var href = $(elem).attr('href');
				if(href.search('page') != -1){
					var page = href.replace('?page=','');
					$(elem).attr('href','javascript:getSermons('+page+');');
				}
			});
		}
		$('#sermon_loader').fadeOut();
		//resizeContent();
	});
}


function getSeries(seriesID) {
	$('#sermon_loader').fadeIn();
	$('#ajax_sermons').load('/includes/sermons/sermon.php', {view:'series', "currentSeries":seriesID}, function(){
		
		
		$("a.audioPlayer").click(function(){
			var type = "audio";
			var slug = $(this).attr("rel");
			PlayMedia(type, slug);
			return false;
		});
		$("a.videoPlayer").click(function(){
			var type = "video";
			var slug = $(this).attr("rel");
			PlayMedia(type, slug);
			return false;
			alert("false");
		});
		
		$('#sermon_loader').fadeOut();
		//resizeContent();
	});
}

var previousImage = "";

function PlayMedia(type, $videoslug){
	
	previousImage = $("#flashsermonplayer").html();
	
	var autoVideo = "false";
	var autoAudio = "false";
	switch(type){
		case "audio":
			autoAudio = "true";
			break;
		case "video":
			autoVideo = "true";
		break;
	}
	
	var flashvars = {vidfile:"/xml/NFW_video_player.php?v="+$videoslug, autoPlayVideo:autoVideo, autoPlayAudio:autoAudio, showPlayButtons:"false"};
	var params = {menu: "false", wmode:"opaque", bgcolor:"#000000", allowfullscreen:"true"};
	var attributes = {};
	swfobject.embedSWF("/swf/NFW_sermonplayer.swf", "sermon_embed", "576", "324", "9.0.0","/swf/expressInstall.swf", flashvars, params, attributes);
	
	$.scrollTo("#flashsermonplayer", 800);
}

function resetImage(){
	//$("#flashsermonplayer").html('<div id="sermon_embed" style="visibility:visible;"><img src="/images/sermon_series.jpg" width="576" height="324"></div>');
	$("#flashsermonplayer").html(previousImage);
}



function initMenus(){
	$('#menu_ctg li ul').load('/includes/sermons/sermon_archive.php', {view:'categories'}, function(){ enableMenu('menu_ctg'); });
	$('#menu_month li ul').load('/includes/sermons/sermon_archive.php', {view:'months'}, function(){ enableMenu('menu_month'); });
}

function changeCategory(target, value){
	$('#tf_ctg').val(value);
	$('#sel_ctg').html($(target).text());
	getSermons();
	$('#menu_ctg ul').hide();
}

function changeMonth(target, value){
	$('#tf_month').val(value);
	$('#sel_month').html($(target).text());
	getSermons();
	$('#menu_month ul').hide();
}


/*-----------------------------------------------------------------------------*/
/*	ARCHIVE
/*-----------------------------------------------------------------------------*/
var totalSeries = 0;
function getArchive(viewby) {
	$('#sermon_loader').fadeIn();
		
	$('#ajax_sermons').load('/includes/sermons/sermon_archive.php', {"view":viewby}, function(){
		//getArchiveSeries();
		$('#sermon_loader').fadeOut();
		//resizeContent();
		totalSeries = $('.ajaxseriesloader').length;
		setTimeout(getArchiveSeries, 2000);
	});
}

function getArchiveSeries() {
	var target = $(".ajaxseriesloader:first");
	var loadcontent = $(".ajaxseriesloader:first .serieslist");
	var strid = target.attr("id");
	var parts = strid.split("_");
	var id = parts[1];
	target.attr("class", "ajaxseriesprocessing");
	loadcontent.load("/includes/sermons/sermon_archive.php", {'id':id}, function(){
		var $el = $(".ajaxseriesprocessing");
		$el.hide();
		$el.attr("class", "ajaxdoneloading");
		//$el.css("visibility", "hidden");
		//$el.animate({visibility:"visible"}, 2000);
		$el.fadeIn(1000);
		if(totalSeries > 1) {
			setTimeout(getArchiveSeries, 1000);
			totalSeries--;
		}
		
	});
}


















