﻿//<![CDATA[
$(document).ready( function()
{
    loadevents($(".JSVariable > input").val());
});

var geocoder = null;
var map = null ;
   
function loadevents(strAllInfo)
{    
    if (strAllInfo != "")
    {
       var arrAllInfo = strAllInfo.split("~");

       var strAddress = arrAllInfo[0];
       var strNames = arrAllInfo[1];
       var strPhone = arrAllInfo[2];
       var strContName = arrAllInfo[3];

       // parse data into an array
       var arrAddress = strAddress.split("|");
       var arrNames = strNames.split("|");
       var arrPhone = strPhone.split("|");
       var arrContName = strContName.split("|");
       
       if (GBrowserIsCompatible()) 
        {
            map = new GMap2(document.getElementById("Map"));
            map.setCenter(new GLatLng(41, -92.28815625), 12);
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            geocoder = new GClientGeocoder();
            
            //add address markers
            for (var i=0; i < arrAddress.length - 1; i++)
            {
                //Get the geocode information for each featured provider
                showAddress(arrAddress[i], arrNames[i], arrPhone[i], arrContName[i], i);
            }
        }
    }
}  


function createMarker(point, strAddress, strNames, strPhone, strContName, intCounter) 
{ 
    var baseIcon = new GIcon();
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    baseIcon.infoShadowAnchor = new GPoint(18, 25);

    // Create a numbered icon for this point using our icon class from above
    var letter =  1+ intCounter;
    var icon = new GIcon(baseIcon); 
    icon.image = "images/numberedmarkers/marker"+ letter + ".png";
    var marker = new GMarker(point, icon);
    
    //Watch for click to redisplay the information
    GEvent.addListener(marker, "click", function() 
    { 
        // Add information to a marker
        marker.openInfoWindowHtml('<strong>' + strNames + '</strong><br />'+ strAddress + '<br />'+ strPhone + '<br /><br />' + 'Contact: ' 
            + strContName + '<br /></div>');
    });
    return marker;
}

function showAddress(strAddress, strNames, strPhone, strContName, i) 
{
    //Get geocoded info from the http request at google
    if (geocoder) 
    {
        geocoder.getLatLng(strAddress, function(point) 
        {
            //Center map
            map.setCenter(point, 14);
            
            //Create marker and place it
            var marker = createMarker(point, strAddress, strNames, strPhone, strContName, i);
            map.addOverlay(marker);
            if(i == 0)
            {
                map.openInfoWindow(point, '<strong>' + strNames + '</strong><br />' + strAddress + '<br />'+ strPhone + '<br /><br />'
                    + 'Contact: ' + strContName + '<br /></div>');        
            }
        });
    }
}    
//]]>
