/*
 * Call generic wms service for GoogleMaps v2
 * John Deck, UC Berkeley
 * Inspiration & Code from:
 *	Mike Williams http://www.econym.demon.co.uk/googlemaps2/ V2 Reference & custommap code
 *	Brian Flood http://www.spatialdatalogic.com/cs/blogs/brian_flood/archive/2005/07/11/39.aspx V1 WMS code
 *	Kyle Mulka http://blog.kylemulka.com/?p=287  V1 WMS code modifications
 *      http://search.cpan.org/src/RRWO/GPS-Lowrance-0.31/lib/Geo/Coordinates/MercatorMeters.pm
 *
 * Modified by Chris Holmes, TOPP to work by default with GeoServer.
 * Modified by Eduin Yesid Carrillo Vega to work with any map name. 
 *
 * Note this only works with gmaps v2.36 and above.  http://johndeck.blogspot.com 
 * has scripts
 * that do the same for older gmaps versions - just change from 54004 to 41001.
 *
 * About:
 * This script provides an implementation of GTileLayer that works with WMS
 * services that provide epsg 41001 (Mercator).  This provides a reasonable
 * accuracy on overlays at most zoom levels.  It switches between Mercator
 * and Lat/Long at the myMercZoomLevel variable, defaulting to MERC_ZOOM_DEFAULT
 * of 5.  It also performs the calculation from a GPoint to the appropriate
 * BBOX to pass the WMS.  The overlays could be more accurate, and if you 
 * figure out a way to make them so please contribute information back to
 * http://docs.codehaus.org/display/GEOSDOC/Google+Maps.  There is much
 * information at: 
 * http://cfis.savagexi.com/articles/2006/05/03/google-maps-deconstructed
 * 
 * Use:
 * This script is used by creating a new GTileLayer, setting the required
 * and any desired optional variables, and setting the functions here to 
 * override the appropriate GTileLayer ones.   
 * 
 * At the very least you will need:
 * var myTileLayer= new GTileLayer(new GCopyrightCollection(""),1,17);
 *     myTileLayer.myBaseURL='http://yourserver.org/wms?'
 *     myTileLayer.myLayers='myLayerName';
 *     myTileLayer=CustomGetTileUrl
 *
 * After that you can override the format (myFormat), the level at
 * which the zoom switches (myMercZoomLevel), and the style (myStyles)
 * - be sure to put one style for each layer (both are separated by
 * commas).  You can also override the Opacity:
 *     myTileLayer.myOpacity=0.69
 *     myTileLayer.getOpacity=customOpacity
 *
 * Then you can overlay on google maps with something like:
 * var layer=[G_SATELLITE_MAP.getTileLayers()[0],tileCountry];
 * var custommap = new GMapType(layer, G_SATELLITE_MAP.getProjection(), "WMS");
 * var map = new GMap(document.getElementById("map"));
 *     map.addMapType(custommap);
 */

/*
* extra-dit bestand is een kopie van het origineel, maar de methode CustomGetTileUrl is aangepast
* NB: normaal moet je een eigen method maken (bv :CustomGetTileUrl_x) en die gebruiken, dit is alleen
* bedoeld zodat de klant mbv van een "include" in de html kan wisselen van methode........
*/
var MAGIC_NUMBER=6356752.3142;
var DEG2RAD=0.0174532922519943;
var PI=3.14159267;

//Default image format, used if none is specified
var FORMAT_DEFAULT="image/png";

//Google Maps Zoom level at which we switch from Mercator to Lat/Long.  
var MERC_ZOOM_DEFAULT = 4;
function dd2MercMetersLng(p_lng) { 
	return MAGIC_NUMBER*(p_lng*DEG2RAD); 
}

function dd2MercMetersLat(p_lat) {
	if (p_lat >= 85) p_lat=85;
	if (p_lat <= -85) p_lat=-85;
	return MAGIC_NUMBER*Math.log(Math.tan(((p_lat*DEG2RAD)+(PI/2)) /2));
}

//CustomGetTileUrl aangepast om ervoor te zorgen dat punten goed worden weergegeven.
//aanpassingen: grotere boundingbox aanmaken, ook een grotere width en height opvragen(zelfde verhouding groter dan de boundingbox
//daarna wordt er een dot-net webaaplicatie aangeroepen die het plaatje "cropt".(De link naar deze url zit nu ook hierin, maar
//het zou oom als myBaseURL kunnen worden doorgegeven...Nu wordt deze overruled met
//http://localhost/testcrop/Default.aspx?map=../docs/zeus/maps/all.map
CustomGetTileUrl =function(a,b,c) {	
        if (this.myMercZoomLevel == undefined) {
	    this.myMercZoomLevel = MERC_ZOOM_DEFAULT;
        }

        if (this.myFormat == undefined) {
	    this.myFormat = FORMAT_DEFAULT;
        }

        if (this.myMapname == undefined) {
	    this.myMapname = "map";
        }

	if (typeof(window['this.myStyles'])=="undefined") this.myStyles=""; 
	//ORIGINEEL
	//var lULP = new GPoint(a.x*256,(a.y+1)*256);
	//var lLRP = new GPoint((a.x+1)*256,a.y*256);	
	//AANGEPAST voor DOT NET	
	//Extra info:
	//LULP = Links Onder (Dus niet Upper Left)
	//LLRP = Rechts Boven (Dus niet Lower Right)
	//a is de UpperLeft van een google tile
	//x gaat van links naar rechts, y van boven naar beneden
	
		//16pixels groter w+h =272
		//var lULP = new GPoint((a.x-0.03125)*256,(a.y+1.03125)*256);
		//var lLRP = new GPoint((a.x+1.03125)*256,(a.y-0.03125)*256);	
	//8 pixels groter w+h =264(WIDTH en HEIGHT: moeten in de URL gezet worden(hieronder)
	var lULP = new GPoint((a.x-0.015625)*256,(a.y+1.015625)*256);
	var lLRP = new GPoint((a.x+1.015625)*256,(a.y-0.015625)*256);	
	
	
	var lUL = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lULP,b,c);
	var lLR = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lLRP,b,c);
	
	// switch between Mercator and DD if merczoomlevel is set
	eval("var lwz = "+ this.myMapname + ".getZoom()");
	if (this.myMercZoomLevel!=0 && lwz < this.myMercZoomLevel) {
    	var 
lBbox=dd2MercMetersLng(lUL.x)+","+dd2MercMetersLat(lUL.y)+","+dd2MercMetersLng(lLR.x)+","+dd2MercMetersLat(lLR.y);
        //Change for GeoServer - 41001 is mercator and installed by default.
    	var lSRS="EPSG:41001";
	} else {
    	var lBbox=lUL.x+","+lUL.y+","+lLR.x+","+lLR.y;    	
    	var lSRS="EPSG:4326";
	}	
	//var lURL='http://localhost/testcrop/Default.aspx?map=../docs/zeus/maps/all.map';
	this.myBaseURL='http://zeusworld.geodan.nl/cropimage/Default.aspx?map=../docs/zeus/maps/all.map';
	var lURL=this.myBaseURL;	
	lURL+="&REQUEST=GetMap";
	lURL+="&SERVICE=WMS";
	lURL+="&VERSION=1.1.1";
	lURL+="&LAYERS="+this.myLayers;
	lURL+="&STYLES="+this.myStyles; 
lURL+="&FORMAT="+this.myFormat;
	lURL+="&BGCOLOR=0xFFFFFF";
	lURL+="&TRANSPARENT=TRUE";
	lURL+="&SRS="+lSRS;
	lURL+="&BBOX="+lBbox;
	lURL+="&WIDTH=264";
	lURL+="&HEIGHT=264";	
	lURL+="&reaspect=false"	
	

//document.write(lURL + "<br/>")        
	return lURL;
}

function customOpacity() {	
   return this.myOpacity;
}