/*global Mediasite*/
this.Mediasite = this.Mediasite || {};
Mediasite.Geometry = Mediasite.Geometry || {};


Mediasite.Geometry.Rect = function (left, top, width, height) {
    this.left = function () { return left; };
    this.top = function () { return top; };
    this.width = function () { return width; };
    this.height = function () { return height; };
    this.bottom = function () { return top + height; };
    this.right = function () { return left + width; };

    this.equals = function (other) {
        if (this === other) {
            return true;
        }
        return this.left() === other.left() &&
			this.top() === other.top() &&
			this.width() === other.width() &&
			this.height() === other.height();
    };

    this.area = function () {
        return width * height;
    };
};

Mediasite.Geometry.Size = function (width, height) {
    this.width = function () { return width; };
    this.height = function () { return height; };

    this.equals = function (other) {
        if (!other) {
            return false;
        }
        if (this === other) {
            return true;
        }
        return this.width() === other.width() &&
			this.height() === other.height();
    };

    this.toString = function () {
        return '[Size ' + width + 'x' + height + ']';
    };

    this.area = function () {
        return width * height;
    };
};
Mediasite.Geometry.Size._zero = new Mediasite.Geometry.Size(0, 0);
Mediasite.Geometry.Size.Zero = function () { return Mediasite.Geometry.Size._zero; };

Mediasite.Geometry.Point = function (x, y) {
    this.x = function () { return x; };
    this.y = function () { return y; };

    this.equals = function (other) {
        if (!other) {
            return false;
        }
        if (this === other) {
            return true;
        }
        return this.x() === other.x() &&
			this.y() === other.y();
    };

    this.toString = function () {
        return '[Point x:' + x + ', y:' + y + ']';
    };
};

Mediasite.Geometry.ScaleToFit = function (childRect, containerRect) {
    /// Returns suggested dimensions for childRect so that's letterboxed inside containerRect
    var containerAspectRatio = containerRect.width() / containerRect.height();
    var childAspectRatio = childRect.width() / childRect.height();


    if (Math.abs(containerAspectRatio - childAspectRatio) < .05) {
        // child and container are approximately the same aspect ratio
        return containerRect;
    } else if (containerAspectRatio > childAspectRatio) {
        // child is taller/skinnier than container
        var height = containerRect.height();
        var top = 0;

        var width = height * childAspectRatio;
        var left = (containerRect.width() - width) / 2;

        return new Mediasite.Geometry.Rect(left, top, width, height);
    } else {
        // child is shorter/wider than container
        var width = containerRect.width();
        var left = 0;

        var height = width / childAspectRatio;
        var top = (containerRect.height() - height) / 2;

        return new Mediasite.Geometry.Rect(left, top, width, height);
    }
};