﻿//
// Google __Map API Helper
// Gen-i ChCH SWS for NZFS
//
// the globla map object
var __Map = null;
var __PinIcon = null;
// nzfsStationMarkers
var __StationMarkers = new Array();

var __DefaultMarker = null;

var __longitude = 0.0;
var __latitude = 0.0;
var __longitudeControlId = null;
var __latitudeControlId = null;



function gMap_MapInit(id) {
	__Map = new GMap2(document.getElementById(id));
	__Map.addControl(new GMapTypeControl());
	__Map.addControl(new GSmallMapControl());
	__Map.enableScrollWheelZoom();

	__PinIcon = new GIcon();
	__PinIcon.iconSize = new GSize(8, 12);
	__PinIcon.shadowSize = new GSize(8, 12);
	__PinIcon.iconAnchor = new GPoint(8, 12);
	__PinIcon.infoWindowAnchor = new GPoint(8, 12);
	__PinIcon.infoShadowAnchor = new GPoint(8, 12);
	__PinIcon.image = "/SiteCollectionImages/nzfs-pin.png";
	__PinIcon.shadow = "";

	gMap_GotoLocation();
}

function gMap_GotoLocation_(longitude, latitude) {
	if (__Map != null) {
		var point = new GLatLng(latitude, longitude);
		__Map.setCenter(point, 7);
	}
}

function gMap_CreateMarker(point, popupMessage, pTitle) {

	var gMarker = new GMarker(point, { title: pTitle, icon: __PinIcon });
	gMarker.bindInfoWindowHtml(popupMessage, {});
	
	return gMarker;

}

function gMap_CenterMarker(marker, zoom) {
	if (marker != null) {
		__Map.setCenter(marker.getLatLng(), zoom);
	}
}

function gMap_DisplayStations() {
	for (var index in __StationMarkers) {
		if (__StationMarkers[index]) {
			__Map.addOverlay(__StationMarkers[index]);
		}
	}

}

function gMap_AddStation(point, pupupMessage, pTitle) {
	var marker = gMap_CreateMarker(point, pupupMessage, pTitle);
	__StationMarkers.push(marker);
}


//** special for the page, need change if possible.

function gMap_MakeAddressPopup(title, address, zip, phone, url) {
	var msg = 
		"<div clas='mapPopup'><h4 style='color:#000;padding:0px; margin:0px 0px 5px 0px;'>" + 
		title + "</h4><p style='color:#000;padding:0px; margin:0px;'>" + 
		address + "<br />" + 
		city + "<br />" +
		zip + "<br />Phone: " +
		phone + "<br />";
	if (url != null)
	{
		msg += "<a href='" + url + "' title='" + title + "'" + ">Read More</a>";
	}
	msg += "</p></div>";
	return msg;
}


function gMap_GotoLocation() {
	var oLongitude = document.getElementById("hLongitude");
	var longitude = 0;
	if (oLongitude) {
		longitude = Number(oLongitude.innerHTML.replace('&nbsp;', ''));
	}
	var oLatitude = document.getElementById("hLattitude");
	var latitude = 0;
	if (oLatitude) {
		latitude = Number(oLatitude.innerHTML.replace('&nbsp;', ''));
	}
	var point = new GLatLng(latitude, longitude);
	var msgPopup = document.getElementById('gMapPopupMessage').innerHTML;
	__DefaultMarker = gMap_CreateMarker(point, msgPopup);
	gMap_CenterMarker(__DefaultMarker, 7);
	__Map.addOverlay(__DefaultMarker);

}

function GetCoordinate() {
	//ctl00_PlaceHolderRightMenu_ctl01_ctl00_TextField (the id for Address field in our sample)
	//ctl00_PlaceHolderRightMenu_ctl02_ctl00_TextField (the id for city field in our sample)

	var addressField = document.getElementById('ctl00_PlaceHolderSideColumnContent_EditModePanel3_ctl04_ctl00_TextField');
	var cityField = document.getElementById('ctl00_PlaceHolderSideColumnContent_EditModePanel3_ctl05_ctl00_TextField');
	var address = '';
	if (addressField) {
		address = address + addressField.value;
	}
	if (cityField) {
		address = address + ',' + cityField.value;
	}

	var geocoder = new GClientGeocoder();
	//get co-ords for address
	//alert(address);
	geocoder.getLatLng(address, CallBackForCoordinate);
}

function CallBackForCoordinate(point) {
	// After retrieve a point, we can:

	if (point) {
		__Map.setCenter(point, 7);

		var gMarker = new GMarker(point);
		htmlMsg = "<h4 style='color:#000;padding:0px; margin:0px 0px 5px 0px;'>Arapawa</h4><p style='color:#000;padding:0px; margin:0px;'>Exchange Place<br />Level 6, 5-7 Willeston Street<br />Wellington<br />6011 Phone: 04 470 8000<br /></p>";
		gMarker.bindInfoWindowHtml(htmlMsg);
		__Map.addOverlay(gMarker);

		var mapLng = document.getElementById('ctl00_PlaceHolderMainContent_nzfsEditModePanel_ctl00_ctl00_TextField');
		if (mapLng) {
			mapLng.value = point.lng();
		}

		var mapLat = document.getElementById('ctl00_PlaceHolderMainContent_nzfsEditModePanel_ctl01_ctl00_TextField');
		if (mapLat) {
			mapLat.value = point.lat();
		}
	} else {
		alert("Failed to get the coordinate for the address.");
	}
}


