
/* Used to scale objects to fit the control's size
 * If maintainAspectRatio is false, this will center the element.
 * @param element element to scale
 * @param wpfeControl control to size the contents to
 * @param maintainAspectRatio false to stretch to fit, true to keep ratio.
 */
function scaleElement(element, wpfeControl, maintainAspectRatio)
{
	var width = wpfeControl.clientWidth;
	var height = wpfeControl.clientHeight;
	
	var naturalWidth = element.Width;
	var naturalHeight = element.Height;
	
	var scaleRatio = naturalWidth / naturalHeight;
	var offsetX = 0;
	var offsetY = 0;
	
	if (maintainAspectRatio) {
		if (width / scaleRatio > height) {
			offsetX = (width - height * scaleRatio) / 2;
			width = height * scaleRatio;
		}
		else {
			offsetY = (height - width / scaleRatio) / 2;
			height = width / scaleRatio;
		}
	}
	
	var scaleX = width / naturalWidth;
	var scaleY = height / naturalHeight;
	
	setScale(element, scaleX, scaleY, offsetX, offsetY, wpfeControl);
}

function setScale(element, x, y, offsetX, offsetY, wpfeControl) {
	var renderTransform = "<TransformGroup>"
	renderTransform += "<ScaleTransform ScaleX='" + x + "' ScaleY='" + y + "'/>";
	renderTransform += "<TranslateTransform X='" + offsetX + "' Y='" + offsetY + "'/>";
	renderTransform += "</TransformGroup>";
	
	var transform = wpfeControl.createFromXaml(renderTransform);
	element.RenderTransform = transform;
}