// JavaScript Document


var selected;

var initWidth;
var initImgX;

var animateTime = 500;

$(document).ready(function(){
	
	initWidth = $("#people_images .box:first").width() + "px";
	
	$("#people_content").css("height",0);
	$(".viewfull")
	.show()
	.click(function(){
		if($(this).text() == "View full resume >>"){
			$(this).text("Hide full resume >>");
			$(this).parent().next(".resume").show();
		}else{
			$(this).text("View full resume >>");
			$(this).parent().next(".resume").hide();
		}
		scrollContent($(this).parents(".content_in"));
		return false;
	});
	
	$(".resume").hide();
	
	$("#people_images .clickable").click(function(){
		focusSelected(this);
		scrollContent(String($(this).attr("href")));
		
		return false;
	});
	
	
	//Scrolling
	$("#next").click(function(){
		$("#people_wrapper").animate(
			{marginLeft: -$(".box:first").width()}, 
			animateTime, 
			"easeInOutQuad"
		).queue(function(){
			$(".box:first").appendTo($(this));
			$(this).css("margin-left","0");
			$(this).dequeue();
		});
		return false;
	});
	
	$("#prev").click(function(){
		$("#people_wrapper").queue(function(){
			$(this).prepend($(".box:last")).css("margin-left",-$(".box:first").width()+"px");
			$(this).dequeue();
		})
		.animate(
			{marginLeft: 0}, 
			animateTime, 
			"easeInOutQuad"
		);
		return false;
	});
	
});

function focusSelected(newSelection){
	if(selected != null){
		//Set the image straight in the old select
		$(selected).children("div").animate({
			left:initImgX
		},animateTime, 'easeInOutQuad');
	}
	
	if(selected == newSelection){
		selected = null;
		
		//Reset
		$("#people_images .box").animate({
			width:initWidth,
			marginLeft:"4px",
			marginRight:"4px"
		},animateTime, 'easeInOutQuad');
		
		$("#people_nav").slideDown("fast");
		
		return;
	}
	
	selected = newSelection;
	
	//Record Initial values
	var offset = $(selected).offset();
	var imgOffset = $(selected).children("div").offset();
	
	initImgX = imgOffset.left - offset.left;
	
	
	$("#people_images .box").not(selected).each(function(){
		try{
			$(this).animate({
				width:0,
				marginLeft:0,
				marginRight:0
			},animateTime, 'easeInOutQuad');
		}catch(error){
			alert(error + "\n" + $("#people_images .box").index(this));
		}
	});
	
	$(selected).animate({
		width:$("#people_images").width() + "px"
	},animateTime, 'easeInOutQuad');
	
	$("#people_nav").slideUp("fast");
	
	$(selected).children("div").animate({
		left:0
	},animateTime, 'easeInOutQuad');
	
	return false;
}

function scrollContent(target){
	
		var target = $(target);
		var parent = $("#content_in_wrapper");
		var viewport = $("#people_content");
		
		var targetY = ($(parent).offset().top - $(viewport).offset().top) - ($(target).offset().top - $(viewport).offset().top);
		
		$(parent).animate({
			top:targetY+"px"
		}, animateTime, 'easeInOutQuad');
		
		$(viewport).animate({
			height:$(target).height()+40+"px"
		}, animateTime, 'easeInOutQuad');
}