// JavaScript Document

var isArchive = false;
var ajaxURL = '/includes/sermons/sermons-coverflow.php';
var siteID = '290';
var domain = '';

/*	SERMON LIST
/*----------------------------------------------------------*/
function getSermons(series){
	$('#ajax_sermon').fadeOut('fast');
	$('#ajax_sermonlist').fadeOut('fast', function(){
		$('#ajax_sermonlist').load(ajaxURL, {'series':series}, function(response){
			var series = response.split('@@');
			$('#ajax_sermonlist').html(series[0]);
			$('#series_title').text(series[1]);
			initSermonList();
			$('#ajax_sermonlist').fadeIn();
		});
	});
}

function initSermonList(){
	var browser = getBrowser();
	if(browser['name'] == 'ie' && browser['version'] == '6'){
		$('.sermon_item').hover(
			function(){
				$(this).attr('class', $(this).attr('class') + ' sermon_item_hover');	
			},
			function(){
				$(this).attr('class', $(this).attr('class').replace(' sermon_item_hover',''));	
			}
		);
	}
	$('.sermon_item').click(function(){
		var slug = $(this).find('.slug').html();
		if(isArchive){
			window.location = "/sermon/"+slug+"/";
		}else{
			loadSermon(slug);
		}
	});
	
	if($('#pagination').length > 0){
		$('#pagination a').each(function(i,elem){
			if($(elem).attr('id') != "off"){
				var href = $(elem).attr('href');
				href = href.replace(webroot + uri, '');
				var page = href.replace('?page=','');
				$(elem).attr('href','#');
				$(elem).unbind('click');
				$(elem).click(function(){ viewPage(page); return false; });
			}
		});
	}
}

var ITEM_WIDTH = 0;
var CURRENT_POS = 0;
var MAX_POS = 0;

/*-------------------------------------------------------------*/
/* Initializes "sliding" functionality of product sections
/*-------------------------------------------------------------*/
function initSlider(){
	var totalItems = $("#series_container .series").length;
	ITEM_WIDTH = $("#series_container .series:first").outerWidth();
	var containerWidth = ITEM_WIDTH * totalItems;
	$("#series_container").css("width", containerWidth);
	if(totalItems > 2){
		MAX_POS = totalItems - 2;
		$("#btn_next").click(function(){ doSlide("next"); });
	}else{
		$("#btn_prev").hide();	
		$("#btn_next").hide();
	}
	
	$("#series_container .series").each(function(i,elem){
		$(this).hover(
			function(){
				$(this).attr('class', 'series series_hover');
			},
			function(){
				$(this).attr('class', 'series');
			}
		);
	});
}

/*-------------------------------------------------------------*/
/* Do the actual slide (backwards or forwards)
/*-------------------------------------------------------------*/
function doSlide(direction){
	if(direction == "prev"){
		CURRENT_POS--;
		$("#series_container").animate({"left": "+="+ITEM_WIDTH});
		$("#btn_next").unbind("click");
		$("#btn_next").click(function(){ doSlide("next"); });	
		$("#btn_next").attr("class", "");
		if(CURRENT_POS == 0){
			$("#btn_prev").unbind("click");	
			$("#btn_prev").attr("class", "disabled");
		}
	}else{
		CURRENT_POS++;
		$("#series_container").animate({"left": "-="+ITEM_WIDTH});
		$("#btn_prev").unbind("click");
		$("#btn_prev").click(function(){ doSlide("prev"); });	
		$("#btn_prev").attr("class", "");
		if(CURRENT_POS == MAX_POS){
			$("#btn_next").unbind("click");
			$("#btn_next").attr("class", "disabled");
		}
	}
}

/*	SERMON DETAILS
/*----------------------------------------------------------*/
function loadSermon(slug){
	$('#ajax_sermonlist').fadeOut('fast', function(){
		$('#ajax_sermon').load(ajaxURL, {'slug':slug}, function(){
			$('#ajax_sermon').fadeIn('medium', function(){
				var audioLoaded = loadAudio();
				//if(!audioLoaded) loadVideo();
				$('#ajax_sermon .media_icons .icon_video a').attr('title','Watch');
				clearClick();
			});
		});
	});
}

function loadAudio(){
	var audio = $('#ap_file').val() || "";
	if(audio){
		$('#flash_player').replaceWith('<div id="flash_player"></div>');
		var arrAudio = audio.split('&');
		for(var i=0; i<arrAudio.length; i++){
			var next = arrAudio[i];
			var tmp = next.split('=');
			if(tmp[0] && tmp[1] && tmp[0] == 'url'){
				audio = tmp[1].replace('http://'+domain, '');
				break;
			}
		}
		var title = $('#ap_title').val() || "";
		var artist = $('#ap_artist').val() || "";
		init_player();
		play_file('flash_player', audio, title, artist);
		return true;
	}else{
		return false;
	}
}

function loadVideo(){
	var videoflash = $('#sermon_details #videoflash').val() || "";
	var video = (videoflash) ? videoflash : $('#sermon_details #video').val();
	if(video){
		$('#flash_player').replaceWith('<div id="flash_player"></div>');
		video = video.replace('http://'+domain,'');
		video = video.replace('.flv','');
		var soVideo = new SWFObject("/swf/FLVPlayer_Progressive.swf", "sermon_video", "244", "168", "8", "#000000");
		soVideo.addParam("base", "/swf/");
		soVideo.addParam("wmode", "opaque");
		soVideo.addParam("quality", "high");
		soVideo.addParam("scale", "noscale");
		soVideo.addParam("salign", "lt");
		soVideo.addParam("FlashVars", "&amp;MM_ComponentVersion=1&amp;skinName=Corona_Skin_2&amp;streamName="+video+"&amp;autoPlay=true&amp;autoRewind=false&startVolume=100");
		var success = soVideo.write("flash_player");
		//console.log(success);
		return true;
	}else{
		return false;
	}
}

function showSermonList(){
	$('#ajax_sermon').fadeOut('fast', function(){
		$('#flash_player').remove();
		$('#ajax_sermonlist').fadeIn();
	});	
}


/*	SEARCH, CATEGORIES, PAGING
/*----------------------------------------------------------*/
function searchSermons(){
	var keywords = $('#searchterm').val().trim();
	$('#filter_ctg').val('');
	$('#menu_ctg li a.selection').html('All Categories');
	$('#ajax_sermon').fadeOut('fast');
	$('#sermons_loader').show();
	$('#ajax_sermonlist').fadeOut('fast', function(){
		$('#ajax_sermonlist').load(ajaxURL, {'keywords':keywords}, function(response){
			$('#series_title').text('Search Results');
			initSermonList();
			$('#ajax_sermonlist').fadeIn();
			$('#sermons_loader').fadeOut();
		});
	});
}

function initSermonSearch(){
	$('#searchterm').keyup(function(event){
		if(isEnterKey(event)) searchSermons();
	});
}

function loadCategories(series){
	$('#menu_ctg ul').load(ajaxURL, {'load_categories':'true'});
}

function viewCategory(target, ctg){
	var display = $(target).text().trim();
	$('#filter_ctg').val(ctg);
	$('#searchterm').val('Search');
	$('#menu_ctg li a.selection').html(display);
	$('#menu_ctg ul').hide();
	$('#ajax_sermon').fadeOut('fast');
	$('#sermons_loader').show();
	$('#ajax_sermonlist').fadeOut('fast', function(){
		$('#ajax_sermonlist').load(ajaxURL, {'category':ctg, 'display':display}, function(response){
			$('#series_title').text(display);
			initSermonList();
			$('#ajax_sermonlist').fadeIn();
			$('#sermons_loader').fadeOut();
		});
	});
}

function viewPage(num){
	var ctg = $('#filter_ctg').val();
	var display = $('#menu_ctg li a.selection').text();
	$('#sermons_loader').show();
	$('#ajax_sermonlist').fadeOut('fast', function(){
		$('#ajax_sermonlist').load(ajaxURL+'?page='+num, {'category':ctg, 'display':display}, function(response){
			//$('#series_title').text(display);
			initSermonList();
			$('#ajax_sermonlist').fadeIn();
			$('#sermons_loader').fadeOut();
		});
	});
}


/*	SERMON DETAILS
/*----------------------------------------------------------*/

function showTab(tab){
	if(tab == 'details'){
		$('#sermon_notes').hide();
		$('#tab_notes').attr('class','');
		$('#sermon_details').show();
		$('#tab_details').attr('class','current');
	}else{
		$('#sermon_details').hide();
		$('#tab_details').attr('class','');
		$('#sermon_notes').show();
		$('#tab_notes').attr('class','current');
	}
}

/*	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 save 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.");	
	}
}
