//<![CDATA[
/* Script Copyright (C) 2008, Christopher S. Swingley <cswingle@gmail.com>
   and licensed under the terms of the GNU General Public License Version 3.
   The license is available at: http://swingleydev.info/gpl.txt
   or from the Free Software Foundation (http:/www.fsf.org/)

   This program shows the common organism name after the user enters
   the ccode value.

    NOTES:
        Only tested with FF3 and Safari.  Don't give a crap about IE.
*/
var map;
var highlightLayer;
var wfs_url = "http://swingleydev.info/cgi-bin/mapserv?map=/var/www/swingleydev/gis/goldstream.map&SERVICE=WFS&VERSION=1.0.0";
function highlightFeatures(typename,attribute,value) {
    var wfsurl = wfs_url + '&REQUEST=getfeature&typename=' + typename +
        '&Filter=<Filter><PropertyIsEqualTo><PropertyName>'+attribute+'</PropertyName><Literal>'+value+'</Literal></PropertyIsEqualTo></Filter>';
    OpenLayers.loadURL(wfsurl, '', this, highlight_them);
}
function highlight_them(response) {
    // use the GML parser to turn the XML into a list of Feature objects
    var features = new OpenLayers.Format.GML({extractAttributes: true}).read(response.responseText);
    // have the Vector layer purge its feature list, replace them with the new ones
    highlightLayer.destroyFeatures();
    highlightLayer.addFeatures(features);
    highlightLayer.setVisibility(true);
};

window.onload = function () {
    map = new OpenLayers.Map('map',
            {units: 'm', projection: 'EPSG:32606',
            maxExtent: new OpenLayers.Bounds(453000, 7190000, 464000, 7204000), // center of map can't be outside);
            // restrictedExtent: new OpenLayers.Bounds(454635, 7191443, 461093, 7201605), // edges of map can't be outside
            restrictedExtent: new OpenLayers.Bounds(453000, 7190000, 464000, 7204000), // edges of map can't be outside
            maxResolution: 'auto',
            tileSize: new OpenLayers.Size(128, 128) // Size of tiles downloaded from MapServer in pixels
            } );
    var doqLayer = new OpenLayers.Layer.WMS("Goldstream Valley DOQQ",
            "http://swingleydev.info/cgi-bin/mapserv?map=/var/www/swingleydev/gis/goldstream.map&",
            {layers: 'doq', 'FORMAT': 'image/jpeg'},
            {gutter: 0});
    var spotLayer = new OpenLayers.Layer.WMS("Goldstream Valley SPOT",
            "http://swingleydev.info/cgi-bin/mapserv?map=/var/www/swingleydev/gis/goldstream.map&",
            {layers: 'spot', 'FORMAT': 'image/jpeg'},
            {gutter: 0});
    var pointsLayer = new OpenLayers.Layer.WMS("All points",
            "http://swingleydev.info/cgi-bin/mapserv?map=/var/www/swingleydev/gis/goldstream.map&",
            {layers: 'gps', 'FORMAT': 'image/png', transparent: 'true'},
            {gutter: 0});
    // var gsat = new OpenLayers.Layer.Google("Google Satellite", {type: G_SATELLITE_MAP, numZoomLevels: 20});
    var highlightColors = ['#000000', '#0000ff', '#00ffff', '#00ff00', '#ffff00', '#ff8000', '#ff0000', '#ff0080'];
    var highlightContext = {
        getColor: function(feature) {
            var mph = feature.attributes["mph"];
            var symbology;
            if (parseInt(mph) >= 40) {
               symbology = 7;
            } else {
                symbology = parseInt(mph / 5);
            }
            return highlightColors[symbology];
        }
    };
    var highlightTemplate = {
        pointRadius: 3.0,
        strokeColor: '#000000',
        strokeWidth: 1,
        fillOpacity: 1.0,
        fillColor: "${getColor}"
    };
    var highlightStyle = new OpenLayers.StyleMap(
        {"default": new OpenLayers.Style(highlightTemplate, {context: highlightContext})}
    );
    highlightLayer = new OpenLayers.Layer.Vector("Selected points",
            {isBaseLayer:false, features:[], visibility:true, styleMap:highlightStyle});
    map.addLayers([doqLayer, spotLayer, pointsLayer]);
    map.addLayer(highlightLayer);
    pointsLayer.setVisibility(false);
    map.setCenter(new OpenLayers.LonLat(458864, 7196024), 2);
    map.addControl(new OpenLayers.Control.LayerSwitcher());
    map.addControl(new OpenLayers.Control.ScaleLine({topOutUnits: '', topInUnits: ''}));
}
//]]>
