﻿var geocoder;
var map;
var arrMarkers;
var infoWindow;
var marker;
var i = 0;
var tmr;
var mapId = "";
var latlng;
var oReq = null;
var retries = 5;    // number of times to retry getting the geocode for any given location
var arrRetries = new Array();

var RETRY_TIMEOUT = 1500;

function initialize(zoomLevel, namedMapCenter, mapLatitude, mapLongitude) {
    geocoder = new google.maps.Geocoder();

    var latlng;

    if (namedMapCenter.length > 0) {
        codeAddress(namedMapCenter, true, false);
    }
    else {
        latlng = new google.maps.LatLng(mapLatitude, mapLongitude);
    }

    var myOptions = {
        zoom: zoomLevel,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        draggable: true
    }

    map = new google.maps.Map(document.getElementById(mapId == "" ? "map_canvas" : mapId), myOptions);

    //infoWindow = new google.maps.InfoWindow();

    setMarkers();

    google.maps.event.addListener(map, "drag", function()
    {
        if ( infoWindow != null )
        {
            infoWindow.close();
            infoWindow = null;
        }
        else
        {
            var oWindow = document.getElementById("locInfoWindow");
            if ( oWindow != null )
            {
                oWindow.parentNode.removeChild(oWindow);
            }
        }
    }
    );

}

function getContent(contentContainer) {
    var container = document.getElementById(contentContainer);
    return container.innerHTML;
}

function createMarker(latlng, markerTitle, markerIndex, setCenter, containerId, markerImage) {
    var marker = new google.maps.Marker({
        map: map,
        position: latlng,
        title: markerTitle,
        zIndex: markerIndex,
        icon: markerImage
    });

    if (setCenter == true && map != null) {
        map.setCenter(latlng);
    }

    google.maps.event.addListener(marker, "click", function (event) {
        //var iw = new rcInfoWindow(event);
        infoWindow = new rcInfoWindow(event);
        infoWindow.content = getContent(containerId);
        //iw.show();
    });
}

function codeAddress(myAddress, setCenter, createMarker, markerTitle, markerIndex, containerId, markerImage) {
    var address = myAddress;

    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            latlng = results[0].geometry.location;

            addToCache(containerId, latlng);

            if (setCenter) {
                if (map != null) {
                    map.setCenter(latlng);
                }
            }

            if (createMarker) {
                var marker = new google.maps.Marker({
                    map: map,
                    position: latlng,
                    title: markerTitle,
                    zIndex: markerIndex,
                    icon: markerImage
                });

                google.maps.event.addListener(marker, "click", function (event) {

                    //var iw = new rcInfoWindow(event);
                    infoWindow = new rcInfoWindow(event);
                    infoWindow.content = getContent(containerId);
                    //iw.show();

                });

            }
        }
        else {
            var blnRetry = false;
            // try again after a short delay;
            if (arrRetries[containerId] != null) {
                if (arrRetries[containerId] <= retries) {
                    blnRetry = true;
                    arrRetries[containerId]++;
                }
            }
            else {
                blnRetry = true;
                arrRetries[containerId] = 1;
            }

            if (blnRetry) {
                setTimeout("codeAddress('" + address + "', " + setCenter + ", " + createMarker + ", '" + markerTitle + "', " + markerIndex + ", '" + containerId + "', '" + markerImage + "')", RETRY_TIMEOUT);
            }
        }
    });
}

function addToCache(site, newLatLng) {
    // add this lat/lng to the cache
    //var oReq = null;

    if (oReq == null) {

        if (window.XMLHttpRequest) {
            oReq = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            oReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    if (oReq != null) {
        //        var strUrl = location.href;
        // Method page for caching geocodes for sites returned from google maps 
        var strUrl = "/Rockwell/layouts/Geocode Cache.aspx";
        var strCacheString = "geocode=true&type=store&site=" + site + "&lat=" + newLatLng.lat() + "&lng=" + newLatLng.lng();
        if (strUrl.indexOf("?") >= 0) {
            strUrl = strUrl + "&" + strCacheString;
        }
        else {
            strUrl = strUrl + "?" + strCacheString;
        }

        try {
            oReq.open("GET", strUrl, true);
            oReq.onreadystatechange = function () {
                try {
                    if (oReq.readyState == 4 && oReq.status == 200) {
                        //alert("Got it!");

                    }
                }
                catch (ex) {
                    // empty
                }
            }
            oReq.send(null);
        }
        catch (ex) {
            // empty
        }
    }

}

// attach click event to map canvas
try {
        $(document).ready(
        function (e) {
            $("#map_canvas").bind("click", function (evt) {
                if (infoWindow != null) {
                    infoWindow.setPosition(evt.pageX, evt.pageY);
                    infoWindow.show();
                    infoWindow = null;
                }
            }
            );
        }
    );
}
catch (err) {
    //Handle errors here
}

