var currentMarker;
var currentPropCountLabel;
var currentPolygon;
var currentLabels;
var currentPolygons;
var currentRoutes;
var currentPorts;

var contextmenu;
var clickedPixel;

var directionMarkers;

var routeIndex = 0;
var currentRoute;
var routeStart;
var routeEnd;

var mapSquareZoomLevel = 8;

var wayPoints = [25];
var wayPointIcons = [26];
var wayPointsProcessed;

wayPointIcons[0] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenA.png";
wayPointIcons[1] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenB.png";
wayPointIcons[2] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenC.png";
wayPointIcons[3] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenD.png";
wayPointIcons[4] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenE.png";
wayPointIcons[5] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenF.png";
wayPointIcons[6] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenG.png";
wayPointIcons[7] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenH.png";
wayPointIcons[8] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenI.png";
wayPointIcons[9] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenJ.png";
wayPointIcons[10] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenK.png";
wayPointIcons[11] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenL.png";
wayPointIcons[12] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenM.png";
wayPointIcons[13] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenN.png";
wayPointIcons[14] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenO.png";
wayPointIcons[15] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenP.png";
wayPointIcons[16] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenQ.png";
wayPointIcons[17] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenR.png";
wayPointIcons[18] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenS.png";
wayPointIcons[19] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenT.png";
wayPointIcons[20] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenU.png";
wayPointIcons[21] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenV.png";
wayPointIcons[22] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenW.png";
wayPointIcons[23] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenX.png";
wayPointIcons[24] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenY.png";
wayPointIcons[25] = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker_greenZ.png";

var locLatMin = 35.981562;
var locLatMax = 50.642902;
var locLngMin = -9.51416;
var locLngMax = 9.002523;

function loadOwnersGMap(adcode)
{
	if (GBrowserIsCompatible())
	{
		var map = document.getElementById("map");
		gmap = new GMap2(map);

		gmap.addControl( new GSmallMapControl() );
		gmap.addControl( new GMapTypeControl()) ;
		gmap.addControl( new GScaleControl()) ;
		gmap.addControl( new GOverviewMapControl(new GSize(100,100)) );
		gmap.setCenter( new GLatLng(46.93526088057719,2.08740234375), 6);
		gmap.enableScrollWheelZoom();
		gmap.enableDoubleClickZoom();

		var minMapScale = 0;
		var maxMapScale = 7;

		var mapTypes = gmap.getMapTypes();

		for (var i=0; i<mapTypes.length; i++) {
			mapTypes[i].getMinimumResolution = function() {return minMapScale;}
			mapTypes[i].getMaximumResolution = function() {return maxMapScale;}
		}

		mapSquareZoomLevel = 7;

		currentLabels = new Array();
		currentPolygons = new Array();
		
		GEvent.addListener(gmap, "click", function(overlay, point) {
			if(overlay == null)
			{
				searchMapOwner(point);			
			}
		});

		GDownloadUrl('GoogleMapSearchSquare.cfm?adcode=' + adcode, function(data) {parseJsonSearchSquares(data);});

	}
}

function loadAdGMap(adcode)
{
	if (GBrowserIsCompatible()) 
	{
		var map = document.getElementById("map");
		gmap = new GMap2(map);
		
		gmap.addControl( new GSmallMapControl() );
		gmap.addControl( new GMapTypeControl()) ;
		gmap.addControl( new GScaleControl()) ;
		gmap.addControl( new GOverviewMapControl(new GSize(100,100)) );
		gmap.setCenter( new GLatLng(46.93526088057719,2.08740234375), 6);
		gmap.enableScrollWheelZoom();
		gmap.enableDoubleClickZoom();
		
		var minMapScale = 5; 
		var maxMapScale = 8; 
		
		var mapTypes = gmap.getMapTypes(); 
		
		for (var i=0; i<mapTypes.length; i++) { 
			mapTypes[i].getMinimumResolution = function() {return minMapScale;} 
			mapTypes[i].getMaximumResolution = function() {return maxMapScale;} 
		}
		
		mapSquareZoomLevel = 7;
		
		currentLabels = new Array();
		currentPolygons = new Array();
		currentRoutes = new Array();

		GDownloadUrl('GoogleMapSearchSquare.cfm?adcode=' + adcode, function(data) {parseJsonSearchSquares(data);});
	}
}

function loadSearchGMap () 
{
	if (GBrowserIsCompatible()) 
	{
		var map = document.getElementById("map");
		gmap = new GMap2(map);
		
		gmap.addControl( new GLargeMapControl() );
		gmap.addControl( new GMapTypeControl()) ;
		gmap.addControl( new GScaleControl()) ;
		gmap.addControl( new GOverviewMapControl(new GSize(100,100)) );
		gmap.setCenter( new GLatLng(46.93526088057719,2.08740234375), 6);
		gmap.enableScrollWheelZoom();
		gmap.enableDoubleClickZoom();
		
		var minMapScale = 6;
		var maxMapScale = 9;

		var mapTypes = gmap.getMapTypes();

		for (var i=0; i<mapTypes.length; i++) {
			mapTypes[i].getMinimumResolution = function() {return minMapScale;}
			mapTypes[i].getMaximumResolution = function() {return maxMapScale;}
		}
		
		var mgrOptions = { borderPadding: 50, maxZoom: 15, trackMarkers: false };
		
		mgr = new MarkerManager(gmap);
		mgrMarkers[1] = new MarkerManager(gmap);
		
		geocoder = new GClientGeocoder();
		localSearch = new GlocalSearch();

		/* GEvent.addListener(gmap, "moveend", function() {
			processMove();
		}); */
		
		GEvent.addListener(gmap, "click", function(overlay, point) {
			if(overlay == null)
			{
				searchMap(point);			
			}
		});

		/* GDownloadUrl("GoogleMapMarkers.cfm?MarkerType=1", function(data) {
			parseJsonMarkers(data, 1);
		}); */
		
		/* GDownloadUrl("GoogleMapBNB.cfm", function(data) {
			parseJsonMarkers(data, 1);
		}); */

		currentLabels = new Array();
		currentPolygons = new Array();
		currentRoutes = new Array();
		
		loadFerryRoutes(1);
	}
}

function loadRouteGMap () 
{
	if (GBrowserIsCompatible()) 
	{
		var map = document.getElementById("map");
		gmap = new GMap2(map);
		
		gmap.addControl( new GLargeMapControl() );
		gmap.addControl( new GMapTypeControl()) ;
		gmap.addControl( new GScaleControl()) ;
		gmap.addControl( new GOverviewMapControl(new GSize(100,100)) );
		gmap.setCenter( new GLatLng(46.93526088057719,2.08740234375), 6);
		gmap.enableScrollWheelZoom();
		gmap.enableDoubleClickZoom();
		
		var minMapScale = 6;
		var maxMapScale = 11;

		var mapTypes = gmap.getMapTypes();

		for (var i=0; i<mapTypes.length; i++) {
			mapTypes[i].getMinimumResolution = function() {return minMapScale;}
			mapTypes[i].getMaximumResolution = function() {return maxMapScale;}
		}
		
		var mgrOptions = { borderPadding: 50, maxZoom: 15, trackMarkers: false };
		
		mgr = new MarkerManager(gmap);
		mgrMarkers[1] = new MarkerManager(gmap);
		
		geocoder = new GClientGeocoder();
		localSearch = new GlocalSearch();

/*		GEvent.addListener(gmap, "moveend", function() {
			processMove();
		});
		
		GEvent.addListener(gmap, "click", function(overlay, point) {
			if(overlay == null)
			{
 				addWayPoint(point);			
			}
		}); */
				
		GDownloadUrl("GoogleMapBNB.cfm", function(data) {
			parseJsonMarkers(data, 1);
		});
		
		currentLabels = new Array();
		currentPolygons = new Array();
		currentRoutes = new Array();
		
		loadFerryRoutes(6);
		
		GEvent.addListener(gmap, "zoomend", function(oldZoomLevel, newZoomLevel) {
			loadFerryRoutes(newZoomLevel);
		});
		
		contextmenu = document.createElement("div");
		contextmenu.style.visibility="hidden";
		contextmenu.style.background="#ffffff";
		contextmenu.style.border="1px solid #8888FF";
		contextmenu.style.padding="1px";
		contextmenu.innerHTML = '<a href="javascript:addWayPoint()" class="context">&nbsp;&nbsp;Add WayPoint&nbsp;&nbsp;<\/a><br/>'
			+ '<a href="javascript:cancelContextMenu()" class="context">&nbsp;&nbsp;Close&nbsp;&nbsp;<\/a>';
		gmap.getContainer().appendChild(contextmenu);
		
		GEvent.addListener(gmap,"singlerightclick",function(pixel,tile) {
			clickedPixel = pixel;
			
			var x=pixel.x;
			var y=pixel.y;
			if (x > gmap.getSize().width - 120) { x = gmap.getSize().width - 120 }
			if (y > gmap.getSize().height - 100) { y = gmap.getSize().height - 100 }
			var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));  
			pos.apply(contextmenu);
			contextmenu.style.visibility = "visible";
		});
	}
}

function cancelContextMenu()
{
	if(contextmenu)
		contextmenu.style.visibility = "hidden";	
}

function loadOwnersBnBGMap () 
{
	if (GBrowserIsCompatible()) 
	{
		var map = document.getElementById("map");
		gmap = new GMap2(map);

		gmap.addControl( new GLargeMapControl() );
		gmap.addControl( new GMapTypeControl()) ;
		gmap.setCenter( new GLatLng(46.93526088057719,2.08740234375), 6);
		gmap.enableScrollWheelZoom();
		gmap.enableDoubleClickZoom();

		var point = new GLatLng(46.93526088057719,2.08740234375);

		marker = new GMarker(point, {draggable: true});        
		gmap.addOverlay(marker);

		showMarkerPos()

		geocoder = new GClientGeocoder();
		localSearch = new GlocalSearch();

		GEvent.addListener(marker, "dragend", function() {
			showMarkerPos();
		}); 
	} 
}

function showPleaseWait()
{
	var p = document.getElementById('pleaseWaitMessage');
	
	p.style.visibility = 'visible';
	p.style.display = '';
}

function hidePleaseWait()
{
	var p = document.getElementById('pleaseWaitMessage');
	
	p.style.visibility = 'hidden';
	p.style.display = 'none';
}

function addWayPoint()
{
	var point = gmap.fromContainerPixelToLatLng(clickedPixel)

/*	if(confirm('Add a waypoint here?'))
	{ */
	var lastEntry = 0;
	
	for(i = 0; i < wayPoints.length; i++)
	{
		if(wayPoints[i] != "" && wayPoints[i] != null)
		{
			lastEntry = i;
		}
	} 
	
	var lastPos = wayPoints[lastEntry];
	
	wayPoints[lastEntry] = point.y + ',' + point.x;
	wayPoints[lastEntry + 1] = lastPos;
			
	showRoute(false, true);
//	}
	
	cancelContextMenu();
}

function setOwnerMapPosition()
{
	document.getElementById('frmMap').submit();
}

function searchMapOwner(point)
{
	clearOverlays(true);
	
	var url = "GoogleMapSearchSquare.cfm?owner=1&lat=" + point.y + "&lon=" + point.x;
	
	document.getElementById('ownerMapLat').value = point.y;
	document.getElementById('ownerMapLon').value = point.x;

	GDownloadUrl(url, function(data) {
		parseJsonSearchSquares(data);
	});
}

function searchMap(point)
{
	hideLocationMessage();
	showPleaseWait();
	
	clearOverlays(true);
	
//	alert(point.y + ":" + point.x);
	
	var url = "GoogleMapSearchSquare.cfm?lat=" + point.y + "&lon=" + point.x;

	GDownloadUrl(url, function(data) {
		parseJsonSearchSquares(data);
	});
}

function clearRouteOverlays()
{	
	if(currentRoutes)
	{
		for (var i = 0; i < currentRoutes.length; i++) 
		{
			if(currentRoutes[i])
			{
				gmap.removeOverlay(currentRoutes[i]);
			}
		}
	}

	if(currentPorts)
	{
		for (var i = 0; i < currentPorts.length; i++) 
		{
			if(currentPorts[i])
			{
				gmap.removeOverlay(currentPorts[i]);
			}
		}
	}
	
	currentPorts = new Array();
	currentRoutes = new Array();
	routeIndex = 0;
}

function loadFerryRoutes(zoomLevel)
{
	clearRouteOverlays();

	currentPorts[0] = new ELabel(new GLatLng(48.720958,-3.961945), "Roscoff", "mapPortLabel");
	gmap.addOverlay(currentPorts[0]);

	currentPorts[1] = new ELabel(new GLatLng(49.649937,-1.615677), "Cherbourg", "mapPortLabel");
	gmap.addOverlay(currentPorts[1]);

	currentPorts[2] = new ELabel(new GLatLng(49.286418,-0.247536), "Caen", "mapPortLabel");
	gmap.addOverlay(currentPorts[2]);

	currentPorts[3] = new ELabel(new GLatLng(48.639533,-2.032471), "St.&nbsp;Malo", "mapPortLabel");
	gmap.addOverlay(currentPorts[3]);

	currentPorts[4] = new ELabel(new GLatLng(43.461492,-3.782043), "Santander", "mapPortLabel", new GSize(-55, 18));
	gmap.addOverlay(currentPorts[4]);

	currentPorts[5] = new ELabel(new GLatLng(51.790016,-8.267899), "Cork", "mapPortLabel");
	gmap.addOverlay(currentPorts[5]);

	currentPorts[6] = new ELabel(new GLatLng(50.362196,-4.154205), "Plymouth", "mapPortLabel");
	gmap.addOverlay(currentPorts[6]);

	currentPorts[7] = new ELabel(new GLatLng(50.681319,-1.947327), "Poole", "mapPortLabel");
	gmap.addOverlay(currentPorts[7]);

	currentPorts[8] = new ELabel(new GLatLng(50.783452,-1.107559), "Portsmouth", "mapPortLabel");
	gmap.addOverlay(currentPorts[8]);

	currentPorts[9] = new ELabel(new GLatLng(43.331801,-3.029459), "Bilbao", "mapPortLabel", new GSize(-30,18));
	gmap.addOverlay(currentPorts[9]);

	// SW, NE

	// Plymouth - Roscoff	
	var boundaries = new GLatLngBounds(new GLatLng(48.720108,-4.231029), new GLatLng(50.364228,-3.975146));
	if(zoomLevel < 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/alt_route_plymouth_roscoff.png", boundaries);
	}
	else if(zoomLevel == 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/alt_route_plymouth_roscoff_2.png", boundaries);
	}
	else if(zoomLevel == 9)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/alt_route_plymouth_roscoff_3.png", boundaries);
	}
	else if(zoomLevel == 10)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/alt_route_plymouth_roscoff_4.png", boundaries);
	}
	else if(zoomLevel == 11)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/alt_route_plymouth_roscoff_5.png", boundaries);
	}
	gmap.addOverlay(currentRoutes[routeIndex]);
	routeIndex++;
	
	// Poole - Cherbourg	
	var boundaries = new GLatLngBounds(new GLatLng(49.64887,-1.950000), new GLatLng(50.679579,-1.61499));
	if(zoomLevel < 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_poole_cherbourg.png", boundaries);
	}
	else if(zoomLevel == 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_poole_cherbourg_2.png", boundaries);
	}
	else if(zoomLevel == 9)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_poole_cherbourg_3.png", boundaries);
	}
	else if(zoomLevel == 10)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_poole_cherbourg_4.png", boundaries);
	}
	else if(zoomLevel == 11)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_poole_cherbourg_5.png", boundaries);
	}
	gmap.addOverlay(currentRoutes[routeIndex]);
	routeIndex++;
	
	// Portsmouth - Caen
	var boundaries = new GLatLngBounds(new GLatLng(49.289396,-1.107903), new GLatLng(50.785362,-0.236893));
	if(zoomLevel < 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_caen.png", boundaries);
	}
	else if(zoomLevel == 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_caen_2.png", boundaries);
	}
	else if(zoomLevel == 9)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_caen_3.png", boundaries);
	}
	else if(zoomLevel == 10)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_caen_4.png", boundaries);
	}
	else if(zoomLevel == 11)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_caen_5.png", boundaries);
	}
	gmap.addOverlay(currentRoutes[routeIndex]);
	routeIndex++;
	
	// Portsmouth - St Malo
	var boundaries = new GLatLngBounds(new GLatLng(48.642936,-2.422485), new GLatLng(50.776896,-1.011429));
	if(zoomLevel < 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_stmalo.png", boundaries);
	}
	else if(zoomLevel == 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_stmalo_2.png", boundaries);
	}
	else if(zoomLevel == 9)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_stmalo_3.png", boundaries);
	}
	else if(zoomLevel == 10)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_stmalo_4.png", boundaries);
	}
	else if(zoomLevel == 11)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_stmalo_5.png", boundaries);
	}
	gmap.addOverlay(currentRoutes[routeIndex]);
	routeIndex++;
	
	// Portsmouth - Cherbourg
	var boundaries = new GLatLngBounds(new GLatLng(49.64887,-1.61499), new GLatLng(50.776896,-0.86700));
	if(zoomLevel < 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_cherbourg.png", boundaries);
	}
	else if(zoomLevel == 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_cherbourg_2.png", boundaries);
	}
	else if(zoomLevel == 9)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_cherbourg_3.png", boundaries);
	}
	else if(zoomLevel == 10)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_cherbourg_4.png", boundaries);
	}
	else if(zoomLevel == 11)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_cherbourg_5.png", boundaries);
	}
	gmap.addOverlay(currentRoutes[routeIndex]);
	routeIndex++;
	
	// Roscoff - Cork
	var boundaries = new GLatLngBounds(new GLatLng(48.720108,-8.267212), new GLatLng(51.791715,-3.975146));
	if(zoomLevel < 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_roscoff_cork.png", boundaries);
	}
	else if(zoomLevel == 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_roscoff_cork_2.png", boundaries);
	}
	else if(zoomLevel == 9)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_roscoff_cork_3.png", boundaries);
	}
	else if(zoomLevel == 10)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_roscoff_cork_4.png", boundaries);
	}
	else if(zoomLevel == 11)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_roscoff_cork_5.png", boundaries);
	}
	gmap.addOverlay(currentRoutes[routeIndex]);
	routeIndex++;
	
	// Plymouth - Santander
	var boundaries = new GLatLngBounds(new GLatLng(43.469964,-6.04248), new GLatLng(50.364228,-3.812256));
	if(zoomLevel < 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_plymouth_santander.png", boundaries);
	}
	else if(zoomLevel == 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_plymouth_santander_2.png", boundaries);
	}
	else if(zoomLevel == 9)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_plymouth_santander_3.png", boundaries);
	}
	else if(zoomLevel == 10)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_plymouth_santander_4.png", boundaries);
	}
	else if(zoomLevel == 11)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_plymouth_santander_5.png", boundaries);
	}
	gmap.addOverlay(currentRoutes[routeIndex]);
	routeIndex++;
	
	// Portsmouth - Santander
//	var boundaries = new GLatLngBounds(new GLatLng(43.469964,-5.590), new GLatLng(50.776896,-0.922852));
	var boundaries = new GLatLngBounds(new GLatLng(43.469964,-5.7), new GLatLng(50.776896,-0.922852));
	if(zoomLevel < 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_santander.png", boundaries);
	}
	else if(zoomLevel == 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_santander_2.png", boundaries);
	}
	else if(zoomLevel == 9)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_santander_3.png", boundaries);
	}
	else if(zoomLevel == 10)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_santander_4.png", boundaries);
	}
	else if(zoomLevel == 11)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_santander_5.png", boundaries);
	}
	gmap.addOverlay(currentRoutes[routeIndex]);
	routeIndex++;

	// Portsmouth - Bilbao
//	var boundaries = new GLatLngBounds(new GLatLng(43.469964,-5.590), new GLatLng(50.776896,-0.922852));
	var boundaries = new GLatLngBounds(new GLatLng(43.35,-5.3), new GLatLng(50.776896,-0.922852));
	if(zoomLevel < 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_bilbao.png", boundaries);
	}
	else if(zoomLevel == 8)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_bilbao_2.png", boundaries);
	}
	else if(zoomLevel == 9)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_bilbao_3.png", boundaries);
	}
	else if(zoomLevel == 10)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_bilbao_4.png", boundaries);
	}
	else if(zoomLevel == 11)
	{
		currentRoutes[routeIndex] = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_bilbao_5.png", boundaries);
	}
	gmap.addOverlay(currentRoutes[routeIndex]);
	routeIndex++;
}

function addBilbao(){
	var latLng1 = new GLatLng($('#txtLat1').val(), $('#txtLng1').val());
	var latLng2 = new GLatLng($('#txtLat2').val(), $('#txtLng2').val());
	
	// alert(latLng1);
	
	var boundaries = new GLatLngBounds(latLng1, latLng2);

	var bbOverlay = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_bilbao.png", boundaries);
	gmap.addOverlay(bbOverlay);
}

function addSantander(){
	var latLng1 = new GLatLng($('#txtLat3').val(), $('#txtLng3').val());
	var latLng2 = new GLatLng($('#txtLat4').val(), $('#txtLng4').val());
	var boundaries = new GLatLngBounds(latLng1, latLng2);

	var bbOverlay = new GGroundOverlay(strBaseHref + "images/gmaps/route_portsmouth_santander.png", boundaries);
	gmap.addOverlay(bbOverlay);


}

function addPlySantander(){
	var latLng1 = new GLatLng($('#txtLat5').val(), $('#txtLng5').val());
	var latLng2 = new GLatLng($('#txtLat6').val(), $('#txtLng6').val());
	var boundaries = new GLatLngBounds(latLng1, latLng2);

	var bbOverlay = new GGroundOverlay(strBaseHref + "images/gmaps/route_plymouth_santander.png", boundaries);
	gmap.addOverlay(bbOverlay);
}

function clearRoutes(){
	// <input type="button" id="btnClearRoutes" value="Clear" onclick="clearRoutes()">
	gmap.clearOverlays();
}

function oldFerryRoutes()
{
	// Portsmouth - Caen
	polyline = new GPolyline([
	new GLatLng(49.285947,-0.247192)
	,new GLatLng(49.439557,-0.230713)
	,new GLatLng(49.660517,-0.219727)
	,new GLatLng(49.961822,-0.274658)
	,new GLatLng(50.141706,-0.401001)
	,new GLatLng(50.373496,-0.620728)
	,new GLatLng(50.551835,-0.823975)
	,new GLatLng(50.687758,-0.977783)
	,new GLatLng(50.781629,-1.106873)
	,new GLatLng(50.795736,-1.114082)
	,new GLatLng(50.805067,-1.114769)
	,new GLatLng(50.810708,-1.111336)
	,new GLatLng(50.811358,-1.104126)
	,new GLatLng(50.809623,-1.094856)
	,new GLatLng(50.810491,-1.09108)
	], "#ff0000", 3);
	gmap.addOverlay(polyline);

	// Portsmouth - Cherbourg
	polyline = new GPolyline([
	new GLatLng(49.645069,-1.615334)
	,new GLatLng(49.660406,-1.611385)
	,new GLatLng(49.767074,-1.450195)
	,new GLatLng(49.84861,-1.224976)
	,new GLatLng(49.986552,-1.104126)
	,new GLatLng(50.099441,-1.032715)
	,new GLatLng(50.25423,-0.983276)
	,new GLatLng(50.405017,-0.961304)
	,new GLatLng(50.527397,-0.961304)
	,new GLatLng(50.64946,-0.999756)
	,new GLatLng(50.785102,-1.106873)
	,new GLatLng(50.79476,-1.112881)
	,new GLatLng(50.803874,-1.113911)
	,new GLatLng(50.80984,-1.112194)
	,new GLatLng(50.81125,-1.105843)
	,new GLatLng(50.810274,-1.098118)
	,new GLatLng(50.810165,-1.09211)
	], "#ff0000", 3);
	gmap.addOverlay(polyline);

	// Portsmouth - St Malo
	polyline = new GPolyline([
	new GLatLng(48.642097,-2.026463)
	,new GLatLng(48.641757,-2.040367)
	,new GLatLng(48.69096,-2.131348)
	,new GLatLng(48.752567,-2.202759)
	,new GLatLng(48.832182,-2.285156)
	,new GLatLng(48.91528,-2.351074)
	,new GLatLng(49.019859,-2.406006)
	,new GLatLng(49.117029,-2.406006)
	,new GLatLng(49.188884,-2.389526)
	,new GLatLng(49.278557,-2.356567)
	,new GLatLng(49.378797,-2.296143)
	,new GLatLng(49.464554,-2.213745)
	,new GLatLng(49.582226,-2.136841)
	,new GLatLng(49.685401,-2.06543)
	,new GLatLng(49.806087,-2.015991)
	,new GLatLng(49.891096,-1.878662)
	,new GLatLng(49.972422,-1.653442)
	,new GLatLng(50.021858,-1.444702)
	,new GLatLng(50.145226,-1.274414)
	,new GLatLng(50.25423,-1.164551)
	,new GLatLng(50.387508,-1.049194)
	,new GLatLng(50.502946,-1.016235)
	,new GLatLng(50.57626,-0.994263)
	,new GLatLng(50.608517,-0.994263)
	,new GLatLng(50.655555,-0.997009)
	,new GLatLng(50.690368,-1.029968)
	,new GLatLng(50.727763,-1.061554)
	,new GLatLng(50.766865,-1.09314)
	,new GLatLng(50.777938,-1.101723)
	,new GLatLng(50.789226,-1.110306)
	,new GLatLng(50.798557,-1.114426)
	,new GLatLng(50.808104,-1.114426)
	,new GLatLng(50.811575,-1.107903)
	,new GLatLng(50.810057,-1.097946)
	,new GLatLng(50.810057,-1.091766)
	], "#ff0000", 3);
	gmap.addOverlay(polyline);

	// Poole - Cherbourg
	polyline = new GPolyline([
	new GLatLng(49.646069,-1.61499)
	,new GLatLng(49.660517,-1.613274)
	,new GLatLng(49.731581,-1.686401)
	,new GLatLng(49.83444,-1.763306)
	,new GLatLng(49.933544,-1.82373)
	,new GLatLng(50.028917,-1.856689)
	,new GLatLng(50.131143,-1.884155)
	,new GLatLng(50.233152,-1.895142)
	,new GLatLng(50.34546,-1.889648)
	,new GLatLng(50.457504,-1.867676)
	,new GLatLng(50.57626,-1.851196)
	,new GLatLng(50.632042,-1.851196)
	,new GLatLng(50.651202,-1.867676)
	,new GLatLng(50.672095,-1.910248)
	,new GLatLng(50.683408,-1.951447)
	,new GLatLng(50.685474,-1.954021)
	,new GLatLng(50.690151,-1.953678)
	,new GLatLng(50.695153,-1.955738)
	,new GLatLng(50.698632,-1.961746)
	,new GLatLng(50.699937,-1.969986)
	,new GLatLng(50.700372,-1.980457)
	,new GLatLng(50.699828,-1.987324)
	,new GLatLng(50.69972,-1.991787)
	,new GLatLng(50.702873,-1.992817)
	,new GLatLng(50.707004,-1.993332)
	], "#ff0000", 3);
	gmap.addOverlay(polyline);

	// Plymouth - Roscoff
/* 	polyline = new GPolyline([
new GLatLng(50.364214,-4.156013),
new GLatLng(49.9531935,-4.0280065),
new GLatLng(49.542173,-3.900000),
new GLatLng(48.720132,-3.961601)
	], "#ff0000", 3);
	gmap.addOverlay(polyline); */

	// Plymouth - Santander
	polyline = new GPolyline([
	new GLatLng(43.457903,-3.805733)
	,new GLatLng(43.455785,-3.796463)
	,new GLatLng(43.460395,-3.77861)
	,new GLatLng(43.464632,-3.761787)
	,new GLatLng(43.487801,-3.747025)
	,new GLatLng(43.540585,-3.746338)
	,new GLatLng(43.707594,-3.977051)
	,new GLatLng(43.945372,-4.328613)
	,new GLatLng(44.21371,-4.724121)
	,new GLatLng(44.559163,-5.229492)
	,new GLatLng(44.918139,-5.668945)
	,new GLatLng(45.336702,-5.95459)
	,new GLatLng(45.56791,-6.130371)
	,new GLatLng(46.027482,-6.306152)
	,new GLatLng(46.604167,-6.437988)
	,new GLatLng(47.085085,-6.525879)
	,new GLatLng(47.41322,-6.525879)
	,new GLatLng(47.754098,-6.416016)
	,new GLatLng(48.092757,-6.262207)
	,new GLatLng(48.400032,-6.152344)
	,new GLatLng(48.661943,-5.888672)
	,new GLatLng(48.980217,-5.559082)
	,new GLatLng(49.325122,-5.097656)
	,new GLatLng(49.696062,-4.724121)
	,new GLatLng(49.894634,-4.460449)
	,new GLatLng(50.134664,-4.240723)
	,new GLatLng(50.205033,-4.196777)
	,new GLatLng(50.26301,-4.172058)
	,new GLatLng(50.315654,-4.152832)
	,new GLatLng(50.331436,-4.147682)
	,new GLatLng(50.341955,-4.142189)
	,new GLatLng(50.351595,-4.146309)
	,new GLatLng(50.363642,-4.155235)
	], "#ff0000", 3);
	gmap.addOverlay(polyline);

	// Cork - Roscoff
	polyline = new GPolyline([
	new GLatLng(51.835778,-8.328323)
	,new GLatLng(51.843414,-8.304291)
	,new GLatLng(51.843838,-8.272018)
	,new GLatLng(51.776763,-8.266525)
	,new GLatLng(51.570241,-8.294678)
	,new GLatLng(51.158677,-8.22876)
	,new GLatLng(50.680797,-8.041992)
	,new GLatLng(50.169862,-7.723389)
	,new GLatLng(49.710273,-7.272949)
	,new GLatLng(49.346599,-6.70166)
	,new GLatLng(49.131408,-6.16333)
	,new GLatLng(48.958581,-5.471191)
	,new GLatLng(48.879167,-4.746094)
	,new GLatLng(48.79239,-4.042969)
	,new GLatLng(48.790581,-4.004517)
	,new GLatLng(48.774293,-3.930359)
	,new GLatLng(48.734455,-3.913879)
	,new GLatLng(48.721773,-3.935852)
	,new GLatLng(48.719961,-3.960915)
	], "#ff0000", 3);
	gmap.addOverlay(polyline);

	// Portsmouth - St malo
	polyline = new GPolyline([
	new GLatLng(50.810274,-1.092796)
	,new GLatLng(50.810274,-1.099663)
	,new GLatLng(50.811358,-1.106186)
	,new GLatLng(50.809189,-1.110649)
	,new GLatLng(50.805718,-1.114082)
	,new GLatLng(50.801161,-1.114426)
	,new GLatLng(50.795736,-1.113739)
	,new GLatLng(50.789443,-1.109962)
	,new GLatLng(50.729719,-1.06945)
	,new GLatLng(50.653161,-1.016579)
	,new GLatLng(50.555325,-1.032715)
	,new GLatLng(50.443513,-1.230469)
	,new GLatLng(50.331436,-1.625977)
	,new GLatLng(50.148746,-2.087402)
	,new GLatLng(49.95122,-2.680664)
	,new GLatLng(49.866317,-3.186035)
	,new GLatLng(49.639177,-3.713379)
	,new GLatLng(49.425267,-4.262695)
	,new GLatLng(49.21042,-4.724121)
	,new GLatLng(48.980217,-5.097656)
	,new GLatLng(48.69096,-5.29541)
	,new GLatLng(48.400032,-5.493164)
	,new GLatLng(48.034019,-5.449219)
	,new GLatLng(47.635784,-5.427246)
	,new GLatLng(47.368594,-5.20752)
	,new GLatLng(47.025206,-4.768066)
	,new GLatLng(46.739861,-4.438477)
	,new GLatLng(46.346928,-4.042969)
	,new GLatLng(45.243953,-3.647461)
	,new GLatLng(44.715514,-3.55957)
	,new GLatLng(44.229457,-3.493652)
	,new GLatLng(43.818675,-3.55957)
	,new GLatLng(43.628123,-3.625488)
	,new GLatLng(43.570442,-3.67218)
	,new GLatLng(43.500752,-3.738098)
	,new GLatLng(43.474847,-3.751831)
	,new GLatLng(43.462389,-3.762131)
	,new GLatLng(43.460894,-3.773804)
	,new GLatLng(43.459399,-3.785477)
	,new GLatLng(43.456906,-3.795776)
	,new GLatLng(43.457031,-3.799896)
	,new GLatLng(43.458402,-3.804874)
	], "#ff0000", 3);
	gmap.addOverlay(polyline);
}

function drawHFDMapSquare(x, y)
{
	/* 
	var latOffset = 0.28968;
	var lonOffset = 0.40130;
	
	var latShift = 0.012585;
	var lonShift = 0.0152675;
	
	var lat1 = 50.799452;
	var lon1 = -5.5924769; 
	*/
	
	var latOffset = 0.30;
	var lonOffset = 0.4865;
	
	var latShift = 0;
	var lonShift = 0.04;
	
	var lat1 = 50.799452;
	var lon1 = -5.5924769;
	
	lat1 -= (y * latOffset)
	lat1 -= (x * latShift);
	lon1 += (x * lonOffset) 
	lon1 -= (y * lonShift);
	
	var lat2 = lat1 - latShift;
	var lon2 = lon1 + lonOffset;
	
	var lat3 = lat1 - latShift - latOffset;
	var lon3 = lon1 + lonOffset - lonShift;
	
	var lat4 = lat1 - latOffset;
	var lon4 = lon1 - lonShift;
	
	gmap.setCenter( new GLatLng(lat1, lon1), 6);

	currentPolygon = new GPolygon([

		new GLatLng(lat1, lon1),
		
		new GLatLng(lat4, lon4),
		new GLatLng(lat3, lon3),
		new GLatLng(lat2, lon2),
		
		new GLatLng(lat1, lon1)

	], "#f33f00", 1, 1, "#ff0000", 0.2);

	gmap.addOverlay(currentPolygon);

	
	/* for(i = 0; i < 30; i++)
	{
		for(c = 0; c < 27; c++)
		{
			x = i;
			y = c;
			
			if((x == 0 && (y % 2 == 0 || y == 26)) || (y == 0 && x % 2 == 0) || (x == 29 && (y % 2 == 0 || y == 26)) || (y == 26 && x % 2 == 0))
			{

				var lat = 50.788;
				var lon = -5.5700;

				lat -= (y * latOffset);
				lon += (x * lonOffset);

				gmap.setCenter( new GLatLng(lat, lon), 6);

				currentPolygon = new GPolygon([    

					new GLatLng(lat, lon),    
					new GLatLng(lat - latOffset, lon),    
					new GLatLng(lat - latOffset, lon + lonOffset),    
					new GLatLng(lat, lon + lonOffset),    
					new GLatLng(lat, lon)  

				], "#f33f00", 1, 1, "#ff0000", 0.2);  

				gmap.addOverlay(currentPolygon);
				
			}
		}
	} */
	
	
	currentPropCountLabel = new ELabel(new GLatLng(lat1 - latOffset, lon1 + 0.01), "888", "propertyNumberStyle");
	// gmap.addOverlay(currentPropCountLabel);
}

function loadLineDraw () 
{
	if (GBrowserIsCompatible()) 
	{
		var map = document.getElementById("map");
		gmap = new GMap2(map);
		
		gmap.addControl( new GLargeMapControl() );
		gmap.addControl( new GMapTypeControl()) ;
		gmap.addControl( new GOverviewMapControl(new GSize(100,100)) );
		gmap.setCenter( new GLatLng(46.93526088057719,2.08740234375), 6);
		gmap.enableScrollWheelZoom();
		gmap.enableDoubleClickZoom();
		
		var mgrOptions = { borderPadding: 50, maxZoom: 15, trackMarkers: false };
		
		mgr = new MarkerManager(gmap);
		mgrMarkers[1] = new MarkerManager(gmap);
		
		geocoder = new GClientGeocoder();
		localSearch = new GlocalSearch();

		GEvent.addListener(gmap, "click", function(overlay, point) {
			addLinePoint(point);
		});

	} 
	else 
	{
		alert("Sorry, your browser cannot handle the true power of Google Maps");
	}
}

function addLinePoint(point)
{

	if(document.getElementById('mapPoints').value != '')
	{
		document.getElementById('mapPoints').value += ',';
	}
	
	document.getElementById('mapPoints').value += 'new GLatLng(' + point.toUrlValue() + ')\n';
	
	document.getElementById('mapPointsCode').value = 'var polyline = new GPolyline([\n' + document.getElementById('mapPoints').value + '], "#ff0000", 3);\ngmap.addOverlay(polyline);';
}

function clearPoints()
{
	document.getElementById('mapPoints').value = '';
	document.getElementById('mapPointsCode').value = '';
}

function savePostCodePost(postcode) {

    localSearch.setSearchCompleteCallback(null, 
        function() {
            if (localSearch.results[0]) {    
                var resultLat = localSearch.results[0].lat;
                var resultLng = localSearch.results[0].lng;
                	
                ajax.makeRequest('GET', basehref+'GoogleMapPostCodeSaver.cfm?lat='+resultLat+'&long='+resultLng+'&postcode='+postcode, onCodeSaveReponse);
            }else{
                // alert("Postcode not found!");
            }
    });  

	if(postcode.substring(postcode.length - 5).toLowerCase() == 'spain')
	{
		localSearch.execute(postcode);
	}
	else
	{
		localSearch.execute(postcode + ',France');
	}
}

function onCodeSaveReponse()
{
	
}

function usePointFromPostcode(postcode) 
{
	hideLocationMessage();
	showPleaseWait();
	clearOverlays(true);  
	
    	localSearch.setSearchCompleteCallback(null, 
		function() {

			if(localSearch.results.length > 0)
			{

				for (var i = 0; i < localSearch.results.length; i++) 
				{
					var resultLat = localSearch.results[i].lat;
					var resultLng = localSearch.results[i].lng;
					var url = "GoogleMapSearchSquare.cfm?lat=" + resultLat + "&lon=" + resultLng;
									
					GDownloadUrl(url, function(data) {
						parseJsonSearchSquares(data);
					});
				}

			}
			else
			{
				showLocationMessage();
				hidePleaseWait();
			}
	    });  

	if(postcode.substring(postcode.length - 5).toLowerCase() == 'spain')
	{
		localSearch.execute(postcode);
	}
	else
	{
		localSearch.execute(postcode + ',France');
	}
    
    return false;
}

function showLocationMessage()
{
	var m = document.getElementById('locationmessage');
	
	if(m)
	{
		m.style.visibility = 'visible';
		m.style.display = '';
	}
}

function hideLocationMessage()
{
	var m = document.getElementById('locationmessage');
	
	if(m)
	{
		m.style.visibility = 'hidden';
		m.style.display = 'none';
	}
	
	m = document.getElementById('adlocationmessage');
	
	if(m)
	{
		m.style.visibility = 'hidden';
		m.style.display = 'none';
	}
}

function showAdLocationMessage()
{
	var m = document.getElementById('adlocationmessage');
	
	if(m)
	{
		m.style.visibility = 'visible';
		m.style.display = '';
	}
}

function parseJsonSearchSquares (doc) 
{	
	if(doc != '')
	{
		var jsonData = eval("(" + doc + ")");

		if(jsonData.markers.length > 0)
		{

			for (var i = 0; i < jsonData.markers.length; i++) 
			{
				drawMapSquare(jsonData.markers[i], i, jsonData.markers.length);
			}

		}

		hidePleaseWait();
	}
	else
	{
		showLocationMessage();
		hidePleaseWait();
	}
}

function drawMapSquare(input, objIndex) 
{
	drawMapSquare(input, objIndex, 1) 
}

function drawMapSquare(input, objIndex, squareCount) 
{
	if(currentPolygons[objIndex])
	{
		gmap.removeOverlay(currentPolygons[objIndex]);
	}
	
	var borderColor = "#000000";
	
	if(squareCount > 1)
		borderColor = "#ffffff";
	
	currentPolygons[objIndex] = new GPolygon([

		input.point1,
		
		input.point2,
		input.point3,
		input.point4,
		
		input.point1

	], borderColor, 1, 1, input.bgcolor, 0.2);
	gmap.addOverlay(currentPolygons[objIndex]);

	if(input.adcount != '')
	{
		var labelID = 'gridLabel' + objIndex;
		
		if(currentLabels[objIndex])
		{
			gmap.removeOverlay(currentLabels[objIndex]);
		}
		currentLabels[objIndex] = new ELabel(input.point5, input.adcount, "propertyNumberStyle", null, null, null, labelID);
		gmap.addOverlay(currentLabels[objIndex]);
		
		GEvent.addListener(currentPolygons[objIndex], "click", function() {
			startMapSearch('gmap', input.id);
		});
		
		if(input.adcount > 0)
		{
				
			GEvent.addListener(currentPolygons[objIndex], "mouseover", function() {
				showMapHoverDiv(input.html, input.proptypecount, labelID);
			});

			GEvent.addDomListener(currentPolygons[objIndex], "mousemove", function() {
				showMapHoverDiv(input.html, input.proptypecount, labelID);
			});

			GEvent.addDomListener(currentPolygons[objIndex], "mouseout", function() {
				hideMapHoverDiv(labelID);
			});
		} 
	}
	
	gmap.setCenter(input.point1, mapSquareZoomLevel);
}

function showMapHoverDiv(h, p, labelID)
{
	var d = document.getElementById('mapHoverDiv');
	
	if(labelID)
	{
		var gLabel = document.getElementById(labelID);
		if(gLabel)
			gLabel.style.backgroundColor = '#4a88c3';
			gLabel.style.color = '#FFFFFF';
	}
	
	if(d)
	{		
		d.innerHTML = h;
		d.style.visibility = "visible";
		d.style.display = "";
		d.style.width = '100px';
		
		var t = tempMouseY;
		var l = tempMouseX;
		
		var sTop = 0;

		if(document.body.parentElement)
		{
			sTop = document.body.parentElement.scrollTop;
		}
			
		t += sTop;
		
		t = t - ((p * 17) + 40);
		l = l - 140;
		
		d.style.top = t + 'px';
		d.style.left = l + 'px';
	}
}

var tempMouseX = 0
var tempMouseY = 0

function getMouseXY(e) {

	if (document.all)
	{
		tempMouseX = event.clientX
		tempMouseY = event.clientY
	} else {
		tempMouseX = e.pageX
		tempMouseY = e.pageY
	}

	if (tempMouseX < 0){tempMouseX = 0}
	if (tempMouseY < 0){tempMouseY = 0}

	return true
}

function moveMapHoverDiv()
{
	var d = document.getElementById('mapHoverDiv');
	
	if(d)
	{				
		d.style.top = (d.style.top.replace(' ','').replace('px','') - 20) + 'px';
	}
}

function hideMapHoverDiv(l)
{
	if(l)
	{
		var gLabel = document.getElementById(l);
		if(gLabel)
			gLabel.style.backgroundColor = '#FFFFFF';
			gLabel.style.color = '#6a6a87';
	}
		
	var d = document.getElementById('mapHoverDiv');
	
	if(d)
	{
		d.style.visibility = "hidden";
		d.style.display = "none";
	}
}

function callBackAddMarker(point) 
{
    gmap.setCenter(point, 13);
    var marker = new GMarker(point);        
    gmap.addOverlay(marker);
}

/* function processMove()
{
    var center = gmap.getCenter();
    
    document.getElementById('mapLat').innerHTML = 'Lat: ' + center.y;
    document.getElementById('mapLong').innerHTML = 'Long: ' + center.x;
   */ 
/*     GDownloadUrl("getpoints.aspx?lat="+center.y+"&lang="+center.x, function(data) {
        parseJson(data);
    }); */
//}

function formatBubble (input) 
{
    var html = "<div class=\"bubble\">";
    html += "<h1>" + input.homeTeam + " vs. " + input.awayTeam + "</h1>";
    html += "<p>" + input.information + "</p>";
    html += "<p>"
    if(input.fixture != null) 
    {
        html += "<strong>Kick-off:</strong> " + input.fixture + "<br />";
    }
    /* Some more formatting */
    html += "</p></div>";
    return html;
}

function createMarker(input) 
{
        var overIcon = new GIcon(G_DEFAULT_ICON);
        overIcon.image = "images/gmaps/" + input.icon;
        overIcon.iconSize = new GSize(input.iwidth, input.iheight); 
        overIcon.iconAnchor = new GPoint(input.iwidth / 2, input.iheight); 
        overIcon.infoWindowAnchor = new GPoint(6, 0); 
        overIcon.shadow = '';

	var markerOptions = { icon:overIcon };

	var marker = new GMarker(input.point, markerOptions);

	return marker;
}

function parseJson (doc) 
{	   
	var jsonData = eval("(" + doc + ")");
	
	for (var i = 0; i < jsonData.markers.length; i++) 
	{
 		var marker = createMarker(jsonData.markers[i]);
 		mgr.addMarker(marker, jsonData.markers[i].min, jsonData.markers[i].max);
	}
}

function parseJsonMarkers (doc, markerType) 
{	   
	var jsonData = eval("(" + doc + ")");
	
	for (var i = 0; i < jsonData.markers.length; i++) 
	{
 		var marker = createMarker(jsonData.markers[i]);
 		mgrMarkers[markerType].addMarker(marker, jsonData.markers[i].min, jsonData.markers[i].max);
	}
}

function clearOverlays(clearDirections)
{
	if(directions && clearDirections)
	{
		directions.clear();
	}

	if(directionMarkers)
	{
		for (var i = 0; i < directionMarkers.length; i++) 
		{
			if(directionMarkers[i])
			{
				gmap.removeOverlay(directionMarkers[i]);
			}
		}
	}
	
	if(currentRoute)
	{
		gmap.removeOverlay(currentRoute);
		currentRoute = null;
	}
	
	if(currentPolygons)
	{
		for (var i = 0; i < currentPolygons.length; i++) 
		{
			if(currentPolygons[i])
			{
				gmap.removeOverlay(currentPolygons[i]);
			}
		}
	}
	
	if(currentLabels)
	{
		for (var i = 0; i < currentLabels.length; i++) 
		{
			if(currentLabels[i])
			{
				gmap.removeOverlay(currentLabels[i]);
			}
		}
	}	
}

function scrollDirectionsIntoView()
{
	var directionsHeader = document.getElementById('drivingDirectionsHeader');
	
	directionsHeader.scrollIntoView();
}

function drawGridSquares()
{
	var url = "GoogleMapSearchSquare.cfm?fullgrid=1";

	GDownloadUrl(url, function(data) {
		parseJsonSearchSquares(data);
	});
}

function showDrivingDirectionsOwnerCode(listID)
{
	var msg = '';
	
	if(document.getElementById(listID).selectedIndex < 1)
		msg += '\r\n- Ferry Route/Port';
		
	if(document.getElementById('txtOwnerCode').value == '')
		msg += '\r\n- Property owner code';
		
	if(msg == '')
	{	
		if(listID == 'lstPorts')
		{
			document.getElementById('lstRoutes').selectedIndex = 0;
		}
		else if(listID == 'lstRoutes')
		{
			document.getElementById('lstPorts').selectedIndex = 0;
		}
		
		var ownerCode = document.getElementById('txtOwnerCode').value;

		var url = "GoogleMapOwnerNearestPlace.cfm?adcode=" + ownerCode;
		
		GDownloadUrl(url, function(data) {
			getPlaceLocation(data, listID);
		});	
	}
	else
	{
		alert('Please complete the following:' + msg);
	}
}

function getBnBPosition1(oCode)
{
	var url = "GoogleMapOwnerNearestPlace.cfm?adcode=" + oCode;
	GDownloadUrl(url, function(data) {
		getBnBPosition2(data, oCode);
	});	
}

function getBnBPosition2(nearestPlace, oCode)
{
	localSearch.setSearchCompleteCallback(null, 
		function() {
			var locFound = false;
			
			if(localSearch.results.length > 0)
			{

				for (var i = 0; i < localSearch.results.length; i++) 
				{
					var resultLat = localSearch.results[i].lat;
					var resultLng = localSearch.results[i].lng;
				
					if(resultLat >= locLatMin && resultLat <= locLatMax && resultLng >= locLngMin && resultLng <= locLngMax)
					{
						locFound = true;
						var url = "GoogleMapBNBPositionSave.cfm?adcode=" + oCode + "&lat=" + resultLat + "&lng=" + resultLng;
											
						GDownloadUrl(url, function(data) {
							document.location.href = document.location.href;							
						});
					}
				}
			}
			
			if(!locFound)
			{
				var url = "GoogleMapBNBPositionSave.cfm?adcode=" + oCode + "&lat=99&lng=99";

				GDownloadUrl(url, function(data) {
					document.location.href = document.location.href;							
				});
			}
	    });  

	localSearch.execute(nearestPlace);
}

function processRouteLocations(localSearch, listID)
{
	var locFound = false;
	
	if(localSearch.results.length > 0)
	{

		for (var i = 0; i < localSearch.results.length; i++) 
		{
			var resultLat = localSearch.results[i].lat;
			var resultLng = localSearch.results[i].lng;

			// check against map bounds
			if(resultLat >= locLatMin && resultLat <= locLatMax && resultLng >= locLngMin && resultLng <= locLngMax)
			{
				showDrivingDirections(resultLat, resultLng, listID);
				locFound = true;
				i = localSearch.results.length;
			}
		}

	}
	
	return locFound;
}

function getPlaceLocation(nearestPlace, listID)
{
	hideLocationMessage();
	showPleaseWait();
	clearOverlays(true);  

	if(nearestPlace != '')
	{		
		localSearch.setSearchCompleteCallback(null, 
			function() {
				
				if(!processRouteLocations(localSearch, listID))
				{
					localSearch.setSearchCompleteCallback(null, 
						function() {

							if(!processRouteLocations(localSearch, listID))
							{

								hidePleaseWait();
								// Warn user that the location could not be found
								showAdLocationMessage();
							}
					    }); 
					    
					nearestPlace = nearestPlace.split(',')[0] + ', ' + nearestPlace.split(',')[1];
					    
					localSearch.execute(nearestPlace);			    
				}
		    });  
		
		localSearch.execute(nearestPlace);
	}
	else
	{
		hidePleaseWait();
		// Warn user that the location could not be found
		showLocationMessage();
	}
}

function showDrivingDirections(lat, lng, listID)
{	
	hidePleaseWait();
	
	if(document.getElementById(listID).selectedIndex > 0)
	{	
		var portLoc = document.getElementById(listID).options[document.getElementById(listID).selectedIndex].value;

		var searchString = "from: " + portLoc + " to: " + lat + "," + lng;
		
		routeStart = portLoc;
		routeEnd = lat + "," + lng;
		
		wayPoints = new Array(25);
		
		wayPoints[0] = routeStart;
		wayPoints[1] = routeEnd;

		showRoute(true, true);
	}
	else
	{
		alert('Please select a port');
	}
}

function showRoute(setBounds, queryGoogle)
{	
	clearOverlays(false);
	
	if(queryGoogle)
	{
		document.getElementById("drivingDirectionsHeader").style.visibility = 'visible';
		document.getElementById("drivingDirectionsHeader").style.display = 'block';
		
		var directionsPanel = document.getElementById("drivingDirections");
		directionsPanel.innerHTML = '';
	
		directions = new GDirections(null, directionsPanel);
		
		var pointCount = 0;
		
		for(i = 0; i < wayPoints.length; i++)
		{
			if(wayPoints[i] != "" && wayPoints[i] != null)
			{
				pointCount++;
			}
		}
		
		wayPointsProcessed = new Array(pointCount);
		var wpI = 0;
		var wayPointPanel = "<table id=waypointTable border=0 cellpadding=2 cellspacing=0>";
		
		for(i = 0; i < wayPoints.length; i++)
		{
			if(wayPoints[i] != "" && wayPoints[i] != null)
			{
				wayPointPanel += '<tr>';
				
				if(wpI  == 0)
				{
					wayPointPanel += '<td align=center><img src=images/gmaps/port-marker.png /></td>';
				}
				else
				{	
					wayPointPanel += '<td align=center><img src=' + wayPointIcons[wpI] + ' /></td>';
				}
				
				if(wpI == 0)
				{
					wayPointPanel += '<td style="font-weight:bold;">Port</td>';
					wayPointPanel += '<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
				}
				else if (wpI == (pointCount - 1))
				{
					wayPointPanel += '<td style="font-weight:bold;">Property</td>';
					wayPointPanel += '<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
				}
				else
				{					
					wayPointPanel += '<td>&nbsp;</td>';
						
					if(wpI > 1)
					{
						wayPointPanel += '<td><a href="JavaScript:moveWayPointUp(' + i + ')">Up</a></td>';
					}
					else
					{
						wayPointPanel += '<td>&nbsp;</td>';
					}	
						
					if(wpI < (pointCount - 2))
					{
						wayPointPanel += '<td><a href="JavaScript:moveWayPointDown(' + i + ')">Down</a></td>';
					}
					else
					{
						wayPointPanel += '<td>&nbsp;</td>';
					}
						
					wayPointPanel += '<td><a href="JavaScript:removeWayPoint(' + i + ')">Remove</a></td>';
				}
				
				wayPointPanel += '</tr>';
				
				wayPointsProcessed[wpI] = wayPoints[i];
				wpI++;
			}
		}
		
		wayPointPanel += '<tr><td colspan=5><p class="mapnote">Right-click the map below to add waypoints to your route.</p></td></tr>';
		wayPointPanel += '<tr><td width=45>&nbsp;</td><td width=110>&nbsp;</td><td width=65>&nbsp;</td><td width=65>&nbsp;</td><td width=65>&nbsp;</td></tr>';
		
		document.getElementById('googlemapwaypoints').innerHTML = wayPointPanel;
		
		/* var rpHeader = 45 * pointCount;
		rpHeader += 65;
			
		document.getElementById('routePlannerHeader').style.height = rpHeader + 'px'; */
	
		directions.loadFromWaypoints(wayPointsProcessed, {avoidHighways:false,getPolyline:true,getSteps:true});
	
		GEvent.addListener(directions,"load", function() {
			onDirectionsLoad(setBounds);
		});
	}
	else
	{
		onDirectionsLoad(setBounds);
	}
}

function onDirectionsLoad(setBounds)
{
	if(directions)
	{			
		if(setBounds)
		{
			var bounds = directions.getBounds();
			gmap.setCenter(bounds.getCenter(), gmap.getBoundsZoomLevel(bounds));
		}
				
		var ferryIcon = new GIcon(G_DEFAULT_ICON);
		ferryIcon.image = "images/gmaps/port-marker.png";
		ferryIcon.iconSize = new GSize(30, 43); 
		ferryIcon.iconAnchor = new GPoint(15, 43); 
		ferryIcon.infoWindowAnchor = new GPoint(6, 0); 
		ferryIcon.shadow = '';

		var letterIcon = new GIcon(G_DEFAULT_ICON);
		letterIcon.iconSize = new GSize(20, 34); 
		letterIcon.iconAnchor = new GPoint(10, 34); 
		letterIcon.infoWindowAnchor = new GPoint(5, 0); 
		letterIcon.shadow = '';
	
		var ferryMarkerOptions = {icon:ferryIcon};
		var letterMarkerOptions = {icon:letterIcon};
		
		directionMarkers = [wayPointsProcessed.length];
	
		for(i = 0; i < wayPointsProcessed.length; i++)
		{
			var point = new GLatLng(wayPointsProcessed[i].split(",")[0], wayPointsProcessed[i].split(",")[1])
						
			if(i == 0)
			{
				directionMarkers[i]= new GMarker(point, ferryMarkerOptions);
			}
			else
			{
				letterIcon.image = wayPointIcons[i];
				directionMarkers[i]= new GMarker(point, letterMarkerOptions);		
			}
			
			gmap.addOverlay(directionMarkers[i]);		
		} 
		
		currentRoute = directions.getPolyline();
		gmap.addOverlay(currentRoute);
		
		document.getElementById('routePlannerHeader').style.display = '';
		document.getElementById('routePlannerHeader').style.visibility = 'visible';
		document.getElementById('drivingDistance').innerHTML = directions.getDistance().html;
		document.getElementById('drivingDuration').innerHTML = directions.getDuration().html;
	}
}

function loadBNBsOnRoute(directions)
{
	for(i = 0; i < directions.getRoute(0).getNumSteps(); i++)
	{	
		var url = "GoogleMapBNB.cfm?lat=" + directions.getRoute(0).getStep(i).getLatLng().lat() + "&lng=" + directions.getRoute(0).getStep(i).getLatLng().lng();
		GDownloadUrl(url, function(data) {
			parseJsonMarkers(data, 1);
		});
	}
}

function removeWayPoint(i)
{
	if(confirm('Remove waypoint?'))
	{
		wayPoints[i] = "";
		showRoute(false, true);
	}
}

function moveWayPointUp(i)
{
	var tmp1 = wayPoints[i];
	var tmp2 = wayPoints[i - 1];
	
	wayPoints[i] = tmp2;
	wayPoints[i - 1] = tmp1;
	
	showRoute(false, true);
}

function moveWayPointDown(i)
{
	var tmp1 = wayPoints[i];
	var tmp2 = wayPoints[i + 1];
	
	wayPoints[i] = tmp2;
	wayPoints[i + 1] = tmp1;
	
	showRoute(false, true);
}

function centreMapOnRegion()
{
	if(document.getElementById('lstGMapRegions').selectedIndex > 0)
	{
		var rawVal = document.getElementById('lstGMapRegions').options[document.getElementById('lstGMapRegions').selectedIndex].value;
		
		var zoomLevel = rawVal.split(',')[0];
		var mapLat = rawVal.split(',')[1];
		var mapLong = rawVal.split(',')[2];

		gmap.setCenter( new GLatLng(mapLat,mapLong), parseInt(zoomLevel));
	}
	else
	{
		alert('Please select a region');
	}
}

function clearManager()
{
    gmap.clearOverlays();
    mgr.refresh();
}

function showMapProp(propID)	{
	var tab = document.getElementById('propTable'+propID);
	var expandImage = document.getElementById('propExpandImage'+propID);
	var closeImage = document.getElementById('propCloseImage'+propID);

	if(tab.style.visibility == 'visible')
	{
		tab.style.visibility = 'hidden';
		tab.style.display = 'none';
		expandImage.src = 'images/mapplus.gif';
	}
	else
	{
		tab.style.display = "";
		tab.style.visibility = "visible";
		expandImage.src = 'images/mapminus.gif';
	}
}

function closeOverlay() {
	if (currentMarker) {
		gmap.removeOverlay(currentMarker.overlay);
		currentMarker.show();
	}
}

function closeMap()
{
	document.getElementById('mapShower').style.visibility = 'hidden';
	document.getElementById('mapShower').style.display = 'none';
	document.getElementById('mapShower').style.left = -1000;
	
	if(marker)
	{
		gmap.removeOverlay(marker);
	}
}

function usePointFromPostcodeOwners(postcode) 
{ 	
    localSearch.setSearchCompleteCallback(null, 
        function() {      	
            if (localSearch.results[0]) {    
                var resultLat = localSearch.results[0].lat;
                var resultLng = localSearch.results[0].lng;
                var point = new GLatLng(resultLat,resultLng);
                marker.setLatLng(point);
                
                showMarkerPos();
                
                gmap.setCenter(point, 10);
            }else{
                alert("Location not found!");
            }
    });  

	if(postcode.substring(postcode.length - 5).toLowerCase() == 'spain')
	{
		localSearch.execute(postcode);
	}
	else
	{
		localSearch.execute(postcode + ',France');
	}

	return false;
}

function getBnBPosition()
{
	var oCode = document.getElementById('ownerCodeMap').value;
	
	var url = strBaseHref + "GoogleMapOwnerNearestPlace.cfm?adcode=" + oCode;
	GDownloadUrl(url, function(data) {
		usePointFromPostcodeOwners(data);
	});	
}

function showMarkerPos()
{
	var point = marker.getLatLng();
	document.getElementById('txtLat').value = point.y;
	document.getElementById('txtLong').value = point.x;
}
