﻿var map, cluster, selectedLocation;

if(window.addEventListener)
{
    window.addEventListener( "load", myOnload, false );
    //window.addEventListener( "unload", GUnload, false );
} 
else 
{
    window.attachEvent( "onload", myOnload );
    //window.attachEvent( "onunload", GUnload );
}        
    
function myOnload() 
{
    if (GBrowserIsCompatible()) 
    {
        $.ajax({
            type: "POST",
            url: "http://"+Environment+"/ETS.UI/WebServices/LocationService.asmx/GetMappingLocations",
            contentType: "application/json; charset=utf-8",
            data: jsonText = JSON.stringify({ portalId: PortalId }),
            async: true, 
            dataType: "json",
            success: function(response) 
            { 
                var json = eval(response.d);
                loadMap(json);
            }
        });
    }
}

function loadMap(json) 
{
        map=new GMap2(document.getElementById('map'));
        map.setCenter(new GLatLng(StartingLatitude, StartingLongitude), StartingZoom);
        G_PHYSICAL_MAP.getMinimumResolution    = function () { return 2 };
        G_NORMAL_MAP.getMinimumResolution    = function () { return 2 };
        G_SATELLITE_MAP.getMinimumResolution    = function () { return 2 };
        G_HYBRID_MAP.getMinimumResolution    = function () { return 2 }; 
        map.setUIToDefault();

        //added by Saran on 08/15/2011 for icon Changes
      /*  var iconOptions = {};
        iconOptions.width = 20;
        iconOptions.height = 20;
        iconOptions.primaryColor = "#FF0000";
        iconOptions.shape = "circle";
        var normalIcon = MapIconMaker.createFlatIcon(iconOptions);*/


        //below line modified by Saran on 08/15/2011
        //var marker, markersArray = [];

        //following lines un commented by Saran on 08/15/2011
        var marker, markersArray=[], normalIcon=new GIcon();
        
		
        normalIcon.image= ConentDomain+'GoogleMapPin.png';
        normalIcon.shadow= ConentDomain+'GoogleMapPinShadow.png';
        normalIcon.iconSize=new GSize(22,23);
        normalIcon.shadowsize=new GSize(40,40);
        normalIcon.iconAnchor=new GPoint(11,23);
        normalIcon.infoWindowAnchor=new GPoint(11,11);
		
        for (var i=0; i<json.length; i++) 
        {
            marker=newMarker(new GLatLng(json[i].Latitude, json[i].Longitude), normalIcon, json[i]);
            markersArray.push(marker);
        }

        //below lines commented by Saran


        /*function myClusterClick(args) 
        {
            var html = '';
    	    
            cluster.defaultClickAction=function()
            {
                map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds))
                delete cluster.defaultClickAction;
            }

            var msg, list, jsonText;
            list = [];   
            
            for (i=0; i<args.clusteredMarkers.length; i++) {

                if (args.clusteredMarkers[i].$ != undefined) {
                    if (args.clusteredMarkers[i].$ != undefined) {
                        list[i] = args.clusteredMarkers[i].$.value;
                    } //if (args.clusteredMarkers[i]] != undefined) {
                } //if (args.clusteredMarkers[i].$] != undefined) {

                if (args.clusteredMarkers[i].ca != undefined) {
                    if (args.clusteredMarkers[i].ca.value != undefined) {
                        list[i] = args.clusteredMarkers[i].ca.value;
                    } //if (args.clusteredMarkers[i].ca.value] != undefined) {
                } //if (args.clusteredMarkers[i].ca] != undefined) {

                if (args.clusteredMarkers[i].aa != undefined) {
                    if (args.clusteredMarkers[i].aa.value != undefined) {
                        list[i] = args.clusteredMarkers[i].aa.value;
                    } //if (args.clusteredMarkers[i].ca.value] != undefined) {
                } //if (args.clusteredMarkers[i].ca] != undefined) {

                if (args.clusteredMarkers[i].da != undefined) {
                    if (args.clusteredMarkers[i].da.value != undefined) {
                        list[i] = args.clusteredMarkers[i].da.value;
                    } //if (args.clusteredMarkers[i].da.value] != undefined) {
                } //if (args.clusteredMarkers[i].da] != undefined) {
            }
            

            
            jsonText = JSON.stringify({ componentIdCollection: list });

            $.ajax({
                type: "POST",
                url: "http://" + Environment + "/ETS.UI/WebServices/LocationService.asmx/GetMappingLocationDetails",
                contentType: "application/json; charset=utf-8",
                data: jsonText,
                async: true,
                dataType: "json",
                success: function (response) {
                    var html = '<div class="mappingParentDiv"><ul class="mappingUl">';
                    var json = eval(response.d);

                    for (i = 0; i < json.length; i++) {
                        html += bubbleText(json[i]);
                    }

                    html += '</ul></div>';
                  map.openInfoWindowHtml(args.clusterMarker.getLatLng(), html);
                }
            });
        }*/

        // cluster = new ClusterMarker(map, { markers: markersArray, clusterMarkerIcon: normalIcon, clusterMarkerClick: myClusterClick });
        cluster = new ClusterMarker(map, { markers: markersArray });
        //added by Saran
        //cluster.fitMapToMarkers();

        //below line commented by Saran
        cluster.intersectPadding = -1;

        map.savePosition();

        cluster.clusteringEnabled = true;
        cluster.refresh(true); //	true required to force a full update of the markers - otherwise the update would occur next time that the map is zoomed or the active markers change

        //below lines added by Saran
        //	add an HtmlControl to enable toggling of the ClusterMarker cluster function
        
      /*  var html = '<div class="htmlControl" style="padding:0px 3px 3px 3px">Enable clustering: <input type="checkbox" checked="checked" onclick="toggleClustering()" /></div>';
        var control = new HtmlControl(html);
        map.addControl(control, new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7))); */

        //below line commented by saran
        //cluster.refresh();
    }

    //below function added by Saran
  /*  function toggleClustering() {
        cluster.clusteringEnabled = !cluster.clusteringEnabled;
        cluster.refresh(true); //	true required to force a full update of the markers - otherwise the update would occur next time that the map is zoomed or the active markers change
    }*/

function bubbleText(location)
{
   var html = '<li class="mappingLi">'
   html += '<h1 class="mappingH1"><a href="'+location.Website+'">'+location.LocationName+'</a></h1>';
   
   if(map.getZoom() < 19)
   {
    html += '<a href="javascript:map.setCenter(new GLatLng('+location.Latitude+','+location.Longitude+'),'+getZoom()+');map.closeInfoWindow();" title="Zoom to Location" class="ZoomBtn">&nbsp;</a>';
   }
   
   html += '<p class="mappingP">'+location.StreetAddress+'</p>';
   html += '<p class="mappingP">'+location.City+', '+location.State+' '+location.PostalCode+'</p>'
   html += '<p class="mappingP">Toll Free: '+location.TollFree+'</p>'
   html += '<p class="mappingP">Phone: '+location.Phone+'</p>'
   html += '<p class="mappingP">Fax: '+location.Fax+'</p>'
   html += '<p class="mappingP">WebSite: <a href="'+location.Website+'">'+location.Website+'</a></p>';
   html += '<p class="mappingP"><br /></p>'
   html += '<p class="mappingP">'+location.HoursOfOperation+'</p>'
   html += '<div class="mappingDivEnd">&nbsp;</div>'

   return html;
}

function getZoom()
{
    var zoomLevel = 0;
    
    if(map.getZoom() < 15)
    {
        zoomLevel = 15;
    }
    else if(map.getZoom() == 19)
    {
        zoomLevel = 19;
    }
    else
    {
        zoomLevel = (map.getZoom()+1); 
    }
    
    return zoomLevel;
}

function newMarker(markerLocation, icon, location) 
{
   var marker=new GMarker(markerLocation, {icon:icon, title:'EagleRider '+location.LocationName, value:location.ComponentId});
   GEvent.addListener(marker, 'click', function () {
       var html = '<div class="mappingParentDiv"><ul class="mappingUl">'
       html += bubbleText(location);
       html += '</ul></div>';
       marker.openInfoWindowHtml(html);
       
   });

    return marker;
}


