/*
 * 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.
	// dont do this in esoteric..
	//var _tmpImg = new Image();
	//_tmpImg.src = _image.src;

	//esoteric-specific:
	var _tmpImg = _image;

	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
}

function resizeImageToFitInsideContainer(imageId, containerId)
{
	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 image_x = _tmpImg.width;
	var image_y = _tmpImg.height;


	var container_x = _container.offsetWidth;
	var container_y = _container.offsetHeight;


	var resize_ratio = 0;

	if ((image_y > container_y) || (image_x > container_x))
	{
		//only resize if the image is larger than the target..
		if (image_x > image_y)
		{
			//image is wider than tall
			resize_ratio = (image_x * 1.0) / container_x;

			//make sure the resize ratio wouldn't make the image taller than the bounding box..
			if ((image_y / resize_ratio) > container_y)
			{
				resize_ratio = (image_y * 1.0) / container_y;
			}
		}
		else
		{
			//image is either square or taller than wide
			resize_ratio = (image_y * 1.0) / container_y;

			//make sure the resize ratio wouldn't make the image wider than the bounding box..
			if ((image_x / resize_ratio) > container_x)
			{
				resize_ratio = (image_x * 1.0) / container_x;
			}
		}

	}

	var new_image_x = 0;
	var new_image_y = 0;

	if (resize_ratio != 0)
	{
		//we need to resize the image..
		//find the scaled dimensions..
		new_image_x = image_x / resize_ratio;
		new_image_y = image_y / resize_ratio;
	}
	else
	{
		new_image_x = image_x;
		new_image_y = image_y;
	}

	_image.width = new_image_x;
	_image.height = new_image_y;

	if (WWSIMGLIB_DEBUG)
	{
		var message = "[resizeImageToFitInsideContainer debug]\n\n";
		message += "container id is " + containerId + "\n";
		message += "container x,y is " + container_x + "," + container_y + "\n";
		message += "original image id is " + imageId + "\n";
		message += "original image x,y is " + image_x + "," + image_y + "\n\n";

		if (resize_ratio == 0)
		{
			message += "image was not resized.\n";
		}
		else
		{
			message += "image was resized..\n";
			message += "  resize ratio: " + resize_ratio  + "\n";
			message += "  new image x,y is: " + new_image_x + "," + new_image_y + "\n";
		}

		alert(message);
	}

}



