var t = null;
var m = -1;
var imageWidth = -1;
function doScroll(isRight, i)
{
	var container = document.getElementById(containerID);
	var scroll = document.getElementById(scrollID);
	if (m < 0) m = scroll.style.left == "" ? 0 : Number(scroll.style.left.replace('px', ''));
	if (imageWidth < 0)
		imageWidth = getArraySum(widths, i - 1) + 12 * i;
	if (isRight) m -= 4; else m += 4;
	scroll.style.left = m + 'px';
	var containerWidth = Number(container.style.width.replace('px', ''));
	var scrollWidth = Number(scroll.style.width.replace('px', ''));
	if(Math.abs(m) == imageWidth || m + scrollWidth == containerWidth - 6 || m == 0)
	{
		clearTimeout(t);
		scroll.onmouseover = new Function();
		scroll.onmouseout = new Function();
		var left = document.getElementById(leftScrollID);
		var right = document.getElementById(rightScrollID);
		right.style.display = m + scrollWidth == containerWidth - 6 ? "none" : "inline-block";
		left.style.display = m == 0 ? "none" : "inline-block";
		toggleScrollArrows(leftScroll, rightScroll, true);
		left.href = "javascript:scrollLeft(" + (i - scrollCount) + ");";
		right.href = "javascript:scrollRight(" + (i + scrollCount) + ");";
		imageWidth = -1;
	}
	else
		t = setTimeout('doScroll(' + isRight + ', ' + i + ')', 0);
}
function getArraySum(arr, i)
{
	var sum = 0;
	for (j = 0; j <= i; j++)
		sum += arr[j];
	return sum;
}
function scrollLeft(i)
{
	var scroll = document.getElementById(scrollID);
	scroll.onmouseover = new Function('pauseScroll()');
	scroll.onmouseout = new Function('resumeScroll(' + false + ', ' + i + ')');
	toggleScrollArrows(leftScroll, rightScroll, false);
	doScroll(false, i);
}
function scrollRight(i)
{
	var scroll = document.getElementById(scrollID);
	scroll.onmouseover = new Function('pauseScroll()');
	scroll.onmouseout = new Function('resumeScroll(' + true + ', ' + i + ')');
	toggleScrollArrows(leftScroll, rightScroll, false);
	doScroll(true, i);
}
function pauseScroll()
{
	clearTimeout(t);
}
function resumeScroll(isRight, i)
{
	t = setTimeout('doScroll(' + isRight + ', ' + i + ')', 0);
}
function toggleScrollArrows(left, right, status)
{
	left.onclick = new Function('return ' + status + ';');
	right.onclick = new Function('return ' + status + ';');
}
