﻿var map;
var markers;
var directions;
var fbcLatitude = 41.792023;
var fbcLongitude = -88.007786;
function load() {
  if (GBrowserIsCompatible()) {
    var div = document.getElementById("map");
    map = new GMap2(div);
    if (div.clientWidth > 300) {
        map.enableContinuousZoom();
        map.enableScrollWheelZoom();
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());
    }
    var point = new GLatLng(centerLatitude, centerLongitude);
    map.setCenter(point, 12); //, 15);
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(address); });
    map.addOverlay(marker);
    addMembers();
    directions = new GDirections(map, document.getElementById("output"));
  }
}

function addMembers() {
    markers = new Array();
    for (var i = 0; i < latitudes.length; i++) {
        if (latitudes[i] != null) {
            var marker = new GMarker(new GLatLng(latitudes[i], longitudes[i]));
            marker.title = addresses[i];
            GEvent.addListener(marker, "click", function() { openInfoWindowWithDirections(this, this.title, null); });
            map.addOverlay(marker);
            markers[i] = marker;
        }
    }
}

function displayDirections(isShow) {
    var style1 = document.getElementById("directory").style;
    var style2 = document.getElementById("directions").style;
    if (isShow) {
        style1.display = "none";
        style2.display = "";
    }
    else {
        style1.display = "";
        style2.display = "none";
    }
}

function getDirectionsByCode(toLatitude, toLongitude, fromLatitude, fromLongitude) {
    directions.clear();
    directions.load("from: " + fromLatitude + "," + fromLongitude + " to: " + toLatitude + "," + toLongitude);
    showDirections();
    return false;
}

function getDirectionsByEntry(textbox) {
    directions.clear();
    directions.load("from: " + document.getElementById(textbox).value + " to: 929 Maple Ave, Downers Grove, IL 60516");
    return false;
}

function getDirectionsLink(marker, fromLatitude, fromLongitude, fromName) {
    var point = marker.getPoint();
    return "<a href='#' onclick='javascript:return getDirectionsByCode(" + point.lat() + "," + point.lng() + "," + fromLatitude + "," + fromLongitude + ");'>From " + fromName + "</a>";
}

function hideDirections() {
    return displayDirections(false);
}

function showDirections() {
    return displayDirections(true);
}

function openInfoWindow(id, opt_point) {
    if (map && id >= 0) {
        openInfoWindowWithDirections(markers[id], addresses[id], opt_point);
        return false;
    }
    return true;
}

function openInfoWindowWithDirections(marker, address, opt_point) {
    marker.openInfoWindowHtml(address + "<br/><br/>Get directions: " + getDirectionsLink(marker, fbcLatitude, fbcLongitude, "church") + " - " + getDirectionsLink(marker, myLatitude, myLongitude, "my house"), opt_point);
    return true;
}

//GSearch.setOnLoadCallback(load);
