
var DAYLIGHTADJUST=5;function handleOverlayAnimateClick(checked){if(checked){el("OverlayControlAnimCheckboxText").innerHTML="Loading...";setTimeout(function(){mapApp.twcTileHelper.setupAnimate.call(mapApp.twcTileHelper);},50);}else{mapApp.twcTileHelper.animationRunning=false;mapApp.twcTileHelper.removeAllFrames(true,"handleOverlayAnimateClick");}}
function handleOverlayTypeChange(type){if(isBlank(mapApp.twcTileHelper)){mapApp.twcTileHelper=new TWCTileHelper();}
if("WB_TEMPS_OVERLAY"==type){setHighlight("WB_RADAR_OVERLAY",false);setHighlight("WB_CLOUDS_OVERLAY",false);}
if("WB_RADAR_OVERLAY"==type||"WB_CLOUDS_OVERLAY"==type){setHighlight("WB_TEMPS_OVERLAY",false);}
setHighlight(type,!isHighlight(type));mapApp.twcTileHelper.loadTimestamps(mapApp.twcTileHelper.applyOverlays);}
function handleTextMapTypeChange(type){setHighlight("G_NORMAL_MAP",false);setHighlight("G_SATELLITE_MAP",false);setHighlight("G_HYBRID_MAP",false);setHighlight(type,true);if(type=="G_NORMAL_MAP")mapApp.map.setMapType(G_NORMAL_MAP);if(type=="G_SATELLITE_MAP")mapApp.map.setMapType(G_SATELLITE_MAP);if(type=="G_HYBRID_MAP")mapApp.map.setMapType(G_HYBRID_MAP);}
function isHighlight(id){var state=el(id).style.backgroundColor;return!(isBlank(state)||"white"==state);}
function setHighlight(id,value){if(value){el(id).style.backgroundColor="#9999ff";el(id).style.border="solid 2px #9999ff";}else{el(id).style.backgroundColor="white";el(id).style.border="solid 2px white";}}
function TextMapTypeControl(){}
TextMapTypeControl.prototype=new GControl();TextMapTypeControl.prototype.initialize=function(map){var controlDiv=document.createElement("div");controlDiv.innerHTML='<div id="TextMapTypeControl" style="display:block;nowrap" nowrap=""><span><b>VIEW:</b></span><span id="G_NORMAL_MAP" title="Street Map." onclick="handleTextMapTypeChange(\'G_NORMAL_MAP\')">Map</span><span id="G_SATELLITE_MAP" title="Satellite map view." onclick="handleTextMapTypeChange(\'G_SATELLITE_MAP\')">Satellite</span><span id="G_HYBRID_MAP" title="Satellite map with street labels." onclick="handleTextMapTypeChange(\'G_HYBRID_MAP\')">Hybrid</span></div>';map.getContainer().appendChild(controlDiv);setHighlight("G_NORMAL_MAP",true);return controlDiv;}
TextMapTypeControl.prototype.getDefaultPosition=function(){return new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(65,0));}
function TextOverlayTypeControl(){}
TextOverlayTypeControl.prototype=new GControl();TextOverlayTypeControl.prototype.initialize=function(map){var controlDiv=document.createElement("div");controlDiv.innerHTML='<span id="TextOverlayTypeControl" style="display:block;nowrap" nowrap=""><span><b>OVERLAY:</b></span><span title="Shows rain, snow, and ice based on reflection of ground-based radio waves." id="WB_RADAR_OVERLAY" onclick="handleOverlayTypeChange(\'WB_RADAR_OVERLAY\')">Radar</span><span title="Live satellite view of clouds from space." id="WB_CLOUDS_OVERLAY" onclick="handleOverlayTypeChange(\'WB_CLOUDS_OVERLAY\')">Clouds</span><span  id="WB_TEMPS_OVERLAY" title="Current temperatures (US Only)" onclick="handleOverlayTypeChange(\'WB_TEMPS_OVERLAY\')">Temp</span></span><span style="display:none" id="TextMapOverlayControlInfo"></span>';map.getContainer().appendChild(controlDiv);return controlDiv;}
TextOverlayTypeControl.prototype.getDefaultPosition=function(){return new GControlPosition(G_ANCHOR_TOP_RIGHT,new GSize(7,0));}
function TWCTileHelper(){}
TWCTileHelper.prototype.computeOverlayName=function(){if(isHighlight("WB_TEMPS_OVERLAY"))return"temp";if(!USA_BOUNDS.intersects(mapApp.map.getBounds())||mapApp.map.getZoom()<4){if(isHighlight("WB_CLOUDS_OVERLAY")){return"sat"}else{return""};}
if(isHighlight("WB_RADAR_OVERLAY")&&isHighlight("WB_CLOUDS_OVERLAY"))return"satrad";if(isHighlight("WB_RADAR_OVERLAY"))return"radar";if(isHighlight("WB_CLOUDS_OVERLAY"))return"ussat";return"";}
TWCTileHelper.prototype.getSeriesInfo=function(name){if(name=="radar")return this.tileInfo.seriesInfo.radar;if(name=="temp")return this.tileInfo.seriesInfo.temp;if(name=="ussat")return this.tileInfo.seriesInfo.ussat;if(name=="sat")return this.tileInfo.seriesInfo.sat;if(name=="satrad")return this.tileInfo.seriesInfo.satrad;return"";}
TWCTileHelper.prototype.getTimestamp=function(name,index){var seriesInfo=this.getSeriesInfo(name);if(isBlank(seriesInfo))return"";return seriesInfo.series[index].unixDate;}
TWCTileHelper.prototype.applyOverlays=function(){var overlayName=this.computeOverlayName();if(overlayName=="radar"||overlayName=="satrad"){setTileLegend("/weather/image/radarKey.gif");}else if(overlayName=="ussat"||overlayName=="sat"){setTileLegend("/weather/image/cloudKey.gif");}else if(overlayName=="temp"){setTileLegend("");}else{setTileLegend("");}
mapApp.twcTileHelper.removeAllFrames(false);if(isBlank(overlayName)){if(!isBlank(this.currentTileOverlay)){mapApp.map.removeOverlay(this.currentTileOverlay);el("TextMapOverlayControlInfo").style.display="none";this.currentTileOverlay=undefined;}
return;}
var timestamp=this.getTimestamp(overlayName,0);var ts=new Date(parseInt(timestamp)-1000*60*60*DAYLIGHTADJUST);el("TextMapOverlayControlInfo").innerHTML='Time: <span id="OverlayControlTSText">'+ts.toLocaleTimeString()+'</span>. <input id="OverlayControlAnimCheckbox" onclick="handleOverlayAnimateClick(this.checked)" type="checkbox"><nobr id="OverlayControlAnimCheckboxText">Animate</nobr>';this.currentTileOverlayName=overlayName;el("TextMapOverlayControlInfo").style.display="block";var twcLayer=new TRTileLayer().makeTileLayer("http://gima.weather.com/TileServer/imgs/"+overlayName+"/u"+timestamp+"/","radar","image/png","","");if(!isBlank(this.currentTileOverlay)){mapApp.map.removeOverlay(this.currentTileOverlay);}
this.currentTileOverlay=new GTileLayerOverlay(twcLayer);mapApp.map.addOverlay(this.currentTileOverlay);}
TWCTileHelper.prototype.setupAnimate=function(){this.currentTileOverlayArray=[];this.currentTileOverlayTSInfo=this.getSeriesInfo(this.currentTileOverlayName);var numFrames=this.currentTileOverlayTSInfo.series.length;if(numFrames>6)numFrames=6;for(var index=0;index<numFrames;index++){var timestamp=this.currentTileOverlayTSInfo.series[index].unixDate;var twcLayer=new TRTileLayer().makeTileLayer("http://gima.weather.com/TileServer/imgs/"+this.currentTileOverlayName+"/u"+timestamp+"/","radar","image/png","","");var twcLayerOverlay=new GTileLayerOverlay(twcLayer);this.currentTileOverlayArray[index]=twcLayerOverlay;if(logEnabled)GLog.write(timestamp+" created");mapApp.map.addOverlay(twcLayerOverlay);twcLayerOverlay.hide();twcLayerOverlay.textTS=new Date(parseInt(timestamp)-1000*60*60*DAYLIGHTADJUST).toLocaleTimeString();}
this.animationRunning=true;el("OverlayControlAnimCheckboxText").innerHTML="Animate";this.runAnimate(numFrames-1,numFrames,1000);}
TWCTileHelper.prototype.removeAllFrames=function(keepCurrent,debug){this.animationRunning=false;if(isBlank(this.currentTileOverlayArray))return;if(logEnabled)GLog.write("removiong all frames "+keepCurrent+" "+this.currentTileOverlayArray.length)
if(this.currentFrame==undefined)keepCurrent=false;for(var index=0;index<this.currentTileOverlayArray.length;index++){if(keepCurrent&&(index==this.currentFrame)){this.currentTileOverlay=this.currentTileOverlayArray[index];this.currentTileOverlay.show();}else{mapApp.map.removeOverlay(this.currentTileOverlayArray[index]);}}
this.currentTileOverlayArray=undefined;}
TWCTileHelper.prototype.runAnimate=function(frameNo,numFrames,wait){try{if(logEnabled)GLog.write("animate "+frameNo)
if(!this.animationRunning)return;if(!isBlank(this.currentTileOverlay)){mapApp.map.removeOverlay(this.currentTileOverlay);this.currentTileOverlay=undefined;}
el("OverlayControlTSText").innerHTML=this.currentTileOverlayArray[frameNo].textTS;this.currentFrame=frameNo;for(var index=0;index<numFrames;index++){if(index==frameNo){this.currentTileOverlayArray[index].show();}else{this.currentTileOverlayArray[index].hide();}}
var lastFrameWait=(frameNo==0)?2:1;var nextFrameNo=frameNo-1;var thisObject=this;if(nextFrameNo<0)nextFrameNo=numFrames-1;setTimeout(function(){thisObject.runAnimate.call(thisObject,nextFrameNo,numFrames,wait);},wait*lastFrameWait);}catch(e){}}
TWCTileHelper.prototype.loadTimestamps=function(callback){var isStale=isBlank(this.lastLoaded)||(new Date().getTime()-this.lastLoaded>1000*60*2);if(!isBlank(this.tileInfo)&&!isStale){if(!isBlank(callback)){callback.call(this);}
return;}
var helper=this;this.lastLoaded=new Date().getTime();GDownloadUrl("/weather/radar/proxyTileInfo.jsp?"+new Date().getTime(),function(jsonString){jsonString=jsonString.replace("seriesList('null',","");jsonString="("+jsonString.replace(";","")+"";helper.tileInfo=eval(jsonString);if(!isBlank(callback)){callback.call(helper);}})}
function setTileLegend(imageUrl){if(!isDefined(mapApp.tileLegendDiv)){mapApp.tileLegendDiv=document.createElement("div");var pos=new GControlPosition(G_ANCHOR_TOP_RIGHT,new GSize(10,53));pos.apply(mapApp.tileLegendDiv);mapApp.map.getContainer().appendChild(mapApp.tileLegendDiv);}
if(isBlank(imageUrl)){mapApp.tileLegendDiv.innerHTML="";}else{mapApp.tileLegendDiv.innerHTML="<img src='"+imageUrl+"'>";}}