USGSOverlay.prototype = new google.maps.OverlayView();

/** @constructor */
function USGSOverlay(bounds, map, nomeTerminal) {

    // Now initialize all properties.
    this.bounds_ = bounds;
    this.map_ = map;
    this.nomeTerminal_ = nomeTerminal;

    // We define a property to hold the image's div. We'll
    // actually create this div upon receipt of the onAdd()
    // method so we'll leave it null for now.
    this.div_ = null;

    // Explicitly call setMap on this overlay
    this.setMap(map);
}

USGSOverlay.prototype.onAdd = function () {

    // Note: an overlay's receipt of onAdd() indicates that
    // the map's panes are now available for attaching
    // the overlay to the map via the DOM.

    // Create the DIV and set some basic attributes.
    var div = document.createElement('div');
    //div.style.borderStyle = 'solid';
    //div.style.borderWidth = '1px';
    //div.style.borderColor = '#555';
    div.style.color = '#222';
    //div.style.backgroundColor = '#fff';
    div.style.position = 'absolute';

    // Create an IMG element and attach it to the DIV.
    div.innerHTML = this.nomeTerminal_;

    // Set the overlay's div_ property to this DIV
    this.div_ = div;

    // We add an overlay to a map via one of the map's panes.
    // We'll add this overlay to the overlayLayer pane.
    var panes = this.getPanes();
    panes.overlayLayer.appendChild(div);
}

USGSOverlay.prototype.draw = function () {

    // Size and position the overlay. We use a southwest and northeast
    // position of the overlay to peg it to the correct position and size.
    // We need to retrieve the projection from this overlay to do this.
    var overlayProjection = this.getProjection();

    // Retrieve the southwest and northeast coordinates of this overlay
    // in latlngs and convert them to pixels coordinates.
    // We'll use these coordinates to resize the DIV.
    var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());
    var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());

    // Resize the image's DIV to fit the indicated dimensions.
    var div = this.div_;

    div.style.left = sw.x + 'px';
    div.style.top = sw.y + 'px';
    div.style.width = (ne.x - sw.x) + 'px';

    if (div.style.width == '0px') {
        div.style.width = (div.innerText.length * 5) + 'px'
    }

    //div.style.height = (sw.y - ne.y) + 'px';
    div.style.height = '20px';
    div.style.textAlign = "center";
    div.style.fontSize = "16px";
    div.style.fontWeight = "bold";
}

USGSOverlay.prototype.onRemove = function () {
    this.div_.parentNode.removeChild(this.div_);
    this.div_ = null;
}


function addMetrosToLatLng(latLng, bear, metros) {
    var R = 6378.1 //Radius of the Earth
    var brng = toRad(bear);  //Bearing is 90 degrees converted to radians.
    var d = metros;  //Distance in km

    //lat2  52.20444 - the lat result I'm hoping for
    //lon2  0.36056 - the long result I'm hoping for.

    var lat1 = toRad(latLng.lat());  //Current lat point converted to radians
    var lon1 = toRad(latLng.lng());  //Current long point converted to radians

    var lat2 = Math.asin(Math.sin(lat1) * Math.cos(d / R) +
         Math.cos(lat1) * Math.sin(d / R) * Math.cos(brng));

    var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(d / R) * Math.cos(lat1),
                 Math.cos(d / R) - Math.sin(lat1) * Math.sin(lat2));

    var lat2 = toDegrees(lat2);
    var lon2 = toDegrees(lon2);
    return new google.maps.LatLng(lat2, lon2);
}

function toRad(Value) {
    /** Converts numeric degrees to radians */
    return Value * Math.PI / 180;
}
function toDegrees(angle) {
    return angle * (180 / Math.PI);
}