function centerImgInBox(img, targetImgWidth, targetImgHeight, boxWidth, boxHeight, boxLeftMargin, boxTopMargin) {	
	if (!img)
		return;

	if (targetImgWidth < 1 || targetImgHeight < 1 || boxWidth < 1 || boxHeight < 1)
		return;

	if (targetImgWidth > boxWidth)
		targetImgWidth = boxWidth;

	if (targetImgHeight > boxHeight)
		targetImgHeight = boxHeight;

	var curWidth = img.width;
	var curHeight = img.height;

	if (curWidth == 0 || curHeight == 0)
		return;

	var scaleRatio;
	// find ratio against longest side
	if (curWidth >= curHeight)
		scaleRatio = targetImgWidth / curWidth;
	else
		scaleRatio = targetImgHeight / curHeight;

	// don't scale the image _up_
	if (scaleRatio > 1)
		scaleRatio = 1;

	curWidth *= scaleRatio;
	curHeight *= scaleRatio;

	img.width = curWidth;
	img.height = curHeight;

	if (!boxLeftMargin)
		boxLeftMargin = 0;
	if (!boxTopMargin)
		boxTopMargin = 0;

	var left = ((boxWidth - curWidth) / 2) + boxLeftMargin;
	var top = ((boxHeight - curHeight) / 2) + boxTopMargin;

	img.style.position = "absolute";
	img.style.left = left + "px";
	img.style.top = top + "px";
//	img.style.width = curWidth + "px";
//	img.style.height = curHeight + "px";
}

function scaleImg(img, curImgWidth, curImgHeight, maxImgWidth, maxImgHeight) {
    scaleImg(img, curImgWidth, curImgHeight, maxImgWidth, maxImgHeight, false);
}

function scaleImg(img, curImgWidth, curImgHeight, maxImgWidth, maxImgHeight, allowScaleUp) {
	if (curImgWidth == 0 || curImgHeight == 0)
		return;

	if (maxImgWidth < 1 || maxImgHeight < 1)
		return;

	var left = 0;
	if (img.style.left && img.style.left.length > 2) {
		left = img.style.left.substring(0, img.style.left.length - 2);
		left -= 0; // cast
	}

	var top = 0;
	if (img.style.top && img.style.top.length > 2) {
		top = img.style.top.substring(0, img.style.top.length - 2);
		top -= 0; // cast
	}

	var scaleRatio;
	// find ratio against longest side
	if (curImgWidth >= curImgHeight)
		scaleRatio = maxImgWidth * 1.0 / curImgWidth;
	else
		scaleRatio = maxImgHeight * 1.0 / curImgHeight;

    if (scaleRatio > 1 && !allowScaleUp)
		scaleRatio = 1;

	var scaledWidth = Math.round(curImgWidth * scaleRatio);
	var scaledHeight = Math.round(curImgHeight * scaleRatio);

	var reviseScaleRatio = 1;
	if (scaledWidth + left > maxImgWidth)
		reviseScaleRatio = maxImgWidth * 1.0 / (scaledWidth + left);
	scaleRatio *= reviseScaleRatio;

	reviseScaleRatio = 1;
	if (scaledHeight > maxImgHeight)
		reviseScaleRatio = maxImgHeight * 1.0 / (scaledHeight + top);
	scaleRatio *= reviseScaleRatio;

	scaledWidth = Math.round(curImgWidth * scaleRatio);
	scaledHeight = Math.round(curImgHeight * scaleRatio);

	var scaledLeft = Math.round(left * scaleRatio);
	img.style.left = scaledLeft + "px";

	var scaledTop = Math.round(top * scaleRatio);
	img.style.top = scaledTop + "px";

	img.width = scaledWidth;
	img.height = scaledHeight;
	img.style.width = scaledWidth + "px";
	img.style.height = scaledHeight + "px";
}

function getPaperWidthHeight(inventoryId, Constants){
    var dim = new Array(2);
    if (inventoryId == Constants.INVENTORY_PRINT_4X6 || 
		inventoryId == Constants.INVENTORY_PRINT_20X30) {
		dim[0] = 6.0;
		dim[1] = 4.0;
		return dim;
    } else if (inventoryId == Constants.INVENTORY_PRINT_5X7 ||
			   inventoryId == Constants.INVENTORY_PRINT_WALLET) {
		dim[0] = 7.0;
		dim[1] = 5.0;
		return dim;
    } else if (inventoryId == Constants.INVENTORY_PRINT_8X10 ||
			   inventoryId == Constants.INVENTORY_PRINT_16x20) {
		dim[0] = 10.0;
		dim[1] = 8.0;
		return dim;
    }
	return null;
}

function getCroppingParams(photoWidth, photoHeight, inventoryId, Constants){
	//cast to doubles
	photoWidth *= 1.0;
	photoHeight *= 1.0;

	var paperDim = getPaperWidthHeight(inventoryId, Constants);
	var paperWidth = paperDim[0];
	var paperHeight = paperDim[1];
	var whxy = new Array(4);

	if ((photoWidth < photoHeight && paperWidth >= paperHeight) || (photoWidth > photoHeight && paperWidth <= paperHeight)) {
		// photo and paper have opposite aspect ratios, swap paper dimensions
		var temp = paperWidth;
		paperWidth = paperHeight;
		paperHeight = temp;
	}

	if (photoWidth / photoHeight > paperWidth / paperHeight) {
		// crop the sides
		whxy[0] = photoHeight * paperWidth / paperHeight;
		whxy[1] = photoHeight;
		whxy[2] = (photoWidth - whxy[0]) / 2.0;
		whxy[3] = 0;
	} else {
		// crop 20% off the top, 80% off the bottom.
		whxy[0] = photoWidth;
		whxy[1] = photoWidth * paperHeight / paperWidth;
		whxy[2] = 0;
		whxy[3] = .2 * (photoHeight - whxy[1]);
	}

	whxy[0] = Math.round(whxy[0]);
	whxy[1] = Math.round(whxy[1]);
	whxy[2] = Math.round(whxy[2]);
	whxy[3] = Math.round(whxy[3]);

	return whxy;
}


