function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
var preDragPosition = 0;
var doHover = true;
var thumbCenterTimeout;
var leftArrowHovered = false;
var rightArrowHovered = false;
var handleHovered = false;
var mouseDown = false;
$(document).ready(function() {

	//highlight current level 3 nav
	$("#nav-tertiary a").each(function() {
		if (location.href.indexOf(this.href) > -1) {
			$(this).addClass("active");
		}
	});
	
	//Our Team
	$(".team .people-detail .name").click(function() {
		$(this).parent().toggleClass("open");
	});
	
	//thumb strip
	if ($("#thumb-strip").length > 0) {
		MM_preloadImages("/images/scroll-arrow-left-over.png", "/images/scroll-arrow-left-down.png", "/images/scroll-arrow-right-over.png","/images/scroll-arrow-right-down.png");	
		//initialize film-srip constants
		window.thumbStripWidth = $("#thumb-strip").width();
		window.sliderWidth = $("#thumb-slider").width();
		window.thumbStripScrollWidth = $("#thumb-strip").attr("scrollWidth");
		window.maxThumbScroll = thumbStripScrollWidth - thumbStripWidth;
		window.sliderHandleSize = Math.round((thumbStripWidth / thumbStripScrollWidth) * sliderWidth);
		window.sliderHandleDelta = sliderWidth - sliderHandleSize;
		//landing page thumb strip
		//$("#thumb-strip a").click(function() {return false});
		$("#thumb-strip a").hover(function() {
			//bring active thumbnail into full view (if partially obscured)
			if (! mouseDown) {
				clearTimeout(thumbCenterTimeout);
				thumbCenterTimeout = setTimeout("bringActiveThumbIntoView()",500);
				if (! $(this).hasClass("active")) {
					
					//deacivate old thumb
					var oldActiveThumb = $("img", "#thumb-strip a.active");	
					oldActiveThumb.attr("src", oldActiveThumb.attr("src").replace("-on",""));
					$("#thumb-strip a.active").removeClass("active");
																		
					//acivate new thumb
					var newActiveThumb = $("img", this);
					newActiveThumb.attr("src", newActiveThumb.attr("src").replace(".jpg", "-on.jpg"));	
					$(this).addClass("active");
					
					//hide old splash, show new one
					$(".splash-image.active").removeClass("active");
					$("#" + $(this).attr("rel")).addClass("active");
				}
			}
		}, function() {});
		
		//slider
		$("#thumb-slider").each(function() {
			$(this).slider({
				animate: true,
				handle: "#thumb-slider-handle",
				change: handleSliderChange,
				slide: handleSliderSlide
			});
		});
	
		//set slider handle to correct proportional width
		$("#thumb-slider-handle").css("width",sliderHandleSize + "px");
		
			
		//darken handle on mouseover
		$("#thumb-slider-handle").hover(function() {
			$(this).addClass("over");
			handleHovered = true;
		}, function() {
			handleHovered = false;
			if (! mouseDown) $(this).removeClass("over");
		}).mousedown(function() {
			mouseDown = true;
		});
		
		//scroll arrows
		$("#scroll-arrow-left").hover(function() {
			$(this).attr("src", "/images/scroll-arrow-left-over.png");
			leftArrowHovered = true;
		}, function() {
			if (! mouseDown) $(this).attr("src", "/images/scroll-arrow-left.png");
			leftArrowHovered = false;
		}).mousedown(function() {
			mouseDown = true;
			$(this).attr("src", "/images/scroll-arrow-left-down.png");
			if ($("#thumb-strip").scrollLeft() > 0) {
				doScroll(0, Math.round(($("#thumb-strip").scrollLeft() / maxThumbScroll) * 2000));
			}
			return false;
		});
		$("#scroll-arrow-right").hover(function() {
			$(this).attr("src", "/images/scroll-arrow-right-over.png");
			rightArrowHovered = true;
		}, function() {
			if (! mouseDown) $(this).attr("src", "/images/scroll-arrow-right.png");
			rightArrowHovered = false;
		}).mousedown(function() {
			mouseDown = true;
			$(this).attr("src", "/images/scroll-arrow-right-down.png");
			if ($("#thumb-strip").scrollLeft() < thumbStripScrollWidth) {
				doScroll(maxThumbScroll, Math.round((1-($("#thumb-strip").scrollLeft() / maxThumbScroll)) * 2000));
			}
			return false;
		});
		$(document).mouseup(function() {
			mouseDown = false;
			if (! handleHovered) $("#thumb-slider-handle").removeClass("over");
			if (leftArrowHovered) {
				$("#scroll-arrow-right").attr("src", "/images/scroll-arrow-right.png");
				$("#scroll-arrow-left").attr("src", "/images/scroll-arrow-left-over.png");
			} else if (rightArrowHovered) {
				$("#scroll-arrow-right").attr("src", "/images/scroll-arrow-right-over.png");
				$("#scroll-arrow-left").attr("src", "/images/scroll-arrow-left.png");
			} else {
				$("#scroll-arrow-right").attr("src", "/images/scroll-arrow-right.png");
				$("#scroll-arrow-left").attr("src", "/images/scroll-arrow-left.png");	
			}
			$("#thumb-strip").stop();
			$("#thumb-slider-handle").stop();
		});
	}
});

//function to do a manual scroll, which also moves the slider, and with optional animation
function doScroll(newScrollLeft, duration) {
	if (duration > 0) {
		$("#thumb-strip").animate({scrollLeft: newScrollLeft}, duration);
	} else {
		$("#thumb-strip").scrollLeft(newScrollLeft);
	}
	moveHandle(newScrollLeft, duration);
}
function moveHandle(newScrollLeft, duration) {
	newHandleLeft = Math.round(sliderHandleDelta * newScrollLeft / maxThumbScroll);
	if (duration > 0) {
		$("#thumb-slider-handle").animate({left: newHandleLeft}, duration);
	} else {
		$("#thumb-slider-handle").css({left: newHandleLeft});
	}
}
//functions for thumb slider
function bringActiveThumbIntoView() {
	var theThumb = $("#thumb-strip a.active");
	if (theThumb.hasClass("first")) paddingOffset = 0;
	else paddingOffset = 6;
	maxScroll = theThumb.position().left + paddingOffset;
	minScroll = $("img",theThumb).position().left + theThumb.width() - thumbStripWidth;
	scrollValue = $("#thumb-strip").scrollLeft();
	if (scrollValue > maxScroll) doScroll(maxScroll, 400);
	else if (scrollValue < minScroll) doScroll(minScroll, 400);
}
function handleSliderChange(e, ui) {
	$("#thumb-strip").animate({scrollLeft: ui.value * (maxThumbScroll / 100) }, 1000);
}

function handleSliderSlide(e, ui) {
	$("#thumb-strip").attr({scrollLeft: ui.value * (maxThumbScroll / 100) });
}

