﻿function initialize() 
{    
    google.load("maps", "2", {"callback" : loadMap});                    
}

/*******************************************************************
Receives:       Nothing
Returns:        Nothing
Description:  

Retrieves the address from the client and loads the address into a 
google map object.
*******************************************************************/
function loadMap()
{          
    var address = getAddress();
    var map = document.getElementById("map");
    var googleMap = null;
        
    if(address != null)
    {            
        googleMap = new GMap2(map);                    
        addMapControls(googleMap);    
        showAddress(googleMap, address);            
    }    
}

/*******************************************************************
Receives:       map - A GMap2 object.
                address - The address to display.
Returns:        Nothing
Description:  

Utilizes the Google API to retrieve the latitude/longitude of the
address and centers the map with a marker.
*******************************************************************/
function showAddress(map, address) 
{   
    var marker = null;    
    var markerOpts = null;     
    var addressComponents = address.split("--");
    var addressHtml = null;    
    var geocoder = new GClientGeocoder();    
    var icon = new GIcon();
    
    if(addressComponents.length == 4)
    {
        addressHtml = addressComponents[0] + "<br/>" + 
            addressComponents[1] +
            addressComponents[2] +
            addressComponents[3];
    }
    else
    {
        addressHtml = addressComponents[0] +
            addressComponents[1] + 
            addressComponents[2];            
    }
                        
    icon.image = '/Resources/Images/Controls/GoogleMapMarkers/1_pin.png';       
    icon.iconAnchor = new GPoint(15,30);
    icon.infoWindowAnchor = new GPoint(25, 7);
    
    markerOpts = { "icon": icon };
        
    geocoder.getLatLng(address.replace("--", " "), function(point) 
    {
        if (!point) 
        {
            //For now do nothing.  In the future, map the city.        
        } 
        else 
        {            
            map.setCenter(point, 13);
            marker = new GMarker(point, markerOpts);
            map.addOverlay(marker);
            marker.openInfoWindowHtml(addressHtml);            
            
            GEvent.addListener(marker, "click", function() 
            {
                marker.openInfoWindowHtml(addressHtml);
            });
        }
    });                                    
}

/*******************************************************************
Receives:       map - A GMap2 object.
Returns:        Nothing
Description:  

Utilizes the Google API to add map controls to the GMap2 object.
*******************************************************************/
function addMapControls(map)
{        
    var size = new GSize(10, 10);
    
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, size));                      
}

/*******************************************************************
Receives:       Nothing
Returns:        The address of the provider
Description:  

In server code, the address of the provider is stored in a hidden
field.  This function retrieves the address from the hidden field.

The hidden field is a child of a div that has ID="info".
*******************************************************************/
function getAddress()
{   
    var address = null; 
    var elementID = null;
    var parentOfAddress = document.getElementById("info");
                
    for(var i = 0; i < parentOfAddress.childNodes.length; i++)
    {
        if(parentOfAddress.childNodes.item(i).nodeName.toLowerCase() == "input")
        {
            //Because server controls have their parent ID's appended to them,
            //we must split the ID and look at the last string.  
            elementID = parentOfAddress.childNodes[i].id.split("_");
            
            if(elementID[elementID.length - 1].search("addressHF") != -1)
            {
                address = parentOfAddress.childNodes[i].value;
            }
        }
    }    
    
    if(address != null)
    {        
        if(address.search(/unknown/i) != -1)
        {
            address = address.replace(/unknown/i, "");
        }
    }
    
    return address;
}
