// JavaScript Document
StageUtils = {}

StageUtils.fitToStage = function (image, full)
{
	full = full == undefined ? false : full;

	var propX = image.width() / $(window).width();
	var propY = image.height() / $(window).height();
		
	if(full) {
		propX > propY ? StageUtils.fitStageHeight(image) : StageUtils.fitStageWidth(image);
	} else {
		if(propX > propY) {
			(propX - propY) < 0.2 ? StageUtils.fitStageHeight(image) : StageUtils.fitStageWidth(image);
		} else {
			(propY - propX) < 0.2 ? StageUtils.fitStageWidth(image) : StageUtils.fitStageHeight(image);
		}
	}
	
	image.css("margin-left", ($(window).width() - image.width()) / 2);
	image.css("margin-top", ($(window).height() - image.height()) / 2);
}

StageUtils.fitStageWidth = function (image)
{
	var proportion = $(window).width() / image.width();
	var imageHeight = image.height() * proportion;
	
    $(image).css("width", $(window).width());
    $(image).css("height", imageHeight);
}

StageUtils.fitStageHeight = function (image)
{
	var proportion = $(window).height() / image.height();
	var imageWidth = image.width() * proportion;
	
    $(image).css("height", $(window).height());
    $(image).css("width", imageWidth);	
}

StageUtils.fitToArea = function (image, dimensions, full)
{
	full = full == undefined ? false : full;

	var propX = image.width() / dimensions.width;
	var propY = image.height() / dimensions.height;
	
	if(full) {
		propX > propY ? StageUtils.fitAreaHeight(image, dimensions.height) : StageUtils.fitAreaWidth(image, dimensions.width);
	} else {
		if(propX > propY) {
			(propX - propY) < 0.2 ? StageUtils.fitAreaHeight(image, dimensions.height) : StageUtils.fitAreaWidth(image, dimensions.width);
		} else {
			(propY - propX) < 0.2 ? StageUtils.fitAreaWidth(image, dimensions.width) : StageUtils.fitAreaHeight(image, dimensions.height);
		}
	}
}

StageUtils.fitAreaWidth = function (image, width)
{
	var proportion = width / image.width();
	var imageHeight = image.height() * proportion;
	
    $(image).css("width", width);
    $(image).css("height", imageHeight);
}

StageUtils.fitAreaHeight = function (image, height)
{
	var proportion = height / image.height();
	var imageWidth = image.width() * proportion;
	
    $(image).css("height", height);
    $(image).css("width", imageWidth);	
}
