﻿/**
 * @author Jim Baltikauskas
 */
//Hold GMap object and its elements
var MapCenterLongitude = -72.670082;
var MapCenterLatitude = 41.760472;
window.GMap = null;
window.GMarkers = new Array();


/**
 * Add Info message html to the Marker
 * @param {GMarker} marker
 */
function AddInfo(marker){
    GEvent.addListener(marker, "click", function(){
        var infowindow = marker.openInfoWindowHtml(marker.openinfowindowhtml, {
            'maxWidth': 200
        });
    });
}

/**
/**
 * This function picks up the click and opens the corresponding info window
 * @param {Object} identificationid
 */
function OnSidePanelClick(identificationid){
    GEvent.trigger(window.GMarkers[identificationid], "click");
}

/**
 *  Show then page is loaded
 */
$(document).ready(function() {

    // Start function when DOM has completely loaded 
    var locations = new Array();
    //debugger
    // Open the xml forthe map
    $.get(selectedXML, {}, function(xml) {

        // Run the function for each tag in the XML file
        $('location', xml).each(function(i) {
            var location = new Object();
            location.identificationid = $(this).find("identificationid").text();
            location.longitude = $(this).find("longitude").text();
            location.latitude = $(this).find("latitude").text();
            location.iconurl = $(this).find("iconurl").text();
            location.openinfowindowhtml = $(this).find("openinfowindowhtml").text();
            locations.push(location);
        });

        if (GBrowserIsCompatible()) {
            try {

                // Create and Center a Map
                var map = new GMap2(document.getElementById("map"));

                map.setCenter(new GLatLng(MapCenterLatitude, MapCenterLongitude), 14);
                map.addControl(new GLargeMapControl());
                map.addControl(new GMapTypeControl());

                window.Tooltip = document.createElement('div');
                window.Tooltip.setAttribute("class", "tooltip");
                map.getPane(G_MAP_FLOAT_PANE).appendChild(window.Tooltip);
                window.Tooltip.style.visibility = "hidden";

                // Process Data File
                var iconSize = 22;

                for (var i = 0; i < locations.length; i++) {

                    var location = locations[i];
                    // Create our "property" marker icon
                    var tinyIcon = new GIcon(G_DEFAULT_ICON);
                    tinyIcon.image = location.iconurl;
                    tinyIcon.iconSize = new GSize(iconSize, iconSize);
					//Get rid of that pesky icon shadow
					tinyIcon.shadowSize = new GSize(0, 0);

                    // Set up our GMarkerOptions object
                    var latlong = new GLatLng(location.latitude, location.longitude);
                    var marker = new GMarker(latlong, {
                        icon: tinyIcon
                    });
                    marker.tooltip = location.tooltiphtml;
                    marker.openinfowindowhtml = location.openinfowindowhtml;
                    AddInfo(marker);
                    map.addOverlay(marker);
                    window.GMarkers[location.identificationid] = marker;

                
                }
               
                // Make Global access if needed
                window.GMap = map;
            }
            catch (e) {
                //debugger;
            }
               
        }
    });
});
