var HEGA = HEGA || {};

	/**
Provides utility functions for ratio scaling.

@author Aaron Clinger
@version 04/03/09
*/
HEGA.ratioUtil = {};

/**
	Determines the ratio of width to height.
	
	@param sizeRect: The area's width and height expressed as a <code>Rectangle</code>. The <code>Rectangle</code>'s <code>x</code> and <code>y</code> values are ignored.
*/
HEGA.ratioUtil.widthToHeight = function(sizeRect) {
	return sizeRect.getWidth() / sizeRect.getHeight();
};

/**
	Determines the ratio of height to width.
	
	@param sizeRect: The area's width and height expressed as a <code>Rectangle</code>. The <code>Rectangle</code>'s <code>x</code> and <code>y</code> values are ignored.
*/
HEGA.ratioUtil.heightToWidth = function(sizeRect) {
	return sizeRect.getHeight() / sizeRect.getWidth();
};

/**
	Scales an area's width and height while preserving aspect ratio.
	
	@param sizeRect: The area's width and height expressed as a <code>Rectangle</code>. The <code>Rectangle</code>'s <code>x</code> and <code>y</code> values are ignored.
	@param decimalPercentage: The amount you wish to scale by expressed as decimal percentage
	@param snapToPixel: Force the scale to whole pixels <code>true</code>, or allow sub-pixels <code>false</code>.
*/
HEGA.ratioUtil.scale = function(sizeRect, decimalPercentage, snapToPixel) {
  snapToPixel = (typeof snapToPixel === 'undefined') ? true : snapToPixel;
  
	return HEGA.ratioUtil._defineRect(sizeRect, sizeRect.getWidth() * decimalPercentage, sizeRect.getHeight() * decimalPercentage, snapToPixel);
};

/**
	Scales the width of an area while preserving aspect ratio.
	
	@param sizeRect: The area's width and height expressed as a <code>Rectangle</code>. The <code>Rectangle</code>'s <code>x</code> and <code>y</code> values are ignored.
	@param height: The new height of the area.
	@param snapToPixel: Force the scale to whole pixels <code>true</code>, or allow sub-pixels <code>false</code>.
*/
HEGA.ratioUtil.scaleWidth = function(sizeRect, height, snapToPixel) {
  snapToPixel = (typeof snapToPixel === 'undefined') ? true : snapToPixel;
  
	return HEGA.ratioUtil._defineRect(sizeRect, height * HEGA.ratioUtil.widthToHeight(sizeRect), height, snapToPixel);
};

/**
	Scales the height of an area while preserving aspect ratio.
	
	@param size: The area's width and height expressed as a <code>Rectangle</code>. The <code>Rectangle</code>'s <code>x</code> and <code>y</code> values are ignored.
	@param width: The new width of the area.
	@param snapToPixel: Force the scale to whole pixels <code>true</code>, or allow sub-pixels <code>false</code>.
*/
HEGA.ratioUtil.scaleHeight = function(sizeRect, width, snapToPixel) {  
  snapToPixel = (typeof snapToPixel === 'undefined') ? true : snapToPixel;
  
	return HEGA.ratioUtil._defineRect(sizeRect, width, width * HEGA.ratioUtil.heightToWidth(sizeRect), snapToPixel);
};

/**
	Resizes an area to fill the bounding area while preserving aspect ratio.
	
	@param size: The area's width and height expressed as a <code>Rectangle</code>. The <code>Rectangle</code>'s <code>x</code> and <code>y</code> values are ignored.
	@param bounds: The area to fill. The <code>Rectangle</code>'s <code>x</code> and <code>y</code> values are ignored.
	@param snapToPixel: Force the scale to whole pixels <code>true</code>, or allow sub-pixels <code>false</code>.
*/
HEGA.ratioUtil.scaleToFill = function(sizeRect, boundsRect, snapToPixel) {
  snapToPixel = (typeof snapToPixel === 'undefined') ? true : snapToPixel;
  
	var scaledRect = HEGA.ratioUtil.scaleHeight(sizeRect, boundsRect.getWidth(), snapToPixel);
	
	if (scaledRect.getHeight() < boundsRect.getHeight())
		scaledRect = HEGA.ratioUtil.scaleWidth(sizeRect, boundsRect.getHeight(), snapToPixel);
	
	return scaledRect;
};

/**
	Resizes an area to the maximum size of a bounding area without exceeding while preserving aspect ratio.
	
	@param size: The area's width and height expressed as a <code>Rectangle</code>. The <code>Rectangle</code>'s <code>x</code> and <code>y</code> values are ignored.
	@param bounds: The area the rectangle needs to fit within. The <code>Rectangle</code>'s <code>x</code> and <code>y</code> values are ignored.
	@param snapToPixel: Force the scale to whole pixels <code>true</code>, or allow sub-pixels <code>false</code>.
*/
HEGA.ratioUtil.scaleToFit = function(sizeRect, boundsRect, snapToPixel) {
  snapToPixel = (typeof snapToPixel === 'undefined') ? true : snapToPixel;
  
	var scaledRect = HEGA.ratioUtil.scaleHeight(sizeRect, boundsRect.getWidth(), snapToPixel);
	
	if (scaledRect.getHeight() > boundsRect.getHeight())
		scaledRect = HEGA.ratioUtil.scaleWidth(sizeRect, boundsRect.getHeight(), snapToPixel);
	
	return scaledRect;
};

HEGA.ratioUtil._defineRect = function(sizeRect, width, height, snapToPixel) {
  snapToPixel = (typeof snapToPixel === 'undefined') ? true : snapToPixel;
  
	var scaledRect = sizeRect.clone();
	scaledRect.setWidth(snapToPixel ? Math.round(width) : width);
	scaledRect.setHeight(snapToPixel ? Math.round(height) : height);
	
	return scaledRect;
};
