var $thumbs = $("#thumbs");
var loading = '<p id="loading"><img src="/images/loading.gif" alt="Loading..." class="loading" /> loading&hellip;</p>';

$(document).ready(function() {
	$("#mainNavigation ul ul, #mainNavigation ul dl, #loading, #movPlayer, #prev, #photoContainer img").hide();
	
	// link functions
	alinks();
	
	// fix last thumbnail margin
	$("#thumbs ul, #mainNavigation ul ul:visible").find("li:nth-child(3n)").addClass("n-fix");
	
	// sync rollovers
	$("#mainNavigation ul ul li a").hover(function() {
		var x = $(this).parent("li").index();
		$("#thumbs ul li:eq("+x+")").animate({opacity: .8}, 150);
	}, function() {
		var y = $(this).parent("li").index();
		$("#thumbs ul li:eq("+y+")").animate({opacity: 1}, 150);
	});
	
	$("#thumbs a").hover(function() {
		$(this).animate({opacity: .8}, 150);
		var x = $(this).parent("li").index();
		$("#mainNavigation ul ul li:eq("+x+")").addClass("active");
	}, function() {
		$(this).animate({opacity: 1}, 150);
		var y = $(this).parent("li").index();
		$("#mainNavigation ul ul li:eq("+y+")").removeClass("active");
	});
	
	// hide credits
	$("#credits").hide();
	
	// load movie
	$(".play a").click(function(e) {
		e.preventDefault();
		// get height
		var ht = $(this).data("height");
		var ind = $(this).parent("li").index();
		// set navigation class
		$(".play li").removeClass("playing");
		$(".play").each(function() {
			$(this).find("li:eq("+ind+")").addClass("playing");
		});
		var movie = this.href;
		var title = $(this).attr("title");
		var dir = $(this).data("director");
		$("#credits dt").text(title);
		$("#credits dd").text("Director: " + dir);
		$("#credits").css("visibility", "visible");
		$("#credits:hidden").show();
		loadClip(movie, ht);
	});
	
	$("a.reel").click(function(e) {
		e.preventDefault();
		$("li").removeClass("active");
		var me = $(this).parent().attr("class");
		$("li." + me).addClass("active");
		$(this).parent().addClass("active");
		playReel();
	});
	
	// auto play from active clip
	$("#loading").hide();
	$("#movPlayer").live('qt_ended', function() {
		$("#credits").css("visibility", "hidden");
		if ($("#mainNavigation ul ul li.playing").is(":last-child")) {
			$("#credits").height(0);
			$("#movie").empty().animate({
				height: 0
			});
		} else {
				$("#movie").empty().append(loading).fadeIn("normal", function() {
				$("#movie #loading").delay(2000).fadeOut("normal", function() {
					$("#mainNavigation ul ul li.playing").next("li").find("a").trigger("click");
				});
			});
		}
	});
	
	$("#movPlayer").hide();
	$("#movPlayer").load(function() {
		$(this).show();
	});
	
	$("#mainNavigation ul ul").hide();
	
	// set stills menu display
	/*$("#navStills a:first").click(function(e) {
		e.preventDefault();
		$("#navStills ul").slideToggle();
	});*/
	
	$("#mainNavigation > ul > li:not(#nav-contact) > a, h1 > a").live("click", function(e) {
		var bid = $("body").attr("id");
		var lnk = this.href;
		if (bid !== "home") {
			if ((bid == "resume") || (bid == "error")) {
				return;
			} else {
				e.preventDefault();
				$("#mainNavigation ul ul:visible").slideUp('normal', function() {
					window.location.href = lnk;
				});
			}	
		}
	});
	
	// set contact menu display
	$("#nav-contact a:first").click(function(e) {
		e.preventDefault();
		$("#nav-contact ul").slideToggle();
	});
	
	// Stills Functions
	$("#navStills ul a").click(function(e) {
		e.preventDefault();
		if (!$(this).parent("li").hasClass("active")) {
			$("#prev").hide();
			$("#next").show();
			var gallery = this.href;
			$("#photoContainer:visible").fadeOut();
			$("#gallery").fadeOut(function() {
				$(this).load(gallery + " #photoContainer", function() {
					$("#photoContainer").hide().fadeIn();
					$("#stillsList").hide();
					$("#stillsList li:first-child").addClass("active");
					$("#gallery").fadeIn();
				});
			});
			$("#navStills li").removeClass("active");
			$(this).parent("li").addClass("active");
			/*$(this).parentsUntil("#mainNavigation > ul").slideUp(350, function() {
				window.location.href = gallery;
			});*/
		}
	});
	
	// image click
	$("#stills #stillsImg").live("click", function(e) {
		$("#next:visible").trigger("click");
	});
	
	// left & right arrow keys
	$("#stills").keyup(function(e) {
		switch (e.keyCode) {
			case 37:
			$("#prev:visible").trigger("click");
			break;
			
			case 39:
			$("#next:visible").trigger("click");
			break;
		}
	})
	
	// next button
	$("#next").live("click", function(e) {
		e.preventDefault();
		$("#prev:hidden").show();
		var c = $("#stillsList .active");
		var n = c.next("li");
		var s = n.children("a").attr("href");
		$("#stillsImg").fadeOut(150, function() {
			$(this).attr("src", s);
			$(this).load(function() {
				$(this).fadeIn();
			});
		});
		c.removeClass("active");
		n.addClass("active");
		if (n.is(":last-child")) {
			$(this).hide();
		}
	});
	
	// prev button
	$("#prev").live("click", function(e) {
		e.preventDefault();
		$("#next:hidden").show();
		var c = $("#stillsList .active");
		var p = c.prev("li");
		var s = p.children("a").attr("href");
		$("#stillsImg").fadeOut(150, function() {
			$(this).attr("src", s);
			$(this).load(function() {
				$(this).fadeIn();
			});
		});
		c.removeClass("active");
		p.addClass("active");
		if (p.is(":first-child")) {
			$(this).hide();
		}
	});
	
});

$(window).load(function() {
	$("#mainNavigation ul li:not(#nav-contact) ul:first").slideDown("slow", function() {
		if ($("#stills").length) {
			$("#navStills.active #navCommercial a").trigger("click");
		}
	});
})

/*------------------------------------------
	Utility Functions
--------------------------------------------*/
function loadClip(movpath, height) {
	var theMov = QT_GenerateOBJECTText_XHTML(movpath, '100%', '100%', '', 'EnableJavaScript', 'True', 'postdomevents', 'True', 'obj#ID', 'movPlayer', 'emb#ID', 'movPlayerEmbed', 'emb#NAME', 'movPlayer', 'autoplay', 'true', 'showlogo', 'true', 'controller', 'false', 'kioskmode', 'true', 'scale','1', 'allowembedtagoverrides','true', 'loop', 'false', 'bgcolor', 'white');
	$("#movie").empty().animate({
		height: height
	}, 350, function() {
		$(this).append(theMov);
	})
}

function alinks() {
	// open external links in new window
	$("a.external").each(function() {
		$(this).click(function() {
			window.open(this.href);
			return false;
		});
	});
	
	// cloak email address
	$("a.email").each(function(e) {
		$(this).click(function() {
			var to = $(this).attr("id");
			var domain = $(this).attr("title");
			$(this).attr("href", "mailto:" + to + "@" + domain);
		});
	});
}
