

window.Peopleslider = {
	
	inner: null,
	
	panelWidth: 210,
	panelOffset: 0,
	
	numPanels: 0,
	firstPanel: 0,
	selectedPanel: 0,
	
	// Quick way of calculating panel starts
	firstPanelFor: function (panel) {
		//return panel - (panel % 4);
		return panel;
	},
	
	// Shifts to make firstPanel true
	shift: function() {
		var to = Math.floor(selectedPanel/4)*350;
		Peopleslider.inner.css("top", to);
	},
	
	// 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]; 
		var panel = $("#"+anchor).get(0).i;
		Peopleslider.selectedPanel = panel;
		Peopleslider.firstPanel = Peopleslider.firstPanelFor(panel);
		// Select the link, deselecting others
		Peopleslider.reselect();
		Peopleslider.shift();
	},
	
	// Shows the person the anchor (ID) points to
	fromAnchor: function (id) {
		Peopleslider.fromLink($('.navigation li a[href$="' + id + '"]').get(0));
	},
	
	// Returns the selected panel
	getSelectedPanel: function () {
		return $(".scroll .panel").get(Peopleslider.selectedPanel);
	},
	
	reselect: function () {
		// Deselect all links
		$(".navigation li").removeClass("selected");
		// Select the right link
		selectedId = Peopleslider.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(1050);
		$(".scroll .panel").each(function () {
			max_height = Math.max(max_height, $(this).height());
		});
		// $(".scrollContainer, .scroll").height(max_height + 10);
		$(".scrollContainer, .scroll").height(1060);
	},
	
	// 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(1050);
		$(".scroll .panel").css({
			float: "none",
			position: "absolute",
			top: 0
		}).each(function () {
			// Calculate x position
			this.i = i;
			$(this).css({
				left: ((i%4)*Peopleslider.panelWidth)+Peopleslider.panelOffset
			});
			$(this).css({
				top: Math.floor(i/4)*350
			});
			i++;
			// Calc max height
			max_height = Math.max(max_height, $(this).height());
		});
		// $(".scrollContainer, .scroll").height(max_height + 10);
		$(".scrollContainer, .scroll").height(1060);
		//$(".scrollContainer").width(i*Peopleslider.panelWidth);
		
		// Schedule another height recalc for a few milliseconds time to help Safari
		setTimeout(Peopleslider.reHeight, 1050);
		
		Peopleslider.numPanels = i;
		Peopleslider.inner = $(".scrollContainer");
		
		// Bind nav links
		$(".navigation a").click(function (e) {
			Peopleslider.fromLink(this);
			e.preventDefault();
		});
		
		// Bind hover on people divs
		$(".scroll .panel").mouseover(function (e) {
			// Peopleslider.fromAnchor(this.id);
		});
		
		// Either use the anchor in the URL or pick the first person
		if (window.location.hash) {
			Peopleslider.fromAnchor(window.location.hash.substr(1));
		} else {
			$('.navigation a:first').click();
		}
		
	}
}

$(document).ready(Peopleslider.go);
