$(document).ready(function() {
   // makes styling easier
   $('ul').each(function() {
   $(this).find('li:last-child').addClass('last');
   $(this).find('li:first-child').addClass('first');
   });
   $('.monthwrap').each(function() {
      $(this).find('dl:last-child').addClass('last');
   });
   $('.comment:last').addClass('last');
   $('#eventday #content .event:last').addClass('last');
   // 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);
      };
   });
   // add captions to quick links
   $('#quicklinks li a').each(function() {
      $(this).append('<span class="caption">quicklink image</span>');
   });
   // add caption to home banner link (if exists) or paragraph if not
   if ($('#home-banner #rotator').length == 0) {
      if ($('#home-banner a').length == 0) {
         $('#home-banner p').append('<span class="caption">Home Page Banner</span>');
      } else {
         $('#home-banner p a').append('<span class="caption">Home Page Banner</span>');
      }
   }
   // Make external links validate
   $('a.external').attr('target', '_blank');
   // Account for 2-line subnav links
   $('#subnav li li a').each(function() {
      var linkHeight = $(this).height();
      var linkText = $(this).text();
      if (linkHeight >= 30) {
         $(this).addClass('double');
      };
   });
   $('#subnav li li.current.children > a').append('<span>&gt;</span>');
   // Center Pagination
   var pagnWidth = $('#pagination').width()+17;
   var pagnLeft = (762-pagnWidth)/2;
   $('#pagination').css('margin-left', pagnLeft);
   // Event List
   $('#caltabs li a').bind('click', switchCalTabs);
   // Center Category Filter
   var catWidth = $('#cat-filter').width();
   var catLeft = (982-catWidth)/2;
   $('#cat-filter').css('margin-left', catLeft);
   
});

function switchCalTabs(){
	var div = '#'+$(this).attr('rel');
   if (div=="#events-calendar") {
      $('#events-calendar').show();
      $('#list-view').hide();
   } else {
      $('#list-view').show();
      $('#events-calendar').hide();
   };
	$('#caltabs li').removeClass('current');
	$(this).parent().addClass('current');
	return false;
}

$(document).ready(function(){		
	
	sermonSort();
	embedVideo();
	
	$('.sl_watch a, #watch a, a.video').live('click', playVideo);	
	$('.sl_listen a, #listen a, p.play a').live('click', playAudio);		
   
	$('#events-calendar caption a').live('click', switchCalendar);
	
});

// 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(){
	if($(this).attr('rel').length > 1){
		var place = $(this).attr('href');
	} else {
		var place = 'http://cpmassets.com/video.php?video='+$(this).attr('href');
	}
	window.open(place, '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;
	$('.video').each(function(){
		var vurl = $(this).attr('onclick').toString();
		vurl = vurl.split('waspPopup')[1];
		vurl = vurl.split('\',')[0].replace('\'', '').replace('(', '');
		$(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');
		}
		swfobject.embedSWF("http://cpmassets.com/file/media/player.swf", "video"+vid, "620", "440", "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 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;
}

// Cufon
Cufon.replace('#featured h3', { fontFamily: 'vegur-bold' });