/**
 * @author nando
 */

     // defines popup class
    AutoSizeFramedCloud = OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
        'autoSize': true
    });    
    
    AutoSizeFramedCloudMinSize = OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
        'autoSize': true, 
        'minSize': new OpenLayers.Size(300,145)
    });    
    
    OpenLayers.Layer.SOSPoint = OpenLayers.Class(OpenLayers.Layer.Text, {
        
        popupClass: AutoSizeFramedCloud,
       
        parseData: function(ajaxRequest) {
            var text = ajaxRequest.responseText;
            
            var options = {};
            
            OpenLayers.Util.extend(options, this.formatOptions);
            
            if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
                options.externalProjection = this.projection;
                options.internalProjection = this.map.getProjectionObject();
            }    
            
            var parser = new OpenLayers.Format.Text(options);
            features = parser.read(text);
            for (var i = 0; i < features.length; i++) {
                var data = {popupContentHTML: ''};
                var feature = features[i];
                var location;
                var iconSize, iconOffset;
                
                location = new OpenLayers.LonLat(feature.geometry.x, 
                                                 feature.geometry.y);
                
                if (feature.style.graphicWidth 
                    && feature.style.graphicHeight) {
                    iconSize = new OpenLayers.Size(
                        feature.style.graphicWidth,
                        feature.style.graphicHeight);
                }        
                
                // FIXME: At the moment, we only use this if we have an 
                // externalGraphic, because icon has no setOffset API Method.  
                if (feature.style.graphicXOffset 
                    && feature.style.graphicYOffset) {
                    iconOffset = new OpenLayers.Pixel(
                        feature.style.graphicXOffset, 
                        feature.style.graphicYOffset);
                }
                
                if (feature.style.externalGraphic != null) {
                    data.icon = new OpenLayers.Icon(feature.style.externalGraphic, 
                                                    iconSize, 
                                                    iconOffset);
                } else {
                    data.icon = OpenLayers.Marker.defaultIcon();
    
                    //allows for the case where the image url is not 
                    // specified but the size is. use a default icon
                    // but change the size
                    if (iconSize != null) {
                        data.icon.setSize(iconSize);
                    }
                }
                
                if (feature.attributes.title != null) 
                    data['popupContentHTML'] = '<h2>'+feature.attributes.title+'</h2>';
                if (feature.attributes.description != null)
                    data['popupContentHTML'] += '<p>'+feature.attributes.description+'</p>';
                
                data['overflow'] = feature.attributes.overflow || "auto"; 
                
                var markerFeature = new OpenLayers.Feature(this, location, data);
                markerFeature.popupClass = this.popupClass;
                markerFeature.closeBox = true;
                this.features.push(markerFeature);
                var marker = markerFeature.createMarker();
                if ((feature.attributes.title != null) 
                    || (feature.attributes.description != null)) {
                  marker.events.register('click', markerFeature, this.markerClick);
                }
                this.addMarker(marker);
            }
            this.events.triggerEvent("loadend");
        },
        
        /**
         * Property: markerClick
         * 
         * Parameters:
         * evt - {Event} 
         */
        markerClick: function(evt) {
            if (this.popup == null) {
                this.popup = this.createPopup(this.closeBox);
                this.layer.map.addPopup(this.popup, true);
                this.popup.show();
            } else {
                this.layer.map.addPopup(this.popup, true);
                this.popup.show();
            }
            OpenLayers.Event.stop(evt);
        },
        
        CLASS_NAME: "OpenLayers.Layer.SOSPoint"

    });
