var playlistPos = 0;
var playlistPosNew = 0;
var doScroll = true;

var httpRequest;
if (navigator.appName == "Microsoft Internet Explorer")
	httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
else
	httpRequest = new XMLHttpRequest();

function scroll(value)
{
	if (!doScroll)
		return;
	
	if (document.getElementById('tv_playlist').getElementsByTagName('img').length < 4)
		return;
	
	doScroll = false;
	
	// Get current play list position.
	if (window.getComputedStyle)
		playlistPos = parseInt(window.getComputedStyle(document.getElementById("tv_playlist"), "").getPropertyValue("top"), 10);
	else if (document.getElementById("tv_playlist").currentStyle)
		playlistPos = parseInt(document.getElementById("tv_playlist").currentStyle.getAttribute("top"), 10);
	//alert ("playlistPos: " + playlistPos.toString());
	playlistPosNew = playlistPos;
	
	// Get max possible position.
	var maxPos = -(document.getElementById('tv_playlist').getElementsByTagName('img').length - 3) * 93;
	//alert ("maxPos: " + maxPos.toString());
	
	playlistPosNew -= value * 93;
	if (playlistPosNew < maxPos)
		playlistPosNew = maxPos;
	else if (playlistPosNew > 0)
		playlistPosNew = 0;
	//alert ("playlistPos: " + playlistPos.toString());
	
	//document.getElementById("tv_playlist").style.top = playlistPosNew.toString() + "px";
	if (value > 0)
		scrollDown();
	else
		scrollUp();
}

function scrollUp()
{
	if (playlistPos >= playlistPosNew)
	{
		document.getElementById("tv_playlist").style.top = playlistPosNew.toString() + "px";
		doScroll = true;
		return;
	}
	
	playlistPos += 2;
	
	document.getElementById("tv_playlist").style.top = playlistPos.toString() + "px";
	window.setTimeout("scrollUp()", 5);
}

function scrollDown()
{
	if (playlistPos <= playlistPosNew)
	{
		document.getElementById("tv_playlist").style.top = playlistPosNew.toString() + "px";
		doScroll = true;
		return;
	}
	
	playlistPos -= 2;
	
	document.getElementById("tv_playlist").style.top = playlistPos.toString() + "px";
	window.setTimeout("scrollDown()", 5);
}

function showFirstVideo()
{
	document.getElementById('tv_playlist').getElementsByTagName('a')[0].onclick.call();
}

/**
 * Shows a video by id.
 * @param {String} file Html file path.
 * @param {String} title Title for this video.
 * @return
 */
function showVideo(file, title)
{
	document.getElementById('tv_title').innerHTML = title;
	
	httpRequest.open('get', "tvpages/loadpage.php?src=../" + file);
	httpRequest.onreadystatechange = loadFile;
	httpRequest.send(null);
}

function loadFile()
{
	if (httpRequest.readyState == 4)
	{
		if (httpRequest.status == 200)
		{
			document.getElementById('tv_content').innerHTML = httpRequest.responseText;
		}
	}
}

function stopVideo()
{
	document.getElementById('tv_content').innerHTML = "";
}
