var slideShowLength, nextSlideIndex, largePhotoSize;
var currentSlideIndex = 0;
var thumbs;
var s;
var photoDuration = 7500;
var transitionDuration = 666;

$().ready(function() {
	
	thumbs = $("#rightInner a");
	slideShowLength = thumbs.length;

	setRightInnerWidth();
	
	$("#photoInfo .fizzBuzz").css("opacity", 0.5);
	
	$(window).resize(function() {
		setRightInnerWidth();
	});
	
	$("#rightInner a").click(function() {
		if ($(this).children("img").hasClass("selected")) {
			return true;
		} else {
			clearInterval(s);
			updateMain($(this));
			$("#rightInner a img").removeClass();
			$(this).children("img").addClass("selected");
			$(this).blur();
			return false;
		}
	});
	
	$("#middleInner").hover(function() {
		clearInterval(s);
		$("#photoInfo").show();
		pageTracker._trackPageview("/home/hover/" + $("#photoInfo .meta .photoName").text());
	}, function() {
		$("#photoInfo").hide();
	});
	
	$("p.buyPrint a").click(function() {
		pageTracker._trackPageview("/home/outgoing/" + this.href);
	});

	autoSlideShow();
});

function setRightInnerWidth() {
	largePhotoSize = 1000;
	// set cookie
	$.cookie('largePhotoSize', largePhotoSize, { expires: 90, path: '/' });
}

function updateMain(e) {
	$("#photoInfo").hide();
	
	// set new photoInfo values
	$("#photoInfo .photoName").text($(e).children("span.photoName").text());
	if (!$("#photoInfo .photoName").text().length) { $("#photoInfo .photoName").hide; } else { $("#photoInfo .photoName").show(); }
	
	$("#photoInfo .photoDescription").text($(e).children("span.photoDescription").text());
	if (!$("#photoInfo .photoDescription").text().length) { $("#photoInfo .photoDescription").hide; } else { $("#photoInfo .photoDescription").show(); }
	
	$("#photoInfo .buyPrint a").attr("href", $(e).children("span.buyPrint").text());
	if (!$(e).children("span.buyPrint").text().length) { $("#photoInfo .sales .buyPrint").hide(); } else { $("#photoInfo .sales .buyPrint").show(); }
	
	$("#middleInner a img.main").fadeOut(transitionDuration, function() {
		$("#middleInner a.photoLink").attr("href", $(e).attr("href"));
		$("#middleInner a img.main").attr("src", $(e).children("img").attr("src").replace("/75/", "/750/"));
		$("#middleInner a img.main").load(function() {
			$("#middleInner a img.main").fadeIn(transitionDuration, function() {
				// $("#photoInfo").show();
			});
		});
	});
}

function autoSlideShow() {
	// smart increment slideshow
	s = setInterval('updateMain($(thumbs[smartIncrementSlideIndex()]));setSelectedThumb(currentSlideIndex);', photoDuration);
}

function smartIncrementSlideIndex() {
	currentSlideIndex++; 
	if (currentSlideIndex >= slideShowLength) { 
		currentSlideIndex = 0; 
	};
	return currentSlideIndex;
}

function setSelectedThumb(index) {
	$("#rightInner a img").removeClass("selected");
	$($("#rightInner a img")[index]).addClass("selected");
}