/************************************************************************************/
/*	If forEach not supported in the browser (the case in IE7 at least), create it	*/
/************************************************************************************/
	if (!Array.prototype.forEach) {
		Array.prototype.forEach = function(fun /*, thisp*/) {
		var len = this.length;
		if (typeof fun != "function")
		throw new TypeError();
		var thisp = arguments[1];
		for (var i = 0; i < len; i++) { if (i in this) fun.call(thisp, this[i], i, this);}
	};}

/*******************************************************************************************/
/* 	Map function.  Converted from V2 API (deprecated) to V3 API on 12 Mar 2011 by JWJ.     */
/* 	               Variables generalised to arrays 16-17 Mar 2011 by JWJ                   */
/*                 Reinstate Car Park 23 Jul 2011 by JWJ                                   */
/*******************************************************************************************/

	var i = 0;				/* Common index for gMapRefs, gMarkers & gContent    */
	var gMapRefs = [];		/* Holds the LatLng coords for each point            */
	var gMarkers = [];		/* Holds handles for each Marker                     */
	var gContent = [];		/* Holds content string for rach Marker's InfoWindow */
	var map;				/* Handle for the map object                         */
	var directionsDisplay;	/* Handle for the result of a Directions route       */
	var centPoint;			/* Holds LatLng for default centre point of map	     */
	var globalInfoWindow;	/* Handle for single, shared InfoWindow              */

	//	/* Custom start & finish marker icons; used in show_ and hideWalkRoute funcs */
//	var startMarker;		/* Handle for custom start route marker              */
//	var finisMarker;		/* Handle for custom finish route marker             */
//	var startIcon = new google.maps.MarkerImage('images/start.png');
//	var finisIcon = new google.maps.MarkerImage('images/finish.png');
	
function toggleMarkers() {
/* 	Function hides any visible markers  */
	if (!Array.prototype.forEach) {alert('forEach not supported');}
	gMarkers.forEach(function(e, i, a) {
		if (e.getVisible() == true) 
			{e.setVisible(false);}
	});
}	
	
function mapLoad() {
/* 	Define a new map instance and set parameters, create common elements, enable controls */
    map=new google.maps.Map(document.getElementById("map"));
	centPoint = new google.maps.LatLng(50.712600, -1.99055);
	globalInfoWindow = new google.maps.InfoWindow();
    map.setCenter(centPoint);
	map.setZoom(17);
    map.disableDoubleClickZoom = false;
    map.scrollwheel = true;
    map.mapTypeControl = true;
    map.smallMapControl = true;

/*  Add Markers and corresponding InfoWindow content strings; only La Lupa Marker/InfoWindow visible initially */	
/*  Restaurant Marker */
	gMapRefs[0] = new google.maps.LatLng(50.712360,-1.990550);
    gMarkers[0] = new google.maps.Marker({position:gMapRefs[0], visible:false, map:map});
	gContent[0] = '<font size="2" face="Arial"><b>La Lupa 3<br />Yeatman&#39;s Old Mill, Poole Quay<br />Poole BH15 1HJ<br />tel: 01202 670660' +
	'<br /><a href="contactus.html" onclick="setMenu(this);" target="mainframe">Contact Us</a></b></font>';
/*  'Sea Music' Marker */
	gMapRefs[1] = new google.maps.LatLng(50.712120,-1.988370);
    gMarkers[1] = new google.maps.Marker({position:gMapRefs[1], visible:false, map:map});
	gContent[1] = '<font size="2" face="Arial"><b>"Sea Music" sculpture</b><br /><br /><a href="http://www.publicartonline.org.uk/casestudies/regeneration/poole_streetscape/images.php" target="_seamusic">more ...</a></font>';
/*  Old Customs House Marker */
	gMapRefs[2] = new google.maps.LatLng(50.712290,-1.989150);
    gMarkers[2] = new google.maps.Marker({position:gMapRefs[2], visible:false, map:map});
	gContent[2] = '<font size="2" face="Arial"><b>Old Custom House</b></font>';
/*  Lifting Bridge Marker */	
	gMapRefs[3] = new google.maps.LatLng(50.712620,-1.992790);
    gMarkers[3] = new google.maps.Marker({position:gMapRefs[3], visible:false, map:map});
	gContent[3] = '<font size="2" face="Arial"><b>Poole Lifting Bridge</b></font>';
/*  Ferry Terminal Marker */
	gMapRefs[4] = new google.maps.LatLng(50.708700,-1.993280);
    gMarkers[4] = new google.maps.Marker({position:gMapRefs[4], visible:false, map:map});
	gContent[4] = '<font size="2" face="Arial"><b>Poole Ferry Terminal</b></font>';
/*  Car Park Marker */
	gMapRefs[5] = new google.maps.LatLng(50.712539,-1.990812);
    gMarkers[5] = new google.maps.Marker({position:gMapRefs[5], visible:false, map:map});
	gContent[5] = '<font size="2" face="Arial"><b>Pay&Display Car Park</b><p>1 hour - 50p<br />2 hours - \u00A31<br />All day - \u00A34</p></font>';

/*  Set Map Type and enable the Map Type control*/
	map.mapTypeControl = true;
    map.setMapTypeId(google.maps.MapTypeId.HYBRID);
	
/*	Loop to set 'click' Event Listeners on all Markers; Event Listener makes the marker visible, sets the content */
/*	of the InfoWindow, checks whether the new marker (mapref) is within the map bounds and sets the zoom if       */
/*  required before panning to the new marker, and then open the InfoWindow on it                                 */	
	for (i=0;i<gMarkers.length;i++) {
		(function(map, i, mapref, marker, content){
			google.maps.event.addListener(marker, 'click', 
				function() {
					marker.setVisible(true);
					globalInfoWindow.setContent(content);
					if (map.getBounds().contains(mapref) === false) 
						{	//alert('target is outside map bounds');
							map.setZoom(16);
							if (i === 0)
								{map.panTo(centPoint);}
							else
								{map.panTo(mapref);}
							map.panTo(mapref);
							map.setZoom(17);
						}
						else
						{	//alert('target is within map bounds');
							map.panTo(mapref);
						}
					globalInfoWindow.open(map, marker);
				}
			);
		})(map, i, gMapRefs[i], gMarkers[i], gContent[i]);
	}
	
/*	Open the Restaurant InfoWindow to start with 							   */
	gMarkers[0].setVisible(true);
	globalInfoWindow.open(map, gMarkers[0]);
	globalInfoWindow.setContent(gContent[0]);
}
    
function linkClicked(i) {
/* Make the approppriate Marker visible, and open its InfoWindow when the corresponding		*/
/* link has been clicked 																    */
	toggleMarkers();
    google.maps.event.trigger(gMarkers[i], "click");
}

function showWalkRoute(start) {
/* 	Show the appropriate Walking Route */
	toggleMarkers();
	var routeRequest;
	var directionsService = new google.maps.DirectionsService();
	directionsDisplay = new google.maps.DirectionsRenderer({markerOptions:{visible:true}});
//	directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers:true});

	directionsDisplay.setMap(map);
/* 	Close any open InfoWindow */	
	globalInfoWindow.close();
	routeRequest = {origin:gMapRefs[start], destination:gMapRefs[0], travelMode:google.maps.DirectionsTravelMode.WALKING, unitSystem:google.maps.DirectionsUnitSystem.IMPERIAL};
	switch (start) {
	case 1:
	    map.setCenter(gMapRefs[0]);
		map.setZoom(17);
		break;
	case 4: 
		break;
	default:
		break;
	}
	directionsService.route(routeRequest,
		function(result, status) {
			if (status === google.maps.DirectionsStatus.OK) {
				directionsDisplay.setDirections(result);
				var leg = result.routes[0].legs[0];
				document.getElementById('disttime').innerHTML = '<p>Distance: '+leg.distance.text+'</p><p>Duration: '+leg.duration.text+'</p>';
				document.getElementById('disttime').style.display = 'block';
//				startMarker = new google.maps.Marker({position:leg.start_location,map:map,icon:startIcon});
//				finisMarker = new google.maps.Marker({position:leg.end_location,map:map,icon:finisIcon});
			}
		}
	);
}

function hideWalkRoute(){
/* 	Hide the currently displayed Walking Directions, start and finish Markers, and reset map to La Lupa */
	document.getElementById('disttime').style.display = 'none';
	document.getElementById('disttime').innerHTML = "";
	directionsDisplay.setMap(null);
//	startMarker.setMap(null);
//	finisMarker.setMap(null);
	toggleMarkers();
    map.setCenter(centPoint);
	map.setZoom(17);	
	linkClicked(0);
}

function showParking(park) {
/* 	Function open the Popup window for Poole Car Parking */
	window.open(park,
			'Parking',
			'width=800,height=800,scrollbars=1,toolbar=0,titlebar=0,location=0,resizable=1,left=75,top=75,status=0,menubar=0');
}


