﻿function BoundingBox(bounds, backgroundcolor, borderColor, percentOpacity)
{
    this.bounds_ = bounds;

    this.LEFT_POS = 1;
    this.RIGHT_POS = 2;
    this.TOP_POS = 3;
    this.BOTTOM_POS = 4;
    this.DEFAULT_POS = -1;
    this.DEFAULT_WIDTH = 2000;
    this.DEFAULT_HEIGHT = 2000;

    if (percentOpacity)
    {
        if (percentOpacity < 0) { percentOpacity = 0; }
        if (percentOpacity > 100) { percentOpacity = 100; }
    }
    this.percentOpacity_ = percentOpacity;
    this.backgroundcolor_ = backgroundcolor;
    this.borderColor_ = borderColor;
}

// required inheritance from GOverlay
BoundingBox.prototype = new GOverlay();

// required method implementation
BoundingBox.prototype.initialize = function(map)
{
    var dragbox = $.create("div").attr('id', 'drbx')
        .css('font-size', "0px").css('border', "1px solid ")
        .css('position', "absolute").css('background-color', this.backgroundcolor_)
        .css('border-color', this.borderColor_);
    this.div_ = dragbox.get(0);
    map.getPane(G_MAP_MAP_PANE).appendChild(this.div_);

    this.map_ = map;
    if (this.percentOpacity_)
    {
        if (typeof (this.div_.style.filter) == 'string') { dragbox.css('filter', 'alpha(opacity:' + this.percentOpacity_ + ')'); }
        if (typeof (this.div_.style.KHTMLOpacity) == 'string') { dragbox.css('KHTMLOpacity', this.percentOpacity_ / 100); }
        if (typeof (this.div_.style.MozOpacity) == 'string') { dragbox.css('MozOpacity', this.percentOpacity_ / 100); }
        if (typeof (this.div_.style.opacity) == 'string') { dragbox.css('opacity', this.percentOpacity_ / 100); }
    }
}

BoundingBox.prototype.setBounds = function(bounds)
{
    this.bounds_ = bounds;
    this.redraw(true);
}

// required method implementation
BoundingBox.prototype.remove = function()
{
    this.div_.parentNode.removeChild(this.div_);
}

// required method implementation
BoundingBox.prototype.copy = function()
{
    return new BoundingBox(this.bounds_, this.backgroundcolor_, this.percentOpacity_)
}

// required method implementation
BoundingBox.prototype.redraw = function(force)
{
    // We only need to redraw if the coordinate system has changed
    if (!force) return;
    // Calculate the DIV coordinates of two opposite corners of our bounds to
    // get the size and position of our rectangle
    var c1 = this.map_.fromLatLngToDivPixel(this.bounds_.getSouthWest());
    var c2 = this.map_.fromLatLngToDivPixel(this.bounds_.getNorthEast());

    var mapBounds = this.map_.getBounds();
    var c = mapBounds.containsBounds(this.bounds_);
    var c1CurrentMap = this.map_.fromLatLngToDivPixel(mapBounds.getSouthWest());
    var c2CurrentMap = this.map_.fromLatLngToDivPixel(mapBounds.getNorthEast());

    var currentPosTop = this.DEFAULT_POS;
    var currentPosLeft = this.DEFAULT_POS;

    if (c2CurrentMap.y < c2.y)
        currentPosTop = this.TOP_POS;
    else if (c1.y < c1CurrentMap.y)
        currentPosTop = this.BOTTOM_POS;

    if (c2.x < c2CurrentMap.x)
        currentPosLeft = this.RIGHT_POS;
    else if (c1CurrentMap.x < c1.x)
        currentPosLeft = this.LEFT_POS;

    // Now position our DIV based on the DIV coordinates of our bounds
    var width = Math.abs(c2.x - c1.x);
    var height = Math.abs(c2.y - c1.y);
    var newWidth = width;
    var newHeight = height;
    if (width > this.DEFAULT_WIDTH)
        newWidth = this.DEFAULT_WIDTH;
    if (height > this.DEFAULT_HEIGHT)
        newHeight = this.DEFAULT_HEIGHT;

    var left = (Math.min(c2.x, c1.x));
    var top = (Math.min(c2.y, c1.y));

    var newTop = top;
    if (currentPosTop == this.BOTTOM_POS)
        newTop = (top + height - newHeight);
    else if (currentPosTop == this.DEFAULT_POS)
        newTop = (Math.min(c2CurrentMap.y, c1CurrentMap.y)) - 1;

    var newLeft = left;
    if (currentPosLeft == this.RIGHT_POS)
        newLeft = (left + width - newWidth);
    else if (currentPosLeft == this.DEFAULT_POS)
        newLeft = (Math.min(c2CurrentMap.x, c1CurrentMap.x)) - 1;

    $(this.div_).css('width', newWidth + "px").css('height', newHeight + "px").css('left', newLeft + "px").css('top', newTop + "px");
}