/**
 * @author Radu Almasan
 * @version 0.1
 */



/**
 *
 */
google.maps.StopWindow = function StopWindow(opts) {
    opts = opts || {};
    if (opts.latlng) this.latlng_ = opts.latlng;
    if (opts.map) this.map_ = opts.map;

    if (this.map_ && this.latlng_) this.open(this.map_, this.latlng_);
}



/**
 * StopWindow extends GOverlay class from the Google Maps API
 */
google.maps.StopWindow.prototype = new google.maps.OverlayView();



/**
 *
 */
google.maps.StopWindow.prototype.onAdd = function() {
    div = document.createElement("div");
    jQuery(div).addClass('stopwindow');

    this.div_ = div;
}



/**
 * Redraw the Bar based on the current projection and zoom level
 */
google.maps.StopWindow.prototype.draw = function() {
    //this.onRemove();

    // Add label and set the type (color)
    jQuery(this.div_).html(this.label_ || '');

    if ( ! this.div_) return;

    var panes = this.getPanes();
    panes.floatPane.appendChild(this.div_);

    // Calculate the DIV coordinates of two opposite corners of our bounds to
    // get the size and position of our Bar
    var pixPosition = this.getProjection().fromLatLngToDivPixel(this.latlng_);
    if ( ! pixPosition) return;

    // Now position our DIV based on the DIV coordinates of our bounds
    this.div_.style.left = (pixPosition.x + 9) + "px";
    this.div_.style.top = (pixPosition.y - 26) + "px";
};



/**
 *
 */
google.maps.StopWindow.prototype.onRemove = function() {
    if (this.div_) {
        this.div_.parentNode.removeChild(this.div_);
        this.div_ = null;
    }
}



/**
 * Sets the position where the box will appear
 */
google.maps.StopWindow.prototype.setPosition = function(latlng) {
    latlng = latlng || {};
    if (typeof latlng.lat != 'function' || typeof latlng.lng != 'function')
        throw('Object sent is not of the correct format. Instance of google.maps.LatLng needed.');
    else
        this.latlng_ = latlng;
}



/**
 *
 */
google.maps.StopWindow.prototype.setLabel = function(content) {
    this.label_ = label;
}



/**
 *  latlng, type, content
 */
google.maps.StopWindow.prototype.open = function(map, marker) {
    if (map) this.map_ = map;
    if (marker) {
        this.maker_ = marker;
        this.setPosition(marker.getPosition());
        if (marker.label) this.label_ = marker.label;
    }

    if ( ! this.map_ || ! this.latlng_) {
        if ( ! this.map_) throw('No map given');
        if ( ! this.latlng_) throw('No position given.');
        return;
    }

    this.setMap(this.map_);
}
