/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
var mainMap;
var cityMarkers = new Array();
var allMarkers = new Array();
var cityIconZoomLevel = 8;//the zoom level below which we will display only the city icons
var areaZoomLevel = 11;//the zoom level when displaying an area
var zoneZoomLevel = 14;//the zoom level when displaying individual zones
var markerManager;
var myPano;
var panoClient;
var city_list = new Object();// object used as associative array for maintaining list of "seen" cities when iterating through zones
var city_gis = new Object();// object used as associative array for maintaining list of gis points for cities
var city_list_html = new Object();// object used as associative array for maintaining list of zones within a city with link to display on map etc
var zone_details_html = new Object();// object used as associative array for maintaining html to display in top right box when specific zone is displayed in main map.
var general_images_path = "";// global variable holding path to images for map on server
// 'helper' function to determine size of associative array
Array.prototype.size = function () {
var l = this.length ? --this.length : -1;
for (var k in this) {
l++;
}
return l;
}
function toggleZonesInCityListDisplay(city_div_id,toggle_link_id){
if(document.getElementById(city_div_id).style.display == 'none'){
document.getElementById(city_div_id).style.display = 'block';
document.getElementById(toggle_link_id).innerHTML = '
';
} else {
document.getElementById(city_div_id).style.display = 'none';
document.getElementById(toggle_link_id).innerHTML = '
';
}
}
function displayZoneDetailsInTopBox(zone_id){
document.getElementById("zones_map_top_right_box").innerHTML = "
"+zone_details_html[zone_id] + "
";
}
function showStreetView(lat, long) {
document.getElementById("pano").innerHTML = "Please wait for the street view of this location.
";
point = new GLatLng(lat, long);
toggleStreetMapView('street');
mainMap.setCenter(point, zoneZoomLevel);
panoClient.getNearestPanorama(point, showPanoData);
}
function toggleStreetMapView(view) {
if (document.getElementById("map_frame").style.display == 'none' && view=='map'){
document.getElementById("pano").style.display = 'none';
//if (!pano.isHidden()){pano.hide()}
document.getElementById("map_frame").style.display = 'inline';
mainMap.checkResize();
}else if (document.getElementById("pano").style.display == 'none' && view=='street'){
document.getElementById("pano").style.display = 'inline';
//if (pano.isHidden()){pano.show()}
document.getElementById("map_frame").style.display = 'none';
myPano.checkResize();
}
}
function toggleOverlay() {
if (!overlayInstance) {
overlayInstance = new GStreetviewOverlay();
map.addOverlay(overlayInstance);
} else {
map.removeOverlay(overlayInstance);
overlayInstance = null;
}
}
function showPanoData(panoData) {
if (panoData.code != 200) {
document.getElementById("pano").innerHTML = "Sorry but street view is not yet available for this location.
Click here to display the regular map view.
";
return;
} else {
document.getElementById("pano").innerHTML = "";
var displayString = [
"Panorama ID: " + panoData.location.panoId,
"LatLng: " + panoData.location.latlng,
"Copyright: " + panoData.copyright,
"Description: " + panoData.location.description
].join("
");
var angle = computeAngle(mainMap.getCenter(), panoData.location.latlng);
myPano.setLocationAndPOV(panoData.location.latlng, {yaw: angle});
}
}
function handleNoFlash(errorCode) {
if (errorCode == 603) {
//alert("Sorry but flash doesn't appear to be supported by your browser");
document.getElementById("pano").innerHTML = "Sorry but Flash doesn't appear to be supported by your browser and it's needed for displaying street view images.
Click here to download the latest version.
";
return;
}
}
function computeAngle(endLatLng, startLatLng) {
var DEGREE_PER_RADIAN = 57.2957795;
var RADIAN_PER_DEGREE = 0.017453;
var dlat = endLatLng.lat() - startLatLng.lat();
var dlng = endLatLng.lng() - startLatLng.lng();
var yaw = Math.atan2(dlng * Math.cos(endLatLng.lat() * RADIAN_PER_DEGREE), dlat) * DEGREE_PER_RADIAN;
return wrapAngle(yaw);
}
function wrapAngle(angle) {
if (angle >= 360) {
angle -= 360;
} else if (angle < 0) {
angle += 360;
}
return angle;
}
// Translations
function HotspotsMapTranslations(browser_support, homepage, show_on_map, loading)
{
this.browser_support = browser_support;
this.homepage = homepage;
this.show_on_map = show_on_map;
this.loading = loading;
}
// Constructor
function HotspotsMap(viewport, external_object_name, translations, images_path)
{
// Init translations
this.translations = translations;
if (GBrowserIsCompatible()) {
// Create the map attribute
this.map = new GMap2(document.getElementById(viewport));
this.map.addControl(new GLargeMapControl());
this.map.addControl(new GMapTypeControl());
// Create the array that will contain refs to markers
this.markers = Array();
// Init source url
this.xml_source = null;
// This is quite stupide, but it's needed since we need to build onclick urls
this.external_object_name = external_object_name;
// Init server path
this.images_path = images_path;
general_images_path = images_path;
mainMap = this.map;
// also setup streetview
panoClient = new GStreetviewClient();
myPano = new GStreetviewPanorama(document.getElementById("pano"));
GEvent.addListener(myPano, "error", handleNoFlash);
} else {
alert(this.translations.browser_support);
}
}
HotspotsMap.prototype.buildHtmlFromHotspot = function(hotspot_element, icon)
{
// Init HTML
var html = " | ";
// Prepare fragment that will go in the info bubbles
/*
* Hotspot name
*/
var name = hotspot_element.getElementsByTagName("name");
var hotspotId = hotspot_element.getElementsByTagName("hotspotId");
if (name.length == 1) {
html += "" + GXml.value(name[0]) + "";
}
html += " ";
/*
* Phone
*
var phone = hotspot_element.getElementsByTagName("contactPhoneNumber");
if (phone.length == 1) {
html += "" + GXml.value(phone[0]) + " ";
}
*/
/*
* Email
*
var email = hotspot_element.getElementsByTagName("contactEmail");
if (email.length == 1) {
html += GXml.value(email[0]);
}
*/
/*
* Civic number
*/
var civicNumber = hotspot_element.getElementsByTagName("civicNumber");
if (civicNumber.length == 1) {
html += GXml.value(civicNumber[0]) + " ";
}
/*
* Street address
*/
var streetAddress = hotspot_element.getElementsByTagName("streetAddress");
if (streetAddress.length == 1) {
html += GXml.value(streetAddress[0]) + " ";
}
html += " ";
/*
* Suburb & City
*/
var suburb = hotspot_element.getElementsByTagName("suburb");
var city = hotspot_element.getElementsByTagName("city");
if (suburb.length == 1) {
html += GXml.value(suburb[0]);
if (city.length == 1) {
html += ", ";
}
}
if (city.length == 1) {
html += GXml.value(city[0]) + " ";
}
html += ' [';
/*
* Add booking link if possible
*/
var vianet_id = GXml.value(hotspot_element.getElementsByTagName("vianetId")[0]);
if (vianet_id!= ''){
html += 'Book';
}
/*
* Add Website if exists
*/
var websiteUrl = hotspot_element.getElementsByTagName("webSiteUrl");
if (websiteUrl.length == 1 && GXml.value(websiteUrl[0]).search(/zenbu.net.nz/) == -1) {
if(vianet_id!=""){
html+="|";
}
html += "" + this.translations.homepage + "";
}
/*
* Street view link
*/
var gis = hotspot_element.getElementsByTagName("gisCenterLatLong");
if (hotspotId.length == 1 && gis.length == 1 && gis[0].getAttribute("lat") != "" && gis[0].getAttribute("long") != "") {
// Extract GIS data
displayDiv = mainMap.getContainer();
if(vianet_id!="" ||(websiteUrl.length == 1 && GXml.value(websiteUrl[0]).search(/zenbu.net.nz/) == -1)){
html += "|";
}
html += 'Street View';
}
html += '] ';
/*
* Postal code (ZIP)
*
var postalCode = hotspot_element.getElementsByTagName("postalCode");
if (postalCode.length == 1) {
html += "" + GXml.value(postalCode[0]) + ", ";
}*/
/*
* Country
*
var country = hotspot_element.getElementsByTagName("country");
if (country.length == 1) {
html += "" + GXml.value(country[0]) + "";
}
html += " ";*/
/*
* Phone
*
var phone = hotspot_element.getElementsByTagName("contactPhoneNumber");
if (phone.length == 1) {
html += "" + GXml.value(phone[0]) + " ";
}*/
/*
* Transit
*
var transit = hotspot_element.getElementsByTagName("massTransitInfo");
if (transit.length == 1) {
html += "" + GXml.value(transit[0]) + " ";
}*/
html += " |
";
return html;
}
// GIcon factory method
HotspotsMap.prototype.createIcon = function (imageUrl, iSize, shadowUrl, sSize, iconAnchor, bubbleAnchor)
{
var icon = new GIcon();
icon.image = imageUrl;
icon.iconSize = iSize;
icon.shadow = shadowUrl;
icon.shadowSize = sSize;
icon.iconAnchor = iconAnchor;
icon.infoWindowAnchor = bubbleAnchor;
return icon;
}
HotspotsMap.prototype.createCityBubble = function(point, icon, html)
{
var marker = new GMarker(point, icon);
GEvent.addListener(marker, "click", function()
{
marker.openInfoWindowHtml(html);
});
GEvent.addListener(marker, "mouseover", function()
{
marker.openInfoWindowHtml(html);
});
return marker;
}
HotspotsMap.prototype.createInfoBubble = function(point, icon, html)
{
var marker = new GMarker(point, icon);
GEvent.addListener(marker, "click", function()
{
marker.openInfoWindowHtml(html);
});
GEvent.addListener(marker, "mouseover", function()
{
marker.openInfoWindowHtml(html);
});
GEvent.addListener(marker, "openinfo", function()
{
var iwAnchor = icon.infoWindowAnchor;
var iconAnchor = icon.iconAnchor;
var offset = new GSize(iwAnchor.x-iconAnchor.x,iwAnchor.y-iconAnchor.y);
mainMap.openInfoWindow(point, html, {pixelOffset:offset});
});
return marker;
}
HotspotsMap.prototype.openInfoBubble = function(bubbleId)
{
var marker = this.markers[bubbleId];
var center = marker.getPoint();
this.map.setCenter(center, zoneZoomLevel);
// Trigger click ( NB. markers is a global var )
GEvent.trigger(this.markers[bubbleId], "openinfo");
toggleStreetMapView('map');
}
HotspotsMap.prototype.loadHotspotsStatus = function()
{
// Make sure the source has been set
if (this.xml_source != null) {
// Drop the pointer to this in a global var for async needs
var self = this;
GDownloadUrl(this.xml_source, function(data, responseCode) {
self.parseHotspotsStatus(GXml.parse(data));
});
} else {
return false;
}
}
HotspotsMap.prototype.parseHotspotsStatus = function(xml_doc)
{
var html_list = "";
var rh_city_list = "";
var rh_city_list_array = new Array();
var down_list = "";
// we'll iterate through this and add to map overlay AFTER down markers - appears better
var up_markers = new Array();
var display_markers = new Array();
//var city_list = new Object();// object used as associative array for maintaining list of "seen" cities when iterating through zones
//var city_gis = new Object();// object used as associative array for maintaining list of gis points for cities
var image_extension = ".png";
// Init marker icons
var upIcon = this.createIcon(this.images_path + "ZonesMap/zone_up" + image_extension, new GSize(24, 30),
this.images_path + "ZonesMap/shadow.png", new GSize(37, 34),
new GPoint(10, 20), new GPoint(10, 1));
var downIcon = this.createIcon(this.images_path + "ZonesMap/zone_down" + image_extension, new GSize(24, 30),
this.images_path + "ZonesMap/shadow.png", new GSize(37, 34),
new GPoint(10, 20), new GPoint(10, 1));
var unknownIcon = this.createIcon(this.images_path + "ZonesMap/unknown" + image_extension, new GSize(24, 30),
this.images_path + "ZonesMap/blank.gif", new GSize(22, 34),
new GPoint(11, 30), new GPoint(11, 1));
// Parse the XML DOM
var hotspots = xml_doc.documentElement.getElementsByTagName("hotspot");
for (var i = 0; i < hotspots.length; i++) {
var hotspotId = hotspots[i].getElementsByTagName("hotspotId");
var gis = hotspots[i].getElementsByTagName("gisCenterLatLong");
if (hotspotId.length == 1 && gis.length == 1 && gis[0].getAttribute("lat") != "" && gis[0].getAttribute("long") != "") {
// Extract GIS data
var point = new GLatLng(parseFloat(gis[0].getAttribute("lat")), parseFloat(gis[0].getAttribute("long")));
var status = hotspots[i].getElementsByTagName("globalStatus");
var vianet_id = GXml.value(hotspots[i].getElementsByTagName("vianetId")[0]);
var markerIcon;
var status_label = 1;
if (status.length == 1) {
switch (GXml.value(status[0])) {
case "100":
markerIcon = upIcon; // Hotspot is up
break;
case "0":
markerIcon = downIcon; // Hotspot is down
status_label = 0;
break;
default:
markerIcon = unknownIcon; // Unknown hotspot status
break;
}
} else {
markerIcon = unknownIcon; // Unknown hotspot status
}
// Prepare the fragment that will go in sidebar
var html = this.buildHtmlFromHotspot(hotspots[i], markerIcon);
if (status_label == 1){
// add the zones which are online first
html_list += html + "
";
} else {
// we'll append this after iterating through all zones
down_list += html + "
";
}
var marker = this.createInfoBubble(point, markerIcon, html);
// markers is a global var
this.markers[GXml.value(hotspotId[0])] = marker;
allMarkers.push(marker);
var city = "";
var city_value = "";
var suburb = "";
var suburb_value = "";
var city_list_link_html = ''+GXml.value(hotspots[i].getElementsByTagName("name")[0])+'';
if (vianet_id!= ''){
city_list_link_html += ' [book]';
}
city_list_link_html += '
';
if (hotspots[i].getElementsByTagName("city").length == 1){
city = GXml.value(hotspots[i].getElementsByTagName("city")[0]);
city_value = city.toUpperCase();
}
if (hotspots[i].getElementsByTagName("suburb").length == 1){
suburb = GXml.value(hotspots[i].getElementsByTagName("suburb")[0]);
suburb_value = suburb.toUpperCase();
}
if (suburb_value != "" && typeof city_list[suburb_value] != "undefined"){
city_list[suburb_value]++;
city_list_html[suburb_value] += city_list_link_html;
} else if (city_value != "" && typeof city_list[city_value] != "undefined"){
city_list[city_value]++;
city_list_html[city_value] += city_list_link_html;
} else if(city_value != "" && typeof city_list[city_value] == "undefined"){
// this city isn't yet added to the list
city_list[city_value]= 1;
city_gis[city_value]=point;//the point we'll use for this city
city_list_html[city_value] = city_list_link_html;
}
// ################# Prepare the details that will go in top info box ########################
var name = GXml.value(hotspots[i].getElementsByTagName("name")[0]);
var box_info_html = ""+name+" [";
if (vianet_id!= ''){
box_info_html += 'Book';
}
/*
* Street view link
*/
if (hotspotId.length == 1 && gis.length == 1 && gis[0].getAttribute("lat") != "" && gis[0].getAttribute("long") != "") {
if(vianet_id!= ''){
box_info_html += '|';
}
// Extract GIS data
displayDiv = mainMap.getContainer();
box_info_html += 'Street View|';
box_info_html += 'Map View';
}
box_info_html += ']
';
/*
* Civic number
*/
var civicNumber = hotspots[i].getElementsByTagName("civicNumber");
if (civicNumber.length == 1) {
box_info_html += GXml.value(civicNumber[0]) + " ";
}
/*
* Street address
*/
var streetAddress = hotspots[i].getElementsByTagName("streetAddress");
if (streetAddress.length == 1) {
box_info_html += GXml.value(streetAddress[0]) + " ";
}
box_info_html += "
";
/*
* Suburb & City
*/
if (suburb_value != "") {
box_info_html += suburb;
if (city_value != "") {
box_info_html += ", ";
}
}
if (city_value != "") {
box_info_html += city + "
";
}
var phone = hotspots[i].getElementsByTagName("contactPhoneNumber");
if (phone.length == 1) {
box_info_html += "ph: " + GXml.value(phone[0]) + " ";
}
var email = hotspots[i].getElementsByTagName("contactEmail");
if (email.length == 1) {
box_info_html += ""+GXml.value(email[0])+"";
}
zone_details_html[GXml.value(hotspotId[0])] = box_info_html;
// ################ Detail for top right box done #######################
}
}
for (var city in city_list){
if (typeof(city_list[city]) != "undefined" && typeof(city_gis[city]) != "undefined"){
// build list to display in right hand panel - listing of cities
//rh_city_list += ""+ city + " ("+city_list[city]+")
";
var style = 'style="font-size: 0.90em; color: #888888; font-weight: 400;"';
if (city_list[city] > 12){
style = 'style="font-size: 1.20em; color: #333333; font-weight: 900;"';
} else if (city_list[city] > 5){
style = 'style="font-size: 1.10em; color: #444444; font-weight: 700;"';
} else if (city_list[city] > 2){
style = 'style="font-size: 1.0em; color: #555555; font-weight: 600;"';
} else if (city_list[city] > 1){
style = 'style="font-size: 0.90em; color: #666666; font-weight: 600;"';
}
city_list_str = ""+city+"";
city_list_str += "
";
city_list_str += "("+city_list[city]+")";
city_list_str += ""+city_list_html[city]+"
";
rh_city_list_array.push(city_list_str+"
");
label = "" + city + "" + " has " + city_list[city] + " zone";
if (city_list[city] > 1){
label += "s";
}
label += ".
Click here to show details.
";
cityMarkers.push(this.createCityBubble(city_gis[city], upIcon, label));
}
}
rh_city_list_array.sort();
rh_city_list = rh_city_list_array.join("");
markerManager = new MarkerManager(mainMap);
markerManager.addMarkers(cityMarkers, 0, cityIconZoomLevel);
markerManager.addMarkers(allMarkers, cityIconZoomLevel+1);
markerManager.refresh();
// put rh_city_list first..
html_list = rh_city_list ;//+ "
" + html_list + down_list;
// append the zones which are down to the end of the list
// html_list += down_list;
// Load the prepared HTML fragment in the right-hand list
this.hotspots_info_list.innerHTML = html_list;
}
HotspotsMap.prototype.redraw = function()
{
this.hotspots_info_list.innerHTML = this.translations.loading;
this.loadHotspotsStatus();
}
HotspotsMap.prototype.setHotspotsInfoList = function(hotspots_info_list)
{
this.hotspots_info_list = document.getElementById(hotspots_info_list);
}
HotspotsMap.prototype.setInitialPosition = function(lat, lng, zoom)
{
this.map.setCenter(new GLatLng(lat, lng), zoom);
}
HotspotsMap.prototype.setMapType = function(map_type)
{
this.map.setMapType(map_type);
}
HotspotsMap.prototype.setOnClickListener = function(callback_fct)
{
GEvent.addListener(this.map, 'click', callback_fct);
}
HotspotsMap.prototype.setXmlSourceUrl = function(xml_source)
{
this.xml_source = xml_source;
}
// geocoder for working out where to centre map when search
if (GBrowserIsCompatible()) {
var geocoder = new GClientGeocoder();
}
function showLocationOnMap(response) {
if (GBrowserIsCompatible()) {
var point;
if (!response || response.Status.code != 200) {
alert("The location you specified couldn't be found.\nPlease try a broader search.");
} else {
place = response.Placemark[0];
point = new GLatLng(place.Point.coordinates[1],
place.Point.coordinates[0]);
mainMap.setCenter(point, areaZoomLevel);
toggleStreetMapView('map');
}
}
}
function showZonesAt(location) {
if (geocoder != null) {
geocoder.getLocations(location+' New Zealand', showLocationOnMap);
}
}
function showZonesAtPoint(lat, lng){
pt = new GLatLng(parseFloat(lat),parseFloat(lng));
mainMap.setCenter(pt, areaZoomLevel);
}