$(document).ready(function() {

   
   $('ul li:last-child').addClass('last');
   $('ul li:first-child').addClass('first');
   
   // clear and restore search input
   $('input.clickClear').focus(function() {
      startText = $(this).val();
      $(this).val('');
   });
   $('input.clickClear').blur(function() {
      blurText = $(this).val();
      if (blurText == '') {
         $(this).val(startText);
      };
   });
   
   $('#subnav li li a').wrapInner('<span></span>');
   $('#subnav li li li ul').parent().addClass('children');
   
    sermonSort();
	
	$('.s_video a, #mb_video a, a.video').live('click', playVideo);	
	$('.s_listen a, #mb_jukebox a, a.play').live('click', playAudio);			
 
   	$('a.external, #newsletter').attr('target', '_blank');
});

// launch audio player
function playAudio(){
	window.open('http://cpmassets.com/audio-player.php?audio='+$(this).attr('href'), 'audioPlayer', 'width=210,height=30,status=0,toolbar=0'); 
	return false;
}

// launch video player
function playVideo(){
	var $self = $(this);

	if($self.attr('nodeName') !== 'A'){
		if($self.parent().attr('nodeName') == 'A'){
			var $self = $self.parent();
		} else {
			return;
		}
	}
	
	if($self.attr('rel').length > 1){
		var place = $self.attr('href');
	} else {
		var place = 'http://cpmassets.com/video.php?video='+$self.attr('href');
	}
	window.open(place, 'videoPlayer', 'width=640,height=320,scrollbars=0,statusbar=0,address=0');
	return false;
}


// sermon sorting functions
function sermonSort(){
/* SERMON SORTING */		
	// see if there is something in the query string to initially load
	var sort = window.location.hash;
	
	if(sort){
		var parts = sort.split('_');
		var key	= parts[0].replace('#', '');
		var val = parts[1];			
		$('#pagination').css('display','none');
		
		$('#sermon-content').html('').css({height:'300px', background:'url(/_img/ajax-loader.gif) no-repeat center center'});
		$.get('/ajax/sermons.php', {		
			key:key,
			val:val
		},
		function(o){			
			$('#sermon-content').html(o).css({height:'auto', background:'none'});			
			$('#current').hide();
			$('#all-messages').focus();
			Logos.ReferenceTagging.lbsBibleVersion = "NLT";
		    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
		    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
		    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
		    Logos.ReferenceTagging.tag();
		}
		);
		
	}	
	
	$('.sorter').change(function(){
		$('#pagination').css('display', 'none');
		window.location.hash = this.id+'_'+this.value;
		$('#sermon-content').html('').css({height:'300px', background:'url(/_img/ajax-loader.gif) no-repeat center center'});
		if(this.selectedIndex !== 0){
			$.get('/ajax/sermons.php', { 
				'key':this.id,
				'val':this.value			
				}, 
				function(o){				

					//$('#primaryContent > h2').html(response[0]);
					$('#sermon-content').html(o).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 = "NLT";
				    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
				    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
				    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
				    Logos.ReferenceTagging.tag();					
				}
			);
		}
	});		
	/* END SERMON SORTING */
}