// WMS.js - Web Map Service map types
//
//
// The current version of this code is always available at:
// http://www.acme.com/javascript/
//
//
// Copyright © 2005,2006,2007 by Jef Poskanzer <jef@mail.acme.com>.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
// For commentary on this license please see http://www.acme.com/license.html


function WMSCreateMap( name, copyright, baseUrl, layer, format, transparent, opacity, minResolution, maxResolution, extraTileLayers, urlArg )
    {
    var tileLayer = new GTileLayer( new GCopyrightCollection( copyright ), minResolution, maxResolution );
    tileLayer.baseUrl = baseUrl;
    tileLayer.layer = layer;
    tileLayer.format = format;
    tileLayer.transparent = transparent;
    tileLayer.getTileUrl = WMSGetTileUrl;
    tileLayer.getOpacity = function () { return opacity; };
    tileLayer.getCopyright = function () { return { prefix: '', copyrightTexts: [ copyright ] }; };
    var tileLayers = [];
    for ( var i in extraTileLayers )
	tileLayers.push( extraTileLayers[i] );
    tileLayers.push( tileLayer );
    return new GMapType( tileLayers, G_SATELLITE_MAP.getProjection(), name, { urlArg: 'o' } );
    }


function WMSGetTileUrl( tile, zoom )
    {
    var southWestPixel = new GPoint( tile.x * 256, ( tile.y + 1 ) * 256 );
    var northEastPixel = new GPoint( ( tile.x + 1 ) * 256, tile.y * 256 );
    var southWestCoords = G_NORMAL_MAP.getProjection().fromPixelToLatLng( southWestPixel, zoom );
    var northEastCoords = G_NORMAL_MAP.getProjection().fromPixelToLatLng( northEastPixel, zoom );
    var bbox = southWestCoords.lng() + ',' + southWestCoords.lat() + ',' + northEastCoords.lng() + ',' + northEastCoords.lat();
    var transparency = this.transparent ? '&TRANSPARENT=TRUE' : '';
    return this.baseUrl + '?VERSION=1.1.1&REQUEST=GetMap&LAYERS=' + this.layer + '&STYLES=&SRS=EPSG:4326&BBOX=' + bbox + '&WIDTH=256&HEIGHT=256&FORMAT=' + this.format + '&BGCOLOR=0xCCCCCC&EXCEPTIONS=INIMAGE' + transparency;
    }


var WMS_TOPO_MAP = WMSCreateMap( 'Topo', 'Imagery by USGS / Revised 1979 / Service by TerraServer', 'http://www.terraserver-usa.com/ogcmap6.ashx', 'DRG', 'image/jpeg', false, 1.0, 4, 17, [], 't' );
var WMS_DOQ_MAP = WMSCreateMap( 'DOQ', 'Imagery by USGS / Revised 1998 / Service by TerraServer', 'http://www.terraserver-usa.com/ogcmap6.ashx', 'DOQ', 'image/jpeg', false, 1.0, 4, 18, [], 'o' );
var WMS_NEXRAD_MAP = WMSCreateMap( 'NEXRAD', 'Data by NWS / Service by Iowa U. Ag. Dept.', 'http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi', 'nexrad-n0r', 'image/png', true, 0.666, 4, 10, G_HYBRID_MAP.getTileLayers(), 'n' );
