/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license.
 * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt 
 * for the full text of the license. */


/**
 * @class
 * 
 * @requires OpenLayers/Feature.js
 */
OpenLayers.Feature.Flickr = OpenLayers.Class.create();
OpenLayers.Feature.Flickr.prototype = 
  OpenLayers.Class.inherit( OpenLayers.Feature, {
      
    /** 
     * @constructor
     * 
     * @param {OpenLayers.Layer} layer
     * @param {XMLNode} xmlNode
     */
    initialize: function(layer, xmlNode) {
        var newArguments = arguments;
        this.layer = layer;
        var data = this.processXMLNode(xmlNode);
        newArguments = new Array(layer, data.lonlat, data)
        OpenLayers.Feature.prototype.initialize.apply(this, newArguments);
        this.createMarker();
        this.marker.events.register('click', this, this.markerClick);  
        this.layer.addMarker(this.marker);
    },
    
    destroy: function() {
        if (this.marker != null) {
            this.layer.removeMarker(this.marker);  
        }
        OpenLayers.Feature.prototype.destroy.apply(this, arguments);
    },

    /**
     * @param {XMLNode} xmlNode
     * 
     * @returns Data Object with 'id', 'lonlat', and private properties set
     * @type Object
     */
    processXMLNode: function(xmlNode) {
        //this should be overridden by subclasses
        // must return an Object with 'id' and 'lonlat' values set
        var point = OpenLayers.Ajax.getElementsByTagNameNS(xmlNode, "http://www.opengis.net/gml", "gml", "Point");
        var text  = OpenLayers.Util.getXmlNodeValue(OpenLayers.Ajax.getElementsByTagNameNS(point[0], "http://www.opengis.net/gml","gml", "coordinates")[0]);
        var floats = text.split(",");
        
        var feature = point[0].parentNode.parentNode;
        var properties = ['farm', 'server', 'id', 'secret', 'title', 'owner'];
        var attrs = {};
        for(var i = 0; i < properties.length; i++) {
            attrs[properties[i]] = OpenLayers.Util.getXmlNodeValue(OpenLayers.Ajax.getElementsByTagNameNS(feature, "http://example.com/featureserver", "fs", properties[i])[0]);
        }
        var html = "";
        html += "<div style='overflow:auto; width:250px; height:200px;'><b>"+attrs.title+"</b><br /><a href='http://flickr.com/photos/"+attrs.owner+"/"+attrs.id+"/'><img src='http://farm"+attrs.farm+".static.flickr.com/"+attrs.server+"/"+attrs.id+"_"+attrs.secret+"_m.jpg' /></a></div>";
        
        return {lonlat: new OpenLayers.LonLat(parseFloat(floats[0]),
                                              parseFloat(floats[1])),
                icon: new OpenLayers.Icon("http://farm"+attrs.farm+".static.flickr.com/"+attrs.server+"/"+attrs.id+"_"+attrs.secret+"_s.jpg", new OpenLayers.Size(Math.max(25, (75-this.layer.markers.length*3)),Math.max(25, 75-this.layer.markers.length*3))),
                popupContentHTML: html, 
                popupSize: new OpenLayers.Size(275, 220),
                id: null};

    },

    markerClick: function(evt) {
        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(true);
            popup.contentDiv.style.overflow='auto';
            this.layer.map.addPopup(popup);
        }
        OpenLayers.Event.stop(evt);
    },
    
    /** @final @type String */
    CLASS_NAME: "OpenLayers.Feature.Flickr"
});
  
  
  
  

