﻿(function($, module){

//------------
// BOUNDINGBOX
//------------

    // Module and provider requirements
    $.requireModules();
    $.requireProviders();

    $.BoundingBox = function(swlat, swlon, nelat, nelon) {
        //FIXME throw error if box bigger than world
        //alert('new bbox ' + swlat + ',' +  swlon + ',' +  nelat + ',' + nelon);
        this.sw = new $.LatLonPoint(swlat, swlon);
        this.ne = new $.LatLonPoint(nelat, nelon);
    };

    /**
     * getSouthWest returns a LatLonPoint of the south-west point of the bounding box
     * @returns the south-west point of the bounding box
     * @type LatLonPoint
     */
    $.BoundingBox.prototype.getSouthWest = function() {
        return this.sw;
    };

    /**
     * getNorthEast returns a LatLonPoint of the north-east point of the bounding box
     * @returns the north-east point of the bounding box
     * @type LatLonPoint
     */
    $.BoundingBox.prototype.getNorthEast = function() {
        return this.ne;
    };

    /**
     * isEmpty finds if this bounding box has zero area
     * @returns whether the north-east and south-west points of the bounding box are the same point
     * @type boolean
     */
    $.BoundingBox.prototype.isEmpty = function() {
        return this.ne == this.sw; // is this right? FIXME
    };

    /**
     * contains finds whether a given point is within a bounding box
     * @param {LatLonPoint} point the point to test with
     * @returns whether point is within this bounding box
     * @type boolean
     */
    $.BoundingBox.prototype.contains = function(point){
        return point.lat >= this.sw.lat && point.lat <= this.ne.lat && point.lon >= this.sw.lon && point.lon <= this.ne.lon;
    };

    /**
     * toSpan returns a LatLonPoint with the lat and lon as the height and width of the bounding box
     * @returns a LatLonPoint containing the height and width of this bounding box
     * @type LatLonPoint
     */
    $.BoundingBox.prototype.toSpan = function() {
        return new $.LatLonPoint( Math.abs(this.sw.lat - this.ne.lat), Math.abs(this.sw.lon - this.ne.lon) );
    };

    /**
     * extend extends the bounding box to include the new point
     */
    $.BoundingBox.prototype.extend = function(point) {
        if(this.sw.lat > point.lat) {
            this.sw.lat = point.lat;
        }
        if(this.sw.lon > point.lon) {
            this.sw.lon = point.lon;
        }
        if(this.ne.lat < point.lat) {
            this.ne.lat = point.lat;
        }
        if(this.ne.lon < point.lon) {
            this.ne.lon = point.lon;
        }
        return;
    };

    // Register
	$.registerModule(module);

})(mxn, 'boundingbox');