/* Copyright (c) 2006-2008 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/Feature.js
 */

/**
 * Class: OpenLayers.Feature.ckWFS
 * WFS handling class, for use as a featureClass on the WFS layer for handling
 * 'point' WFS types. Good for subclassing when creating a custom WFS like
 * XML application.
 * 
 * Inherits from:
 *  - <OpenLayers.Feature>
 */
OpenLayers.Feature.ckWFS = OpenLayers.Class(OpenLayers.Feature, {
      
    icon: null,
    
    setContentHTML: null,

    onClick: function() {},
    
    /**
     * APIProperty: Div
     * {String} Query results to HTML-Div
     *          id of the div tag
     */
	Div: null,
	
    /**
     * APIProperty: toPopUp
     * {Boolean} Query result to PopUp if true
     *			 else to named Div
     */
	toPopUp: true,

    /** 
     * Constructor: OpenLayers.Feature.ckWFS
     * Create a WFS feature.
     *
     * Parameters:
     * layer - {<OpenLayers.Layer>} 
     * xmlNode - {XMLNode} 
     */
    initialize: function(layer, xmlNode) {
    	this.layer = layer;
    	this.icon = this.layer.options.icon;
    	this.setContentHTML = this.layer.options.setContentHTML;
    	if (this.layer.options.Div != null) {
    		this.Div = this.layer.options.Div;
    		this.toPopUp = false;
    	}
    	if (this.layer.options.onClick != null) {
    		this.onClick = this.layer.options.onClick;
    	}
    	
        var newArguments = arguments;
        var data = this.processXMLNode(xmlNode);
        newArguments = new Array(layer, data.lonlat, data);
        
        OpenLayers.Feature.prototype.initialize.apply(this, newArguments);
        
        this.popupClass = OpenLayers.Class(ckMapParams.popupClass, {
            	'autoSize': true, 'maxSize': ckMapParams.popupMaxSize });

        this.createMarker();
        
        if (this.icon) {
        	this.marker.icon.url = this.icon.url;
        	this.marker.icon.size = this.icon.size;
        	this.marker.icon.offset = this.icon.offset;
        	this.marker.icon.calculateOffset = this.icon.calculateOffset;
        }
        
        this.marker.events.register('click', this, this.markerClick);
       	this.marker.events.register('mouseover', this, this.markerMouseOver);
       	this.marker.events.register('mouseout', this, this.markerMouseOut);

        this.layer.addMarker(this.marker);
    },
    
    /** 
     * Method: destroy
     * nullify references to prevent circular references and memory leaks
     */
    destroy: function() {
        OpenLayers.Feature.prototype.destroy.apply(this, arguments);
    },


    /**
     * @param {Event} evt
     */
    markerClick: function(evt) {
    	if (this.toPopUp == true) {
	    	if (this.popup) {
	    		if (!this.popup.map) {
	    			this.destroyPopup();
	    		} else {
	    			this.popup.toggle();
	    		}
	    	}
			if (!this.popup) {
				this.layer.map.addPopup(this.createPopup(true), true);
			} 
		} else {
			this.onClick(evt);
			if (this.extractAttributes) {
				OpenLayers.Util.getElement(this.Div).innerHTML = this.data.popupContentHTML;
			}
		}
		
        OpenLayers.Event.stop(evt);
    },
    
    /**
     * @param {Event} evt
     * KDVZ 2007-05-30
     */
    markerMouseOver: function(evt) {
        this.layer.map.div.style.cursor = "pointer";
        //OpenLayers.Event.stop(evt);
    },

    /**
     * @param {Event} evt
     * KDVZ 2007-05-30
     */
    markerMouseOut: function(evt) {
        this.layer.map.div.style.cursor = "default";
        //OpenLayers.Event.stop(evt);
    },

    /**
     * @param {Boolean} closeBox
     * KDVZ 2009-01-20
     */
    createPopup: function(closeBox) {

        if (this.lonlat != null) {
            
            var id = this.id + "_popup";
            //var anchor = (this.marker) ? this.marker.icon : null;
            var fact = 0.5;
            var anchor = new OpenLayers.Icon(null, 
            					new OpenLayers.Size(this.marker.icon.size.w*fact, this.marker.icon.size.h*fact),
            					new OpenLayers.Pixel(-(this.marker.icon.size.w*fact/2), -(this.marker.icon.size.w*fact/2)),
            					null);
            if (!this.popup) {
                this.popup = new this.popupClass(id, 
                                                 this.lonlat,
                                                 this.data.popupSize,
                                                 this.data.popupContentHTML,
                                                 anchor, 
                                                 closeBox); 
            }    
            if (this.data.overflow != null) {
                this.popup.contentDiv.style.overflow = this.data.overflow;
            }    
            
            this.popup.feature = this;
        }        
        return this.popup;
    },

    /**
     * Method: processXMLNode
     * When passed an xmlNode, parses it for a GML point, and passes
     * back an object describing that point.
     *
     * For subclasses of Feature.WFS, this is the feature to change.
     *
     * Parameters:
     * xmlNode - {XMLNode} 
     * 
     * Returns:
     * {Object} Data Object with 'id', 'lonlat', and private properties set
     */
    processXMLNode: function(xmlNode) {
		var options = {
			'extractAttributes': this.layer.extractAttributes
		};
		
        this.layer.formatObject = this.layer.format ? new this.layer.format(options) : new OpenLayers.Format.GML(options);
		var feature = this.layer.formatObject.parseFeature(xmlNode);
		var content = "";
		
		if (this.setContentHTML) {
			content = this.setContentHTML(feature);
		} else {
			content = "<div class='FeatureInfo'><span class='FeatureTitle'>Auswahl:</span><br />";
			for (var property in feature.data) { 
				content += "<span class='FeatureProperty'>"+property+": </span>"
				content += "<span class='FeatureValue'>"+feature.data[property]+"</span><br />";
		 	}
		 	content +"</div>";
		}
		       
        return {lonlat: new OpenLayers.LonLat(feature.geometry.x,
                                              feature.geometry.y),
                id: null,
                popupContentHTML: content
                };
    },

    CLASS_NAME: "OpenLayers.Feature.ckWFS"
});
  
  
  
  


