$(document).ready(function(){		

// add id to inner sub navs
jQuery("#secondaryNav ul:first").attr("id","subnav");

// sub nav select hack
if(document.location.href.indexOf("/our-systems/design-philosophies") != -1){
jQuery("#navsub_974503_568206 li:first").addClass("selected");
}

if(document.location.href.indexOf("/what-we-do/5-step-process") != -1){
jQuery("#navsub_974701_568272 li:first").addClass("selected");
}

if(document.location.href.indexOf("/what-we-do/") != -1){
jQuery("#nav_896982 li:nth-child(2)").addClass("selected");
}

if(document.location.href.indexOf("/our-systems/") != -1){
jQuery("#nav_896982 li:nth-child(3)").addClass("selected");
}

if(document.location.href.indexOf("/about/") != -1){
jQuery("#nav_896982 li:nth-child(5)").addClass("selected");
}

if(document.location.href.indexOf("_catalog_") != -1 || document.location.href.indexOf("_product_") != -1){
jQuery("#nav_896982 li:nth-child(4)").addClass("selected");
}



	
	// sermonSort();
	// embedVideo();
	
	$('.sl_video a, #mb_video a, a.video').live('click', playVideo);	
	$('.sl_listen a, #mb_jukebox a, a.play').live('click', playAudio);		
   
   	$('.external').attr('target', '_blank');
   	$('#tabs li a').bind('click', switchCalTabs);
	$('#months').bind('change', switchCalMonth);
	$('#events-calendar caption a').live('click', switchCalendar);
	$('#list-view li a').live('click', switchEventList);
	
	
	
	// latest blog post 1
	
			$("#blog-post-container .post:nth-child(1)").each(function(){
			
				var $postTitle = $(this).find("h3:first").text();
				var $postLink = $(this).find("a").attr("href");
				var $postBody = $(this).find(".postAction").text();
				var $postBody = $postBody.substr(0,100);
				
				$postHtml = "<div class='post'><h4><a href='"+ $postLink + "'>" + $postTitle + "</a></h4><p class='summary'>" + $postBody + "...</p></div>";	
			
		
				$(".latestBlogEntry").html($postHtml);
				
				//$(".postText").truncatable({	limit: 100, more: '' }); 
			
			
			});
			
		
	
			// latest blog post 2
			
			$("#blog-post-container .post:last").each(function(){
			
				var $postTitle = $(this).find("h3:first").text();
				var $postLink = $(this).find("a").attr("href");
				var $postBody = $(this).find(".postAction").text();
				var $postBody = $postBody.substr(0,100);
				
				$postHtml = "<div class='post'><h4><a href='"+ $postLink + "'>" + $postTitle + "</a></h4><p class='summary'>" + $postBody + "...</p></div>";	
			
		
				$(".latestBlogEntry2").html($postHtml);
				
				//$(".postText").truncatable({	limit: 100, more: '' }); 
			
			
			});
	
	
});

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

// launch video player
function playVideo(){
	window.open('/file/media/video.php?video='+$(this).attr('href'), 'videoPlayer', 'width=640,height=320,scrollbars=0,statusbar=0,address=0');
	return false;
}

// embed video player in page
function embedVideo(){
	// Video Embedding
	var vid = 1;
	var body = $('body').attr('id');
	
	$('.video').each(function(){
		var vurl = $(this).attr('href');
		$(this).before('<div id="video'+vid+'">'+vurl+'</div>');		
		$(this).remove();
		
		var flashvars = { 
			file: vurl,
			autostart:'false',
			frontcolor: 'ffffff',
			lightcolor: 'cc9900',
			skin: 'http://www.longtailvideo.com/jw/upload/overlay.swf',
			controlbar: 'over'
		};
		
		var params = { wmode:'transparent' };
		var attributes = {  };
		
		if($(this).find('img').attr('src')){
			flashvars.image = $(this).find('img').attr('src');
		}
		var width = (body == 'home') ? 431 : 620;
		var height = (body == 'home') ? 266 : 440;
		swfobject.embedSWF("/file/media/player.swf", "video"+vid, width, height, "9.0.0", "/file/media/expressInstall.swf", flashvars, params, attributes);		
		
		$(this).remove();								
		vid++;			
	});
}

// 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];			
	
		
		$('#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 = "NIV";
		    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
		    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
		    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
		    Logos.ReferenceTagging.tag();
		}
		);		
	}	
	
	$('.sorter').change(function(){
		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 = "NIV";
				    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
				    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
				    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
				    Logos.ReferenceTagging.tag();					
				}
			);
		}
	});		
	/* END SERMON SORTING */
}

function switchCalTabs(){
	var div = '#'+$(this).attr('rel');
	$('#events-container div').hide();
	$(div).show();
	$('#tabs li').removeClass('current');
	$(this).parent().addClass('current');
	return false;
}

function switchCalMonth(){
	var month = $(this).val();
	$.get('/ajax/events.php', { month:month }, function(data){
		$('#events-calendar').html(data);
	});
}

function switchCalendar(){
	var parts = $(this).attr('href').split('/');
	var month = parts.pop();
	var year  = parts.pop();
	$.get('/ajax/events.php', { month:month, year:year }, function(data){
		$('#events-calendar').html(data);
	});
	return false;
}

function switchEventList(){
	var parts = $(this).attr('href').split('/');
	var month = parts.pop();
	var year  = parts.pop();
	$.get('/ajax/events.php', { month:month, year:year, view:'list'}, function(data){
		$('#list-view').html(data);
	});
	return false;
}
