// going for parasitic inheritance
// GMap(document.getElementById("map"));
var db=Math.PI/180;
var twopi=2*Math.PI;
var pi=Math.PI;
var _LocalUSGSImages;
function TracerMapMercSpec(mt,vt,ts){
    var self= new GGoogleMapMercSpec(mt,vt);
    
    // duplicate initMercator to correct for my smaller tile sizes, if necessary
    if(!ts)ts=128;
    var GMapTS=self.tileSize;
    self.tileSize=ts;
    // self.numZoomLevels=18;
    self.numZoomLevels=18 +1 +1 +1;
    var levelTileSize=GMapTS;
    for(var d=self.numZoomLevels-1;d>=0;--d){
 	self.pixelsPerLonDegree[d]=levelTileSize/360;
 	self.pixelsPerLonRadian[d]=levelTileSize/twopi;
 	var e=levelTileSize/2;
	// try to correct consistent offset versus my cached usgs maps
 	// self.bitmapOrigo[d]=new GPoint(e + (0.0001*levelTileSize),e+ (0.0001*levelTileSize));
 	self.bitmapOrigo[d]=new GPoint(e,e);
	//	var offset=self.getTileCoordinate(-.0002,-0.0003,d)
	//	self.bitmapOrigo[d]=new GPoint(eoffset.x,e-offset.y);
 	self.numTiles[d]=levelTileSize/self.tileSize;
 	levelTileSize*=2;
    }

    self.getLinkText=function(){return "Tracer"};
    self.getShortLinkText=function(){return "Tracer"};
    self.getCopyright=function(a){
	return "orthoimagery from USGS";
    };
    self.getTermsLinkColor=function(){return"gray"};
    self.scaleString=function(scale){
	if(scale<1){
            return "000"+ Math.floor(scale*100);
	}else if(scale<16){
	    return "0"+ scale;
	}else{
	    return scale;
	}
    };
    self.tileName=function(topLat,leftLon,scale){
	// probably have to stick something here to make sure I am
	// fetching the correct coordinates
	var fb="ortho_";
	if(topLat>=0){
	    fb=fb + "N";
	}
	fb=fb+ topLat;
	if(topLat == Math.floor(topLat)){
	    fb=fb+".0";
	}
	if(leftLon>=0){
	    fb =fb+ "W";
	}
	fb =fb+ leftLon;
	if(leftLon == Math.floor(leftLon)){
	    fb=fb+".0";
	}
	fb =fb+ "_"+Math.floor(scale)+".png";
	return fb;
    };

    self.getTileURL=function(a,b,zoom){
	if(b<0||b>=self.numTiles[zoom]){return self.emptyTileUrl}
	var d=a;
	// northern hemisphere bias, a (x) can be <0, not b
	if(a<0||self.numTiles[zoom]<=a){
	    d=a%self.numTiles[zoom];
	    if(d<0){
		d+=self.numTiles[zoom];
	    }
	}
	var e=self.getLatLng(d*self.tileSize,b*self.tileSize,zoom);
// 18 july 2005 try to convert to utm using utm.js, to speed up jtorous 
	var utmPoint=calc_utm(e.y,e.x);
	var zooms=[0.125,.25,.5,1,2,4,8,16,32,64];
	var scale =zooms[zoom];

	if(utmPoint.zone==11){
	    var baseUrl=_LocalUSGSImages;
	    var factor= (scale*self.tileSize);
	    var dx = Math.floor(( Math.floor(utmPoint.x/factor) )* factor) ;
	    var dy = Math.floor(( Math.floor(utmPoint.y/factor) )* factor) ;

	    var url=baseUrl +"/"+ self.scaleString(scale)+"/"+self.tileName(dy,dx,scale);
	    // if(_debug){_debug.log("image url is "+url)};
	    return url;
	} else {
	    var baseUrl="http://jtorous.its.uci.edu/cgi-bin/maptile.pl?";
	    var url=baseUrl+"zoom="+zoom+"&lat="+e.y+"&lon="+e.x
	    return url;
	}
    };
    return self;
}

function USGSMercSpec(a,b){
    var self=new TracerMapMercSpec(a,b,256);
    self.getLinkText=function(){return "USGS"};
    self.getShortLinkText=function(){return "USGS"};
    self.getTileURL=function(a,b,zoom){
	if(b<0||b>=self.numTiles[zoom]){return self.emptyTileUrl}
	var d=a;
	//  a (x) can be <0, not b
	if(a<0||self.numTiles[zoom]<=a){
	    d=a%self.numTiles[zoom];
	    if(d<0){
		d+=self.numTiles[zoom];
	    }
	}
	if(_debug){_debug.log("usgs get tile url a="+a+", b="+b+", zoom="+zoom+", d="+d);}
	var e=self.getLatLng(d*self.tileSize,(b+1)*self.tileSize,zoom);
	var utmPoint=calc_utm(e.y,e.x);
	var f=self.getLatLng(((d+1)*self.tileSize)-1,b*self.tileSize -1,zoom);
	var utmPoint2=calc_utm(f.y,f.x);
	var bboxArray=[utmPoint.x,utmPoint.y,utmPoint2.x,utmPoint2.y];
	// var bboxArray=[e.x,e.y,f.x,f.y];
	// -0.00274658203125, -0.0022863553072483
	// var bboxArray=[e.x,e.y,e.x+0.00274658203125,e.y+0.00274658203125];
	// nope that doesn't work either
 	if(_debug){
 	    _debug.log("e="+e+" f="+f+"bboxArray (utm) is "+ bboxArray.join(", "));
 	    var mypt=new decimalCoord(e.x,e.y);
 	    _debug.log("delta box is  "+mypt.difference(f));}
	// fetch from usgs
	// http://ims.cr.usgs.gov/servlet/com.esri.wms.Esrimap/USGS_EDC_Ortho_Urban?SERVICE=WMS&VERSION=1.1.1&STYLES=&SRS=EPSG:4326&FORMAT=image/png&BGCOLOR=0xFF0000&TRANSPARENT=true&LAYERS=Dec_2003-Feb_2004_0.3m_Color_LosAngeles&WIDTH=256&HEIGHT=256&BBOX=-117.949481492237,33.656916153967,-117.939076942103,33.6631652282849&REQUEST=GetMap&EXCEPTIONS=se_blank
	// utm version ---works better, but still a little jagged at the tile boundaries
	var baseUrl="http://ims.cr.usgs.gov/servlet/com.esri.wms.Esrimap/USGS_EDC_Ortho_Urban?SERVICE=WMS&VERSION=1.1.1&STYLES=&SRS=EPSG:32611&FORMAT=image/png&BGCOLOR=0xFF0000&TRANSPARENT=true&LAYERS=Dec_2003-Feb_2004_0.3m_Color_LosAngeles";
	// var baseUrl="http://ims.cr.usgs.gov/servlet/com.esri.wms.Esrimap/USGS_EDC_Ortho_Urban?SERVICE=WMS&VERSION=1.1.1&STYLES=&SRS=EPSG:4326&FORMAT=image/png&BGCOLOR=0xFF0000&TRANSPARENT=true&LAYERS=Dec_2003-Feb_2004_0.3m_Color_LosAngeles";
	
	//var baseUrl="http://ims.cr.usgs.gov/servlet/com.esri.wms.Esrimap/USGS_EDC_Ortho_Urban?SERVICE=WMS&VERSION=1.1.1&STYLES=&SRS=EPSG:4269&FORMAT=image/png&BGCOLOR=0xFF0000&TRANSPARENT=true&LAYERS=Dec_2003-Feb_2004_0.3m_Color_LosAngeles";
	var width=FormKeyValue('WIDTH',self.tileSize);
	var height=FormKeyValue('HEIGHT',self.tileSize);
	var bbox=FormKeyValue('BBOX',bboxArray.join(','));
	var req="&REQUEST=GetMap&EXCEPTIONS=se_blank";

	var url=baseUrl + width + height + bbox + req;
	if(_debug){_debug.log(d*self.tileSize + " " + b*self.tileSize + " " +(d+1)*self.tileSize + " " + (b-1)*self.tileSize + ":  " +bbox);}
	return url;
    };
    return self;
}



var _TRACER_MAP_TYPE;
var _USGS_MAP_TYPE;

    
var oldCreateMapSpecs=window.createMapSpecs;



window.createMapSpecs=function(){
    // parasite
    _TRACER_MAP_TYPE = new TracerMapMercSpec('', 'w2.5');
    var spec=oldCreateMapSpecs();
    spec.push(_TRACER_MAP_TYPE);
    _USGS_MAP_TYPE = new USGSMercSpec('', 'w2.5');
    spec.push(_USGS_MAP_TYPE);
    return spec;
}

