
function WMSTileLayer(){}
WMSTileLayer.prototype.makeTileLayer=function(baseUrl,layer,imageType,style,copyright){var nlayer=new GTileLayer(new GCopyrightCollection(""),1,17);nlayer.getTileUrl=this.getTileUrl;nlayer.isPng=this.isPng;nlayer.getOpacity=this.getOpacity;nlayer.animate=this.animate;nlayer.baseUrl=baseUrl;nlayer.layer=layer;nlayer.style=style;nlayer.imageType=imageType;return nlayer;}
function makeImgUrl(obj,layer,bbox,width,height){return obj.baseUrl+"version=1.1.1&request=GetMap&Layers="+layer+"&Styles="+obj.style+"&SRS=EPSG:4326&BBOX="+bbox+"&width="+width+"&height="+height+"&format="+obj.imageType+"&TRANSPARENT=true";}
function makeRadarImageArray(obj,bounds){var ret=[];var bbox=bounds.getSouthWest().lng()+","+bounds.getSouthWest().lat()+","+bounds.getNorthEast().lng()+","+bounds.getNorthEast().lat();var width=400;var height=400;ret.push(makeImgUrl(obj,"nexrad-n0r-m45m",bbox,width,height));ret.push(makeImgUrl(obj,"nexrad-n0r-m40m",bbox,width,height));ret.push(makeImgUrl(obj,"nexrad-n0r-m35m",bbox,width,height));ret.push(makeImgUrl(obj,"nexrad-n0r-m30m",bbox,width,height));ret.push(makeImgUrl(obj,"nexrad-n0r-m25m",bbox,width,height));ret.push(makeImgUrl(obj,"nexrad-n0r-m20m",bbox,width,height));ret.push(makeImgUrl(obj,"nexrad-n0r-m15m",bbox,width,height));ret.push(makeImgUrl(obj,"nexrad-n0r-m10m",bbox,width,height));ret.push(makeImgUrl(obj,"nexrad-n0r-m05min",bbox,width,height));ret.push(makeImgUrl(obj,"nexrad-n0r",bbox,width,height));return ret;}
WMSTileLayer.prototype.stop=function(){if(logEnabled)GLog.write("stopping")
this.singleOverlay.isRunning=false;}
WMSTileLayer.prototype.animate=function(speed,captionDiv){if(logEnabled)GLog.write("calling animate");var map=mapApp.map;if(isDefined(this.singleOverlay)){map.removeOverlay(this.singleOverlay)}
var images=makeRadarImageArray(this,map.getBounds());var ov=new EInsert(map.getBounds(),images[0],.7,50);ov.captionDiv=captionDiv;this.singleOverlay=ov;this.noRemove=true;map.addOverlay(ov);if(logEnabled)GLog.write("have overlay "+typeof(ov));ov.isRunning=true;try{mapApp.radarControlToggle._ov.hide();}catch(e){}
var captions=new Array('45min ago','40min ago','35min ago','30min ago','25min ago','20min ago','15min ago','10min ago','5min ago','now');ov.startAnimate(images,speed,0,captions);}
WMSTileLayer.prototype.minResolution=function(){return this.minResolution;}
WMSTileLayer.prototype.maxResolution=function(){return this.maxResolution;}
WMSTileLayer.prototype.getTileUrl=function(tile,zoom){var proj=G_NORMAL_MAP.getProjection();var p1=proj.fromPixelToLatLng(new GPoint(tile.x*256,tile.y*256),zoom);var p2=proj.fromPixelToLatLng(new GPoint((tile.x+1)*256,(tile.y+1)*256),zoom);var bbox=p1.lng()+","+p2.lat()+","+p2.lng()+","+p1.lat();var url=this.baseUrl+"version=1.1.1&request=GetMap&Layers="+this.layer+"&Styles="+this.style+"&SRS=EPSG:4326&BBOX="+bbox+"&width="+256+"&height="+256+"&format="+this.imageType+"&TRANSPARENT=true";return url;}
WMSTileLayer.prototype.isPng=function(){return this.imageType=='image/png';}
WMSTileLayer.prototype.getOpacity=function(){return.7;}
function TRTileLayer(){}
TRTileLayer.prototype=new WMSTileLayer();TRTileLayer.prototype.isPng=function(){return this.imageType=='image/png';}
TRTileLayer.prototype.getOpacity=function(){return.7;}
TRTileLayer.prototype.getTileUrl=function(tile,zoom){var proj=G_NORMAL_MAP.getProjection();var url=this.baseUrl+quadKey(tile.x,tile.y,zoom);return url+".png";}
function quadKey(tx,ty,zl){var quad="";for(var i=zl;i>0;i--){var mask=1<<(i-1);var cell=0;if((tx&mask)!=0)cell++;if((ty&mask)!=0)cell+=2;quad=quad+cell;}
return quad;}
function SGTileLayer(){}
SGTileLayer.prototype=new TRTileLayer();SGTileLayer.prototype.getTileUrl=function(tile,zoom){return this.baseUrl+"zoom="+zoom+"&x="+tile.x+"&y="+tile.y;}
function RadarControl(){}
RadarControl.prototype=new GControl();RadarControl.prototype.initialize=function(map){var container=document.createElement("div");var zoomInDiv=document.createElement("div");this.setButtonStyle_(zoomInDiv);container.appendChild(zoomInDiv);zoomInDiv.appendChild(document.createTextNode("Zoom In"));GEvent.addDomListener(zoomInDiv,"click",function(){map.zoomIn();});var zoomOutDiv=document.createElement("div");this.setButtonStyle_(zoomOutDiv);container.appendChild(zoomOutDiv);zoomOutDiv.appendChild(document.createTextNode("Zoom Out"));GEvent.addDomListener(zoomOutDiv,"click",function(){map.zoomOut();});map.getContainer().appendChild(container);return container;}
RadarControl.prototype.getDefaultPosition=function(){return new GControlPosition(G_ANCHOR_TOP_RIGHT,new GSize(7,41));}
RadarControl.prototype.setButtonStyle_=function(button){button.style.textDecoration="underline";button.style.color="#0000cc";button.style.backgroundColor="white";button.style.font="small Arial";button.style.border="1px solid black";button.style.padding="2px";button.style.marginBottom="3px";button.style.textAlign="center";button.style.width="6em";button.style.cursor="pointer";}
function RadarControlToggle(ov){this._ov=ov;}
RadarControlToggle.prototype=new GControl();RadarControlToggle.prototype.initialize=function(map){var container=document.createElement("div");var animLabelDiv=document.createElement("div");animLabelDiv.style.position="absolute";animLabelDiv.style.right="6.5em";animLabelDiv.style.width="10em";animLabelDiv.style.textAlign="right";animLabelDiv.innerHTML="<span id='radarAnimBox' style='background-color:white;visibility:hidden'><span id='radarTimestamp'></span><input type='checkbox' id='animateRadarCheck' onclick='animateClick()' name='la' value='click'>Animate</span>";container.appendChild(animLabelDiv);var radarToggleDiv=document.createElement("div");this.radarToggleDiv=radarToggleDiv;this.setButtonStyle_(radarToggleDiv);container.appendChild(radarToggleDiv);radarToggleDiv.appendChild(document.createTextNode("Radar"));var control=this;GEvent.addDomListener(radarToggleDiv,"click",function(){if(!control.initialized){mapApp.map.addOverlay(control._ov);control.initialized=true;control.makeActive(true);}else{if(control.active){control._ov.hide();control.makeActive(false);}else{control._ov.show();control.makeActive(true);}}});GEvent.addListener(map,"moveend",handleMoveEndRadar)
map.getContainer().appendChild(container);return container;}
function handleMoveEndRadar(){if(logEnabled)GLog.write("map moved")
if(isDefined(mapApp.radarLayer.singleOverlay)){if(mapApp.radarLayer.singleOverlay.isRunning){mapApp.radarControlToggle._ov.hide();if(logEnabled)GLog.write("*** need new tiles for the radar")
mapApp.animateImageCacheInvalid=true;var newImages=makeRadarImageArray(mapApp.radarLayer,mapApp.map.getBounds());mapApp.radarLayer.singleOverlay.swapImages(newImages,mapApp.map.getBounds());mapApp.radarLayer.singleOverlay.redraw(true);}}}
RadarControlToggle.prototype.getDefaultPosition=function(){return new GControlPosition(G_ANCHOR_TOP_RIGHT,new GSize(7,29));}
RadarControlToggle.prototype.setButtonStyle_=function(button){button.style.textDecoration="none";button.style.color="#000000";button.style.backgroundColor="white";button.style.font="12px Arial,sans-serif ";button.style.border="1px solid black";button.style.padding="2px";button.style.marginBottom="3px";button.style.textAlign="center";button.style.width="5.0em";button.style.cursor="pointer";button.style.position="absolute";button.style.right="0em";}
RadarControlToggle.prototype.makeActive=function(isActive){if(isActive){this.active=true;this.radarToggleDiv.style.fontWeight='bold';el("radarAnimBox").style.visibility='visible';}else{this.active=false;el("animateRadarCheck").checked=false;el("radarAnimBox").style.visibility='hidden';this.radarToggleDiv.style.fontWeight='normal';el("radarTimestamp").innerHTML="";try{mapApp.radarLayer.singleOverlay.isRunning=false;mapApp.map.removeOverlay(mapApp.radarLayer.singleOverlay);}catch(e){}}}
function animateClick(){if(el("animateRadarCheck").checked){mapApp.radarControlToggle._ov.hide();mapApp.radarLayer.animate(800,el("radarTimestamp"));}else{if(logEnabled)GLog.write("stopping radar "+mapApp.radarLayer)
mapApp.radarLayer.singleOverlay.isRunning=false;mapApp.animateImageCacheInvalid=true;}}