			var map,vectorLayer,tObj;
			var bng = new OpenLayers.Projection('EPSG:27700');
			var latlon = new OpenLayers.Projection('EPSG:4326');
			var basemapLegends = [	{img:'images/rect-ffe0b6.png', title: '7.5T Weight Restriction Area', zoom:[1,2,3,4,5,6]},
								{img:'images/line-7676f3.png', title: 'CPZ Boundary', zoom:[0,1,2,3,4,5,6]}
							];
			
			var fb_msg = "Thank you for using the London Borough of Hackney`s Freight Mapping Solution. We appreciate the time you have taken to submit feedback on this service.\n\nFeedback about this service is very important to us and we value all the comments provided. The feedback received will be reviewed and if appropriate we will action your comments accordingly. All agreed feedback intends be represented on the map within x working days, however please note that not all feedback submitted will be deemed appropriate for inclusion on the map. Please also be aware that we will not be responding directly to all comments received.\n\nThe London Borough of Hackney\nhttp://www.hackney.gov.uk";
			
			var feedback_tool_options = {email_body: fb_msg};
			var h_disclaimer = 'http://www.hackney.gov.uk/f-ict-dataprotection-826.htm';
			
			function init(){
				
				var extent = new OpenLayers.Bounds(510000.0,150012.5,600000.0,225012.5);
				var mapControls = [
						new OpenLayers.Control.Navigation(),
                        new OpenLayers.Control.PanZoomBar2(),
                        new OpenLayers.Control.Pdf({div: document.getElementById('pdfDiv'), linkText: 'Printable Map'}),
                        new OpenLayers.Control.ScaleLine({maxWidth:150,bottomInUnits:''}),
                        new OpenLayers.Control.KeyboardDefaults(),
						new OpenLayers.Control.MapFeedback({disclaimerLink:h_disclaimer,commentLimit:150, confirmationEmail: true, confirmationEmailOptions: feedback_tool_options}),
						new OpenLayers.Control.Feedback({disclaimerLink:h_disclaimer,commentLimit:150, confirmationEmail: true, confirmationEmailOptions: feedback_tool_options}),
						new OpenLayers.Control.PieLayerSwitcher({
							div: document.getElementById('layerswitcher'),
							updateLegendDivVisibility: false,
							basemapLegends: basemapLegends,
							legendDiv: document.getElementById('legend'),
							//legendDimension: {height:14,width:18},
							type: 'image',
							noBorder: true,
							noTitle: true,
							allowFeatureToggle: true,
							showVisibleFeaturesInfo: true,
							categories: _layer_cats	})//,
						//new OpenLayers.Control.Search({div:document.getElementById('search')})
						];

				map = new OpenLayers.Map('map', {
                    controls: mapControls,
					maxExtent: extent,
					restrictedExtent: extent
                });
				
				var cSearch = new OpenLayers.Control.Search({
					div:document.getElementById('search'),
					searchTitle:'Search Hackney', 
					searchLabel: 'Enter postcode or street'
				});
				var cNearest = new OpenLayers.Control.PieNearest({
					div: document.getElementById('nearest'),
					markerOffset: {x:-13, y:-35 },
					backButtonText: 'Back',
					backButtonDisplay: 'bottom',
					selectedResultsDiv: document.getElementById('div_selected_destinations'),
					//legendDimension: {height:18,width:19},
					categories: _layer_cats,
					searchControl: cSearch
				});
				
				map.addControl(cSearch);
				map.addControl(cNearest);

				
				// URL definitions
				var mapDir = "/ms/mapfiles/";
				var mapFile = mapDir + "wms-9-hackney.map";
				var wmsURL = getWMSURL() + "?map=" + mapFile;
				
				// Basemap WMS layer
				var layerNames = 'basemap';

				var attribution_navteq = 'Content &copy;thePIEguide 2010 and map data &copy;Navteq 2009';
				var attribution_os = 'Content &copy;thePIEguide 2010 and map data &copy;Crown Copyright. All rights reserved. License number 100019635 2008';
				var wmsLayer = new OpenLayers.Layer.WMS("Street", wmsURL,
					{layers: layerNames, format: 'png'},
					{//4582360
						scales: [35410,14150,4230,1400,1370,700,340], 
						buffer: 0, 
						projection:'EPSG:27700', 
						units:'m',
						attributionByZoom: [attribution_navteq,attribution_navteq,attribution_navteq,attribution_navteq,attribution_os,attribution_os,attribution_os],
						transitionEffect: 'resize'
					}	
				);
				
				var attribution = new OpenLayers.Control.Attribution()
				map.addControl(attribution);
								
                map.addLayers([wmsLayer]);
				map.zoomToMaxExtent();
				
				// ---- Layers
				var layers = [];
	
				for (var i = 0, l = _layer_feats.length; i < l; i++) {
					var layer_def = _layer_feats[i];
					//console.log(layer_def['typeId']);
					layer_def['ms_url'] = getWFSURL() + "?map=" + layer_def['ms_url'];
					var layer = new OpenLayers.Layer.WFS(layer_def['name'], layer_def['ms_url'], layer_def['options'], layer_def['options2'], layer_def['featuretypes'], layer_def['typeId']);
					layers.push(layer);
				}
			
				map.addLayers(layers);	
				var select = new OpenLayers.Control.SelectTemplateFeature({'layers':layers});
				map.addControl(select);
				select.activate();
				
				
			    var miniMapLayer = new OpenLayers.Layer.Image('Mini Map',
    		    	'images/minimap.png',
        			new OpenLayers.Bounds(529855, 181560, 538461, 188543),
		        	new OpenLayers.Size(172, 140),
	    	    	{
        			    numZoomLevels: 1
		    	    }
			    );
				map.addControl(new OpenLayers.Control.PieOverviewMap({
			        div: document.getElementById("minimap_div"),
			        layers: [miniMapLayer],
			        units: 'm', 
			        projection: "EPSG:27700",
			        size: new OpenLayers.Size(172, 140),
			        minRectSize: 2,
			        maxRatio: 400
			    }));
				
				/*
				var ovOptions = {
					//minRatio: 16,				// Default is 8
					//maxRatio: 5000,				// Default is 32
					//layers: [wms_layer1,wms_layer2],
					div: document.getElementById('minimap_div'),
					autoPan: true,
					size: new OpenLayers.Size(200,130),
					startMinimized: false,
					
					mapOptions: {
						//numZoomLevels: 1,
						scales: [65410,35410,14150,4230,1400,1370,700],
						projection: 'EPSG:27700',
						units: 'm'
						//tileSize = new OpenLayers.Size(100,100),
					}
				};
				
				
				var ovControl = new OpenLayers.Control.PieOverviewMap(ovOptions);
				map.addControl(ovControl);
				*/
				
				//var permalink = new OpenLayers.Control.Permalink({div:document.getElementById('permalinkDiv')}); 
				//map.addControl(permalink);		
				map.setCenter(new OpenLayers.LonLat(533821, 184874));
				/*
				if (!map.getCenter()) {
					map.setCenter(new OpenLayers.LonLat(545000, 184256));
					//map.zoomToMaxExtent();
				}*/
				
				// Show nearest box on start
				toggleTabs('nearest');
				
				// CPZ colors legend
				buildMiniLegend();
			}
		
	

function log(obj) {
	if (typeof(console) != 'undefined')
		console.log(obj);
}



function show_permalink() {
	var permalink = map.getControlsByClass('OpenLayers.Control.Permalink')[0];
	if (!permalink)
		return true;
		
	var href = permalink.base;
    if (href.indexOf('?') != -1) {
        href = href.substring( 0, href.indexOf('?') );
    }

    href += '?' + OpenLayers.Util.getParameterString(permalink.createParams());
	
	
    jPrompt('Please copy the link below',href);
}

/** pans map to given direction */
function pan(direction) {
	
	var panFactor = 70;
	if (direction == 'top')
		map.pan(0,0-panFactor);
	else if (direction == 'bottom')
		map.pan(0,panFactor);
	else if (direction == 'left')
		map.pan(0-panFactor,0);
	else if (direction == 'right')
		map.pan(panFactor,0);
		 
}		
function toggleTabs(tab) {

	if (tab=='nearest') {
		var div = document.getElementById('legend');
		div.style.display = 'none';
		div = document.getElementById('nearest');
		div.style.display = 'block';
		var btnLegend = document.getElementById('tabLegend');
		var btnNearest = document.getElementById('tabNearest');
		btnNearest.style.background = "url('images/btn-nearest-on.png') no-repeat";
		btnLegend.style.background = "url('images/btn-legend-off.png') no-repeat";
		
	}
	else if (tab=='legend') {
		var div = document.getElementById('nearest');
		div.style.display = 'none';
		div = document.getElementById('legend');
		div.style.display = 'block';
		var btnLegend = document.getElementById('tabLegend');
		var btnNearest = document.getElementById('tabNearest');
		btnLegend.style.background = "url('images/btn-legend-on.png') no-repeat";
		btnNearest.style.background = "url('images/btn-nearest-off.png') no-repeat";
	}
}


function buildMiniLegend() {
    var miniMap = document.getElementById('minimap_div');
    var div = document.getElementById('miniLegend');
    
    if (div != null) {
        div.className = 'miniLegend';
        //div.style.display = 'block';
        var loc = jQuery(miniMap).offset();
        div.style.top = (loc.top-(jQuery(div).height()-jQuery(miniMap).height()))+"px";
        div.style.left = (jQuery(miniMap).width() + loc.left) +"px";
        div.style.zIndex = 10000;
        var img = document.createElement('img');
        img.src = 'images/hackney-cpz-codes.png';
        div.appendChild(img);
        OpenLayers.Event.observe(miniMap, 'mouseover', function() {
            div.style.display = 'block';
        });
        OpenLayers.Event.observe(miniMap, 'mouseout', function() {
            div.style.display = 'none';
        });
    }
}


/**
 * @returns URL in address bar without page or file name with trailing '/'
 */
function getBaseURL() {
	var url = document.URL;
	// Remove page name if exists in url
	url = url.substring(0, (url.lastIndexOf("/") + 1));
	// check trailing '/'
	url = (url.charAt(url.length - 1) == '/') ? url : url + '/';
	return url;
}

