// JavaScript Document

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

	/* 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/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/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 soVideo = new SWFObject("/swf/FLVPlayer_Progressive.swf", "AM_Video", "768", "432", "8", "#000000", true);
		soVideo.addParam("base","/swf/");
		soVideo.addParam("menu","false");
		soVideo.addVariable("MM_ComponentVersion","1");
		soVideo.addVariable("skinName","Clear_Skin_3");
		soVideo.addVariable("streamName",video);
		soVideo.addVariable("autoPlay","true");
		soVideo.addVariable("autoRewind","false");
		soVideo.write("am_video-"+id);
	}
	
	/* 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 = "Preacher: "+$('#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;
		});	
	}