var loadedFirst = false;

function playerReady(obj) {
	var idToLookFor = '';
	if (typeof($('.open embed')) != 'undefined') {
		idToLookFor = $('.open embed')[0].id;
	} else if (typeof($('.open object')) != 'undefined') {
		idToLookFor = $('.open object')[0].id;
	}
	if (obj.id == idToLookFor) {
		document.getElementById(idToLookFor).sendEvent('PLAY', 'true');
	}
}

$(function(){

	$('#gallery').gallerify();
	
	// easter egg :)
	$('<div id="worm"></div>').appendTo('#footer_sleeve').toggle(function() {
		$(this).parents('#footer_sleeve').animate({height:'835px'},{queue:false,duration:2000});
		$('html,body').animate({scrollTop:$('#footer_sleeve').offset().top + 865},{queue:false,duration:2000});
	}, function(){
		$(this).parents('#footer_sleeve').animate({height:'70px'},{queue:false,duration:2000});
	});

	// navigation
    lastBlock = $("#page-navigation li.open");
    maxWidth = 717;
    minWidth = 29;

	$("#page-navigation li a.navLink").click(function() {
		
		// for when the FLVs are attached via JS
		
		var stopMovie = function(){
			var player = document.getElementById(this.id);
			player.sendEvent("STOP");
		};
		
		var startMovie = function(){
			var player = document.getElementById(this.id);
			player.sendEvent("PLAY","true");
		};
		
		//we do both because of IE/Firefox differences.  Cover the bases.
		$(".open embed").each(stopMovie);
		$(".open object").each(stopMovie);
		
		thisBlock = $(this).parent();
		$(lastBlock).animate({ width: minWidth + "px" }, { queue: false, duration: 500, easing: "easeOutExpo" }).removeClass('open');
		$(thisBlock).animate({ width: maxWidth + "px" }, { queue: false, duration: 500, easing: "easeOutExpo" }).addClass('open');
		lastBlock = thisBlock;
		
		$("#content-wrapper").load(""+$(this).attr("href")+"?ajax=true");
		
		$(".open embed").each(startMovie);
		$(".open object").each(startMovie);
		
		return false;
	});
		
});




jQuery.fn.gallerify = function() {
	return this.each(function(){
		var images = $('img.thumb',this);
		images.hide().css({opacity:0});
		$(images[0]).show().css({opacity:1});
		
		if (images.length > 1) {
			$(this).after('<ul id="gallery_changer"></ul>');
			var changer = $('+ul',this);
			images.each(function(){
				var numberLink = (images.index(this)+1).toString();
				if (numberLink.length == 1) numberLink = '0' + numberLink;
				$('<li><a href="#">'+numberLink+'</a></li>').click(showImage).appendTo(changer);
			});
			$('li:first',changer).addClass('first current');
		}

		function showImage() {
			$(this).addClass('current').siblings().removeClass('current');
			var clicked = this;
			images.each(function(){
				if ($(this).is(':visible')) {
					$(this).animate({opacity:0},200,function(){
						$(this).hide();
						$(images[$(clicked).parent().children('li').index(clicked)]).css({display:'block'}).animate({opacity:1},200);
					});
				}
			});
			return false;
		}
	});
}