var photoSize = 500;
var analyticsBasePath;
var thumbs;


var slideShowLength, nextSlideIndex, largePhotoSize;
var currentSlideIndex = 0;
var s;
var photoDuration = 6500;
var transitionDuration = 666;

$(document).ready(function() {
	/*
	setRightColumnWidth();
	$(window).resize(function() {
		setRightColumnWidth();
	});
	*/
	
	thumbs = $(".leftRight #right .photo a img");
	slideShowLength = thumbs.length;

	$(".leftRight #right .photo a img").click(function() {
		if ($(this).hasClass("selected")) {
			return true;
		}
		else {
			// stop slideshow
			clearInterval(s);
			updateLarge($(this).parent().parent());
			currentSlideIndex = ($(this).parent().attr("rel"));
			setSelectedThumb(currentSlideIndex);
			$(this).parent().blur();
			// analytics tracking
			pageTracker._trackPageview(analyticsBasePath + $(this).parent().attr("href"));
			return false;
		}
	});
	
	// load image other than standard/default?
	getImageFromHash();
	
	$("#playSlideshow").click(function() {
		autoSlideShow();
		$("#stopSlideshow").css("display", "block").show();
		$(this).hide();
		return false;
	});
	
	$("#stopSlideshow").click(function() {
		clearInterval(s);
		$(this).hide();
		$("#playSlideshow").show();
		return false;
	});
		
});

function updatePhotoSizes(oldSize, newSize) {
	var p = $("#photos .photo img");
	
	$.each(p, function() {
		var origSrc = $(this).attr("src");
		// replace the old size with the new
		var newSrc = origSrc.replace("/" + oldSize + "/", "/" + newSize + "/");
		$(this).attr("src", newSrc);
	});
	
	$("#photos.grid .photo").removeClass("w" + oldSize);
	$("#photos.grid .photo").addClass("w" + newSize);
}

//function updateLarge(src, name, link) {
// receives an element at the level of div.photo
function updateLarge(e) {
	
	// upsize from thumb
	src = $(e).children("a").children("img").attr("src").replace("/75/", "/" + photoSize + "/");
	name = $(e).children("h2").text();
	link = $(e).children("a").attr("href");
	stubTemp = link.split("/");
	stub = stubTemp[4];
	
	// hide
	$("#left a img").hide();
	$("#left h2").hide();
	
	// update
	$("#left a img").attr("src", src);
	$("#left h2 span").text(name);
	$("#left a").attr("href", link);
	
	// dazzle
	$("#left a img").load(function() {
		$("#left a img").fadeIn(500, function() {
			$("#left h2").show();
		})	
	});
	
	// update location hash
	//window.location.hash = stub;
}

function setRightColumnWidth() {
	var rightColumnMaxWidth = $("html").width() - $("#left").width();
	var rightColumnWidth = rightColumnMaxWidth - 60;
	if (rightColumnWidth > 700) {
		rightColumnWidth = 700;
	}
	$(".leftRight #right").width(rightColumnWidth);
}

function getImageFromHash() {
	hash = window.location.hash;
	index = 0;
	if (hash) {
		hashStub = hash.replace("#", "");
		// cycle through thumbs and look for a match
		$(thumbs).each(function() {
			pStub = $(this).parent().attr("href");
			pStub = pStub.split("/");
			if (pStub[4] == hashStub) {
				updateLarge($(this).parent().parent());
				setSelectedThumb(index);
				currentSlideIndex = index;
			}
			index++;
		});
	}
}

function autoSlideShow() {
	// start right away
	updateLarge($(thumbs[smartIncrementSlideIndex()]).parent().parent());
	setSelectedThumb(currentSlideIndex);
	
	// continue with interval smart increment slideshow
	s = setInterval('updateLarge($(thumbs[smartIncrementSlideIndex()]).parent().parent());setSelectedThumb(currentSlideIndex);', photoDuration);
}

function smartIncrementSlideIndex() {
	currentSlideIndex++; 
	if (currentSlideIndex >= slideShowLength) { 
		currentSlideIndex = 0; 
	};
	return currentSlideIndex;
}

function setSelectedThumb(index) {
	$("#right .photo a img").removeClass("selected");
	$($("#right .photo a img")[index]).addClass("selected");
}