/*
Call cached ArcIMS service for GoogleMaps v2
Jeremy Bartley, Kansas Geological Survey
Inspiration & Code from:

    John Deck for reworking the overlay code to work with Gooogle Maps version2
	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
*/

/*var MAGIC_NUMBER=6356752.3142;
var DEG2RAD=0.0174532922519943;
var PI=3.14159267;
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));
}

CustomGetTileUrl1 = function (x, y, zoom){
    var ts = 256;
    var lULP = new GPoint(x*ts,(y+1)*ts, zoom);
    var lLRP = new GPoint((x+1)*ts,y*ts, zoom);
    var ul = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lULP,zoom,true);
	var lr = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lLRP,zoom,true);
    var bbox = ul.x + "," + ul.y + "," + lr.x + "," + lr.y;
    var url = this.myBaseURL + "&BBOX=" + bbox + "&WIDTH=" + ts + "&HEIGHT=" + ts + "&X=" + x + "&Y=" + y + "&zoom=" + zoom;
    return url;
   };
*/
CustomGetTileUrl=function(a,b,c) {
	//alert(a.x)
	//alert(a.y);
	//alert(b);
	//alert(c);
	if (typeof(window['this.myStyles'])=="undefined") this.myStyles=""; 
	var ts = 256;
	var lULP = new GPoint(a.x*ts,(a.y+1)*ts);
	var lLRP = new GPoint((a.x+1)*ts,a.y*ts);
	var UL = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lULP,b,c);
	var LR = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lLRP,b,c);
        var bbox = UL.x + "," + UL.y + "," + LR.x + "," + LR.y;
        var newzoom = map.getZoom();
        var oldzoom = 17 - newzoom;
        var url = this.myBaseURL + "&BBOX=" + bbox 
                                 + "&WIDTH=" + ts 
                                 + "&HEIGHT=" + ts 
                                 + "&X=" + a.x 
                                 + "&Y=" + a.y 
                                 + "&zoom=" + newzoom
                                 + "&newzoom=" + newzoom;
                                //alert(url);
        return url;
	// switch between Mercator and DD if merczoomlevel is set

//	if (this.myMercZoomLevel!=0 && map.getZoom() < this.myMercZoomLevel) {
//    	var lBbox=dd2MercMetersLng(lUL.x)+","+dd2MercMetersLat(lUL.y)+","+dd2MercMetersLng(lLR.x)+","+dd2MercMetersLat(lLR.y);
//    	var lSRS="EPSG:54004";
//	} else {
//    	var lBbox=lUL.x+","+lUL.y+","+lLR.x+","+lLR.y;
//    	var lSRS="EPSG:4326";
//	}
//	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=256";
//	lURL+="&HEIGHT=256";
//	lURL+="&reaspect=false";
//	return lURL;
}

