Utils={iconLocation:"http://google-maps-icons.googlecode.com/files/",imageLocation:"http://maps.peterrobins.co.uk/images/",getQueryParams:function(){var qsParm=new Array();var query=window.location.search.substring(1);var parms=query.split('&');for(var i=0;i<parms.length;i++){var pos=parms[i].indexOf('=');if(pos>0){var key=parms[i].substring(0,pos);var val=parms[i].substring(pos+1);qsParm[key]=val;}}
return qsParm;},getPageName:function(){var pathName=window.location.pathname;var start=pathName.lastIndexOf('/')+1;var end=pathName.indexOf('.')
return pathName.slice(start,end);},getPath:function(){var pathName=window.location.pathname;var end=pathName.lastIndexOf('/')+1;return pathName.slice(0,end);},latlongtoNews:function(lat,long){var pos=Math.abs(lat);var dir=(lat>0?'N':'S');var deg=Math.floor(pos);var min=Math.floor((pos-deg)*60);var sec=Math.floor((pos-deg-min/60)*3600);var ns=deg+'\u00B0'+min+"'"+sec+'"'+dir;var pos=Math.abs(long);var dir=(long>0?'E':'W');var deg=Math.floor(pos);var min=Math.floor((pos-deg)*60);var sec=Math.floor((pos-deg-min/60)*3600);var ew=deg+'\u00B0'+min+"'"+sec+'"'+dir;return ns+', '+ew+' ('+(Math.round(lat*10000)/10000)+', '+(Math.round(long*10000)/10000)+')';},showPanel:function(panel){document.getElementById(panel+'Panel').style.display="";},hidePanel:function(panel){document.getElementById(panel+'Panel').style.display="none";return true;},getViewport:function(){var e=window,a='inner';if(!('innerWidth'in window)){a='client';e=document.documentElement||document.body;}
return{width:e[a+'Width'],height:e[a+'Height']}},showTooltip:function(ttText,x,y){var windowWidth=Utils.getViewport().width;o=document.getElementById('tooltip');o.innerHTML=ttText;if(o.offsetWidth){ew=o.offsetWidth;}else if(o.clip.width){ew=o.clip.width;}
y=y+16;x=x-(ew/4);if(x<2){x=2;}else if(x+ew>windowWidth){x=windowWidth-ew-4;}
o.style.left=x+'px';o.style.top=y+'px';o.style.visibility='visible';},hideTooltip:function(){document.getElementById('tooltip').style.visibility='hidden';},gotoCoords:function(){var lat=document.getElementById("lat").value;var long=document.getElementById("long").value;Map.Markers.addMarker(long,lat);},jsCallify:function(func,text){return'<span class="jscall" onclick="'+func+'">'+text+'</span>'}}
StatusLog={logDiv:null,msgs:{'init':'Initialising map','mapsetup':'Setting up map','gdata':'GData feed loading','geoload':'GeoJSON feed loading','maploaded':'Map loaded. '+Utils.jsCallify("StatusLog.logDiv.hide();return","Close")},init:function(div){this.logDiv=document.getElementById(div);this.logMsg('init');},logMsg:function(msg,param){if(msg=='fetch')
this.msgs[msg]='Fetching '+param+' data';if(msg=='loaded')
this.msgs[msg]=param+' data loaded';this.logDiv.innerHTML+=this.msgs[msg]+'<br />';},endMsg:function(){this.logMsg('maploaded');setTimeout("StatusLog.logDiv.style.display='none'",5000);}}
WindowOpen={win:null,title:'Route Details',statusBar:'Links open in new tab/window',url:function(url,width){width=width||500;if(!this.win)
this.win=new Window('urlWindow',{className:'popup',width:width});this.win.setTitle(this.title);this.win.setURL(url);this.win.setStatusBar(this.statusBar);this.win.setZIndex(100);this.win.parentWin=window;this.win.showCenter();this.win.toFront();},div:function(div){if(!this.win)
this.win=new Window('divWindow',{className:'popup',width:300,height:200});else{var content=this.win.getContent();if(content.firstChild.getAttribute('id')!=div){content.firstChild.style.display='none';document.body.appendChild(content.firstChild);}}
this.win.setTitle(this.title);this.win.setContent(div);this.win.setStatusBar(this.statusBar);this.win.setZIndex(100);this.win.showCenter();this.win.toFront();},html:function(html){if(!this.win)
this.win=new Window('htmlWindow',{className:'popup',width:400,height:200});this.win.setTitle(this.title);this.win.setHTMLContent(html);this.win.setZIndex(100);this.win.showCenter();this.win.toFront();},help:function(){var helpWin=new Window('helpWindow',{className:'popup',width:500,destroyOnClose:true});helpWin.setTitle('Help');helpWin.setAjaxContent(ServerVars.serverPath+ServerVars.helpUrl,{method:'get'});helpWin.setStatusBar(WindowOpen.statusBar);helpWin.setZIndex(100);helpWin.showCenter();helpWin.toFront();},search:function(gmaps){var searchWin=new Window('searchWindow',{className:'popup',width:500,destroyOnClose:true});searchWin.setTitle('Search for coordinates/placenames');var url=ServerVars.serverPath+'/searchPanel.html'+(gmaps?'?gmaps=true':'');searchWin.setAjaxContent(url,{method:'get'});searchWin.setStatusBar('');searchWin.setZIndex(100);searchWin.showCenter();searchWin.toFront();},palette:function(){var paletteWin=new Window('paletteWindow',{className:'popup',width:150,height:200,destroyOnClose:true});paletteWin.setAjaxContent(ServerVars.serverPath+'/palette.html',{method:'get'});paletteWin.setStatusBar('Pick a colour');paletteWin.setZIndex(100);paletteWin.showCenter();paletteWin.toFront();}}
Feed={worksheetCount:0,createScript:function(id,url){var old=document.getElementById(id);if(old!=null){old.parentNode.removeChild(old);delete old;}
url=url.replace(/amp;/g,'');var script=document.createElement('script');script.id=id;script.type='text/javascript';script.charset='utf-8';script.src=url;document.getElementsByTagName("head")[0].appendChild(script);},Json:{getPlaces:function(){var place=document.getElementById("place").value;var url='http://ws5.geonames.org/searchJSON?maxRows=10&isNameRequired=true&continentCode=eu&featureClass=P&style=full&callback=Feed.Json.showPlaces&q='+place;Feed.createScript('places',url);},showPlaces:function(feed){if(feed==null){alert('Nothing returned from geonames.org');return;}
var geonames=feed.geonames;if(geonames.length==0){alert('No such place on geonames.org');return;}
if(geonames.length==1){var lat=geonames[0].lat;var long=geonames[0].lng;Map.Markers.addMarker(long,lat);}
else{var html='';for(i=0;i<geonames.length;i++){var geoname=geonames[i];var lat=geoname.lat;var long=geoname.lng;var fullName=geoname.name+', '+geoname.adminName1+', '+geoname.adminName2+', '+geoname.adminName3+', '+geoname.adminName4;html=html+Utils.jsCallify("Map.Markers.addMarker("+long+", "+lat+")",fullName)+'<br />';}
document.getElementById('searchResults').innerHTML=html;}},getWikipedia:function(long,lat){var url='http://ws5.geonames.org/findNearbyWikipediaJSON?callback=Feed.Json.showWikipedia&lat='+lat+'&lng='+long;Feed.createScript('wiki',url);},showWikipedia:function(feed){if(typeof feed.geonames=='undefined')
var html='No nearby Wikipedia entries or service unavailable';else{var geonames=feed.geonames;if(geonames.length==0){alert('No Wikipedia entries');return;}
var html='';for(i=0;i<geonames.length;i++){var geoname=geonames[i];html+='<b>'+geoname.title+'</b> ('+geoname.lat+', '+geoname.lng+': '+geoname.distance+'km away)';html+='<br />'+geoname.summary;html+='<br />'+Utils.jsCallify("Map.Markers.addMarker("+geoname.lng+","+geoname.lat+")","Show on map")+' | <a href="http://'+geoname.wikipediaUrl+'" target="_blank">View Wikipedia page</a><br />';}}
document.getElementById('wikiResults').innerHTML=html;},getMetar:function(long,lat){var url='http://ws5.geonames.org/findNearByWeatherJSON?callback=Feed.Json.showMetar&lat='+lat+'&lng='+long;Feed.createScript('metar',url);},showMetar:function(feed){if(typeof feed.weatherObservation=='undefined')
var html='No nearby weather stations or report unavailable';else{var weather=feed.weatherObservation;var html='<b>'+weather.stationName+'</b> ('+weather.ICAO+')';html+='<br />'+weather.lat+', '+weather.lng+'; altitude: '+weather.altitude;html+='<br />'+weather.datetime;html+='<br />'+weather.weatherCondition+'; '+weather.clouds+'; '+weather.temperature+'&deg;C; '+weather.hectoPascAltimeter+' hPa; humidity: '+weather.humidity+'%; wind: '+weather.windDirection+'&deg; @ '+weather.windSpeed+' knots';}
document.getElementById('weatherResults').innerHTML=html;}}};Map={wgs84:new OpenLayers.Projection("EPSG:4326"),map:null,addBaseLayers:function(){},addSpecifics:function(){},zoomToFeatures:false,defaultLonglat:new OpenLayers.LonLat(0,0),defaultZoom:0,zoomChangeNeeded:false,create:function(){StatusLog.init('status');var options={controls:[new OpenLayers.Control.Navigation(),new OpenLayers.Control.KeyboardDefaults(),new OpenLayers.Control.PanZoomBar(),new OpenLayers.Control.Scale(),new OpenLayers.Control.Attribution()]};this.map=new OpenLayers.Map('map',options);this.addBaseLayers();this.map.addControl(new OpenLayers.Control.MousePosition({id:"ll_mouse",formatOutput:this.formatMousePosition}));this.addSpecifics();if((this.zoomToFeatures==false)&&(!location.search)){this.map.setCenter(this.defaultLonglat,this.defaultZoom);}
if(location.search){var args=OpenLayers.Util.getParameters();if(args.mark=='y'){this.Markers.addMarker(args.lon,args.lat,false);}}
if(this.zoomToFeatures==false){if(this.zoomChangeNeeded)
this.map.events.register('zoomend',this.map,this.zoomChanged);StatusLog.endMsg();}},formatMousePosition:function(lonLat){return Utils.latlongtoNews(lonLat.lat,lonLat.lon);},Markers:{icon:new OpenLayers.Icon(Utils.imageLocation+'bluearrow.png',new OpenLayers.Size(16,16),new OpenLayers.Pixel(-8,-16)),layer:null,map:null,addLayer:function(){this.map=Map.map;this.layer=new OpenLayers.Layer.Markers("Markers");this.map.addLayer(this.layer);},addMarker:function(long,lat,isMapProj,title){var point=new OpenLayers.LonLat(long,lat);if(isMapProj!=true)
if(this.map.projection!="EPSG:4326")
point.transform(this.map.displayProjection,this.map.baseLayer.projection);var bounds=this.map.getMaxExtent();if(bounds.containsLonLat(point)){if(!title){var zoom=this.map.getZoom();if(zoom<3)
zoom=zoom+3;this.map.setCenter(point,zoom);}
var mark=new OpenLayers.Marker(point,this.icon.clone());this.layer.addMarker(mark);var imgTitle=title?title:'Lat '+lat+', long '+long;if(isMapProj==true)
imgTitle='x '+long+', y '+lat;mark.icon.imageDiv.firstChild.setAttribute('title',imgTitle);}
else alert('Coordinates not on map!');},clearMarkers:function(){this.map.getLayersByName('Markers')[0].clearMarkers();}},Panel:{panel:null,create:function(){this.panel=new OpenLayers.Control.Panel();this.panel.addControls([new OpenLayers.Control.Button({displayClass:"helpButton",trigger:WindowOpen.help,title:'Help'})]);Map.map.addControl(this.panel);},addSearch:function(){this.panel.addControls([new OpenLayers.Control.Button({displayClass:"searchButton",trigger:WindowOpen.search,title:'Search for coords/placename'}),new OpenLayers.Control.Button({displayClass:"clearButton",trigger:Map.Markers.clearMarkers,title:'Clear search markers'})]);}},addClick:function(trigger){var click=new OpenLayers.Control.Click({'trigger':trigger});this.map.addControl(click);click.activate();},addPerma:function(){this.map.addControl(new OpenLayers.Control.Permalink(null,null,{id:'thisPerma',title:"Link to display this map again"}));},positionDisplay:function(e){var map=e.object;var lonlat=map.getLonLatFromViewPortPx(e.xy);if(map.projection!="EPSG:4326")
lonlat.transform(map.baseLayer.projection,map.displayProjection);lat=Math.round(lonlat.lat*1000000)/1000000;lon=Math.round(lonlat.lon*1000000)/1000000;var html='You clicked at latitude '+lat+', longitude '+lon;html+='<p>Get '+Utils.jsCallify("Feed.Json.getMetar(lon, lat)","METAR weather report")+' from nearest station</p>';html+='<div id="weatherResults"></div>';html+='<p>Show nearby '+Utils.jsCallify("Feed.Json.getWikipedia(lon, lat)","Wikipedia entries")+'</p>';html+='<div id="wikiResults"></div>';WindowOpen.title='Selected Item: Details';WindowOpen.html(html);}}
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':options.trigger},this.handlerOptions);}});