﻿//<![CDATA[
var undefined2 = "undefined";
var map;
var gmarkers = [];
// ===== This function picks up the click and opens the corresponding info window ====
function myclick(i) {
    GEvent.trigger(gmarkers[i], "click");
}

function load() {
    /*  HACK: Have to check because IE will throw an error because a child container HTML element 
    contains script code that tries to modify the parent container element of the child container.
    SOURCE: http://support.microsoft.com/default.aspx/kb/927917
            
    This error is thrown if we attempt to remove the load() call from the body onload event and 
    place it into a RegisterStartupScript. The google JS script attempts to modify part of the DOM and this 
    error is then thrown, in turn, shutting down the APP in IE (version 7 at least).
            
    The homeSpatialInfo and ProximityLocations variables are not loaded until the "Go" button is clicked. 
    Therefore, on the first intial load, they are of type "undefined", therefore the load does not occur. 
    After a post back occurs from the "Go" button, these JSON variables are written to the client and the 
    conditional will pass because they are defined and the method call will continue. 
    VERY HACKY
    */
    if (typeof (homeSpatialInfo) != undefined2 && typeof (ProximityLocations) != undefined2) {
        if (GBrowserIsCompatible()) {
            // ===== this variable will collect the html which will eventually be placed in the side_bar ====
            var side_bar_html = "";

            // ===== arrays to hold copies of the markers and html used by the side_bar ====
            // ===== because the function closure trick doesnt work there ====
            map = new GMap2(document.getElementById("map"));
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            //tlw --map.setCenter(new GLatLng(homeSpatialInfo.latitude, homeSpatialInfo.longitude), 9);
            // ==== It is necessary to make a setCenter call of some description before adding markers ====
            // ==== At this point we dont know the real values ====
            map.setCenter(new GLatLng(0, 0), 0);

            // ===== Create a base icon for all of our markers that specifies the ====
            // ===== shadow, icon dimensions, etc. ====
            var baseIcon = new GIcon();
            baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
            baseIcon.iconSize = new GSize(32, 32); //new GSize(20, 34);
            baseIcon.shadowSize = new GSize(37, 34);
            baseIcon.iconAnchor = new GPoint(9, 34);
            baseIcon.infoWindowAnchor = new GPoint(9, 2);
            baseIcon.infoShadowAnchor = new GPoint(18, 25);




            // ===== Creates a marker ====
            function createMarker(point, name, address, urladdress, locationNumber, theurl, street, ncrlink, ncrurl, ncrentid, ncrdbid, ncrlocid) {
                var icon = new GIcon(baseIcon);
                icon.image = "http://content.wellspan.org/images/blue-dot.png" //"http://maps.google.com/mapfiles/kml/pal3/icon46.png"//"images/markers/marker" + locationNumber + ".png";
                var marker = new GMarker(point, icon);
                GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml('<b>' + name + '</b><br>' + street + '<\/a><br />' + address + '<br>' + '<a href="directions.aspx?name=' + name + '&from=' + homeSpatialInfo.fromAddress + '&to=' + point + '">Directions</a>' + ' | <a href="http://www.wellspan.org/body.cfm?id=' + theurl + '">See Details</a><br /><br />' + '<a style="COLOR: #2BA524" href="' + ncrurl + '?streetID=' + ncrentid + '&dbID=' + ncrdbid + '&locID=' + ncrlocid + '" target="_blank">' + ncrlink);
                });
                // The new marker "mouseover" listener        
                // GEvent.addListener(marker, "mouseover", function() {
                // marker.openInfoWindowHtml('<b>' + name + '</b><br>' + street + '<\/a><br />' + address + '<br>' + '<a target="_blank" href="http://maps.google.com/maps?f=q&hl=en&q=from:' + homeSpatialInfo.fromAddress + '+to:' + street + ',' + urladdress + '">Directions</a>' + ' | <a href="http://www.wellspan.org/body.cfm?id=' + theurl + '">See Details</a><br /><br />');
                // }); 
                // ===== save the info we need to use later for the side_bar ====
                gmarkers.push(marker);
                // ===== add a line to the side_bar html ====
                side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length - 1) + ')">' + name + '<\/a><br />' + street + '<\/a><br />' + address + '<br />' + '<a href="directions.aspx?name=' + name + '&from=' + homeSpatialInfo.fromAddress + '&to=' + point + '">Directions</a>' + ' | <a href="http://www.wellspan.org/body.cfm?id=' + theurl + '">See Details</a><br />' + '<a style="COLOR: #2BA524" href="' + ncrurl + '?streetID=' + ncrentid + '&dbID=' + ncrdbid + '&locID=' + ncrlocid + '" target="_blank">' + ncrlink + '<br /><br />';
                return marker;
            }

            // ===== Creates a home marker ====
            function createHomeMarker(point) {
                var icon = new GIcon(baseIcon);
                icon.image = "http://maps.google.com/mapfiles/kml/pal3/icon56.png"; //"images/markers/marker" + locationNumber + ".png";
                var marker = new GMarker(point, icon);
                GEvent.addListener(marker, "click", function() {
                    marker.openInfoWindowHtml('<b>Your Location</b><br /><img src="http://maps.google.com/mapfiles/kml/pal3/icon56.png" />');
                });
                // ===== save the info we need to use later for the side_bar ====
                //                    gmarkers.push(marker);
                return marker;
            }


            // ===== Start with an empty GLatLngBounds object =====
            var bounds = new GLatLngBounds();


            // ===== Load all the markers from the JSON ProximityLocations variable ====
            //var bounds = map.getBounds();
            var southWest = bounds.getSouthWest();
            var northEast = bounds.getNorthEast();
            var lngSpan = northEast.lng() - southWest.lng();
            var latSpan = northEast.lat() - southWest.lat();
            for (var i = 0; i < ProximityLocations.locations.length; i++) {
                var point = new GLatLng(ProximityLocations.locations[i].latitude, ProximityLocations.locations[i].longitude);
                map.addOverlay(createMarker(point, ProximityLocations.locations[i].name, ProximityLocations.locations[i].address, ProximityLocations.locations[i].urladdress, i + 1, ProximityLocations.locations[i].theurl, ProximityLocations.locations[i].street, ProximityLocations.locations[i].ncrLink, ProximityLocations.locations[i].ncrUrl, ProximityLocations.locations[i].ncrEntID, ProximityLocations.locations[i].ncrdbID, ProximityLocations.locations[i].ncrLocID));
                // ==== Each time a point is found, extend the bounds to include it =====
                bounds.extend(point);
            }
            var Homepoint = new GLatLng(homeSpatialInfo.latitude, homeSpatialInfo.longitude);
            map.addOverlay(createHomeMarker(Homepoint));
            bounds.extend(Homepoint);


            // ===== put the assembled side_bar_html contents into the side_bar div ====
            document.getElementById("side_bar").innerHTML = side_bar_html;

            // ===== determine the zoom level from the bounds =====
            map.setZoom(map.getBoundsZoomLevel(bounds));

            // ===== determine the center from the bounds ======
            map.setCenter(bounds.getCenter());

        }
    }
}

//]]>+