var PHOTO_LEFT_MARGIN = 20;

var interval;
var photos = $("div.photoContainer");
var currentPhoto;
var nextPhoto;
var leftPosition;

var offset, lastOffset, position,  seconds;
seconds = 0;
offset = 0;
var timeout = setInterval("updateTestData(offset, position)", 1000);

$(document).ready(function()
{
	currentPhoto = $("div.first");
	nextPhoto = $("div.first").next();
	leftPosition = $("div#gallery").position().left;
	
	$("div.button").click(function()
	{
		if ($(this).hasClass("buttonLeft"))
		{
			nextPhoto = currentPhoto.prev();
			if (nextPhoto.hasClass("photoContainer"))
			{
				lastOffset = offset;
				offset = nextPhoto.width() + PHOTO_LEFT_MARGIN;
				leftPosition += offset;
				showNextPhoto(nextPhoto.width() + PHOTO_LEFT_MARGIN);
				currentPhoto = nextPhoto;
			}
		}
		else
		{
			nextPhoto = currentPhoto.next();
			if (nextPhoto.hasClass("photoContainer"))
			{
				lastOffset = offset;
				offset = -(currentPhoto.width() + PHOTO_LEFT_MARGIN);
				leftPosition += offset;
				showNextPhoto(-(currentPhoto.width() + PHOTO_LEFT_MARGIN));
				currentPhoto = nextPhoto;
			}
		}
	});
});

function showNextPhoto(offset)
{
	$("div#gallery").animate({
		left: leftPosition + "px"
	}, 250);
}

function updateLeftPosition()
{
	leftPosition = $("div#gallery").position().left;
}

function updateTestData()
{
	position = leftPosition;
	seconds++;
	$("span#offset").text("Offset: " + offset);
	$("span#lastOffset").text("Last Offset: " + lastOffset);
	$("span#galleryPosition").text("Gallery Position: " + position);
	$("span#time").text("Seconds Passed: " + seconds);
}