var t = null;
var t3 = null;
var imageWidth = -1;
var timeout = 7000;
function setScroll(i)
{
	if (widths.length > 1)
	{
		var container = document.getElementById(containerID);
		var scroll = document.getElementById(scrollID);
		var m = scroll.style.left == "" ? 0 : Number(scroll.style.left.replace('px', ''));
		var containerWidth = Number(container.style.width.replace('px', ''));
		var scrollWidth = Number(scroll.style.width.replace('px', ''));
		var f;
		if (m + scrollWidth == containerWidth - 6)
			f = 'scrollLeft(' + (i - scrollCount) + ', 1)';
		else
			f = 'scrollRight(' + (i + scrollCount) + ', 1)';
		scroll.onmouseover = new Function('clearTimeout(t3)');
		scroll.onmouseout = new Function("t3 = setTimeout('" + f + "', timeout)");
		t3 = setTimeout(f, timeout);
	}
}
function doScroll(isRight, i)
{
	var container = document.getElementById(containerID);
	var scroll = document.getElementById(scrollID);
	var 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";
		left.href = "javascript:scrollLeft(" + (i - scrollCount) + ", 1);";
		right.href = "javascript:scrollRight(" + (i + scrollCount) + ", 1);";
		toggleScrollArrows(leftScroll, rightScroll, true);
		imageWidth = -1;
		var f;
		if (m == 0)
			f = 'scrollRight(' + (i + scrollCount) + ', 1)';
		else if (m + scrollWidth == containerWidth - 6)
			f = 'scrollLeft(' + (i - scrollCount) + ', 1)';
		else if (isRight)
			f = 'scrollRight(' + (i + scrollCount) + ', 1)';
		else if (!isRight)
			f = 'scrollLeft(' + (i - scrollCount) + ', 1)';
		scroll.onmouseover = new Function('clearTimeout(t3)');
		scroll.onmouseout = new Function("t3 = setTimeout('" + f + "', timeout)");
		t3 = setTimeout(f, timeout);
	}
	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 startScroll(i, isRight, s)
{
	var scroll;
	if (s == 1)
	{
		clearTimeout(t3);
		scroll = document.getElementById(scrollID);
		scroll.onmouseover = new Function('pauseScroll()');
		scroll.onmouseout = new Function('resumeScroll(' + isRight + ', ' + i + ')');
		toggleScrollArrows(leftScroll, rightScroll, false);
		doScroll(isRight, i);
	}
	else if (s == 2)
	{
		scroll = document.getElementById(scrollID + '2');
		scroll.onmouseover = new Function('pauseScroll2()');
		scroll.onmouseout = new Function('resumeScroll2(' + isRight + ', ' + i + ')');
		toggleScrollArrows(leftScroll2, rightScroll2, false);
		doScroll2(isRight, i);
	}
}
function scrollLeft(i, s)
{
	startScroll(i, false, s);
}
function scrollRight(i, s)
{
	startScroll(i, true, s);
}
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 + ';');
}
