/* Copyright (c) 1006-1008 MetaCarta, 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/Popup/Framed.js
 * @requires OpenLayers/Util.js
 */

/**
 * Class: OpenLayers.Popup.SmallPopup
 * 
 * Inherits from: 
 *  - <OpenLayers.Popup.Framed>
 */

OpenLayers.Popup.SmallPopup = 
  OpenLayers.Class(OpenLayers.Popup.Framed, {

    /**
     * APIProperty: autoSize
     * {Boolean} Framed Cloud is autosizing by default.
     */
    autoSize: true,
    
    /**
     * APIProperty: panMapIfOutOfView
     * {Boolean} Framed Cloud does pan into view by default.
     */
    panMapIfOutOfView: true,
    
    /**
     * Property: imageSrc
     * {String}
     */
    imageSrc: 'smallpopup.png',
    
    /**
     * APIProperty: imageSize
     * {<OpenLayers.Size>}
     */
    imageSize: new OpenLayers.Size(741, 743),
    
    /** 
     * APIProperty: fixedRelativePosition
     * {Boolean} The Framed Cloud popup works in just one fixed position.
     */
    fixedRelativePosition: false,
    
    /**
     * APIProperty: relativePosition
     * {String} Framed Cloud popup always goes top right
     */
    relativePosition: null,

    /**
     * Property: positionBlocks
     * {Object} Hash of differen position blocks, keyed by relativePosition
     *     two-character code string (ie "tl", "tr", "bl", "br")
     */
    positionBlocks: {
        "tr": {
            'offset': new OpenLayers.Pixel(0, 0),
            'padding': new OpenLayers.Bounds(10, 10, 10, 10),
            'blocks': [
                { // top-left
                    size: new OpenLayers.Size('auto', 'auto'),
                    anchor: new OpenLayers.Bounds(6, 10, 10, 0),
                    position: new OpenLayers.Pixel(0, 0)
                },
                { //top-right
                    size: new OpenLayers.Size(10, 'auto'),
                    anchor: new OpenLayers.Bounds(null, 10, 0, 0),
                    position: new OpenLayers.Pixel(-731, 0)
                },
                { //bottom-left
                    size: new OpenLayers.Size('auto', 12),
                    anchor: new OpenLayers.Bounds(6, 0, 10, null),
                    position: new OpenLayers.Pixel(0, -713)
                },
                { //bottom-right
                    size: new OpenLayers.Size(10, 12),
                    anchor: new OpenLayers.Bounds(null, 0,0, null),
                    position: new OpenLayers.Pixel(-731, -713)
                },
                { // stem
                    size: new OpenLayers.Size(19, 16),
                    anchor: new OpenLayers.Bounds(0, 0, 10, null),
                    position: new OpenLayers.Pixel(0, -728)
                }
            ]
        },
        "tl": {
            'offset': new OpenLayers.Pixel(0, 0),
            'padding': new OpenLayers.Bounds(10, 10, 10, 10),
            'blocks': [ 
                { // top-left
                    size: new OpenLayers.Size('auto', 'auto'),
                    anchor: new OpenLayers.Bounds(6, 10, 13, 0),
                    position: new OpenLayers.Pixel(0, 0)
                },
                { //top-right
                    size: new OpenLayers.Size(10, 'auto'),
                    anchor: new OpenLayers.Bounds(null, 10, 6, 0),
                    position: new OpenLayers.Pixel(-731, 0)
                },
                { //bottom-left
                    size: new OpenLayers.Size('auto', 12),
                    anchor: new OpenLayers.Bounds(6, 0, 10, null),
                    position: new OpenLayers.Pixel(0, -713)
                },
                { //bottom-right
                    size: new OpenLayers.Size(10, 12),
                    anchor: new OpenLayers.Bounds(null, 0,6, null),
                    position: new OpenLayers.Pixel(-731, -713)
                },
                { // stem
                    size: new OpenLayers.Size(19, 16),
                    anchor: new OpenLayers.Bounds(null, 0, 0, null),
                    position: new OpenLayers.Pixel(-19, -728)
                }
            ]
        },
        "bl": {
            'offset': new OpenLayers.Pixel(0, 0),
            'padding': new OpenLayers.Bounds(10, 10, 10, 10),
            'blocks': [
                { // top-left
                    size: new OpenLayers.Size('auto', 'auto'),
                    anchor: new OpenLayers.Bounds(0, 10, 10, 0),
                    position: new OpenLayers.Pixel(0, 0)
                },
                { //top-right
                    size: new OpenLayers.Size(10, 'auto'),
                    anchor: new OpenLayers.Bounds(null, 10, 6, 0),
                    position: new OpenLayers.Pixel(-731, 0)
                },
                { //bottom-left
                    size: new OpenLayers.Size('auto', 12),
                    anchor: new OpenLayers.Bounds(0, 0, 12, null),
                    position: new OpenLayers.Pixel(0, -713)
                },
                { //bottom-right
                    size: new OpenLayers.Size(10, 12),
                    anchor: new OpenLayers.Bounds(null, 0, 6, null),
                    position: new OpenLayers.Pixel(-731, -713)
                },
                { // stem
                    size: new OpenLayers.Size(19, 16),
                    anchor: new OpenLayers.Bounds(null, null, 0, 0),
                    position: new OpenLayers.Pixel(-59, -728)
                }
            ]
            
        },
        "br": {
            'offset': new OpenLayers.Pixel(0, 0),
            'padding': new OpenLayers.Bounds(10,10,10,10),
            'blocks': [
                { // top-left
                    size: new OpenLayers.Size('auto', 'auto'),
                    anchor: new OpenLayers.Bounds(6, 10, 10, 0),
                    position: new OpenLayers.Pixel(0, 0)
                },
                { //top-right
                    size: new OpenLayers.Size(10, 'auto'),
                    anchor: new OpenLayers.Bounds(null, 10, 0, 0),
                    position: new OpenLayers.Pixel(-731, 0)
                },
                { //bottom-left
                    size: new OpenLayers.Size('auto', 12),
                    anchor: new OpenLayers.Bounds(6, 0, 10, null),
                    position: new OpenLayers.Pixel(0, -713)
                },
                { //bottom-right
                    size: new OpenLayers.Size(10, 12),
                    anchor: new OpenLayers.Bounds(null, 0,0, null),
                    position: new OpenLayers.Pixel(-731, -713)
                },
                { // stem
                    size: new OpenLayers.Size(19, 16),
                    anchor: new OpenLayers.Bounds(0, null, 10, 0),
                    position: new OpenLayers.Pixel(-39, -728)
                }
            ]
        }
    },

    /**
     * APIProperty: minSize
     * {<OpenLayers.Size>}
     */
    minSize: new OpenLayers.Size(105, 10),

    /**
     * APIProperty: maxSize
     * {<OpenLayers.Size>}
     */
    maxSize: new OpenLayers.Size(721, 704),

    /**
     * Property: halfSize 
     * Limit popup to half the size of the map.
     */
    halfSize: false, 

    /** 
     * Constructor: OpenLayers.Popup.SmallPopup
     * 
     * Parameters:
     * id - {String}
     * lonlat - {<OpenLayers.LonLat>}
     * size - {<OpenLayers.Size>}
     * contentHTML - {String}
     * anchor - {Object} Object to which we'll anchor the popup. Must expose 
     *     a 'size' (<OpenLayers.Size>) and 'offset' (<OpenLayers.Pixel>) 
     *     (Note that this is generally an <OpenLayers.Icon>).
     * closeBox - {Boolean}
     * closeBoxCallback - {Function} Function to be called on closeBox click.
     */
    initialize:function(id, lonlat, size, contentHTML, anchor, closeBox, 
                        closeBoxCallback) {

        OpenLayers.Popup.Framed.prototype.initialize.apply(this, arguments);
    },

    /** 
     * APIMethod: destroy
     */
    destroy: function() {
        OpenLayers.Popup.Framed.prototype.destroy.apply(this, arguments);
    },

    draw: function() {
        if (this.halfSize) {
            this.maxSize = new OpenLayers.Size(
                Math.min(this.map.getSize().w/2-20, 
                         OpenLayers.Popup.SmallPopup.prototype.maxSize.w), 
                Math.min(this.map.getSize().h/2-20, 
                         OpenLayers.Popup.SmallPopup.prototype.maxSize.h) 
            );
        }
        return OpenLayers.Popup.Framed.prototype.draw.apply(this, arguments);

    },

    CLASS_NAME: "OpenLayers.Popup.SmallPopup"
});
