$(document).ready(function() {
	switch(document.body.className) {
		case "instructors":
			$(".menu a").click(function() {
				if($("#vid-shell:visible").length > 0) { instructors.close(); }
				$(".menu li").removeClass("on");
				$(this).parent().addClass("on");
				$(".peer, .prof", $(".grid")).removeClass("last");
				if($(this).hasClass("tab2")) {
					$(".grid .peer").removeClass("hidden");
					$(".grid .prof").addClass("hidden");
					instructors.series = "peer";
				} else if($(this).hasClass("tab3")) {
					$(".grid .prof").removeClass("hidden");
					$(".grid .peer").addClass("hidden");
					instructors.series = "prof";
				} else {
					$(".grid li").removeClass("hidden");
					instructors.series = "all";
				}
				$(".peer:visible, .prof:visible", $(".grid")).each(function(index) {
					if(index % 4 == 3) { $(this).addClass("last"); }
				});
				instructors.findLastRow();
				return false;
			});
			break;
		case "why":
			$(".content-tab").addClass("positioned");
			var tabs = $(".content-tab");
			tabs.hide();
			var hash = location.hash;
			if(hash == "" || $(hash).length == 0) {
				$(tabs.get(0)).show();
				$($(".menu li").get(0)).addClass("on");
			} else {
				$(hash).show();
				$(".menu a[href='" + hash + "']").parent().addClass("on");
				$("html, body").scrollTop(0);
			}
			$(".menu a").click(function() {
				hash = this.hash;
				$(".menu li").removeClass("on");
				$(this).parent().addClass("on");
				tabs.hide();
				$(hash).show();
				return false;
			});
	}
	// Headshot rollovers
	$(".instructors .grid a, .home .person a").mouseenter(function() {
		$(".roll", this).fadeIn(400);
	});
	$(".instructors .grid a, .home .person a").mouseleave(function() {
		$(".roll", this).fadeOut(400);
	});
	instructors.init();
	bgImageSize.init();
});

instructors = {
	vidShell: null,
	lastRowAt: 0,
	series: "all",
	init: function() {
		this.vidShell = $("#vid-shell");
		$(".home .person a").click(function() {
			instructors.vidShell.show();
			swfobject.embedSWF("swf/video_player.swf", "vid", "958", "474", "9.0.0", null, {xml:"xml/people.xml", video:$(this).attr("alt").toLowerCase().replace(/\s+/g, ""), link:"true"}, null, null, instructors.checkEmbedSuccess);
			return false;
		});
		this.findLastRow();
		$(".instructors .grid a").click(function() {
			var top = $(this).parent().position().top - (($(this).parent().index(".peer:visible, .prof:visible") >= instructors.lastRowAt) ? 242 : 0) + "px";
			instructors.vidShell.show().css("top", top);
			if($(this).attr("alt") != undefined) {
				instructors.vidShell.removeClass("as-info").html("<div id='vid'></div>");
				swfobject.embedSWF("swf/video_player.swf", "vid", "958", "474", "9.0.0", null, {xml:"xml/people.xml", video:$(this).attr("alt").toLowerCase().replace(/\s+/g, ""), series:instructors.series}, null, null, instructors.checkEmbedSuccess);
			} else {
				instructors.vidShell.addClass("as-info").html("<a href='#' id='info-close'></a>");
				$("span, .lg", $(this).parent()).clone().prependTo("#vid-shell");
				$("#info-close").click(function() {
					instructors.close();
					return false;
				});
			}
			return false;
		});
	},
	findLastRow: function() {
		var gridCount = $(".peer:visible, .prof:visible", $(".grid")).length;
		this.lastRowAt = gridCount - (gridCount % 4);
	},
	close: function() {
		this.vidShell.hide().html("<div id='vid'></div>");
	},
	checkEmbedSuccess: function(e) {
		var vid = $("#vid");
		if(vid[0].nodeName.toLowerCase() == "div") {
			vid.addClass("error").html("<a href='#' id='info-close'></a>");
			$("#info-close").click(function() {
				instructors.close();
				return false;
			});
		}
	}
}

bgImageSize = {
	container: $("#bg-image"),
	image: $("#bg-image img"),
	init: function() {
		if(this.image.css("position") == "static") { return; } // no scaling in IE6
		if(this.image.height() == 0) {
			this.image.load(function() {
				bgImageSize.calcSize();
				$(window).resize(bgImageSize.calcSize);
			});
		} else {
			this.calcSize();
			$(window).resize(this.calcSize);
		}
	},
	calcSize: function() {
		var cHeight = bgImageSize.container.height();
		if(bgImageSize.container.width() / cHeight < bgImageSize.image.width() / bgImageSize.image.height()) {
			bgImageSize.image.height(cHeight);
			bgImageSize.image.width("auto");
		} else {
			bgImageSize.image.width("100%");
			bgImageSize.image.height("auto");
		}
	}
}
