var map;
var markersArray = [];

var churches = new Array();
churches[67]=['Aberdalgie and Dupplin', 'NO079203 ', 56.3660, -3.4924];
churches[10]=['Abernethy', 'NO190164 ', 56.3330, -3.3116];
churches[68]=['Aberuthven', 'NN979155 ', 56.3209, -3.6524];
churches[12]=['Almondbank and Tibbermore', 'NO065264 ', 56.4205, -3.5173];
churches[14]=['Ardoch', 'NN839098 ', 56.2665, -3.8761];
churches[11]=['Arngask (Glenfarg)', 'NO133104 ', 56.2781, -3.4018];
churches[15]=['Auchterarder', 'NN948129 ', 56.2968, -3.7014];
churches[13]=['Auchtergaven and Moneydie', 'NO061347 ', 56.4950, -3.5267];
churches[16]=['Blackford', 'NN899092 ', 56.2625, -3.7790];
churches[17]=['Cargill Burrelton', 'NO202377 ', 56.5245, -3.2987];
churches[18]=['Cleish', 'NT095981 ', 56.1669, -3.4589];
churches[19]=['Collace', 'NO197320 ', 56.4733, -3.3051];
churches[20]=['Comrie', 'NN770221 ', 56.3752, -3.9932];
churches[21]=['Crieff', 'NN867219 ', 56.3758, -3.8361];
churches[22]=['Dunbarney', 'NO130185 ', 56.3508, -3.4093];
churches[23]=['Dundurn', 'NN697241 ', 56.3912, -4.1123];
churches[69]=['Dunning', 'NO020147 ', 56.3145, -3.5858];
churches[24]=['Errol', 'NO253230 ', 56.3933, -3.2116];
churches[51]=['Forgandenny', 'NO087183 ', 56.3482, -3.4788];
churches[70]=['Forteviot', 'NO052175 ', 56.3403, -3.5351];
churches[25]=['Fossoway : St Serf’s and Devonside', 'NO033001 ', 56.1837, -3.5595];
churches[26]=['Fowlis Wester', 'NN928241 ', 56.3970, -3.7383];
churches[29]=['Gask', 'NO003203 ', 56.3645, -3.6154];
churches[30]=['Kilspindie and Rait', 'NO220258 ', 56.4180, -3.2659];
churches[31]=['Kinross', 'NO118023 ', 56.2051, -3.4233];
churches[27]=['Madderty', 'NN947217 ', 56.3758, -3.7066];
churches[32]=['Methven and Logiealmond', 'NO026260 ', 56.4161, -3.5803];
churches[28]=['Monzie', 'NN879250 ', 56.4039, -3.8180];
churches[52]=['Muthill', 'NN868171 ', 56.3327, -3.8324];
churches[49]=['Orwell', 'NO121051 ', 56.2303, -3.4194];
churches[53]=['Perth: Cragie', 'NO110228 ', 56.3891, -3.4431];
churches[55]=['Perth: Kinnoull', 'NO123235 ', 56.3956, -3.4223];
churches[54]=['Perth: Moncreiffe', 'NO113218 ', 56.3801, -3.4379];
churches[57]=['Perth: North', 'NO116237 ', 56.3973, -3.4337];
churches[56]=['Perth: Perth: Letham St. Marks', 'NO095243 ', 56.4023, -3.4679];
churches[58]=['Perth: Riverside', 'NO110256 ', 56.4142, -3.4441];
churches[60]=['Perth: St John the Baptists', 'NO119235 ', 56.3955, -3.4288];
churches[61]=['Perth: St Leonards-in-the-Fields and Trinity', 'NO117232 ', 56.3928, -3.4319];
churches[59]=['Perth: St Matthews', 'NO121235 ', 56.3956, -3.4256];
churches[50]=['Portmoak', 'NO183019 ', 56.2027, -3.3184];
churches[62]=['Redgorton and Stanely', 'NO110329 ', 56.4798, -3.4466];
churches[63]=['Scone: New', 'NO136262 ', 56.4201, -3.4022];
churches[64]=['Scone: Old', 'NO132256 ', 56.4146, -3.4084];
churches[66]=['St Madoes and Kinfauns', 'NO197212 ', 56.3763, -3.3018];
churches[65]=['St Martins', 'NO154304 ', 56.4581, -3.3743];
churches[71]=['Trinty Gask and Kinkell', 'NN963183', 56.3457, -3.6793];

function initialiseMaps() {
	var latlng = new google.maps.LatLng(56.400, -3.45);
	var myOptions = {
				      zoom: 10,
				      center: latlng,
				      mapTypeId: google.maps.MapTypeId.ROADMAP
				    };
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	var image   = new google.maps.MarkerImage('/themes/site_themes/perth/js/images/cofs_icon.png',
		          new google.maps.Size(19, 32),
		          new google.maps.Point(0,0),
		          new google.maps.Point(10, 32));
	var shadow   = new google.maps.MarkerImage('/themes/site_themes/perth/js/images/cofs_icon_shadow.png',
		          new google.maps.Size(43, 32),
		          new google.maps.Point(0,0),
		          new google.maps.Point(4, 32));
		          
 
  
	for (var i = 0; i < churches.length; i++) {
		if (churches[i]==null) continue;
	    var church = churches[i];
	    var infowindow = new google.maps.InfoWindow({});
	    var latLng = new google.maps.LatLng(church[2], church[3]);
	    var marker = new google.maps.Marker({
	        position: latLng,
	        map: map,
	        shadow: shadow,
	        icon: image,
	        title: church[0],
	        zIndex: i
	    });
	    markersArray.push(marker);
	    google.maps.event.addListener(marker, 'click', function() {
		  infowindow.setContent("<div style='width:180px'><b>" + this.title + "</b></div>");
		  infowindow.setOptions({maxWidth: 180});
	      infowindow.open(map,this);
	    });
	  }   
}

function initialiseResults(mapArray, myLocation) {
		var latlng = new google.maps.LatLng(56.400, -3.45);
		// Centre on Perth, unless a user location is specified
		if (myLocation !=":" && myLocation != "") {
			var myLat  = myLocation.substring(0, myLocation.indexOf(":"));
			var myLong = myLocation.substring(myLocation.indexOf(":")+1);
			latlng = new google.maps.LatLng(myLat, myLong);
		}
	 
		var myOptions = {
					      zoom: 10,
					      center: latlng,
					      mapTypeId: google.maps.MapTypeId.ROADMAP
					    };
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		var image   = new google.maps.MarkerImage('/themes/site_themes/perth/js/images/cofs_icon.png',
			          new google.maps.Size(19, 32),
			          new google.maps.Point(0,0),
			          new google.maps.Point(10, 32));
		var shadow   = new google.maps.MarkerImage('/themes/site_themes/perth/js/images/cofs_icon_shadow.png',
			          new google.maps.Size(43, 32),
			          new google.maps.Point(0,0),
			          new google.maps.Point(4, 32));
      
			          
		// Show source location, if it exists
		if (myLocation !=":" && myLocation != "") {
			var marker = new google.maps.Marker({
			    position: latlng,
			    map: map,
			    zIndex: 0
			});
		}
		for (var i = 0; i < mapArray.length; i++) {
		
		    var church = churches[mapArray[i]];
		    
		    var infowindow = new google.maps.InfoWindow({});
		    var latLng = new google.maps.LatLng(church[2], church[3]);
		    var marker = new google.maps.Marker({
		        position: latLng,
		        map: map,
		        shadow: shadow,
		        icon: image,
		        title: church[0],
		        zIndex: i
		    });
		    markersArray.push(marker);
		    google.maps.event.addListener(marker, 'click', function() {
			  infowindow.setContent("<div style='width:180px'><b>" + this.title + "</b></div>");
			  infowindow.setOptions({maxWidth: 180});
		      infowindow.open(map,this);
		    });
		  }   
	
}

// Removes the overlays from the map, but keeps them in the array
  function clearOverlays() {
    if (markersArray) {
      for (i in markersArray) {
        markersArray[i].setMap(null);
      }
    }
  }

// Deletes all markers in the array by removing references to them
function deleteOverlays() {
  if (markersArray) {
    for (i in markersArray) {
      markersArray[i].setMap(null);
    }
    markersArray.length = 0;
  }
}

function display_geo_availability() {
	
	if (Modernizr.geolocation) {
		document.write("<p style='padding: 10px;'><input type='button' value='Geolocate Me!' onClick='javascript:get_location()'></p>");
	} else {
		document.write("<p>No Geolocation available</p>");
	}
}

function get_location() {
	
	if (Modernizr.geolocation) {
		navigator.geolocation.getCurrentPosition(show_map);
	} else {
	}
}

function show_map(position) {
	var latitude = position.coords.latitude;
	var longitude = position.coords.longitude;
	var latLng = new google.maps.LatLng(latitude, longitude);
	var marker = new google.maps.Marker({
	    position: latLng,
	    map: map,
	    title: "Me"
	});
	map.panTo(latLng);
}		


