var map;    // object of type OpenLayers.Map, for base map layer
var track;  // object of type OpenLayers.Layer.GML, for track overlay

//Initialise the 'map' object
function basemap() {
    map = new OpenLayers.Map ("map", {
	    controls:[
		      new OpenLayers.Control.Navigation(),
		      new OpenLayers.Control.PanZoom(),
		      new OpenLayers.Control.Permalink()
		      ],
	    maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
	    maxResolution: 156543.0399,
	    numZoomLevels: 19,
	    units: 'm',
	    projection: new OpenLayers.Projection("EPSG:900913"),
	    displayProjection: new OpenLayers.Projection("EPSG:4326")
	} );
 
    // Define the map layer
    layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik");
    map.addLayer(layerMapnik);
}

// Add a track overlay using a GPX file.
function addgpxtrack(tname, fname, colour, opacity) {
    track = new OpenLayers.Layer.GML(tname, fname, {
	    format: OpenLayers.Format.GPX,
	    style: {strokeColor: colour,
		    strokeWidth: 10,
		    strokeOpacity: opacity
	    },
	    projection: new OpenLayers.Projection("EPSG:4326")
	});
    //    addtrack();
    map.addLayer(track);
}

function addtrack() {
    map.addLayer(track);
    if (!map.getCenter()) {
	track.events.register("loadend", track, function() {
		if (this.features.length) { 
		    var extent =  this.features[0].geometry.getBounds();
		    for (var i = 1; i < this.features.length; i++) {
			extent.extend(this.features[i].geometry.getBounds());
		    }
		}
		if (extent) {
		    this.map.zoomToExtent(extent);
		} else {
		    this.map.zoomToMaxExtent();
		}
	    } );
	track.loadGML();
    } 
}

// add a 30 mile circle
function addlocale(lat, lon) {
    var vectors, origin, circleStyle, circleFeature;

    vectors = new OpenLayers.Layer.Vector("LocAle Radius");

    map.addLayer(vectors);

    origin = new OpenLayers.Geometry.Point(lon,lat);
    origin.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")); 
    circleStyle = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style["default"]);
    circleStyle.strokeColor="blue";
    circleStyle.fillColor="blue";
    circleStyle.fillOpacity=0.05;
    
    circleFeature = new OpenLayers.Feature.Vector(
						  OpenLayers.Geometry.Polygon.createRegularPolygon(origin,
												   30*2600,
												   60,
												   0),
						  null,
						  circleStyle );
    vectors.addFeatures( [circleFeature] ); 

}

// Centre the map on a particular lat/lon and draw a small red circle
// around the point.
function centremap (lat,lon,zoom) {
    var lonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
    map.setCenter(map.setCenter (lonLat, zoom));
    var vectors, origin, circleStyle, circleFeature;

    vectors = new OpenLayers.Layer.Vector("Pub");

    map.addLayer(vectors);

    origin = new OpenLayers.Geometry.Point(lon,lat);
    origin.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")); 
    circleStyle = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style["default"]);
    circleStyle.strokeColor="red";
    circleStyle.fillColor="red";
    circleStyle.fillOpacity=0.05;
    
    circleFeature = new OpenLayers.Feature.Vector(
						  OpenLayers.Geometry.Polygon.createRegularPolygon(origin,
												   20,
												   60,
												   0),
						  null,
						  circleStyle );
    vectors.addFeatures( [circleFeature] ); 
}

// Centre and zoom the map such that the whole branch area is visible.
function brancharea () {
    var bounds = new OpenLayers.Bounds(-1.63,51.6566,-1.035,51.85).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
    if (!map.getCenter()) {
	map.zoomToExtent(bounds);
    }
}

