/*
	Image Scroller

	Copyright by Jens Duttke
	http://www.duttke.de
*/

var SCROLL_HEIGHT = 143;
var scrollPos = 0;
var scrollInterval = null;
var scrollTimeout = null;

/* ------------------------ Initialization ------------------------ */

$.addEvent(window, 'load', function ()
{
	scrollAutoScroll();
	$.setUnselectable($('scrollImgBackground'));
	$.setUnselectable($('scrollImgLarge'));

	document.getElementById('imageScroller').onmousewheel = mouseWheel;
	$.addEvent($('imageScroller'), 'DOMMouseScroll', mouseWheel);

	function mouseWheel(e)
	{
		var delta = 0;

		if (scrollInterval === null)
		{
			if (e.wheelDelta)
			{
				delta = event.wheelDelta / 120;
				if (window.opera)
				{
					delta = -delta;
				}
			}
			else if (e.detail)
			{
				delta = -e.detail / 3;
			}

			if (delta > 0)
			{
				scrollUp(); 
			}
			else if (delta < 0)
			{
				scrollDown();
			}
		}
		return false;
	}
});

/* ------------------------ Scrolling ------------------------ */

function scrollAutoScroll()
{
	scrollTimeout = setTimeout(function ()
	{
		scrollTimeout = null;
		scrollDown();
	}, 5000);
}

function scrollUp()
{
	if (scrollTimeout != null)
	{
		clearTimeout(scrollTimeout);
		scrollTimeout = null;
	}

	scrollPos += SCROLL_HEIGHT;

	if (scrollInterval === null)
	{
		scrollInterval = setInterval(scrollAnim, 30);
	}
}

function scrollDown()
{
	if (scrollTimeout != null)
	{
		clearTimeout(scrollTimeout);
		scrollTimeout = null;
	}

	scrollPos -= SCROLL_HEIGHT;

	if (scrollInterval === null)
	{
		scrollInterval = setInterval(scrollAnim, 30);
	}
}

function scrollAnim()
{
	var max = (-$('scrollDiv').scrollHeight + (3 * SCROLL_HEIGHT) - 11)

	var curPos = parseInt($('scrollDiv').style.marginTop);
	if (isNaN(curPos)) { curPos = 0; }

	if (curPos > scrollPos + 10)
	{
		curPos -= 10;
		if (curPos < max)
		{
			curPos -= max;
			scrollPos -= max;
		}
	}
	else if (curPos < scrollPos - 10)
	{
		curPos += 10;
		if (curPos > 0)
		{
			curPos += max;
			scrollPos += max;
		}
	}
	else
	{
		curPos = scrollPos;
		clearInterval(scrollInterval)
		scrollInterval = null;
		scrollAutoScroll();
	}
	$('scrollDiv').style.marginTop = curPos + 'px';
}

/* ------------------------ onClick Handling ------------------------ */

var scrollImgLayerOpacity = 0.0;
var scrollImgLayerInterval = null;
var scrollImgOpacity = 0.0;
var scrollImgInterval = null;

function scrollImgClick(src)
{
	if (scrollInterval != null) { return; }
	if (scrollTimeout != null)
	{
		clearTimeout(scrollTimeout);
		scrollTimeout = null;
	}
	if (scrollImgInterval != null)
	{
		clearInterval(scrollImgInterval);
		scrollImgInterval = null;
	}

	$('scrollImgBackground').style.display = 'block';

	$('scrollImgLarge').onload = showLargeImg;
	$('scrollImgLarge').onreadystatechange = function () { if (document.readyState == 'complete') { showLargeImg(); } };
	$('scrollImgLarge').onerror = function () { scrollImgClose(); }
	$('scrollImgLarge').src = src;

	$('contentDiv').style.overflow = 'hidden';
	scrollImgLayerOpacity = 0.0;
	$.setOpacity($('scrollImgBackground'), scrollImgLayerOpacity);

	scrollImgLayerInterval = setInterval(function ()
	{
		scrollImgLayerOpacity += 0.1;
		if (scrollImgLayerOpacity > 0.8) { scrollImgLayerOpacity = 0.8; }

		$.setOpacity($('scrollImgBackground'), scrollImgLayerOpacity);
		if (scrollImgLayerOpacity == 0.8)
		{
			clearInterval(scrollImgLayerInterval);
			scrollImgLayerInterval = null;
		}
	}, 20);

	function showLargeImg()
	{
		if ($('scrollImgBackground').style.display != 'none')
		{
			$('scrollImgLarge').style.left = '50%';
			$('scrollImgLarge').style.top = '50%';
			$('scrollImgLarge').style.marginLeft = -Math.round($('scrollImgLarge').offsetWidth / 2) + 'px';
			$('scrollImgLarge').style.marginTop = -Math.round($('scrollImgLarge').offsetHeight / 2) + 'px';
			scrollImgOpacity = 1.0;
			$.setOpacity($('scrollImgLarge'), scrollImgOpacity);
		}
	}
}

function scrollImgClose()
{
	if (scrollImgInterval != null) { return; }
	if (scrollTimeout != null)
	{
		clearTimeout(scrollTimeout);
		scrollTimeout = null;
	}
	if (scrollImgLayerInterval != null)
	{
		clearInterval(scrollImgLayerInterval);
		scrollImgLayerInterval = null;
	}

	$.setOpacity($('scrollImgBackground'), scrollImgLayerOpacity);

	scrollImgLayerInterval = setInterval(function ()
	{
		scrollImgLayerOpacity -= 0.1;
		if (scrollImgLayerOpacity < 0.0) { scrollImgLayerOpacity = 0.0; }

		$.setOpacity($('scrollImgBackground'), scrollImgLayerOpacity);
		if (scrollImgLayerOpacity == 0.0)
		{
			clearInterval(scrollImgLayerInterval);
			scrollImgLayerInterval = null;

			$('scrollImgBackground').style.display = 'none';
			$('contentDiv').style.overflow = 'auto';

			scrollImgInterval = setInterval(function ()
			{
				scrollImgOpacity -= 0.05;
				$.setOpacity($('scrollImgLarge'), scrollImgOpacity);

				if (scrollImgOpacity <= 0)
				{
					clearInterval(scrollImgInterval);
					scrollImgInterval = null;

					$('scrollImgLarge').style.visibility = 'hidden';
					$('scrollImgLarge').style.left = '0';
					$('scrollImgLarge').style.top = '0';
					$('scrollImgLarge').style.marginLeft = '0';
					$('scrollImgLarge').style.marginTop = '0';

					scrollAutoScroll();
				}
			}, 40);
		}
	}, 20);
}