OpenLayers.Layer.PointsLayer = OpenLayers.Class(OpenLayers.Layer.Markers, {

    /**
     * Property: features
     * {Array(<OpenLayers.Feature>)}
     */
    features: null,

    /**
     * APIProperty: formatOptions
     * {Object} Hash of options which should be passed to the format when it is
     * created. Must be passed in the constructor.
     */
    formatOptions: null,

    /**
     * Property: selectedFeature
     * {<OpenLayers.Feature>}
     */
    selectedFeature: null,

    /**
     * APIProperty: popupSize
     * {<OpenLayers.Size>} This determines the size of GeoRSS popups. If
     * not provided, defaults to 250px by 120px.
     */
    popupSize: null,

    iconSize: null,

    /**
    * Constructor: OpenLayers.Layer.GeoRSS
    * Create a GeoRSS Layer.
    *
    * Parameters:
    * name - {String}
    * options - {Object}
    */
    initialize: function(name, options)
    {
        OpenLayers.Layer.Markers.prototype.initialize.apply(this, [name, options]);
        this.features = [];
    },

    /**
     * Method: destroy
     */
    destroy: function()
    {
        OpenLayers.Layer.Markers.prototype.destroy.apply(this, arguments);
        this.clearFeatures();
        this.features = null;
    },

    /**
     * Method: moveTo
     *
     * Parameters:
     * bounds - {Object}
     * zoomChanged - {Object}
     * minor - {Object}
     */
    moveTo:function(bounds, zoomChanged, minor)
    {
        OpenLayers.Layer.Markers.prototype.moveTo.apply(this, arguments);
    },

    /**
     * Method: markerClick
     *
     * Parameters:
     * evt - {Event}
     */
    markerClick: function(evt)
    {
        var sameMarkerClicked = (this == this.layer.selectedFeature);
        this.layer.selectedFeature = (! sameMarkerClicked) ? this : null;
        for (var i=0; i < this.layer.map.popups.length; i++) {
            this.layer.map.removePopup(this.layer.map.popups[i]);
        }
        if (! sameMarkerClicked) {
            var popup = this.createPopup();
            OpenLayers.Event.observe(popup.div, "click",
                OpenLayers.Function.bind(function() {
                    for(var i=0; i < this.layer.map.popups.length; i++) {
                        this.layer.map.removePopup(this.layer.map.popups[i]);
                    }
                }, this)
            );
            this.layer.map.addPopup(popup);
            if (this.layer.selectedFeature.data.gallery && typeof(MgUz) != "undefined") {
                MgUz.setGallery(this.layer.selectedFeature.data.gallery);
            }
        }
        OpenLayers.Event.stop(evt);
    },

    /**
     * Method: clearFeatures
     * Destroy all features in this layer.
     */
    clearFeatures: function()
    {
        if (this.features != null) {
            while(this.features.length > 0) {
                var feature = this.features[0];
                OpenLayers.Util.removeItem(this.features, feature);
                feature.destroy();
            }
        }
    },

    dataToMarker: function(geometry, icon, title, description, link, gallery)
    {
        if (! geometry) {
            return;
        }
        var iconSize = this.iconSize ? this.iconSize.clone() : new OpenLayers.Size(10, 10);
        var data = {
            "icon": new OpenLayers.Icon(icon, iconSize),
            "title": title,
            "description": description,
            "link": link,
            "popupSize": this.popupSize ? this.popupSize.clone() : new OpenLayers.Size(250, 120),
            "gallery": gallery
        };
        if (title || description) {
            var contentHTML = '<div class="olLayerGeoRSSClose">[x]</div>';
            if (title) {
                contentHTML += '<div class="olLayerGeoRSSTitle">';
                if (link) {
                    contentHTML += '<a class="link" href="'+link+'" target="_blank">';
                }
                contentHTML += title;
                if (link) {
                    contentHTML += '</a>';
                }
                contentHTML += '</div>';
            }
            if (description) {
                contentHTML += '<p style="" class="olLayerGeoRSSDescription">';
                contentHTML += description;
                contentHTML += '</p>';
            }
            data['popupContentHTML'] = contentHTML;
        }
        var location = geometry.getBounds().getCenterLonLat();
        var feature = new OpenLayers.Feature(this, location, data);
        this.features.push(feature);
        var marker = feature.createMarker();
        marker.events.register('click', feature, this.markerClick);
        this.addMarker(marker);
    },

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

