// jQuery by Jonathan Diba, inspired by Tim Van Damme, modified by Glenn Hitchcock

jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	
	return this;
};

$(window).load(function() {
    var current = window.location.hash;
		if(current == "#play") {
			$(this).delay(3000,function(){
			    $("#unityplayer").show();
			});
			$(".pagetitle").hide(function() {
				$(".pagetitle").css("background", "url(images/navitext.png) 0 -22px no-repeat").fadeIn('slow');
			});
		} 
		if(current == "#dlog") {
    		$("#unityplayer").hide();
			$(".pagetitle").hide(function() {
				$(".pagetitle").css("background", "url(images/navitext.png) 0 -44px no-repeat").fadeIn('slow');
			});
    	}
    	else {
    		$(".pagetitle").hide(function() {
				$(".pagetitle").css("background", "url(images/navitext.png) 0 0 no-repeat").fadeIn('slow');
			});
		}
}); 

$('.navigation').each(function () {
	var $links = $(this).find('a'),
		panelIds = $links.map(function() { return this.hash; }).get().join(","),
		$panels = $(panelIds),
		$panelwrapper = $panels.filter(':first').parent(),
		delay = 750,
		heightOffset = 0;
		
	$panels.hide();  
		
	$links.click(function () {
		var link = this, 
			$link = $(this);
	
		if ($link.is('.selected')) {
			return false;
		}
	
		$links.removeClass('selected');
		$link.addClass('selected');
	
		document.title = 'Magnetic Butterfly by Trent Polack - ' + $link.text();
	  
		if ($.support.opacity) {
			$panels.stop().animate({opacity: 0 }, delay);
		}
	      
	    $(".home").click(function() {
			$(".pagetitle").fadeOut('slow', function() {
				$(".pagetitle").css("background", "url(images/navitext.png) 0 0 no-repeat").fadeIn('slow');
			});
			
			$("#unityplayer").hide();
		});

    	$(".play").click(function() {
			$(".pagetitle").fadeOut('slow', function() {
				$(".pagetitle").css("background", "url(images/navitext.png) 0 -22px no-repeat").fadeIn('slow');
			});
	
			$(this).delay(3000,function(){
			    $("#unityplayer").show();
			});
		});
		
		$(".dlog").click(function() {
			$(".pagetitle").fadeOut('slow', function() {
				$(".pagetitle").css("background", "url(images/navitext.png) 0 -44px no-repeat").fadeIn('slow');
			});
			
			$("#unityplayer").hide();
		});
		
		$panelwrapper.stop().animate({
			height: 0
			}, delay, function () {
				var height = $panels.hide()
					.filter(link.hash)
					.css('opacity', 1).show()
					.height() + heightOffset;
	
		$panelwrapper.animate({
			height: height
			}, delay);
		});
		
	});

$links.filter(window.location.hash ? '[hash=' + window.location.hash + ']' : ':first').click();

});