﻿// JavaScript Document
// global arrays to hold copies of the markers used by the side_bar

var gmarkers = [];

// global "map" variable
var map;

// This function picks up the click and opens the corresponding info window
function myclick(i) {
  GEvent.trigger(gmarkers[i], "click");
}
  
function onLoad(center_lat, center_lng, xml_path) {
    if (GBrowserIsCompatible()) {
        // this variable will collect the html which will eventualkly be placed in the side_bar
        var mapInfo_html = "";

        // A function to create the marker and set up the event window
        function createMarker(point,name,address,phone,delphis,html,isMN) {
            var marker = new GMarker(point);
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(html);
            });
            
            // save the info we need to use later for the side_bar
            gmarkers.push(marker);
            
            if(isMN) {
                phone = GetPhoneText("(NNN) NNN-NNNN");
            }
            
            // add a line to the side_bar html
            mapInfo_html += '<div id="listing_content"><div id="listing"><p class="address">' + address + '</p><p class="phone">' + phone + '</p><p class="moreInfo" style="padding-top:5px;"><a href="javascript:myclick(' + (gmarkers.length-1) + ')">More info</a></p></div><div id="listing_button"><a href="' + delphis + '"><img src="images/btn_order_here.gif" border="0" alt="Order Here" class="btn_order_here" /></a></div></div>';
            // ' + '<p>' + phone + '</p> <p class="moreInfo"><a href="javascript:myclick(' + (gmarkers.length-1) + ')">More info</p></div>
            return marker;
        }

        // create the map
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng( center_lat,center_lng), 7);

        // Read the data from example.xml
        GDownloadUrl(xml_path, function(doc) {
            var xmlDoc = GXml.parse(doc);
            var markers = xmlDoc.documentElement.getElementsByTagName("marker");
          
            for (var i = 0; i < markers.length; i++) {
                // obtain the attribues of each marker
                var lat = parseFloat(markers[i].getAttribute("lat"));
                var lng = parseFloat(markers[i].getAttribute("lng"));
                var point = new GLatLng(lat,lng);
                var label = markers[i].getAttribute("label");
        	    var address = markers[i].getAttribute("address");
	            var phone = markers[i].getAttribute("telephone");
	            var delphis = markers[i].getAttribute("delphis");
	            var html = '<div id="infoBox"><p class="listingInfo">' + address + '</p><p class="infoBoxLink"><a href="' + delphis + '">Order Here</a></p></div>';
                // create the marker                                
                var isMN = false;            
                if(xml_path.indexOf('MN.xml') > -1 || xml_path.indexOf('Minneapolis.xml') > -1) {
                    isMN = true;
                }
                var marker = createMarker(point,label,address,phone,delphis,html,isMN);
                
                map.addOverlay(marker);
            }
            // put the assembled side_bar_html contents into the side_bar div
            document.getElementById("mapInfo").innerHTML = mapInfo_html;
        });

    } else {
        alert("Sorry, the Google Maps API is not compatible with this browser");
    }
} // end of onLoad function

// This Javascript is based on code provided by the
// Blackpool Community Church Javascript Team
// http://www.commchurch.freeserve.co.uk/   
// http://econym.googlepages.com/index.htm


   