/**
 * <p>Title: imageTools.js</p>
 * <p>Description: Collection of image management tools.</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: Kreber Graphics, Inc.</p>
 * @author Charlie Reading
 */

// Includes
var imagetools_js = 1;
var includes = "";
if (includes != "")
{
	alert("imageTools.js: you must first include:\n" + includes);
}

// Fit-To
kFitTo_Default = -1;
kFitTo_Inner = 0;
kFitTo_Outer = 1;


//
// Image Fitting
//
// Fitting is aligning the image to the target dimensions:
//    kFitTo_Inner  = fit-to inner dimension (no clipping)
//    kFitTo_Outer  = fit-to outer dimension (may clip)
//

/**
 * Fit width/height dimension to target's, maintaining aspect ratio.
 *
 * @param width - width of image (in points or pixels).
 * @param height - height of image (in points or pixels).
 * @param targetWidth - target width (in points or pixels).
 * @param targetHeight - target height (in points or pixels).
 * @param fitTo - how to fit-to "target" (kFitTo_x values).
 * @return - {width: x, height: y} representing width/height (likely floating in non-Round version).
 */
function fitImage(width, height, targetWidth, targetHeight, fitTo)
{
	var dim = null;

	var widthRatio = targetWidth / width;
	var heightRatio = targetHeight / height;
	switch (fitTo)
	{
		case kFitTo_Inner:
		{
			dim = ((widthRatio < heightRatio)
					? _fitImageDimension(targetWidth, height*widthRatio)
					: _fitImageDimension(width*heightRatio, targetHeight));
			break;
		}
		case kFitTo_Outer:
		{
			dim = ((widthRatio < heightRatio)
					? _fitImageDimension(width*heightRatio, targetHeight)
					: _fitImageDimension(targetWidth, height*widthRatio));
			break;
		}
	}

	return dim;
}
function fitImageRound(width, height, targetWidth, targetHeight, fitTo)
{
	var dim = fitImage(width, height, targetWidth, targetHeight, fitTo);
	return _fitImageDimension(Math.round(dim.width), Math.round(dim.height));
}
function fitImageFloor(width, height, targetWidth, targetHeight, fitTo)
{
	var dim = fitImage(width, height, targetWidth, targetHeight, fitTo);
	return _fitImageDimension(Math.floor(dim.width), Math.floor(dim.height));
}
function fitImageCeil(width, height, targetWidth, targetHeight, fitTo)
{
	var dim = fitImage(width, height, targetWidth, targetHeight, fitTo);
	return _fitImageDimension(Math.ceil(dim.width), Math.ceil(dim.height));
}


/**
 * Fit PDFDimension to targets, maintaining aspect ratio.
 *
 * @param dimension - {width: x, height: y} of image (in points or pixels).
 * @param targetWidth - target width (in points or pixels).
 * @param targetHeight - target height (in points or pixels).
 * @param fitTo - how to fit-to "target" (kFitTo_x values).
 * @return - {width: x, height: y} representing width/height (likely floating in non-Round version).
 */
function fitImageDim(dimension, targetWidth, targetHeight, fitTo)
{
	return fitImage(dimension.width, dimension.height, targetWidth, targetHeight, fitTo);
}
function fitImageDimRound(dimension, targetWidth, targetHeight, fitTo)
{
	var dim = fitImageDim(dimension, targetWidth, targetHeight, fitTo);
	return _fitImageDimension(Math.round(dim.width), Math.round(dim.height));
}
function fitImageDimFloor(dimension, targetWidth, targetHeight, fitTo)
{
	var dim = fitImageDim(dimension, targetWidth, targetHeight, fitTo);
	return _fitImageDimension(Math.floor(dim.width), Math.floor(dim.height));
}
function fitImageDimCeil(dimension, targetWidth, targetHeight, fitTo)
{
	var dim = fitImageDim(dimension, targetWidth, targetHeight, fitTo);
	return _fitImageDimension(Math.ceil(dim.width), Math.ceil(dim.height));
}


/**
 * Fit width dimension to a target, maintaining aspect ratio.
 *
 * @param width - width of image (in points or pixels).
 * @param height - height of image (in points or pixels).
 * @param targetWidth - target width (in points or pixels).
 * @param targetHeight - target height (in points or pixels).
 * @param fitTo - how to fit-to "target" (kFitTo_x values).
 * @return - width to fit targetWidth/targetHeight (likely floating in non-Round version).
 */
function fitImageWidth(width, height,
		targetWidth, targetHeight, fitTo)
{
	var imageWidth = 0;

	var widthRatio = targetWidth / width;
	var heightRatio = targetHeight / height;
	switch (fitTo)
	{
		case kFitTo_Inner:
		{
			imageWidth = ((widthRatio < heightRatio)
					? targetWidth
					: (width * heightRatio));
			break;
		}
		case kFitTo_Outer:
		{
			imageWidth = ((widthRatio < heightRatio)
					? (width * heightRatio)
					: targetWidth);
			break;
		}
	}

	return imageWidth;
}
function fitImageWidthRound(width, height, targetWidth, targetHeight, fitTo)
{
	return Math.round(fitImageWidth(width, height, targetWidth, targetHeight, fitTo));
}
function fitImageWidthFloor(width, height, targetWidth, targetHeight, fitTo)
{
	return Math.floor(fitImageWidth(width, height, targetWidth, targetHeight, fitTo));
}
function fitImageWidthCeil(width, height, targetWidth, targetHeight, fitTo)
{
	return Math.ceil(fitImageWidth(width, height, targetWidth, targetHeight, fitTo));
}


/**
 * Fit height dimension to a target, maintaining aspect ratio.
 *
 * @param width - width of image (in points or pixels).
 * @param height - height of image (in points or pixels).
 * @param targetWidth - target width (in points or pixels).
 * @param targetHeight - target height (in points or pixels).
 * @param fitTo - how to fit-to "target" (kFitTo_x values).
 * @return - height to fit targetWidth/targetHeight (likely floating in non-Round version).
 */
function fitImageHeight(width, height,
		targetWidth, targetHeight, fitTo)
{
	var imageHeight = 0;

	var widthRatio = targetWidth / width;
	var heightRatio = targetHeight / height;
	switch (fitTo)
	{
		case kFitTo_Inner:
		{
			imageHeight = ((widthRatio < heightRatio)
					? (height * widthRatio)
					: targetHeight);
			break;
		}
		case kFitTo_Outer:
		{
			imageHeight = ((widthRatio < heightRatio)
					? targetHeight
					: (height * widthRatio));
			break;
		}
	}

	return imageHeight;
}
function fitImageHeightRound(width, height, targetWidth, targetHeight, fitTo)
{
	return Math.round(fitImageHeight(width, height, targetWidth, targetHeight, fitTo));
}
function fitImageHeightFloor(width, height, targetWidth, targetHeight, fitTo)
{
	return Math.floor(fitImageHeight(width, height, targetWidth, targetHeight, fitTo));
}
function fitImageHeightCeil(width, height, targetWidth, targetHeight, fitTo)
{
	return Math.ceil(fitImageHeight(width, height, targetWidth, targetHeight, fitTo));
}

var _fitImageDimension;
if ((typeof geometry_js != 'undefined') && (geometry_js))
{
	_fitImageDimension =  Geometry.dimension;
}
else
{
	// for backward compatibility
	_fitImageDimension = function(width, height)
	{
		var dimension = [ width, height ];
		dimension.width = width;
		dimension.height = height;
		return dimension;
	}
}
