//
// IconRoll.js
//
// Copyright 2005 by Anthony Howe.  All rights reserved.
//

if (window.contains == null)
	alert('Requires Box.js script.');

/////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////

function IconRollResize(e)
{
	e = getEvent(this, e);

	var img = e.currentTarget;
	var w = img.iconRoll.maxWidth;
	var h = img.iconRoll.maxHeight;

	img.width = w;
	img.height = h;

	for (var curr = img.chainPrev; curr != null; curr = curr.chainPrev) {
		if (img.iconRoll.minWidth + img.iconRoll.stepWidth <= w)
			w -= img.iconRoll.stepWidth;

		if (img.iconRoll.minHeight + img.iconRoll.stepHeight <= h)
			h -= img.iconRoll.stepHeight;

		curr.width = w;
		curr.height = h;
	}

	w = img.iconRoll.maxWidth;
	h = img.iconRoll.maxHeight;

	for (var curr = img.chainNext; curr != null; curr = curr.chainNext) {
		if (img.iconRoll.minWidth + img.iconRoll.stepWidth <= w)
			w -= img.iconRoll.stepWidth;

		if (img.iconRoll.minHeight + img.iconRoll.stepHeight <= h)
			h -= img.iconRoll.stepHeight;

		curr.width = w;
		curr.height = h;
	}
}

function ShowCoords(e, img)
{
	alert(
		'mouse=('+pageX(e.clientX)+', '+pageY(e.clientY)+') box=('
		+getOffsetLeft(img)+', '+getOffsetTop(img)+', '
		+(getOffsetLeft(img)+img.offsetWidth)+', '+(getOffsetTop(img)+img.offsetHeight)
		+')'
	);
}

function IconRollNormal(e)
{
	e = getEvent(this, e);

	if (e.currentTarget.chainHead != null && !contains(e.currentTarget, e.pageX, e.pageY)) {

//	for (var leftMost = e.currentTarget; leftMost.chainPrev != null; leftMost = leftMost.chainPrev)
//		;
//	for (var rightMost = e.currentTarget; rightMost.chainNext != null; rightMost = rightMost.chainNext)
//		;
//	if (!containsY(e.currentTarget, e.pageY)
//	|| (e.currentTarget == leftMost && e.pageX < getOffsetLeft(leftMost))
//	|| (e.currentTarget == rightMost && getOffsetLeft(rightMost)+rightMost.offsetWidth <= e.pageX)
//	) {
//		// Reset those to the left.
//		for (var img = e.currentTarget.chainPrev; img != null; img = img.chainPrev) {
//			img.width = img.original.offsetWidth;
//			img.height = img.original.offsetHeight;
//			img.style.top = img.original.offsetTop +'px';
//			img.style.left = img.original.offsetLeft +'px';
//		}

		// Reset the current image and those to the right.
		for (var img = e.currentTarget.chainHead; img != null; img = img.chainNext) {
			img.width = img.original.offsetWidth;
			img.height = img.original.offsetHeight;
			img.style.top = img.original.offsetTop +'px';
			img.style.left = img.original.offsetLeft +'px';
		}
	}
}

/*
 * @param arg
 *	An object containin the following properties:
 *
 *	{
 *		idPrefix,	// string
 *		maxWidth,
 *		maxHeight,
 *		minWidth,
 *		minHeight,
 *		stepWidth,
 *		stepHeight,
 *	}
 */
function IconRollInit(arg)
{
	var prevImg = null;

	for (var i = 0; ; i++) {
		// Find the first image id="prefix0".
		var img = document.getElementById(arg.idPrefix+i);
		if (img == null)
			break;

		// Remember the original box.
		img.original = new Box(img.offsetLeft, img.offsetTop, img.width, img.height);

		// Setup the handlers.
		img.onmouseover = IconRollResize;

		// Make sure we can get our arguments in event handlers.
		img.iconRoll = arg;

		// Build a double-linked list of images.
		if (prevImg != null)
			prevImg.chainNext = img;
		img.chainPrev = prevImg;
		img.chainNext = null;
		prevImg = img;
	}

	var glass = document.getElementById(arg.container);
	glass.chainHead = document.getElementById(arg.idPrefix+0);
	glass.onmouseout = IconRollNormal;
}



/////////////////////////////////////////////////////////////
// END
/////////////////////////////////////////////////////////////
