// JavaScript Document

/*---------------------------------------------------*/
/*	Supporting functions for the Sermons section
/*---------------------------------------------------*/

function getSermons(series){
	jQuery('#ajaxbody').fadeOut();
	jQuery('#loader').fadeIn();
	jQuery('#ajaxbody').load("/includes/views/ajax/sermon.ajax.php" , {series: series}, function(response){
		var series = response.split('@@');
		$('#ajaxbody').html(series[0]);
		$('#sermons_header h2').text(series[1]);
		jQuery('#ajaxbody').fadeIn("slow");
		jQuery('#loader2').fadeOut("slow");
	});	
}


	/* load sermon content for the specified sermon record  
	---------------------------------------------------------------------*/
	function loadview(id, slug, view){
		// show ajax loader graphic
		$('#loader_gif').show();
		// determine whether the sermon content is expanded or collapsed
		var sermonbox = $('#'+id);
		if(sermonbox.currentState == null || sermonbox.currentState == 'up'){
			hideall();
			$('#'+id+' .sermonajax .content').load('/includes/views/ajax/sermon.ajax.php', {'display':'detail','slug':slug,'view':view,'id':id}, 
				function(){
					if (view == "video")
						loadvideo(id);
					else if (view == "audio")
						loadaudio(id);
					$('#'+id+' .sermonajax').slideDown();
					$('#loader_gif').hide();
					addCloseClick();
				});
			sermonbox.currentState = "down";
		}else{
			$('#'+id+' .sermonajax .content').fadeOut("fast");
			$('#'+id+' .sermonajax .content').load('/includes/views/ajax/sermon.ajax.php', {'display':'detail','slug':slug,'view':view,'id':id}, 
				function(){
					if (view == "video")
						loadvideo(id);
					else if (view == "audio")
						loadaudio(id);
					$(this).fadeIn("slow");
					$('#loader_gif').hide();
					addCloseClick();
				});
		}
	}
	
	/* load video content for current sermon  
	---------------------------------------------------------------------*/
	function loadvideo(id){
		var video = $('#am_video-'+id).html();
		var rand = Math.floor(Math.random(10000,99999));
		/* Sub Header */
		var fvars = {'MM_ComponentVersion': '1', 'skinName': "Clear_Skin_3", 'streamName': video, 'autoPlay': "true", 'autoRewind': "false"};
		var param = {'wmode':'transparent', 'menu':'false', 'base':'/swf/', 'allowfullscreen':'true'};
		var attrs = {};
		swfobject.embedSWF("/swf/FLVPlayer_Progressive.swf?"+rand,"am_video-"+id,"896","504","9.0.0","/swf/expressInstall.swf",fvars,param,attrs);
	}
	
	/* load audio content for current sermon  
	---------------------------------------------------------------------*/
	function loadaudio(id){
		var audio = $('#am_audio-'+id+' #file').html();
		audio = trim(ltrim(rtrim(audio)));
		var author = "Sermon Title: "+$('#am_audio-'+id+' #title').html();
		var title = "Teacher: "+$('#am_audio-'+id+' #author').html();
		
		AudioPlayer.embed("am_audio-"+id, {  
			soundFile: audio,  
			titles: title,  
			artists: author
		});
	}
	
	/* load full content text for current sermon  
	---------------------------------------------------------------------*/
	function loadcontent(slug, title){
		TB_show(title, "/sermon.ajax.query.php?display=detail&slug="+slug+"&view=content&height=400&width=400", false);
	}
	
	/* show/hide the AJAX loader graphic  
	---------------------------------------------------------------------*/
	/* function ajaxloader(container, show){
		var loader = document.getElementById("ajax-loader");
		try{
			container.removeChild(loader);
		}catch(e){}
		if(show){
			loader = document.createElement("div");
			loader.setAttribute("id", "ajax-loader");
			loader.innerHTML = '<img src="/images/ajax-loader.gif" alt="Loading..." />';
			container.appendChild(loader);
		}
	} */
	
	/* collapse all currently expanded sermon boxes  
	---------------------------------------------------------------------*/
	function hideall(){
		$('#ajaxbody .sermon_box .sermonajax').slideUp("fast", function(){
			jQuery(this).children('.content').html("");
		});
		var ajaxbody = document.getElementById("ajaxbody");
		var sermons = ajaxbody.getElementsByTagName("div");
		for (var i=0; i<sermons.length; i++){
			sermons[i].currentState = "up";
		}
	}
	
	/* load sermon list for the specified grouping  
	---------------------------------------------------------------------*/
	function showbygroup(groupby){
		$('#ajaxbody').fadeOut("fast");
		$('#ajaxbody').load('/sermon.ajax.query.php', {'display':'list','groupby':groupby}, 
			function(){
				$(this).fadeIn("slow");
			});
	}
	
	/* load sermon list for the specified category  
	---------------------------------------------------------------------*/
	function showbycategory(category){
		$('#ajaxbody').fadeOut("fast");
		$('#ajaxbody').load('/sermon.ajax.query.php', {'display':'list','category':category,'groupby':'category'}, 
			function(){
				$(this).fadeIn();
			});
	}

	/* add functionality for the close btn
	---------------------------------------------------------------------*/
	function addCloseClick(){
		jQuery(".sermon_close_btn").click(function () {
			jQuery(this).parents('.sermonajax').slideUp(function() {
				jQuery(this).children('.content').html("");
			});
			return false;
		});	
	}