var RectScaler = function() {

	 function scaleToRectangle( $s, $r, $scaleMode )
	 {
		if ( $scaleMode == null || $scaleMode == undefined )
		{
			$scaleMode = "scale";
		}
	
		s = resolveRect( $s );
		$r = resolveRect( $r );
	
		switch ( $scaleMode )
		{
			case "exact":
				s.width = $r.width;
				s.height = $r.height;
				break;
			case "scale-down":
				if ( !fitsInRectangle( s, $r ) )
				{
					s = proportionScaleByLargest( s, $r );
				}
				break;
			case "scale-fill":
				s = proportionScaleToFill( s, $r );
				break;
			default:
				if ( fitsInRectangle( s, $r ) )
				{
					s = proportionScaleByLargest( s, $r );
				}
				 else
				{
					s = scaleToRectangle( s, $r, "scale-down" );
				}
				break;
		}
	
		if ( $scaleMode != "none" ) s = centerInRectangle( s, $r );
		return s;
	}



	function proportionScaleByLargest( $s, $r )
	{
		var dW = Math.abs( $r.width - $s.width );
		var dH = Math.abs( $r.height - $s.height );

		//alert ("delta w = " + dW + ", h = " + dH );
		//return dH < dW ? proportionScaleByWidth( $s, $r ) : proportionScaleByHeight( $s, $r );
		if ( dW < dH )
		{
			$s =  proportionScaleByHeight( $s, $r );
		}
		 else
		{
			$s = proportionScaleByWidth( $s, $r );
		}
		
		if ( !fitsInRectangle( $s, $r ) )
		{
			$s = proportionScaleByLargest( $s, $r );
		}
		return $s;
	}

	function createRect( $x, $y, $width, $height )
	{
		return {x: $x, y: $y, width: $width, height: $height};
	}

	function resolveRect( $s )
	{
		return {width: $s.width ? $s.width : 0, height: $s.height ? $s.height : 0, x: $s.x ? $s.x : 0, y: $s.y ? $s.y : 0 };
	}

	function createNewRect()
	{
		return {width:0,height:0,x:0,y:0};
	}
	
	function copyRect( $r )
	{
		return {width: $r.width, height: $r.height, x: $r.x, y: $r.y };
	}
	
	function proportionScaleToFill( $s, $r ) 
	{
 				
		var returnRect = createNewRect();
	
		var scaleW = $r.width / $s.width;
		var scaleH = $r.height / $s.height;
	
		if ( scaleW > scaleH )
		{
			returnRect.width = $s.width * scaleW;
			returnRect.height = $s.height * scaleW;
		}                                       
		else
		{
			returnRect.width = $s.width * scaleH;
			returnRect.height = $s.height * scaleH;
		}   
		return returnRect;
	} 

	function proportionScaleByHeight( $s, $r )
	{
		var s = copyRect( $s );
		var p = $r.height / $s.height;

		s.width *= p;
		s.height *= p;

		return s;
	}

	function proportionScaleByWidth ( $s, $r  )
	{
		var s = copyRect( $s );
		var p = $r.width / $s.width;

		s.width *= p;
		s.height *= p;

		return s;
	} 

	function fitsInRectangle( $s, $r )
	{
		return $s.width <= $r.width && $s.height <= $r.height ? true : false;
	}

	function centerInRectangle ( $s, $r )
	{
		var s = copyRect( $s );
		s.x = ($r.width - $s.width) / 2;
		s.y = ($r.height - $s.height) / 2;
	
		return s;
	}
	
	function scaleRectangle( $r, $percent )
	{
		return {x: $r.x, y: $r.y, width: $r.width * $percent, height: $r.height * $percent };
	}
	
	function toString( $rect )
	{
		return "Rectangle {x:" + $rect.x + ", y:" + $rect.y + ", w:" + $rect.width + ", h:" + $rect.height + "}";
	}
	return {
		scaleToRectangle: scaleToRectangle,
		createRect: createRect,
		copyRect: copyRect,
		centerInRectangle: centerInRectangle,
		scaleRectangle: scaleRectangle,
		proportionScaleByHeight: proportionScaleByHeight,
		proportionScaleByWidth: proportionScaleByWidth,
		toString: toString
	}
	
}();

