﻿// ************* Enable layer via URL ***********
// get the layer to turn on
var QueryStringTerms = location.search.length == 0 ? new Array() : location.search.substr(1).split(";");
var layersParam = "";
for (var i = 0; i < QueryStringTerms.length; i++) {
    if (QueryStringTerms[i].indexOf("layer=") == 0)
        layersParam = QueryStringTerms[i].substr(6).toLowerCase();
}
// *********************************************
function MsuMapControls() {
}

MsuMapControls.prototype = new google.maps.LargeMapControl3D();

MsuMapControls.prototype.initialize = function(map) {
    this.DOMElement = google.maps.LargeMapControl3D.prototype.initialize.call(this, map);
    this.map = map;
    if (this.DOMElement.childNodes.length > 0) {
        var Slider = null; // document.getElementById('lmcslider');
        for (var j = 0; j < this.DOMElement.childNodes.length; j++) {
            if (this.DOMElement.childNodes[j].tagName.toLowerCase() == 'div') {
                var flag = (Slider != null);
                Slider = this.DOMElement.childNodes[j];
                if (flag)
                    break;
            }
        }

        for (var i = 1; i < Slider.parentNode.childNodes.length; i++) {
            var Node = Slider.parentNode.childNodes[i];
            Node.style.top = (parseInt(Node.style.top.substring(0, Node.style.top.length - 2), 10) + 30) + "px";
        }
        var Div1 = MsuCreateElement("div", { "style": "overflow: hidden; position: absolute; left: 19px; top: 0px; width: 20px; height: 27px;" });
        Slider.appendChild(Div1);
        var Div2 = MsuCreateElement("div", { "style": "overflow: hidden; width: 20px; height: 27px;" });
        Div1.appendChild(Div2);
        var Img1 = MsuCreateElement("img", { "style": "border: 0px none ; margin: 0px; padding: 0px; position: absolute; left: -17px; top: -432px; width: 59px; height: 492px; cursor: pointer;",
            "src": "http://maps.gstatic.com/intl/en_us/mapfiles/mapcontrols3d5.png"
        });
        Div2.appendChild(Img1);
        var Div3 = MsuCreateElement("div", { "style": "position: absolute; left: 0px; top: 0px; width: 20px; height: 27px; cursor: pointer;",
            "title": "Zoom In", "log": "zi"
        });
        Div1.appendChild(Div3);
        google.maps.Event.addDomListener(Div3, "click", function() { map.zoomIn(); });
    }
    return this.DOMElement;
}


MsuStreetViewControls.prototype = new google.maps.Control();
MsuStreetViewControls.prototype.constructor = MsuStreetViewControls;
function MsuStreetViewControls(container) {
    this.container = container;
}

MsuStreetViewControls.prototype.initialize = function(map) {
    this.PanoClient = new google.maps.StreetviewClient();
    this.map = map;
    var t = this;
    var Container = MsuCreateElement("div", { "id": "cbctl", "style": " z-index: 10000;" });
    var LPDiv = MsuCreateElement("div", { "id": "cb_launchpad", "style": "position: absolute; left: 0px; top: 0px; z-index: 1;" });
    var SVDiv2 = MsuCreateElement("div", { "style": "overflow: hidden; width: 30px; height: 32px; position: absolute; left: 21px; top: 70px; display: none;" });
    SVDiv2.appendChild(MsuCreateElement("img", { "style": "border: 0px none; margin: 0px; padding: 0px; position: absolute; left: -30px; top: -460px; width: 59px; height: 492px;",
        "src": "http://maps.gstatic.com/intl/en_us/mapfiles/cb/mod_cb_scout/cb_scout_sprite_003.png"
    }));
    Container.appendChild(SVDiv2);

    var LPEmptyDiv = MsuCreateElement("div", { "style": "position: absolute; left: 0px; top: 0px; z-index: 2; width: 30px; height: 32px;" });
    var LPEmptyImgDiv = MsuCreateElement("div", { "style": "overflow: hidden; width: 30px; height: 32px; position: absolute; left: 14px; top: 63px;", "id": "launchpad_empty" });
    LPEmptyDiv.appendChild(LPEmptyImgDiv);
    LPEmptyImgDiv.appendChild(MsuCreateElement("img", { "style": "border: 0px none; margin: 0px; padding: 0px; position: absolute; left: -98px; top: -849px; width: 147px; height: 935px;",
        "src": "http://maps.gstatic.com/intl/en_us/mapfiles/cb/mod_cb_scout/cb_scout_sprite_003.png"
    }));
    Container.appendChild(LPEmptyDiv);

    var SubContainer = MsuCreateElement("div", { "style": "z-index: 1000; position: absolute; left: 21px; top: 70px;" });
    SubContainer.appendChild(MsuCreateElement("div", { "style": "width: 16px; height: 52px; position: absolute; left: -10px; top: -10px;" }));
    SubContainer.appendChild(MsuCreateElement("div", { "style": "width: 16px; height: 52px; position: absolute; left: 25px; top: -10px;" }));
    SubContainer.appendChild(MsuCreateElement("div", { "style": "width: 19px; height: 10px; position: absolute; left: 6px; top: -10px;" }));
    var ZoomDiv = MsuCreateElement("div", { "style": "position: absolute; left: 6px; top: 26px; width: 19px; height: 20px; cursor: pointer; overflow: hidden;", "title": "Zoom In", "log": "zi" });
    var ZoomDivImg = MsuCreateElement("img", { "style": "border: 0px none; margin: 0px; padding: 0px; position: absolute; left: -100px; top: -250px; width: 147px; height: 935px;",
        "src": "http://maps.gstatic.com/intl/en_us/mapfiles/cb/mod_cb_scout/cb_scout_sprite_003.png"
    });
    ZoomDiv.appendChild(ZoomDivImg);
    SubContainer.appendChild(ZoomDiv);
    Container.appendChild(SubContainer);

    var zoomInStreetView = function() {
        if (map.getZoom() < 19)
            map.zoomIn();
        else {
            t.PanoClient.getNearestPanorama(map.getCenter(),
                function(panoData) {
                    if (panoData == null || panoData.location == null || panoData.code != 200)
                        return;
                    t._PanoObject = new google.maps.StreetviewPanorama(t.container, { latlng: panoData.location.latlng });
                    t.container.style.display = "block";
                    var CloseFrame = MsuCreateElement("div", { "class": "closeFrame" });
                    t.container.parentNode.appendChild(CloseFrame);
                    var CloseDivListener = null;
                    CloseDivListener = google.maps.Event.addDomListener(CloseFrame, "click", function() {
                        t.container.style.display = "none";
                        CloseFrame.style.display = "none";
                        t._PanoObject.remove();
                        t._PanoObject = null;
                        google.maps.Event.removeListener(CloseDivListener);
                        t.container.parentNode.removeChild(CloseFrame);
                    });
                });
        }
    }
    google.maps.Event.addDomListener(ZoomDiv, "click", zoomInStreetView);
    SubContainer = MsuCreateElement("div", { "style": "position: absolute; left: 5px; top: 53px; z-index: 3;", "class": "gmnoprint" });

    var SVLockingDiv = MsuCreateElement("div", { "id": "cb_locking", "style": "position: absolute; display: none; z-index: 10002;" });
    var SVLockingImgDiv = MsuCreateElement("div", { "style": "position: absolute; left: 10px; top: 40px; z-index: 200;" });
    SVLockingImgDiv.appendChild(MsuCreateElement("img", { "src": "http://maps.gstatic.com/intl/en_us/mapfiles/cb/target_locking.gif" }));
    SVLockingDiv.appendChild(SVLockingImgDiv);

    var SVCircleDiv = MsuCreateElement("div", { "style": "overflow: hidden; width: 49px; height: 52px; position: absolute; left: 10px; top: 40px; z-index: 100;" });
    var SVCircleImgDiv = MsuCreateElement("div", { "style": "overflow: hidden; width: 49px; height: 52px;" });
    SVCircleImgDiv.appendChild(MsuCreateElement("img", { "style": "border: 0px none; margin: 0px; padding: 0px; position: absolute; left: 0px; top: -184px; width: 147px; height: 935px;", "src": "http://maps.gstatic.com/intl/en_us/mapfiles/cb/mod_cb_scout/cb_scout_sprite_003.png" }));
    SVCircleDiv.appendChild(SVCircleImgDiv);
    SVLockingDiv.appendChild(SVCircleDiv);
    SubContainer.appendChild(SVLockingDiv);

    var SVControlDiv = MsuCreateElement("div", { "style": "overflow: hidden; width: 49px; height: 52px; position: absolute;" });
    var SVScountDiv = MsuCreateElement("div", { "style": "overflow: hidden; width: 49px; height: 52px; z-index: 10001;", "id": "cb_scout" });
    var SVScoutImg = MsuCreateElement("img", { "style": "border: 0px none; margin: 0px; padding: 0px; position: absolute; left: -49px; top: -34px; width: 147px; height: 935px;", "src": "http://maps.gstatic.com/intl/en_us/mapfiles/cb/mod_cb_scout/cb_scout_sprite_003.png" });
    var DragObject = new google.maps.DraggableObject(SubContainer, { container: this.map.getContainer(), draggableCursor: "default", draggingCursor: 'url(http://maps.gstatic.com/intl/en_us/mapfiles/closedhand_8_8.cur)', delayDrag: true });
    DragObject.dragging = false;

    google.maps.Event.addDomListener(SVScoutImg, "mouseout", function() { if (!DragObject.dragging) this.style.top = "-34px"; });
    google.maps.Event.addDomListener(SVScoutImg, "mouseover", function() { if (!DragObject.dragging) this.style.top = "-313px"; });

    google.maps.Event.addListener(DragObject, "dragstart", function(evt) {
        this.clientX = evt.clientX;
        this.clientY = evt.clientY;
        this.dragging = true;
        SVLockingDiv.style.display = "";
        SVScoutImg.style.left = "-49px";
        SVScoutImg.style.top = "-797px";
        t.StreetviewOverlay = new google.maps.StreetviewOverlay();
        map.addOverlay(t.StreetviewOverlay);
    });

    google.maps.Event.addListener(DragObject, "dragend", function(evt) {
        SVScoutImg.style.left = "-49px";
        SVScoutImg.style.top = "-34px";
        t.PanoClient.getNearestPanorama(map.fromContainerPixelToLatLng(new google.maps.Point(
                      parseInt(SubContainer.style.left.substring(0, SubContainer.style.left.length - 2), 10) + 38,
                      parseInt(SubContainer.style.top.substring(0, SubContainer.style.top.length - 2), 10) + 61)),
                      function(panoData) {
                          if (panoData == null || panoData.location == null || panoData.code != 200) {
                              return;
                          }
                          t._PanoObject = new google.maps.StreetviewPanorama(t.container, { latlng: panoData.location.latlng });
                          t.container.style.display = "block";
                          var CloseFrame = MsuCreateElement("div", { "class": "closeFrame" });
                          t.container.parentNode.appendChild(CloseFrame);
                          var CloseDivListener = null;
                          CloseDivListener = google.maps.Event.addDomListener(CloseFrame, "click", function() {
                              t.container.style.display = "none";
                              CloseFrame.style.display = "none";
                              t._PanoObject.remove();
                              t._PanoObject = null;
                              google.maps.Event.removeListener(CloseDivListener);
                              t.container.parentNode.removeChild(CloseFrame);
                          });
                      });
        SVLockingDiv.style.display = "none";
        this.moveTo(new google.maps.Point(5, 53));
        this.dragging = false;
        map.removeOverlay(t.StreetviewOverlay);
        t.StreetviewOverlay = null;
    });
    google.maps.Event.addListener(DragObject, "drag", function(evt) {
        if (evt.clientX > this.clientX) {
            SVScoutImg.style.left = "-49px";
            SVScoutImg.style.top = "-797px";
        }
        else {
            SVScoutImg.style.left = "0";
            SVScoutImg.style.top = "-313px";
        }
        this.clientX = evt.clientX;
    });

    SVScountDiv.appendChild(SVScoutImg);
    SVControlDiv.appendChild(SVScountDiv);
    SubContainer.appendChild(SVControlDiv);

    Container.appendChild(SubContainer);

    map.getContainer().appendChild(Container);
    return Container;
}

MsuStreetViewControls.prototype.printable = function() { return false; }
MsuStreetViewControls.prototype.selectable = function() { return false; }

MsuStreetViewControls.prototype.getDefaultPosition = function() {
    return new google.maps.ControlPosition(google.maps.ANCHOR_TOP_LEFT, new google.maps.Size(0, 0));
}

window.currentPin = null;
if (google.maps.BrowserIsCompatible()) {
    /*
    Web and New Media Office
    -Matt Seigel (Pages,Documentation, and some PS Manipulations)
    -Nate Bundy (PS Manipulations, Tilecutter, and some Documentation)
    */

    // *********************** USED FOR ALL OVERLAYS  *********************** //
    // Create the map (the center is set to Carrington Hall on Missouri State University campus in Springfield, MO)
    var map = new google.maps.Map2(document.getElementById("gmap"));
    map.setCenter(new google.maps.LatLng(37.20100515755486, -93.28124284744263), 16);

    // Throw a couple of controls on the map to switch between map types as well as change the zoom
    if (window.UseDefaultMapControls)
        map.addControl(new google.maps.LargeMapControl3D());
    else {
        map.addControl(new MsuMapControls());
        map.addControl(new MsuStreetViewControls(document.getElementById('gmapPano')));
    }
    map.addControl(new google.maps.HierarchicalMapTypeControl());
    map.enableScrollWheelZoom();

    // Create Copyright
    var copyright = new google.maps.Copyright(1,
                                                          new google.maps.LatLngBounds(new google.maps.LatLng(-90, -180),
                                                          new google.maps.LatLng(90, 180)),
                                                          0,
                                                          "©2008 Missouri State University");
    // Create Copyright Collection
    var copyrightCollection = new google.maps.CopyrightCollection("© Missouri State University");
    // Add our copyright information to Collection
    copyrightCollection.addCopyright(copyright);
    // ********************************************************************* //

    // *************************** BASE OVERLAY **************************** //
    // Create a google.maps.TileLayer for the base map
    var basetilelayer = new google.maps.TileLayer(copyrightCollection, 12, 18);
    // Custom tile url for the map for Overlay
    basetilelayer.getTileUrl = function(tile, zoom) { return "tilesets/baselayer/" + zoom + "_" + tile.x + "_" + tile.y + ".png"; };
    // Specify you are using png tile images for Overlay (without png there would not be any transparency in the image)
    basetilelayer.isPng = function() { return true; };
    // Create a new Overlay Tile Layer (google.maps.TileLayerOverlay) for the Overlay
    var baseOverlay = new google.maps.TileLayerOverlay(basetilelayer);
    // Add the overlay to the google map
    map.addOverlay(baseOverlay);
    var BaseOverlayAdded = true;
    google.maps.Event.addListener(map, "maptypechanged", function() {
        if (this.getCurrentMapType() == google.maps.SATELLITE_MAP) {
            map.removeOverlay(baseOverlay);
            BaseOverlayAdded = false;
        }
        else if (!BaseOverlayAdded) {
            map.addOverlay(baseOverlay);
            BaseOverlayAdded = true;
        }
    });

    // ********************************************************************* //
    // Create our shuttle stop marker icon
    var shuttleIcon = new google.maps.Icon();
    shuttleIcon.image = "/images/ShuttleStopMarker.png";
    shuttleIcon.shadow = "/images/ShuttleStopMarkerShadow.png";
    shuttleIcon.iconSize = new GSize(32, 32);
    shuttleIcon.shadowSize = new GSize(40, 32);
    shuttleIcon.iconAnchor = new GPoint(16, 16);
    shuttleIcon.infoWindowAnchor = new GPoint(16, 0);

    // ************************** LOCATION LINKS *************************** //
    // Normal MarkerManager
    var mgr = new MarkerManager(map);
    var shuttleStopManager = new MarkerManager(map);

    // On search result load add their markers
    window.onLoadAddMarkers = function(Id, Letter, Name, ShortDesc, Lat, Lon, Address, ImageLoc) {
        // Theres are the values retrieved from the DB for map bubble information
        var point = new google.maps.LatLng(Lat, Lon);

        // Make the alphabetical marker icon
        var baseIcon = new google.maps.Icon(google.maps.DEFAULT_ICON);
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new google.maps.Size(20, 34);
        baseIcon.shadowSize = new google.maps.Size(37, 34);
        baseIcon.iconAnchor = new google.maps.Point(9, 34);
        baseIcon.infoWindowAnchor = new google.maps.Point(9, 2);
        var letteredIcon = new google.maps.Icon(baseIcon);
        letteredIcon.image = "http://www.google.com/mapfiles/marker" + Letter + ".png";
        markerOptions = { icon: letteredIcon };
        var marker = new google.maps.Marker(point, markerOptions);
        // Html that goes in the marker's bubble
        var windowHtml = '<div class="gMarker" >';
        windowHtml += '<p class="Title">' + UnescapeHtml(Name) + '</p>';
        windowHtml += '<a id="MoreInfo" href="BldgTemplate.asp?b=' + Id + '">More Info</a>';
        // !!!! Deal with parking lots not having images
        windowHtml += '<div class="AddressAndImage">';
        if (ImageLoc != '') {
            windowHtml += '<img src="http://www.missouristate.edu/graphics/buildings/' + ImageLoc + '"\>';
        }
        windowHtml += UnescapeHtml(Address) + '<br /><br />';
        windowHtml += '</div>';
        windowHtml += ShortDesc;
        windowHtml += '<div class="Clear"></div><br />';
        windowHtml += '<a href="http://maps.google.com/maps?f=d&hl=en&geocode=&daddr=';
        windowHtml += Address.replace(/<br \/>/, " ");
        windowHtml += '&z=15">Get Directions</a>';
        windowHtml += '</div>';
        // Click event
        google.maps.Event.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(windowHtml, { maxWidth: 300 });
        });
        // Add the marker to the marker manager
        mgr.addMarker(marker, 6);
    }

    // Handles clicks of search results
    window.showSearchLocation = function(lat, lng) {
        var marker = mgr.getMarker(lat, lng, map.getZoom());
        map.setCenter(new google.maps.LatLng(lat, lng));
        google.maps.Event.trigger(marker, "click");
    }

    // Refreshs the marker manager after they're all added with onLoadAddMarkers
    window.finishLoadRefresh = function() {
        mgr.refresh();
    }

    // Clear all markers in the marker manager
    window.clearAllMarkers = function() {
        mgr.clearMarkers();
        mgr.refresh();
    }
    // ********************************************************************* //

    // ************************** OVERLAY LINKS **************************** //
    // Holds the overlay objects
    var gOverlays = [];
    // Holds the html for the list of overlays
    var htmlOverlays = "<form name=\"overlayForm\">";
    // Holds whether the layer is on or off
    // 0 is off 1 is on
    var oStatus = [];

    function createOverlay(name, tileBase, opacity, callback, defaultChecked) {
        var linkId = "overlay" + (gOverlays.length);
        var tileLayer = new google.maps.TileLayer(copyrightCollection, 12, 18);
        tileLayer.getTileUrl = function(tile, zoom) { return tileBase + zoom + "_" + tile.x + "_" + tile.y + ".png"; };
        tileLayer.isPng = function() { return true; };
        //tileLayer.getOpacity = function() { return opacity; };
        var overlay = new google.maps.TileLayerOverlay(tileLayer);
        oStatus.push(0);
        gOverlays.push(overlay);
        htmlOverlays += '<div id="div' + linkId + '"' + (defaultChecked ? ' style="background-color:#D3E4E5;"' : '') + '>';
        htmlOverlays += '<input type="checkbox" id="' + linkId + '" name="' + linkId + '" onClick=oClicked(' + (gOverlays.length - 1) + ')' + (callback ? ';' + callback + '();' : '') + (defaultChecked ? ' checked="checked"' : '') + ' />';
        htmlOverlays += '<label for="' + linkId + '">' + name + '</label><br><\/div>';
        return overlay;
    }

    // Called when an overlay link is clicked
    function oClicked(i) {
        var divId = "divoverlay" + i;
        var linkId = "overlay" + i;
        // Check the status of the overlay
        if (oStatus[i] == 0) {
            map.addOverlay(gOverlays[i]);
            if (document.getElementById(divId))
                document.getElementById(divId).style.background = "#D3E4E5";
            // Set the layer status to on
            oStatus[i] = 1;
        } else if (oStatus[i] == 1) {
            map.removeOverlay(gOverlays[i]);
            if (document.getElementById(divId))
                document.getElementById(divId).style.background = "#FFFFFF";
            // Set the layer status to off
            oStatus[i] = 0;
        }

    }

    function toggleShuttleStopMarkers() {
        if (shuttleStopManager.getMarkerCount(14) > 0) {
            shuttleStopManager.clearMarkers();
            shuttleStopManager.refresh();
        }
        else {
            for (var i = 0; i < shuttleStops.length; i++) {
                var stopMarker = new google.maps.Marker(new google.maps.LatLng(shuttleStops[i].CLat, shuttleStops[i].CLng), { icon: shuttleIcon });
                stopMarker.Name = shuttleStops[i].Name;
                stopMarker.ShortDesc = shuttleStops[i].ShortDesc;
                google.maps.Event.addListener(stopMarker, "click", function() {
                    this.openInfoWindowHtml('<div class="gMarker" ><p class="Title">' + this.Name + '</p>' + this.ShortDesc + '<div class="Clear"></div></div>', { maxWidth: 300 });
                });
                // Add the marker to the marker manager
                shuttleStopManager.addMarker(stopMarker, 14);
            }
        }
    }

    var parkingPolygonsShown = false;
    function toggleParkingPolygons() {
        if (parkingPolygonsShown) {
            for (var i = 0; i < parkingPolygons.length; i++)
                parkingPolygons[i].hide();

            parkingPolygonsShown = false;
        }
        else {
            for (var i = 0; i < parkingPolygons.length; i++)
                parkingPolygons[i].show();
            parkingPolygonsShown = true;
        }
    }

    // Add a parking lot overlay
    createOverlay("Parking Lots", "tilesets/parkinglot/", 1, "toggleParkingPolygons", layersParam.indexOf("parking") >= 0);
    if (layersParam.indexOf("parking") >= 0) {
        setTimeout("toggleParkingPolygons()", 1000);
        oClicked(0);
    }

    createOverlay("Shuttle Routes", "tilesets/shuttle/", 1, "toggleShuttleStopMarkers", layersParam.indexOf("shuttle") >= 0);
    if (layersParam.indexOf("shuttle") >= 0) {
        setTimeout("toggleShuttleStopMarkers()", 1000);
        oClicked(1);
    }
    createOverlay("Bikeways", "tilesets/bikeways/", 1, false);

    // Display the list of overlays to turn on and off
    htmlOverlays += "</form>";
    document.getElementById("gOverlays").innerHTML += htmlOverlays;
    // ********************************************************************* //

    // ************************** POLYGON CLICK **************************** //
    var lastPolyMarker = -1;

    // This function is activated when you click on a polygon/location
    window.showPolyLocation = function() {
        // If the info window belonged to a polygon, delete its marker
        if (lastPolyMarker) {
            map.removeOverlay(lastPolyMarker);
            lastPolyMarker = -1;
        }
        // Make the marker
        var point = new google.maps.LatLng(this.clat, this.clon);
        var marker = new google.maps.Marker(point);
        // Html that goes in the bubble
        var windowHtml = '<div class="gMarker" >';
        windowHtml += '<p class="Title">' + this.name + '</p>';
        windowHtml += '<a id="MoreInfo" href="BldgTemplate.asp?b=' + this.locationid + '">More Info</a>';
        windowHtml += '<div class="AddressAndImage">';
        if (this.imageloc != '') {
            windowHtml += '<img src="http://www.missouristate.edu/graphics/buildings/' + this.imageloc + '"\>';
        }
        windowHtml += this.address + '<br /><br />';
        windowHtml += '</div>';
        windowHtml += this.shortdesc;
        windowHtml += '<div class="Clear"></div><br />';
        windowHtml += '<a href="http://maps.google.com/maps?f=d&hl=en&geocode=&daddr=';
        windowHtml += this.address.replace(/<br \/>/, " ");
        windowHtml += '&z=15">Get Directions</a>';
        windowHtml += '</div>';
        // Add click event to marker
        google.maps.Event.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(windowHtml, { maxWidth: 300 });
        });
        // Add it to the map
        map.addOverlay(marker);
        // Set it as the last clicked poly
        lastPolyMarker = marker;
        // Open the markers info window
        marker.openInfoWindow(windowHtml, { maxWidth: 300 });
    };


    for (i = 0; i < buildings.length; i++) {
        var NewBuilding = buildings[i];
        if (NewBuilding.Name) {
            var points = [new google.maps.LatLng(NewBuilding.NLat, NewBuilding.WLng),
                                new google.maps.LatLng(NewBuilding.NLat, NewBuilding.ELng),
                                new google.maps.LatLng(NewBuilding.SLat, NewBuilding.ELng),
                                new google.maps.LatLng(NewBuilding.SLat, NewBuilding.WLng),
                                new google.maps.LatLng(NewBuilding.NLat, NewBuilding.WLng)];
            var polygon = new google.maps.Polygon(points, "#6699FF", 0, 1, "#66CCFF", 0);
            polygon.locationid = NewBuilding.id;
            polygon.name = NewBuilding.Name;
            polygon.shortdesc = NewBuilding.ShortDesc;
            polygon.clat = NewBuilding.CLat;
            polygon.clon = NewBuilding.CLng;
            polygon.address = NewBuilding.Address;
            polygon.imageloc = NewBuilding.ImageLoc;
            polygon.letter = -1;
            google.maps.Event.bind(polygon, "click", polygon, showPolyLocation);
            map.addOverlay(polygon);
        }
    }

    for (i = 0; i < parking.length; i++) {
        var NewLot = parking[i];
        if (NewLot.Name) {
            var points = [new google.maps.LatLng(NewLot.NLat, NewLot.WLng),
                                new google.maps.LatLng(NewLot.NLat, NewLot.ELng),
                                new google.maps.LatLng(NewLot.SLat, NewLot.ELng),
                                new google.maps.LatLng(NewLot.SLat, NewLot.WLng),
                                new google.maps.LatLng(NewLot.NLat, NewLot.WLng)];
            var polygon = new google.maps.Polygon(points, "#6699FF", 0, 1, "#66CCFF", 0);
            polygon.locationid = NewLot.id;
            polygon.name = NewLot.Name;
            polygon.shortdesc = NewLot.ShortDesc;
            polygon.clat = NewLot.CLat;
            polygon.clon = NewLot.CLng;
            polygon.address = NewLot.Address;
            polygon.imageloc = NewLot.ImageLoc;
            polygon.letter = -1;
            polygon.hide();
            google.maps.Event.bind(polygon, "click", polygon, showPolyLocation);
            parkingPolygons.push(polygon);
            map.addOverlay(polygon);
        }
    }
    // ********************************************************************* //

    // *************************** URL LOCATION **************************** //
    function showLocationURL(name, id, shortdesc, clat, clng, physicalAddress, image) {
        var marker = new google.maps.Marker(new google.maps.LatLng(clat, clng));
        var windowHtml = '<div class="gMarker" >';
        windowHtml += '<p class="Title">' + name + '</p>';
        windowHtml += '<a id="MoreInfo" href="BldgTemplate.asp?b=' + id + '">More Info</a>';
        windowHtml += '<div class="AddressAndImage">';
        if (image != '') {
            windowHtml += '<img src="http://www.missouristate.edu/graphics/buildings/' + image + '"\>';
        }
        windowHtml += physicalAddress + '<br /><br />';
        windowHtml += '</div>';
        windowHtml += shortdesc;
        windowHtml += '<div class="Clear"></div><br />';
        windowHtml += '<div id="streetview"></div><br />';
        windowHtml += '<a href="http://maps.google.com/maps?f=d&hl=en&geocode=&daddr=';
        windowHtml += physicalAddress.replace(/<br \/>/, " ");
        windowHtml += '&z=15">Get Directions</a>';
        windowHtml += '</div>';
        google.maps.Event.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(windowHtml, { maxWidth: 300 });
        });
        map.addOverlay(marker);
        lastPolyMarker = marker;
        google.maps.Event.trigger(marker, "click");
    }
    // ********************************************************************* //

}

function UnescapeHtml(str) {
    return str.replace(/&gt;/g, '>').replace(/&lt;/g, '<');
}

var MenuHover = false;
function MenuOver(evt) {
    document.getElementById("PrintableMenu").style.display = "block";
    MenuHover = true;
}

function MenuOut(evt) {
    MenuHover = false;
    setTimeout("HideMenu()", 50);
}

function HideMenu() {
    if (MenuHover)
        return;
    document.getElementById('PrintableMenu').style.display = 'none';
}

google.maps.Event.addDomListener(document.getElementById("MenuHeader"), "mouseover", MenuOver);
google.maps.Event.addDomListener(document.getElementById("PrintableMenu"), "mouseover", MenuOver);
google.maps.Event.addDomListener(document.getElementById("MenuHeader"), "mouseout", MenuOut);
google.maps.Event.addDomListener(document.getElementById("PrintableMenu"), "mouseout", MenuOut);