//var selectResults;
/**
 * ScratchLayer extends OpenLayers.Layer.Vector
 * This is a "scratchpad" layer built to draw geometries returned by queryies in order
 * to highlight them on the map.
 */
ScratchLayer = function(name, options) {
    ScratchLayer.superclass.constructor.call(this, name, options);
};

ScratchLayer.instance = null;

ScratchLayer.getInstance = function() {
   if (ScratchLayer.instance == null) {
      ScratchLayer.instance = new ScratchLayer();
      map.addLayers([ScratchLayer.instance]);
      //selectResults = OpenLayers.Control.SelectFeature(ScratchLayer.getInstance(), {'selectStyle':{strokeColor:'#FFFF00'}});
   }
   return ScratchLayer.instance;
};

Ext.extend(ScratchLayer, OpenLayers.Layer.Vector, {
   
    // Define styles for polygon and point features
    polyStyle: {
        strokeColor: "#00FFFF",
        strokeWidth: 2,
        fillOpacity: .4,
        fillColor: "#00FFFF",
        pointRadius: 6
    },    

    pointStyle: {
        strokeColor: "#00FFFF",
        strokeWidth: 2,
        fillOpacity: 1,
        fillColor: "#000000",
        pointRadius: 6
    },

    /**
     * Add an array of geometries to the layer
     * @param {Array} Array of strings; Each string is a textual representation of a
     *                geometry as returned by postgis astext()
     */
    addGeometries: function(geom) {
        var geomArray = new Array();
        for (var i=0; i<geom.length; i++) {
            geomArray[i] = this.getFeatureFromGeom(geom[i]);
        }
        this.addFeatures(geomArray);
        this.setZIndex(100000);
        // set map extent to zoom to the geometries
        map.zoomToExtent(this.getDataExtent());        
    }, 

    /**
     * Add a single geometry to the layer
     * @param {String} textual representation of a geometry as returned by postgis astext()
     */
    addGeometry: function(geom) {
        this.addFeatures([this.getFeatureFromGeom(geom)]);
        this.setZIndex(100000);
    },

    /**
     * Return an OpenLayers.Feature.Vector object for a given geometry string
     * TODO: Should be a private, static method
     * @param {String} geometry string
     * @return {OpenLayers.Feature.Vector} vector object for geometry
     */
    getFeatureFromGeom: function(geom) {

        // Determine whether the provided geometry is in WKT (postgres) or GML (oracle)
        // format
        var reader = "";
        var geomObj = "";
        var substr = geom.substring(0,4);
        if (substr == "<gml") {
           // More hacks: I can't get the GML that Oracle produces to work in openlayers
           // using OpenLayers.Format.GML. So instead I'm going to parse the xml by hand
           // and create a Vector object that way. This only works for points.
           var parser;
           var doc;
           try { // non-IE browsers
               parser = new DOMParser();
               doc = parser.parseFromString(geom, "text/xml");
           } catch(e) { // IE
               doc = new ActiveXObject("Microsoft.XMLDOM");
               doc.async = false;
               doc.loadXML(geom);
           } 
           var lonlat = doc.getElementsByTagName("gml:coordinates")[0].childNodes[0].nodeValue;

           // lonlat is a string like "lon, lat" parse it into separate vars 
           var elements = lonlat.split(",");
           geomObj = new OpenLayers.Feature.Vector(
                     new OpenLayers.Geometry.Point(elements[0], elements[1])); 
        } else {
           reader = new OpenLayers.Format.WKT();
           geomObj = reader.read(geom);
        }
   
        // Transform the geometry from lat/lon to google coordinates
        geomObj.geometry.transform(map.displayProjection, map.getProjectionObject());
    
        // Apply the appropriate style based on whether this is a point or a polygon
        if (geomObj.geometry.id.toLowerCase().indexOf("polygon", 0) >= 0) {
            geomObj.style = this.polyStyle;
        } else {            
            geomObj.style = this.pointStyle;
        }
        return geomObj;
    },

    /**
     * Clear all features from the layer
     */
    clear: function() {
        this.removeFeatures(this.features);
    }

});
