$(document).ready(function(){		
	
	sermonSort();
	
	$('.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');
   	$('#views li a').click(switchCalTabs);
	$('#months').bind('change', switchCalMonth);
	$('#events-calendar caption a, #calendar caption a').live('click', switchCalendar);
	$('#list-view li a').live('click', switchEventList);
	
	// 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);
      };
   });
   
	$('.tabs a').bind('click', tabSwitcher);
	$('#kids li:last').addClass('last');

});

// sermon sorting functions
function sermonSort(){
	// see if there is something in the query string to initially load
	var sort = window.location.hash;
	
	if(sort && document.body.id == 'sermons'){
		var parts = sort.split('_');
		var key	= parts[0].replace('#', '');
		var val = parts[1];					

		$.get('/ajax/sermons.php', {		
			key:key,
			val:val
		},
		function(o){			
			$('#sermon-content').html(o);
			
			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;
		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);			
					$('#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();					
				}
			);
		}
	});		
}

function switchCalTabs(){
	var div = '#'+$(this).attr('rel');
	$('#events-container div').hide();
	$(div).show();
	$('#views 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 source = $(this).parent().parent().parent().parent();	
	var parts = $(this).attr('href').split('/');
	var month = parts.pop();
	var year  = parts.pop();
	$.get('/ajax/events.php', { month:month, year:year }, function(data){
		source.html(data);
	});
	return false;
}

function switchEventList(){
	var parts = $(this).attr('href').split('/'),
		date_parts = parts.pop().split('-');	
	var month = date_parts.pop();
	var year  = date_parts.pop();

	$.get('/ajax/events.php', { month:month, year:year, view:'list'}, function(data){
		$('#list-view').html(data);
	});	
	return false;
}

function tabSwitcher(){
	var div = $('#'+this.href.split('#')[1]), wrap = $('.right_wrapper');
	wrap.find('.current').removeClass('current');
	div.addClass('current');
	$('.tabs .current').removeClass('current');
	this.parentNode.className = 'current';
	
	return false;
}
