

window.Frameslider = {
	
	inner: null,
	
	panelWidth: 210,
	panelOffset: 0,
	
	numPanels: 0,
	firstPanel: 0,
	selectedPanel: 0,
	
	// Shifts to the next 4
	shiftRight: function () {
		Frameslider.firstPanel += 4;
		if (Frameslider.firstPanel >= Frameslider.numPanels) {
			Frameslider.firstPanel = 0;
		}
		Frameslider.shift();
	},
	
	// Shifts to the previous 4
	shiftLeft: function () {
		Frameslider.firstPanel -= 4;
		if (Frameslider.firstPanel < 0) {
			Frameslider.firstPanel = Frameslider.firstPanelFor(Frameslider.numPanels);
		}
		Frameslider.shift();
	},
	
	// Quick way of calculating panel starts
	firstPanelFor: function (panel) {
		return panel - (panel % 4);
	},
	
	// Shifts to make firstPanel true
	shift: function() {
		var to = 0 - (Frameslider.panelWidth * Frameslider.firstPanel);
		Frameslider.inner.css("top", 0);
		Frameslider.inner.get(0).scrollTop = 0;
		if (Frameslider.inner.get(0).offsetLeft != to) {
			Frameslider.inner.animate({left: to}, "normal", "swing");
		}
	},
	
	// Shows the person the link points to
	fromLink: function (link) {
		// Find which panel has the right ID, then get its number
		var anchor = link.href.split("#");
		anchor = anchor[1]; // Yarrrrrrr anchorrrrrs
		var panel = $("#"+anchor).get(0).i;
		Frameslider.selectedPanel = panel;
		Frameslider.firstPanel = Frameslider.firstPanelFor(panel);
		Frameslider.shift();
		// Select the link, deselecting others
		Frameslider.reselect();
	},
	
	// Shows the person the anchor (ID) points to
	fromAnchor: function (id) {
		Frameslider.fromLink($('.navigation li a[href$="' + id + '"]').get(0));
	},
	
	// Returns the selected panel
	getSelectedPanel: function () {
		return $(".scroll .panel").get(Frameslider.selectedPanel);
	},
	
	reselect: function () {
		// Deselect all links
		$(".navigation li").removeClass("selected");
		// Select the right link
		selectedId = Frameslider.getSelectedPanel().id;
		$('.navigation li a[href$="' + selectedId + '"]').parent().addClass("selected");
		// Highlight the panel
		$(".scroll .panel").removeClass("selected");
		$("#"+selectedId).addClass("selected");
	},
	
	reHeight: function () {
		// Recalculates the height of the bio div
		var max_height = 0;
		$(".scrollContainer, .scroll").height(1000);
		$(".scroll .panel").each(function () {
			max_height = Math.max(max_height, $(this).height());
		});
		$(".scrollContainer, .scroll").height(max_height + 10);
	},
	
	// Called on page load
	go: function () {
		
		// Start by unfloating the bio panels and instead absolutely positioning them
		var i = 0;
		var max_height = 0;
		$(".scrollContainer, .scroll").height(1000);
		$(".scroll .panel").css({
			float: "none",
			position: "absolute",
			top: 0
		}).each(function () {
			// Calculate x position
			this.i = i;
			$(this).css({
				left: (i*Frameslider.panelWidth)+Frameslider.panelOffset
			});
			i++;
			// Calc max height
			max_height = Math.max(max_height, $(this).height());
		});
		$(".scrollContainer, .scroll").height(max_height + 10);
		//$(".scrollContainer").width(i*Frameslider.panelWidth);
		
		// Schedule another height recalc for a few milliseconds time to help Safari
		setTimeout(Frameslider.reHeight, 1000);
		
		Frameslider.numPanels = i;
		Frameslider.inner = $(".scrollContainer");
		
		// Bind buttons
		$(".scrollButtons .right").click(function (e) {
			Frameslider.shiftRight();
		}).hover(
			function () {
				$(this).attr("src", "pix/scroll_right_hover.gif");
			},
			function () {
				$(this).attr("src", "pix/scroll_right.gif");
			}
		);
		$(".scrollButtons .left").click(function (e) {
			Frameslider.shiftLeft();
		}).hover(
			function () {
				$(this).attr("src", "pix/scroll_left_hover.gif");
			},
			function () {
				$(this).attr("src", "pix/scroll_left.gif");
			}
		);
		
		// Bind nav links
		$(".navigation a").click(function (e) {
			Frameslider.fromLink(this);
			e.preventDefault();
		});
		
		// Bind hover on people divs
		$(".scroll .panel").mouseover(function (e) {
			Frameslider.fromAnchor(this.id);
		});
		
		// Either use the anchorrrr in the URL or pick the first person
		if (window.location.hash) {
			Frameslider.fromAnchor(window.location.hash.substr(1));
		} else {
			$('.navigation a:first').click();
		}
		
	}
}

$(document).ready(Frameslider.go);
