$(document).ready(function(){
	$('#search_term').focus(function(){
		$(this).val('');
	});
	$('#ea').focus(function(){
		$(this).val('');
	});
	
	$('#nav li').each(function(){
		$(this).children('ul').parent().addClass('children');
	});
	
	$('#footer form').attr('target', '_blank');
	
	// add slide toggle ability to top level navigation items
	$('#nav li a').click(function(){
		var ul = $(this).parent().children('ul:first');		

		var node = $(ul).attr('nodeName');
		
		// find the first ul child of this link
		if( node.toString() !== 'undefined' ) { 		
			
			$(this).parent('li').addClass('children');
			
			$(ul).slideToggle('slow', function(){			
				if( $(this).parent('li').hasClass('current') ) {					
					$(this).parent('li').removeClass('current');			
				} else {			
					$(this).parent('li').addClass('current');													
				}
			});
			
			return false;
		}			
		
	});
	

	$('a.play').click(function(){
		var id = this.id.split('-')[1];
		var url = 'http://my.ekklesia360.com/Clients/sermonaudioplayer.php?CMSCODE=EKK&siteid=2105&sermonid='+id+'&useSkin=skin_plain.xml&CMS_LINK=http://my.ekklesia360.com';
		window.open(url, 'wimpyMP3player','width=350,height=140,title=blah')
		return false;
	});
	
	
	$('#all-messages').click(function(){
		$('#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]);			
			$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
			$('#current').hide();
			$('.sorter').attr({selectedIndex:0});		
			$('#all-messages').focus();
			$('.sl_video a').click(function(){
				var title = $(this).attr('href');
				waspPopup(title, 450, 300);
				return false;
			});	
		    Logos.ReferenceTagging.lbsBibleVersion = "ESV";
		    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
		    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
		    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
		    Logos.ReferenceTagging.tag();
					
		}
		);
		return false;
	});
	
	$('.sorter').change(function(){
		$('#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-->');
					$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
					$('#current').hide();
					$('.sorter').attr({selectedIndex:0});		
					$('#all-messages').focus();
					$('.sl_video a').click(function(){
						var title = $(this).attr('href');
						waspPopup(this.title, 450, 300);
						return false;
					});					
				    Logos.ReferenceTagging.lbsBibleVersion = "ESV";
				    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
				    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
				    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
				    Logos.ReferenceTagging.tag();					
				}
			);
		}
	});		

	$('#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';
	});	
	
	$('.external').attr('target', '_blank');
});