document.write('<div id="traildiv"></div>');

$(document).ready(function(){	
	$('#snav li.current ul').parent().addClass('active');
	
	// shelf slide down
	$('#guide').click(function(){
		var self = $(this);
		$('#sitemap').slideToggle('fast', function(){
			if($(this).is(':visible')){
				self.addClass('on');
			} else {
				self.removeClass('on');
			}
		});
		return false;
	});
	
	$('#close').click(function(){
		$('#sitemap').slideUp('fast');
		$('#guide').removeClass('on');
		return false;
	});
		
	$('#articleLists').change(function(){
		if(this.selectedIndex == 1) window.location = '/mcms_page_articlelist_sort.php?groupby=series';
		if(this.selectedIndex == 2) window.location = '/mcms_page_articlelist_sort.php?groupby=category';
		if(this.selectedIndex == 3) window.location = '/mcms_page_articlelist_sort.php?groupby=month';
		if(this.selectedIndex == 4) window.location = '/mcms_page_articlelist_sort.php?groupby=author';		
	});
	
	$('#all-messages').click(function(){
		$('#sermon-content').css({
			background:'url(/images/ajax-loader.gif) no-repeat center center',
			height:'200px'
		}).html('');
		
		$.get('/ajax-sermons.php', {
			'groupby':'all',
			'key':'current',
			'val':'current'
		},
		function(o){
			var response = o.split('<!--split-->');
			//$('#primaryContent > h2').html(response[0]);
			$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
			$('#current').hide();
			$('.sorter').attr({selectedIndex:0});		
    		Logos.ReferenceTagging.lbsBibleVersion = "ESV";
    		Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
    		Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
    		Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
    		Logos.ReferenceTagging.tag();
    		mediaOptions();
				
		}
		);
		return false;
	});
	
	$('.sorter').change(function(){
		$('#sermon-content').css({
			background:'url(/images/ajax-loader.gif) no-repeat center center',
			height:'200px'
		}).html('');
		if(this.selectedIndex != 0){
			$.get('/ajax-sermons.php', { 
				'groupby':this.id,
				'key':this.id,
				'val':this.value			
				}, 
				function(o){
					var response = o.split('<!--split-->');
					//$('#primaryContent > h2').html(response[0]);
					if(response[1] == '') {
						$('#sermon-content').html('<p>No sermons could be found for the selected filtering option.</p>')
						.css({
							height:'auto',
							background:'none'
						}).focus();
					} else {
						$('#sermon-content').html(response[1])
						.css({
							height:'auto',
							background:'none'
						});			
					}
					$('#current').addClass('hidden');					
					$('.sorter').attr({ selectedIndex:0});
					$('#all-messages').focus().css({outline:'none'});					
    				Logos.ReferenceTagging.lbsBibleVersion = "ESV";
	    			Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
	    			Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
	    			Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
	    			Logos.ReferenceTagging.tag();
	    			mediaOptions();
				}
			);
		}
	});
	
	// launch links with class external in new window
	$('.external').attr('target', '_blank');
	
	// on load, setup event listeners for media related options
	mediaOptions();		
});

function mediaOptions(){
	$('.sl_listen a, a.play').bind('click', playAudio);
	$('#mb_video a, .sl_video a').bind('click', watchVideo);
}

/**
* Launch audio player
*/
function playAudio(){
	var id = $(this).attr('id').split('sermon')[1];
	var url = 'http://my.ekklesia360.com/Clients/sermonaudioplayer.php?CMSCODE=EKK&siteid=2473&sermonid='+id+'&useSkin=skin_plain.xml&CMS_LINK=http://my.ekklesia360.com';
	window.open(url, 'wimpyMP3player','width=350,height=140,title=blah');
	return false;
}

/**
* Launch video player based on HREF of link clicked
*/
function watchVideo(){
	waspPopup($(this).attr('href'), 320, 240);
	return false;
}


