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

function actions(){
	$('.sl_listen a, #mb_jukebox a, .play').bind('click', playAudio);
	$('.sl_video a, #mb_video a, .popup-video').bind('click', playVideo);	
}

function playAudio(){
	var url = 'http://my.ekklesia360.com/Clients/sermonaudioplayer.php?CMSCODE=EKK&siteid=2805&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;
}

$(document).ready(function() {
	actions();
	$('#header form').attr('target', '_blank');
	$('#header p a:last, #submenu a:last').css('background', 'none');	

	
	// Video Embedding
	var vid = 1;
	$('.video').each(function(){
		var vurl = $(this).attr('onclick');
		if(vurl){ 
			vurl = vurl.toString();
			vurl = vurl.split('waspPopup')[1];
			vurl = vurl.split('\',')[0].replace('\'', '').replace('(', '');
		} else {
			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');
		}
		swfobject.embedSWF("/_js/player.swf", "video"+vid, "620", "440", "9.0.0", "/_js/expressInstall.swf", flashvars, params, attributes);		
		$(this).remove();								
		vid++;			
	});

   	//$('#search input#search_term').addClass('clickClear').val('search this site');
   	$('input.clickClear').focus(function() {
		startText = $(this).val();
   	   	$(this).val('');
   	});
   	
   	$('input.clickClear').blur(function() {
   	   blurText = $(this).val();
   	   if (blurText == '') {
   	      $(this).val(startText);
   	   };
   	});
	
	/* Home page tabs */
	$('#branchnav li:first').addClass('current');   	
	$('#branchnav a').click(function(){
		var div = $(this).attr('href');
		
		// show the click slug
		$('.branch-text').hide();
		$('#branchnav li').removeClass('current');
		$(this).parent().addClass('current');
		$(div).show();
		return false;
	});
	/* end Home page tabs */
	
   	$('#home #next-events .event:last').addClass('last');
   
	
	/* SERMON SORTING */		
	
	$('#all-messages').click(function(){
		window.location.hash = 'all';
		$('#sermon-content').html('').css({height:'300px', background:'url(/_img/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();
		    Logos.ReferenceTagging.lbsBibleVersion = "NIV";
		    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
		    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
		    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
		    Logos.ReferenceTagging.tag();
			actions();	
		}
		);
		return false;
	});
	
	$('.sorter').change(function(){
		window.location.hash = this.id+'_'+this.value;
		if(this.selectedIndex !== 0){
			$('#sermon-content').html('').css({height:'300px', background:'url(/_img/ajax-loader.gif) no-repeat center center'});
			$.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 })		
				    Logos.ReferenceTagging.lbsBibleVersion = "NIV";
				    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();
			});
		}
	});
	
	$('.external').attr('target', '_blank');
	
});
