/*
 * World's Worst Software Javascript Image Library (c) Jason Baker 2006
 *
 *	An image library written by Jason for the following sites:
 *     www.onejasonforsale.com
 *     www.anesotericvision.com
 *     www.worldsworstsoftware.com
 *
 *
 *  Feel free to use it on your sites!
 *
 *  Note: the library has only been tested in FF 1.X+ and IE 6+
 */

WWSIMGLIB_DEBUG = false;

function imageFlip(targetImageId, sourceUrl)
{
  	if (WWSIMGLIB_DEBUG)
  	{
  		alert("imageFlip target:" + targetImageId + " image:" + sourceUrl);
	}

	var _image = document.getElementById(targetImageId);

	if (_image == false)
	{
		alert("Error: imageFlip target image \"" + targetImageId + "\" doesn't exist.");
		return false;
	}

	_image.src = sourceUrl;
	return true;
}

function showImage(target)
{
	document.getElementById(target).style.visibility="visible";
}

function hideImage(target)
{
	document.getElementById(target).style.visibility="hidden";
}


//centerMode can be "X" or "Y" or "BOTH"
function centerImage(imageId, containerId, centerMode)
{

	if ((centerMode != "X") && (centerMode != "Y") && (centerMode != "BOTH"))
	{
		alert("Error: centerImage unknown centerMode: \"" + centerMode + "\".");
		return false;
	}

	var _image = document.getElementById(imageId);

	if (_image == false)
	{
		alert("Error: centerImage target image \"" + targetImageId + "\" doesn't exist.");
		return false;
	}

	var _container = document.getElementById(containerId);

	if (_container == false)
	{
		alert("ERROR: centerImage could not find container \"" + divId + "\"");
		return false;
	}

	//copy the image into a new Image() resource so the width and height parameters will
	// properly return in case the image that was just flipped has different dims than the
	// previous image.
	var _tmpImg = new Image();
	_tmpImg.src = _image.src;

	var xoffset = 0;
	var yoffset = 0;

	if (centerMode == "X" || centerMode == "BOTH")
	{
		//determine the left offset
		xoffset = getCenteredOffset(_tmpImg.width, _container.offsetWidth);

		//move the photo
		_image.style.left = xoffset + "px";
	}

	if (centerMode == "Y" || centerMode == "BOTH")
	{
		//determine the top offset
		yoffset = getCenteredOffset(_tmpImg.height, _container.offsetHeight);

		//move the photo
		_image.style.top = yoffset + "px";
	}

	if (WWSIMGLIB_DEBUG)
	{
		var message = "[centerImage debug]\n\n";
		message += "container id is " + containerId + "\n";
		message += "container x,y is " + _container.offsetWidth + "," + _container.offsetHeight + "\n";
		message += "target image id is " + imageId + "\n";
		message += "target image x,y is " + _tmpImg.width + "," + _tmpImg.height + "\n\n";

		if (centerMode == "X")
		{
			message += "centerMode is X\n";
			message += "x offset is " + xoffset + "\n";
		}
		else if (centerMode == "Y")
		{
			message += "centerMode is Y\n";
			message += "y offset is " + yoffset + "\n";
		}
		else if (centerMode == "BOTH")
		{
			message += "centerMode is BOTH\n";
			message += "x offset is " + xoffset + "\n";
			message += "y offset is " + yoffset + "\n";
		}
		else
		{
			message += "centerMode is UNKNOWN\n";
		}

		alert(message);
	}
}

//this is a helper function for centerImage()
function getCenteredOffset(dimension, boundingdimension)
{

	var offset = boundingdimension - dimension;

	if (offset > 0)
	{
		//dont want a div by zero error
		offset = offset / 2;
	}
	else
	{
		//reset to zero if the value is zero or negative
		offset = 0;
	}

	return offset
}



