document.write('<div id="traildiv"></div>');

function subnav(){
	$('#snav li.current li').parent().addClass('active');
}


function actions(){
	$('.sl_listen a, #mb_jukebox a').bind('click', playAudio);
}

function playAudio(){
	var url = 'http://my.ekklesia360.com/Clients/sermonaudioplayer.php?CMSCODE=EKK&siteid=2974&sermonid='+this.id.split('sermon')[1]+'&useSkin=skin_plain.xml&CMS_LINK=http://my.ekklesia360.com';
	wimpyPopPlayer(url,'wimpyMP3player','width=350,height=140,title=blah');
	return false;		 
};

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

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 = {};
		var attributes = {};
		var the_video = new SWFObject('/_js/player.swf', 'player', '640', '480', '9');
		swfobject.embedSWF("/_js/player.swf", "video"+vid, "540", "405", "9.0.0", "/_js/expressInstall.swf", flashvars, params, attributes);		
		$(this).remove();								
		vid++;			
	});
	

}

$(document).ready(function() {
	actions();
	subnav();
	// give class to empty calendar cells
/*	$('#calendar td').each(function() {
      colSpan = $(this).attr('colspan');
      if (colSpan) {
         $(this).addClass('empty');
      };
   }); */
   $('a.play').click(function(){
		var id = this.id.split('-')[1];
		var url = 'http://my.ekklesia360.com/Clients/sermonaudioplayer.php?CMSCODE=EKK&siteid=2974&sermonid='+id+'&useSkin=skin_plain.xml&CMS_LINK=http://my.ekklesia360.com';
		window.open(url, 'wimpyMP3player','width=350,height=140,title=blah')
		return false;
	});
	embedVideo();
	
	// Slide ministry index - up or down

	$('#guide').toggle(function(){				
		$('#sitemap:hidden').slideDown();
		$(this).addClass('on');
		return false;
	},function(){
		$('#sitemap:visible').slideUp();
		$(this).removeClass('on');		
		return false;
	});
	
	$('#close').click(function(){
		$('#sitemap:visible').slideUp();
		$('#guide').removeClass('on');		
		return false;
	});

	
	/* SERMON SORTING */	
	
	$('#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-->');
			//$('#primaryContent > h2').html(response[0]);
			$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
			$('#current').hide();
			$('#all-messages').focus();
		    Logos.ReferenceTagging.lbsBibleVersion = "ESV";
		    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
		    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
		    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
		    Logos.ReferenceTagging.tag();
		    actions();
			
		}
		);
		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-->');
					//$('#primaryContent > h2').html(response[0]);
					$('#sermon-content').html(response[1]).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 = "ESV";
				    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
				    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
				    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
				    Logos.ReferenceTagging.tag();
				    actions();

				}
			);
		}
	});		
	/* END SERMON SORTING */
	
	$('#articleLists').change(function(){		
		if(this.selectedIndex > 0){
			$('#sermon-content').html('').css({height:'300px', background:'url(/_img/ajax-loader.gif) no-repeat center center'});
		
			var value = $(this).val().toLowerCase();
			$.get('/ajax-articles.php', { groupby:value}, function(o){
				$('#sermon-content').html(o).css({height:'auto', background:'none'});							
				actions();
			});
		}
	});
	// makes styling easier
   $('ul').each(function() {
   $(this).find('li:last-child').addClass('last');
   $(this).find('li:first-child').addClass('first');
   });
	$('.external').attr('target', '_blank');
});
