/* 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/Marker.js
 */

/**
 * Class: OpenLayers.Marker.Annotation
 * Note that this class is not implemented to handle changes to its attributes after
 * initialization.  This object is currently not mutable.
 *
 * Inherits from:
 *  - <OpenLayers.Marker> 
 */
OpenLayers.Marker.Annotation = OpenLayers.Class(OpenLayers.Marker, {

    /** 
     * Property: text 
     * {String} the annotation text
     */
    text: null,

    /** 
     * Property: div 
     * {DOMElement} 
     */
    div: null,
    
    /** 
     * Property: px 
     * {<OpenLayers.Pixel>} 
     */
    px: null,
    
    /** 
     * Property: calculateOffset 
     * {Function(px, div)} function to calculate offset where the annotation should be placed.
     * ie. the pixel value that you want to put into OpenLayers.Pixel.offset(yourPixelHere) method
     * Returns a new {OpenLayers.Pixel}
     */
     calculateOffset: null,

    /** 
     * Constructor: OpenLayers.Marker.Annotation
     *
     * Parameters:
     * text - {String} the annotation text
     * {Function(px, div)} function to calculate offset where the annotation should be placed.
     * 	ie. the pixel value that you want to put into OpenLayers.Pixel.offset(yourPixelHere) method
     * options - {Object} An optional object whose properties will be set on the annotation.
     */
    initialize: function(text, lonlat, calculateOffset, options) {
        this.text = text;
        this.lonlat = lonlat;
        this.calculateOffset = calculateOffset;
        this.div = OpenLayers.Util.createDiv();
        
        if (options) {
            var option, styleProp;
            for (option in options) {
                if (options.hasOwnProperty(option)) {
                    
                    //style is readonly so we handle it specifically
                    if (option === 'style') {
                        for (styleProp in options.style) {
                            if (options.style.hasOwnProperty(styleProp)) {
                                this.div.style[styleProp] = options.style[styleProp];
                            }
                        }
                    
                    }
                    else {
                        this.div[option] = options[option];
                    }
                }
            }
        }

        this.div.innerHTML = text;
        this.div.style.overflow = 'hidden';        
        this.events = new OpenLayers.Events(this, this.div, null);
    },

    /**
     * Method: destroy 
     */    
    destroy: function() {
        this.text = null;
        this.lonlat = null;
        this.div = null;
        this.calculateOffset = null;

        OpenLayers.Marker.prototype.destroy.apply(this, arguments);
    },

    /** 
    * Method: draw
    * 
    * Parameters:
    * px - {<OpenLayers.Pixel>} 
    * 
    * Returns: 
    * {DOMElement} A new DOM Image with this marker«s icon set at the 
    *         location passed-in
    */
    draw: function(px) {
        if (this.div && px) {
            OpenLayers.Util.modifyDOMElement(this.div, null, px);
            return this.div;
        }
    }, 
    
    /**
     * Method: moveTo
     * move div to passed in px and use calculateOffset if available.
     *
     * Parameters:
     * px - {<OpenLayers.Pixel>} 
     */
    moveTo: function (px) {
        if (this.div && px) {
            this.px = px;
            this.lonlat = this.map.getLonLatFromLayerPx(this.px);
            
            if (this.calculateOffset) {
                this.px = this.px.offset(this.calculateOffset(this.div));
            }
            OpenLayers.Util.modifyDOMElement(this.div, null, this.px);            
        }
    },
    
    /**
     * Method: display
     * Hide or show the annotation marker
     * 
     * Parameters:
     * display - {Boolean} 
     */
    display: function(display) {
        this.div.style.display = (display) ? "" : "none";
    },

    CLASS_NAME: "OpenLayers.Marker.Annotation"
});


