$(document).ready(function(){	
	/*
	$('#subnav li.current a').hover(function(){
		$(this).animate( { left:'0px' }, { duration:150 });
	});
	*/
	
	$('#news dt:first').css('border-top','none');
	
	$('#subnav li li li.current').children().parent().addClass('children');
	
	
	$('#search_term').focus(function(){ this.value = ''});
	// tab deal
	$('#tabs li a').click(function(){
		var div = this.rel;
		$('#tabs li').attr('className', '');
		$(this).parent().attr('className', 'current');
		
		if(div == 'recent-article'){
			$('#recent-article').fadeIn('fast');
			$('#featured-resources').fadeOut('fast');
		} else {
			$('#featured-resources').removeClass('hidden').fadeIn('fast');
			$('#recent-article').fadeOut('fast');
		}
		return false;
	});
	
	/* SERMON SORTING */		
	// see if there is something in the query string to initially load
	var sort = window.location.hash;
	
	if(sort){
		$('#sermon-content').html('').css({height:'300px'});
		$.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(o).css({height:'auto', background:'none'});			
			$('#current').hide();
			$('#all-messages').focus();
			window.location.hash = 'all';
		}
		);
	}
	
	$('#all-messages').click(function(){
		window.location.hash = 'all';
		$('#sermon-content').html('').css({height:'300px'});
		$.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(o).css({height:'auto'});			
			$('#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();
			
		}
		);
		return false;
	});
	
	$('.sorter').change(function(){
		window.location.hash = this.id+'_'+this.value;
		$('#sermon-content').html('').css({height:'300px'});
		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(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 = "NIV";
				    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
				    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
				    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
				    Logos.ReferenceTagging.tag();
					
				}
			);
		}
	});		
	/* END SERMON SORTING */
	
	$('.play').click(function(){
		var url = 'http://my.ekklesia360.com/Clients/sermonaudioplayer.php?CMSCODE=EKK&siteid=2011&sermonid='+this.id.split('-')[1]+'&useSkin=skin_plain.xml&CMS_LINK=http://my.ekklesia360.com';
		wimpyPopPlayer(url,'wimpyMP3player','width=350,height=140,title=blah');
		return false;		 
	});
	
	$('.sl_listen a').live('click', playAudio);
	$('.sl_video a').live('click', playVideo);
	
	// Luke added: give clickClear class to search input:
   $('input#search_term').removeClass('clearClick').addClass('clickClear');

	// Luke added: for search and e-newsletter fields, dynamic clearing/remembering:
	$('input.clickClear').each(function(){
	   $(this).focus(function() {
         startText = $(this).val();
         $(this).val('');
      });
	   $(this).blur(function() {
         blurText = $(this).val();
         if (blurText == '') {
            $(this).val(startText);
         };
	   });
   });
   	$('.external').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;
}
