//==============================================================
//Spatial Data Logic - 2005
//WMS Map Spec
/*
James Fee and others have pointed out that Cubewerks is offering an add-on to their server product that offers Google Maps data as a WMS service. This is pretty cool but what I’d expect most developers want is the ability to display WMS layers directly in a GMap instance in the browser. Fortunately, this is not too difficult if we base a WMS map spec on the existing Street Map spec and then manipulate the URL returned for each map tile. IMO, this opens up a lot more data for use in simple GMaps applications.

Download the WMSSpec code
Example:
var wms = new WMSSpec(mapTypes[0], "http://wms.jpl.nasa.gov/wms.cgi?", "WMS Test", "modis", "default", "image/jpeg" );
Either add this to your array of specs passed to the GMap constructor or use the undocumented access to the array itself. (MyGMap.mapTypes[MyGMap.mapTypes.length] = wms;)

WMSSpec(baseSpec, baseURLtoWMSServer, name, layer, style, format);
1) the Google Maps Streets spec, usually the first spec in the array but it is also pointed to by the G_MAP_TYPE and _GOOGLE_MAP_TYPE variables
2) The URL to the WMS Server, don’t forget the “?” at the end
3) Name of service, used for labeling
4) Layer – the WMS layer name to use. This parameter is passed in whole to the server so it will accept whatever the WMS server is expecting
5) Style – the WMS style to use. See 4.
6) Format – the image format to return. (e.g. image/png, image/jpeg etc.)
*/
function WMSSpec (spec, baseUrl, name, layers, styles, format, overlay, copywrite)
{
	this.BaseSpec = spec;
	this.baseURL = baseUrl;
	this.Map = map;
    this.tileSize=spec.tileSize;
    //this.tileSize=1024;
    this.backgroundColor=spec.backgroundColor;
    this.emptyTileURL=spec.emptyTileURL;
	this.waterTileUrl=spec.waterTileUrl;
    this.numZoomLevels=spec.numZoomLevels;
    this.pixelsPerDegree=spec.pixelsPerDegree;
    this.mapBounds=spec.mapBounds;
    this.ukBounds=spec.ukBounds;
    this.earthBounds=spec.earthBounds;
	this.Name = name;
	this.baseLayers = layers;
	this.Styles = styles;
	this.Format = format;
	this.overlayFormat = format;
	this.copywrite = copywrite;
	this.overlay = overlay;
	this.overlayLayers = "";
}

/*
SET FUNCTIONS
*/

//sets the wms url to use for the base. Omit url to use default for BaseSpec.
WMSSpec.prototype.setBaseURL=function(url)
{
    this.baseURL = url;
}

//sets the wms url to use for the overlay. Omit url to use default for BaseSpec.
WMSSpec.prototype.setOverlayURL=function(url)
{
    this.overlayURL = url;
}

//sets the layers to use for the overlay. 
WMSSpec.prototype.setOverlayLayers=function(layers)
{
    this.overlayLayers = layers;
}

//sets the format to use for the overlay. 
WMSSpec.prototype.setOverlayFormat=function(format)
{
    this.overlayFormat = format;
}

WMSSpec.prototype.adjustBitmapX=function(a,b)
{
    return this.BaseSpec.adjustBitmapX(a,b);
}

WMSSpec.prototype.getBitmapCoordinate=function(a,b,c,d)
{
    return this.BaseSpec.getBitmapCoordinate(a,b,c,d);
}

WMSSpec.prototype.getLatLng=function(a,b,c,d)
{
    return this.BaseSpec.getLatLng(a,b,c,d);
}

WMSSpec.prototype.getTileCoordinate=function(a,b,c,d)
{
    return this.BaseSpec.getTileCoordinate(a,b,c,d);
}

WMSSpec.prototype.getOverlayURL=function(a,b,c)
{
	if(this.overlayURL){
		return this.getWMSURL(a,b,c, this.overlayURL, this.overlayLayers, this.overlayFormat);
	}else{
		return this.BaseSpec.getOverlayURL(a,b,c);
	}
}

WMSSpec.prototype.getTileURL=function(a,b,c)
{
	if(this.baseURL){
		return this.getWMSURL(a,b,c, this.baseURL, this.baseLayers, this.Format);
	}else{
		return this.BaseSpec.getTileURL(a,b,c);
	}
}

WMSSpec.prototype.getWMSURL=function(a,b,c, url, layers, format)
{
	var ts = this.tileSize;
	var ul = this.getLatLng(a*ts,(b+1)*ts, c);
	var lr = this.getLatLng((a+1)*ts, b*ts, c);
	var bbox = ul.x + "," + ul.y + "," + lr.x + "," + lr.y;
	var url = url + "REQUEST=GetMap&SERVICE=WMS&VERSION=1.1.1&LAYERS=" + layers + "&STYLES=" + this.Styles + "&FORMAT=" + format + "&BGCOLOR=0xFFFFFF&TRANSPARENT=TRUE&SRS=EPSG:4326&BBOX=" + bbox + "&WIDTH=" + ts + "&HEIGHT=" + ts;
	return url;
}


WMSSpec.prototype.getLowestZoomLevel=function(a,b,c)
{
    return this.BaseSpec.getLowestZoomLevel(a,b,c);
}

WMSSpec.prototype.getPixelsPerDegree=function(a)
{
   return this.BaseSpec.getPixelsPerDegree(a);
}

WMSSpec.prototype.getLinkText=function()
{
    return this.Name;
}
WMSSpec.prototype.getShortLinkText=function()
{
    return this.Name;
}
WMSSpec.prototype.hasOverlay=function()
{
    return this.BaseSpec.hasOverlay();
}

WMSSpec.prototype.getURLArg=function()
{
    return this.BaseSpec.getURLArg();
}

WMSSpec.prototype.getCopyright=function()
{
	return this.copywrite;
    //return this.BaseSpec.getCopyright();
}

WMSSpec.prototype.zoomBitmapCoord=function(a,b,c)
{
    return this.BaseSpec.zoomBitmapCoord(a,b,c);
}
