var imageBox = new Object();
imageBox.id = null;
imageBox.thumbs = new Array();
imageBox.photos = new Array();
imageBox.current = 0;
imageBox.scrollInterval = null;


document.ready(initImageBox);
function initImageBox()
{
	imageBox.id = document.getElementById("itemimagebox");
	if (imageBox.id == null || imageBox.id == "undefined" || imageBox.id == undefined)
		return;

	imageBox.mainImageBox = document.getElementById("mainitemimagebox");
	imageBox.mainImage = document.getElementById("mainitemimage");
	imageBox.mainCaption = document.getElementById("mainitemimagecaption");
	imageBox.thumbBox = document.getElementById("itemthumbbox");
	imageBox.scrollLeft = document.getElementById("itemthumbleft");
	imageBox.scrollRight = document.getElementById("itemthumbright");
	imageBox.thumbStrip = document.getElementById("itemthumbstrip");


	var count = 0;
	for (index in imageBox.thumbStrip.childNodes)
	{

		if (imageBox.thumbStrip.childNodes[index].nodeName == "DIV")
		{
			var thumb = new Object();
			thumb = imageBox.thumbStrip.childNodes[index]
			thumb.index = count;

			for (childIndex in thumb.childNodes)
			{
				if (thumb.childNodes[childIndex].nodeName == "IMG")
				{
					thumb.image = thumb.childNodes[childIndex];
					break;
				}
			}

			thumb.onmouseover = thumbHover;
			thumb.onclick = thumbClick;

			imageBox.thumbs[count] = thumb;

			var photo = new Object();
			photo.index = count;
			photo.thumbURL = thumb.image.src;
			photo.URLExtension = photo.thumbURL.substring(photo.thumbURL.search("\.(jpg)?(JPG)?(png)?(PNG)?$"), photo.thumbURL.length)
			photo.URLBase = photo.thumbURL.substring(0, photo.thumbURL.search("-Thumb" + photo.URLExtension + "$"))
			photo.URL = photo.URLBase + photo.URLExtension;
			photo.title = thumb.title;

			imageBox.photos[count] = photo;

			count++;
		}
	}

	addClassName(imageBox.thumbs[imageBox.current], "selected");
	switchImage(imageBox.current);

	if (imageBox.thumbStrip.offsetWidth > imageBox.thumbBox.offsetWidth)
	{
		imageBox.thumbBox.className = "scrollable";
		if (imageBox.thumbBox.addEventListener != undefined)
			imageBox.thumbBox.addEventListener('DOMMouseScroll', imageBoxMouseScroll, false);
		imageBox.thumbBox.onmousewheel = imageBoxMouseScroll;
	}

	imageBox.thumbStrip.leftLimit = 0;
	imageBox.thumbStrip.rightLimit = imageBox.thumbBox.offsetWidth - imageBox.thumbStrip.offsetWidth;

	setScrollArrows();

	imageBox.scrollLeft.onmouseover = leftScrollArrowHover;
	imageBox.scrollLeft.onmouseout = leftScrollArrowLeave;
	imageBox.scrollRight.onmouseover = rightScrollArrowHover;
	imageBox.scrollRight.onmouseout = rightScrollArrowLeave;
}

function setScrollArrows()
{
	if (imageBox.thumbStrip.offsetLeft >= imageBox.thumbStrip.leftLimit)
		imageBox.scrollLeft.className = "limit";
	else
		imageBox.scrollLeft.className = "";

	if (imageBox.thumbStrip.offsetLeft <= imageBox.thumbStrip.rightLimit)
		imageBox.scrollRight.className = "limit";
	else
		imageBox.scrollRight.className = "";
}

function leftScrollArrowHover(e)
{
	if (!e) var e = window.event

	if (imageBox.scrollInterval)
		clearInterval(imageBox.scrollInterval);

	imageBox.scrollInterval = setInterval("scrollThumbsLeft(6)", 10);
}

function leftScrollArrowLeave(e)
{
	if (!e) var e = window.event

	if (imageBox.scrollInterval)
		clearInterval(imageBox.scrollInterval);
}

function scrollThumbsLeft(distance)
{
	var newPos = imageBox.thumbStrip.offsetLeft + distance;
	if (newPos > imageBox.thumbStrip.leftLimit)
		newPos = imageBox.thumbStrip.leftLimit;

	imageBox.thumbStrip.style.left = newPos + "px";

	setScrollArrows();
}

function rightScrollArrowHover(e)
{
	if (!e) var e = window.event

	if (imageBox.scrollInterval)
		clearInterval(imageBox.scrollInterval);

	imageBox.scrollInterval = setInterval("scrollThumbsRight(6)", 10);
}

function rightScrollArrowLeave(e)
{
	if (!e) var e = window.event

	if (imageBox.scrollInterval)
		clearInterval(imageBox.scrollInterval);
}

function scrollThumbsRight(distance)
{
	var newPos = imageBox.thumbStrip.offsetLeft - distance;
	if (newPos < imageBox.thumbStrip.rightLimit)
		newPos = imageBox.thumbStrip.rightLimit;

	imageBox.thumbStrip.style.left = newPos + "px";

	setScrollArrows();
}

function imageBoxMouseScroll(e)
{
	if (!e) var e = window.event

	var DeltaY = 0;
	var DeltaX = 0;

	if(e.wheelDeltaY != undefined)
		DeltaY = e.wheelDeltaY / -40;
	else if (e.detail != undefined)
	{
		if (e.axis == e.VERTICAL_AXIS)
			DeltaY = e.detail;
		else
			DeltaY = 0;
	}

	if(e.wheelDeltaX != undefined)
		DeltaX = e.wheelDeltaX / -40;
	else if (e.detail != undefined)
	{
		if (e.axis == e.HORIZONTAL_AXIS)
			DeltaX = e.detail;
		else
			DeltaX = 0;
	}

	if (DeltaX != 0)
	{
		if (DeltaX > 0)
			scrollThumbsRight(DeltaX * 2);
		else if (DeltaX < 0)
			scrollThumbsLeft(Math.abs(DeltaX * 2));
	}
}


function thumbHover(e)
{
	if (!e) var e = window.event

	var target;
	if (e.target) target = e.target;
	else if (e.srcElement) target = e.srcElement;
	if (target.nodeType == 3) // defeat Safari bug
		target = target.parentNode;

	var index;
	if (target.index != undefined)
		index = target.index;
	else
		index = target.parentNode.index;

	switchImage(index);
}

function thumbClick(e)
{
	if (!e) var e = window.event

	var target;
	if (e.target) target = e.target;
	else if (e.srcElement) target = e.srcElement;
	if (target.nodeType == 3) // defeat Safari bug
		target = target.parentNode;

	var index;
	if (target.index != undefined)
		index = target.index;
	else
		index = target.parentNode.index;

	switchImage(index);

	removeClassName(imageBox.thumbs[imageBox.current], "selected");
	imageBox.current = index;
	addClassName(imageBox.thumbs[imageBox.current], "selected");
}

function switchImage(imageNumber)
{
	imageBox.mainImage.src = imageBox.photos[imageNumber].URL;
	imageBox.mainCaption.innerHTML = imageBox.photos[imageNumber].title;
}


