var boxWidth = 590;

function scrollToX(x)
{
	var div = document.getElementById('scrollboxcontent');
	var startScroll = div.scrollLeft;
	x -= boxWidth / 2;
	var d = new Date();

	var func = function()
	{
		var percentDone = ((new Date()).getTime() - d.getTime()) / 500;
		if(percentDone >= 1)
		{
			div.scrollLeft = x;
		}
		else
		{
			percentDone = Math.sin(percentDone * Math.PI / 2); // Get some deceleration into there.
			div.scrollLeft = (x - startScroll) * percentDone + startScroll;
			setTimeout(function() { func(); }, 42);
		}
	};

	func();
}

function scrollBoxLeft()
{
	var div = document.getElementById('scrollboxcontent');
	scrollToX(div.scrollLeft - boxWidth / 2);
	return false;
}

function scrollBoxRight()
{
	var div = document.getElementById('scrollboxcontent');
	scrollToX(div.scrollLeft + boxWidth + boxWidth / 2);
	return false;
}

function scrollToAnchor(anchor)
{
	var last = document.getElementById('active');
	if(last)
	{
		last.id = '';
	}
	anchor.id = 'active';

	scrollToX(anchor.offsetLeft + anchor.childNodes[0].width / 2);
}

function onYouTubePlayerReady(playerId)
{
	var myplayer = document.getElementById(playerId);
}

function playVideo(video)
{
	var videoobject = document.getElementById('videoobject');
	if(typeof videoobject.loadVideoByUrl !== 'function')
	{
		setTimeout(function(){playVideo(video)}, 50);
		return;
	}
	// It seems auto-played videos don't count toward the view count.
	//videoobject.loadVideoByUrl(video, 0);
	videoobject.cueVideoByUrl(video, 0);
}

function setVideo(videoAnchor)
{
	var videoobject = document.getElementById('videoobject');
	videoobject.className = '';
	playVideo(videoAnchor.href);

	var imageobject = document.getElementById('imageobject');
	imageobject.className = 'hide';

	scrollToAnchor(videoAnchor);

	return false;
}

function setImage(imageAnchor)
{
	var imageobject = document.getElementById('imageobject');
	imageobject.className = '';
	imageobject.src = imageAnchor.href.slice(0, -4) + '%20display.jpg';

	var imagelink = document.getElementById('imagelink');
	imagelink.href = imageAnchor.href;

	var videoobject = document.getElementById('videoobject');
	if(typeof videoobject.stopVideo === "function")
	{
		videoobject.stopVideo();
	}
	videoobject.className = 'hide';

	scrollToAnchor(imageAnchor);
	
	_gaq.push(['_trackPageview', imageobject.src]);

	return false;
}

