$(function() {
		
	function playMessage(id)
	{
		var messageRow = $("#" + id);
		
		if (flashPlayerVersion.major >= 9){
			
			if(!messageRow.hasClass("playing")){
				
				$(".message-row").each(function(){
					$(this).removeClass("playing").find(".message-player").empty();
				});
				
				messageRow.addClass("playing");
				
				var soundFile = messageRow.find(".play-message").attr("href");
				var title = messageRow.find(".play-message").html();
				
				var speaker = $(".message-speaker", messageRow).html();
								
				$(".message-player", messageRow).html('<!--[if !IE]> --><object width="500" height="24" type="application/x-shockwave-flash" style="outline: medium none;" data="http://www.trinitylifechurch.org.uk/wp-content/themes/tlc-theme/js/player.swf"><!-- <![endif]--><!--[if IE]><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="500" height="24"><param name="movie" value="http://www.trinitylifechurch.org.uk/wp-content/themes/tlc-theme/js/player.swf" /><!--><!--dgx--><param name="wmode" value="opaque"><param name="menu" value="false"><param name="flashvars" value="autostart=yes&amp;animation=no&amp;remaining=yes&amp;loader=004C7D&amp;soundFile=' + soundFile + '&amp;titles=' + title + '&amp;artists=' + speaker + '&amp;playerID=' + id + '"></object><!-- <![endif]-->');
				
			}
		} else {
			window.location="http://www.trinitylifechurch.org.uk/mp3s/"+id.substring(8)+".mp3";
		}
	}	
	
	
	var flashPlayerVersion = swfobject.getFlashPlayerVersion();
	
	if(window.location.hash) {
		
		var id = window.location.hash;
		id = id.substring(1);
		
		playMessage(id);
	}
	
	/* MESSAGES */
	$(".play-message").click(function(){
		
		var id = $(this).parent().parent().attr("id");
		
		playMessage(id);
		
		return false;
		
	});	
	
	
	$(".message-row").hover(function(){
		$(this).addClass("hover");
	}, function() {
		$(".message-row").removeClass("hover");
	});
	
	/* SLIDESHOW */
	$(".thumb").click(function (){
		$(".thumb").removeClass("selected");
		$(this).addClass("selected");
	});
	
	
	/* FEATURE */
	$("#feature-small a").removeAttr("href");
	
	var currentFeature = 0;
	$("#feature-small a:first-child").addClass("current");
	var featureTimer = setTimeout(function() { changeFeature(1); }, 6000);
	
	
	$("#feature-large a").css({'position' : 'absolute', 'display' : 'none'});
	$("#feature1").css({'display' : 'block'});
	
	function changeFeature(featureNo)
	{
		if(featureNo != currentFeature)
		{
		if(featureNo > 5){ featureNo = 0; }
		
			$("#feature-small" + (currentFeature + 1)).removeClass("current");
			$("#feature-small" + (featureNo + 1)).addClass("current");
			$("#feature" + (currentFeature + 1)).fadeOut();
			$("#feature" + (featureNo + 1)).fadeIn();
			currentFeature = featureNo;
		}
		featureTimer = setTimeout(function() { changeFeature(currentFeature+1); }, 6000);
	}
	
	
	$("#feature-small a").click(function() {
		clearTimeout(featureTimer);
		var featureNo = $("#feature-small a").index(this);
		changeFeature(featureNo);
	});
	
	
	/* NAV */
	$("#nav ul li").hover(function() {
		$("ul", this).stop(true, true).hide().slideDown(200);
	}, function() {
		$("ul", this).stop(true, true).show().fadeOut(200);
	});
	
});

/* Popup Window */
function popup(url, width, height)
{
	newwindow = window.open(url, 'newWindow', 'width='+width+',height='+height);
	if(window.focus) {newwindow.focus() }
	return false;
}
