/* Copyright (c) 2008 Avencia, Inc., published under the Clear BSD
 * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
 * full text of the license. */

/**
 * @requires OpenLayers/Layer.js
 * @requires OpenLayers/Layer/Markers.js
 */

/**
 * Class: OpenLayers.Layer.Annotations
 * Draw divs as 'boxes' on the layer. 
 *
 * Inherits from:
 *  - <OpenLayers.Layer.Markers>
 */
OpenLayers.Layer.Annotations = OpenLayers.Class(OpenLayers.Layer.Markers, {

    /**
     * Constructor: OpenLayers.Layer.Annotations
     *
     * Parameters:
     * name - {String} 
     * options - {Object} Hashtable of extra options to tag onto the layer
     */
    initialize: function (name, options) {
        OpenLayers.Layer.Markers.prototype.initialize.apply(this, arguments);
    },
    
    /** 
     * Method: drawMarker
     * Calculate the pixel location for the marker, create it, and 
     *    add it to the layer's div.
     *    This is overridden so we can render the div first and have
     *    access to its properties for calculated offsets
     *
     * Parameters:
     * marker - {<OpenLayers.Marker>} 
     */
    drawMarker: function(marker) {
        var px = this.map.getLayerPxFromLonLat(marker.lonlat);
        if (px == null) {
            marker.display(false);
        } else {
            var markerDiv = marker.draw(px);

            if (!marker.drawn) {
                this.div.appendChild(markerDiv);
                
                //We calculate offsets here and not on the annotation marker because the element height 
                //and width are not set until it is actually rendered to the DOM (above)
                if(marker.calculateOffset) {
                    px = px.offset(marker.calculateOffset(marker.div));
                    OpenLayers.Util.modifyDOMElement(marker.div, null, px);
                }
                
                marker.px = px;
                marker.drawn = true;
            }
        }
    },
    
    /**
     * APIMethod: removeMarker 
     * 
     * Parameters:
     * marker - {<OpenLayers.Marker.Box>} 
     */
    removeMarker: function(marker) {
        if (this.markers && this.markers.length) {
            OpenLayers.Util.removeItem(this.markers, marker);
            if ((marker.div !== null) && (marker.div.parentNode == this.div) ) {
                this.div.removeChild(marker.div);
                marker.drawn = false;
            }
        }
    },

    CLASS_NAME: "OpenLayers.Layer.Annotations"
});
