var OLUtils={}
;OLUtils.zoomPastLimit=true;OLUtils.zoomPastWebService="ZoomInHandler.ashx";OLUtils.selectedControl=null;OLUtils.selectedFeature=null;OLUtils.featureClickHandler=null;OLUtils.TMSCustomGetTileURL=function(xh){var ji=this.map.getResolution();var ki=Math.round((xh.left-this.maxExtent.left)/(ji*this.tileSize.w));var li=Math.round((this.maxExtent.top-xh.top)/(ji*this.tileSize.h));var mi=this.map.getZoom();if((!OLUtils.zoomPastLimit)&&(mi>this.maxZoomLevel)){return this.url+"\x2Fblank-tile.png";}
;var c=Math.pow(2,mi);var x=360/c*ki-180;var y=180-360/c*li;var kg=x+360/c;var lg=y-360/c;var qd=x;var pd=(2.0*Math.atan(Math.exp(y/180*Math.PI))-Math.PI/2.0)*180/Math.PI;var mg=kg;var ng=(2.0*Math.atan(Math.exp(lg/180*Math.PI))-Math.PI/2.0)*180/Math.PI;var og=new OpenLayers.Bounds(qd,ng,mg,pd);if(!og.intersectsBounds(this.mapBounds)){return this.url+"\x2Fblank-tile.png";}
;var d=ki;var e=li;var f="t";for(var g=0;g<mi;g++){c=c/2;if(e<c){if(d<c){f+="q"}
else{f+="r";d-=c}
}
else{if(d<c){f+="t";e-=c}
else{f+="s";d-=c;e-=c}
}
}
if(mi>this.maxZoomLevel){return OLUtils.zoomPastWebService+"?z="+this.maxZoomLevel+"&t="+f+"&u="+this.url;}
return this.url+"\x2F"+f+".png"}
;OLUtils.TMSMapDataDescriptorGetTileURL=function(xh){var ji=this.map.getResolution();var ki=Math.round((xh.left-this.maxExtent.left)/(ji*this.tileSize.w));var li=Math.round((this.maxExtent.top-xh.top)/(ji*this.tileSize.h));var mi=this.map.getZoom();var c=Math.pow(2,mi);var d=ki;var e=li;var f="t";for(var g=0;g<mi;g++){c=c/2;if(e<c){if(d<c){f+="q"}
else{f+="r";d-=c}
}
else{if(d<c){f+="t";e-=c}
else{f+="s";d-=c;e-=c}
}
}
return this.url.replace("\x7B2\x7D",f);}
;OLUtils.getNumLayers=function(ni){var lc=ni.getLayersBy("isBaseLayer",false);return lc.length;}
;OLUtils.getFirstDataLayerIndex=function(ni){for(var i=0;i<ni.layers.length;i++)if(!ni.layers[i].isBaseLayer)return i;return-1;}
;OLUtils.getInitialZoom=function(ni){var wh=OLUtils.getAllLayersBounds(ni);var z=6;if(wh!=null){var length=Math.sqrt(wh.getHeight()*wh.getWidth());z=(Math.log(40075016.68)-Math.log(length))/Math.log(2)+1;}
return Math.floor(z);}
;OLUtils.getAllLayersBounds=function(ni){var wh;for(var i=0;i<ni.layers.length;i++){if(!ni.layers[i].isBaseLayer){var xh;if(ni.layers[i]instanceof OpenLayers.Layer.TMS){xh=ni.layers[i].mapBounds.clone();xh.transform(new OpenLayers.Projection("EPSG:4326"),ni.getProjectionObject());}
else
xh=ni.layers[i].getDataExtent();if(xh!=null){if(wh==null)wh=xh;else
wh.extend(xh);}
}
}
return wh;}
;OLUtils.getAllLayersCentre=function(ni){var wh=OLUtils.getAllLayersBounds(ni);if(wh!=null)return wh.getCenterLonLat();return new OpenLayers.LonLat(-0.13102327732138574,51.52650545529106).transform(new OpenLayers.Projection("EPSG:4326"),ni.getProjectionObject());}
;OLUtils.containsKMLLayer=function(ni){for(var i=0;i<ni.layers.length;i++)if((!ni.layers[i].isBaseLayer)&&(ni.layers[i]instanceof OpenLayers.Layer.GML))return true;return false;}
;OLUtils.getNumClickableLayers=function(ni){var $h=0;for(var i=0;i<ni.layers.length;i++){if((ni.layers[i].isclickable)&&(ni.layers[i].isclickable==true))++$h;}
return $h;}
;OLUtils.scheduleMapDefaultView=function(ni){for(var i=0;i<ni.layers.length;i++){if((!ni.layers[i].isBaseLayer)&&(ni.layers[i]instanceof OpenLayers.Layer.GML)){ni.layers[i].events.register("loadend",ni,function(){var xh=OLUtils.getAllLayersBounds(this);if(xh!=null){var z=this.getZoomForExtent(xh,false);this.setCenter(xh.getCenterLonLat(),z);}
}
);}
}
}
;OLUtils.onfeaturechange=function(){}
;OLUtils.getTileCoords=function(ni,e){if(!e)var e=window.event;var x=0,y=0;if(e.offsetX){x=e.offsetX;y=e.offsetY;}
else{x=e.clientX;y=e.clientY;var eb=e.target;while(eb){if(eb.offsetLeft)x-=eb.offsetLeft;if(e.offsetTop)y-=eb.offsetTop;eb=eb.offsetParent;}
}
var kf=ni.div;var oi=ni.getLonLatFromPixel(new OpenLayers.Pixel(e.clientX-kf.offsetLeft,e.clientY-kf.offsetTop));var latlng=oi.clone().transform(ni.projection,new OpenLayers.Projection("EPSG:4326"));var name=e.srcElement?e.srcElement.src:e.target.src;if(name){var xc=name.lastIndexOf("\x2F");var qh=name.lastIndexOf("t=");if(qh>xc)xc=qh+1;if(xc>=0)name=name.substring(xc+1);xc=name.lastIndexOf(".");if(xc>=0)name=name.substring(0,xc);}
else name="";return{'tileX':x,'tileY':y,'tileName':name,'latLng':latlng,'mapLatLng':oi}
;}
;OLUtils.setFeatureClickEnabled=function(ni,th){if(th){if(this.featureClickHandler)return;OpenLayers.Control.Click=OpenLayers.Class(OpenLayers.Control,{'defaultHandlerOptions':{'single':true,'double':false,'pixelTolerance':0,'stopSingle':false,'stopDouble':false
}
,'initialize':function(options){this.handlerOptions=OpenLayers.Util.extend({}
,this.defaultHandlerOptions);OpenLayers.Control.prototype.initialize.apply(this,arguments);this.handler=new OpenLayers.Handler.Click(this,{'click':this.trigger
}
,this.handlerOptions
);}
,'trigger':function(e){var pi=e.srcElement||e.originalTarget;if((pi)&&(pi instanceof HTMLImageElement)&&((pi.width!=256)||(pi.height!=256)))return;var nb=OLUtils.getTileCoords(ni,e);nb.layers=new Array();var ad=OLUtils.getNumLayers(ni);var bd=OLUtils.getFirstDataLayerIndex(ni);for(var i=0;i<ad;i++){var fd=ni.layers[ad-i-1+bd].mapid;if(typeof fd=="undefined")continue;nb.layers.push(fd);}
if(OLUtils.onfeaturechange)OLUtils.onfeaturechange(nb);}
}
);OLUtils.featureClickHandler=new OpenLayers.Control.Click();ni.addControl(OLUtils.featureClickHandler);OLUtils.featureClickHandler.activate();}
else{if(!this.featureClickHandler)return;OLUtils.featureClickHandler.deactivate();ni.removeControl(OLUtils.featureClickHandler);this.featureClickHandler=null;}
}
;OLUtils.onPopupClose=function(qi){OLUtils.selectedControl.unselect(OLUtils.selectedFeature);}
;OLUtils.onFeatureSelect=function(ri){OLUtils.selectedControl=this;OLUtils.selectedFeature=ri;si=new OpenLayers.Popup.FramedCloud("openpopup",ri.geometry.getBounds().getCenterLonLat(),new OpenLayers.Size(100,100),"<div style='font-size:.8em'>"+ri.attributes.description+"<\x2Fdiv>",null,true,OLUtils.onPopupClose);ri.popup=si;map.addPopup(si);}
;OLUtils.onFeatureUnselect=function(ri){map.removePopup(ri.popup);ri.popup.destroy();ri.popup=null;}
;OLUtils.FeatureClickControl=OpenLayers.Class(OpenLayers.Control,{'button':null,'isTicked':true,'onclick':null,'initialize':function(options){OpenLayers.Control.prototype.initialize.apply(this,[options]);this.onclick=options.onclick;}
,'setMap':function(map){OpenLayers.Control.PanZoom.prototype.setMap.apply(this,arguments);this.map.events.register("changebaselayer",this,this.reposition);}
,'destroy':function(){if(this.button)OpenLayers.Event.stopObservingElement(this.button);this.map.events.unregister("changebaselayer",this,this.reposition);OpenLayers.Control.prototype.destroy.apply(this,arguments);}
,'reposition':function(){var bf=map.getControlsByClass("OpenLayers.Control.PanZoomBar");var y=314;if(bf){for(var i=0;i<bf[0].buttons.length;i++){if(bf[0].buttons[i].id.indexOf("_zoomout")>=0){var je=bf[0].buttons[i];y=parseInt(je.style.top)+parseInt(je.style.height);}
}
}
this.div.style.top=(y+8)+"px";}
,'draw':function(){OpenLayers.Control.prototype.draw.apply(this);this.div.style.top="314px";this.div.style.right="";this.div.style.left="14px";this.button=document.createElement("div");this.button.style.border="none";this.button.style.width="24px";this.button.style.height="24px";this.button.style.cursor="pointer";this.button.style.backgroundImage="url(images/mousebutton-24x24-on.gif)";this.button.style.backgroundRepeat="no-repeat";this.button.style.backgroundPosition="top left";this.div.appendChild(this.button);OpenLayers.Event.observe(this.button,"click",OpenLayers.Function.bindAsEventListener(function(e){this.toggle();if(!e)var e=window.event;e.cancelBubble=true;if(e.stopPropagation)e.stopPropagation();}
,this
));return this.div;}
,'toggle':function(){this.setEnabled(!this.isTicked);}
,'setEnabled':function(fc){if(fc){this.isTicked=true;this.button.style.backgroundImage="url(images/mousebutton-24x24-on.gif)";}
else{this.isTicked=false;this.button.style.backgroundImage="url(images/mousebutton-24x24-off.gif)";}
if(this.onclick)this.onclick.call(this,this.isTicked);}
,CLASS_NAME:"FeatureClickControl"}
);OLUtils.KMLButtonControl=OpenLayers.Class(OpenLayers.Control,{'kmlButtonDiv':null,'menuDiv':null,'inputBox':null,'geokmloverlayurl':null,'geokmloverlay':null,'featureControl':null,'setKML':function(dc){if(this.geokmloverlay!=null){this.map.removeLayer(this.geokmloverlay);this.geokmloverlay=null;}
if(this.featureControl!=null){this.map.removeControl(this.featureControl);this.featureControl=null;}
this.geokmloverlayurl=dc;if((dc!=null)&&(dc.length>0)){if(dc.indexOf("http:\x2F\x2F")!=0)dc="http:\x2F\x2F"+dc;this.geokmloverlay=new OpenLayers.Layer.GML("KML Overlay",dc,{'projection':new OpenLayers.Projection("EPSG:4326"),'format':OpenLayers.Format.KML,'formatOptions':{'extractStyles':true,'extractAttributes':true
}
}
);this.map.addLayer(this.geokmloverlay);this.featureControl=new OpenLayers.Control.SelectFeature(this.geokmloverlay,{'onSelect':OLUtils.onFeatureSelect,'onUnselect':OLUtils.onFeatureUnselect}
);this.map.addControl(this.featureControl);this.featureControl.activate();}
this.inputBox.value=dc;}
,'getKML':function(){return this.geokmloverlayurl;}
,'initialize':function(options){OpenLayers.Control.prototype.initialize.apply(this,arguments);}
,'destroy':function(){if(this.kmlDiv)OpenLayers.Event.stopObservingElement(this.kmlDiv);if(this.addButtonDiv)OpenLayers.Event.stopObservingElement(this.addButtonDiv);OpenLayers.Event.stopObservingElement(document);OpenLayers.Control.prototype.destroy.apply(this,arguments);}
,'draw':function(){OpenLayers.Control.prototype.draw.apply(this);this.loadContents();return this.div;}
,'maximizeControl':function(e){if(this.menuDiv.style.display!="block")this.menuDiv.style.display="block";var yb=0,zb=0;var eb=this.div;while(eb){yb+=eb.offsetLeft;zb+=eb.offsetTop;eb=eb.offsetParent;}
var $b=yb+this.menuDiv.offsetWidth;var _b=zb+this.menuDiv.offsetHeight+this.kmlButtonDiv.offsetHeight;OpenLayers.Event.observe(document,"mousemove",OpenLayers.Function.bindAsEventListener(function(e){if(!e)e=window.event;var x=e.clientX;var y=e.clientY;if((x<yb)||(x>$b)||(y<zb)||(y>_b)){this.minimizeControl(e);}
}
,this));if(e!=null){OpenLayers.Event.stop(e);}
}
,'minimizeControl':function(e){if(this.menuDiv.style.display!="none")this.menuDiv.style.display="none";OpenLayers.Event.stopObservingElement(this.menuDiv);if(e!=null){OpenLayers.Event.stop(e);}
}
,'loadContents':function(){this.div.style.top="7px";this.div.style.right="216px";this.div.style.left="";this.kmlButtonDiv=document.createElement("div");this.setButtonStyle(this.kmlButtonDiv,"KML");this.div.appendChild(this.kmlButtonDiv);this.menuDiv=document.createElement("div");this.menuDiv.id=this.id="_opendiv";this.menuDiv.style.display="none";this.menuDiv.style.position="absolute";this.menuDiv.style.width="240px";this.menuDiv.style.padding="4px";this.menuDiv.style.color="#000000";this.menuDiv.style.backgroundColor="#ffffff";this.menuDiv.style.font="small Arial";this.menuDiv.style.borderWidth="1px 2px 2px 1px";this.menuDiv.style.borderColor="gray black black black";this.menuDiv.style.borderStyle="solid ridge ridge solid";this.div.appendChild(this.menuDiv);this.menuDiv.innerHTML="Enter the URL of a KML file here and click on the add button.<br />";this.inputBox=document.createElement("input");this.inputBox.id="kmlinputbox";this.inputBox.name="kmlinputbox";this.inputBox.tabIndex=1;this.inputBox.type="text";this.inputBox.style.width="232px";this.menuDiv.appendChild(this.inputBox);this.addButtonDiv=document.createElement("div");this.setButtonStyle(this.addButtonDiv,"Add");this.menuDiv.appendChild(this.addButtonDiv);OpenLayers.Event.observe(this.addButtonDiv,"click",OpenLayers.Function.bindAsEventListener(function(){this.setKML(this.inputBox.value);}
,this
));OpenLayers.Event.observe(this.inputBox,"change",OpenLayers.Function.bindAsEventListener(function(){this.setKML(this.inputBox.value);}
,this
));OpenLayers.Event.observe(this.kmlButtonDiv,"mouseover",OpenLayers.Function.bind(this.maximizeControl,this));OpenLayers.Event.observe(this.inputBox,"click",OpenLayers.Function.bindAsEventListener(function(){this.focus()}
,this.inputBox));}
,'setButtonStyle':function(button,text){button.style.color="#000000";button.style.backgroundColor="#ffffff";button.style.font="small Arial";button.style.border="1px solid black";button.style.padding="2px";button.style.marginBottom="3px";button.style.padding="0px";button.style.margin="0px";button.style.textAlign="center";button.style.width="65px";button.style.cursor="pointer";var cc=document.createElement("div");cc.style.color="#000000";cc.style.backgroundColor="#ffffff";cc.style.borderTop="none";cc.style.borderLeft="1px solid white";cc.style.borderRight="1px solid #b0b0b0";cc.style.borderBottom="1px solid #b0b0b0";cc.style.margin="0px";cc.style.padding="0px";button.appendChild(cc);cc.appendChild(document.createTextNode(text));}
,CLASS_NAME:"KMLButtonControl"}
);
