$(document).ready(function(){
	// sermon sorting event binding
	actions();
	
	// sermon sorting
	$('#all-messages').click(function(){
		window.location.hash = 'all';
		$('#sermon-content').html('').css({height:'300px', background:'url(/images/ajax-loader.gif) no-repeat center center'});
		$.get('/ajax-sermons.php', {
			'groupby':'all',
			'key':'current',
			'val':'current'
		},
		function(o){
			var response = o.split('<!--split-->');
			$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
			$('#current').hide();
			$('#all-messages').focus();
		    Logos.ReferenceTagging.lbsBibleVersion = "NIV";
		    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
		    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
		    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
		    Logos.ReferenceTagging.tag();
			actions();
		}
		);
		return false;
	});
	
	$('.sorter').change(function(){
		window.location.hash = this.id+'_'+this.value;
		$('#sermon-content').html('').css({height:'300px', background:'url(/images/ajax-loader.gif) no-repeat center center'});
		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]);
					$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
					$('#current').hide();					
					
					// reset sorter selects to first value
					$('.sorter').attr({ selectedIndex:0 })		
					$('#all-messages').focus().css('outline', 'none');
				    Logos.ReferenceTagging.lbsBibleVersion = "NIV";
				    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
				    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
				    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
				    Logos.ReferenceTagging.tag();
				    actions()					
				}
			);
		}
	});		
	
	
	// make first tab the active one
	$('#home-nav li:first').addClass('first').children('a').addClass('current');
	
	// get the first divs rel
	$('#'+$('#home-nav a:first').attr('rel')+'-tab').css('display', 'block').removeClass('hidden');
	

	// home page tab switcher	
	$('#home-nav a').click(function(){
		// remove current class from current tab
		$('#home-nav .current').removeClass('current');
		
		// set div = desired tab
		var div = '#'+$(this).attr('rel')+'-tab';
		
		// hide visible content
		$('#tabbed-content div:visible').fadeOut('slow', function(){
			// fade in selected content
			$(div+', '+div+' div').fadeIn('slow').removeClass('hidden');	
		});
		
		
		// add current class to click tab
		$(this).addClass('current');
		return false;
	});
	
	// article sorting
	$('#articleLists').change(function() {
		if(this.selectedIndex == 1) window.location = '/mcms_page_sermonlist_sort.php?groupby=series';
		if(this.selectedIndex == 2) window.location = '/mcms_page_sermonlist_sort.php?groupby=category';
		if(this.selectedIndex == 3) window.location = '/mcms_page_sermonlist_sort.php?groupby=month';
		if(this.selectedIndex == 4) window.location = '/mcms_page_sermonlist_sort.php?groupby=preacher';
	});
	
	// open in new window
	$('.external').attr('target', '_blank');
	
});

function actions(){
	$('.sl_listen a, a.play, #mb_jukebox a').bind('click', playAudio);
	$('.sl_video a').bind('click', playVideo);
}

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

function playVideo(){
	waspPopup($(this).attr('href'), 320, 240);
	return false;
}