﻿//<![CDATA[
$(document).ready( function()
{
    loadProviders($(".JSVariable > input").val());
});

var map = null;
var geocoder = null;

function loadProviders(strAllInfo)
{
    var arrAllInfo = strAllInfo.split("~");
    
    var strAddress = arrAllInfo[0];
    var strFormattedAddress = arrAllInfo[1]
    var strTitle = arrAllInfo[2];
    var strWeb = arrAllInfo[3];
    var strPhone1 = arrAllInfo[4];
    var strPhone2 = arrAllInfo[5];
    
    var arrAddress = strAddress.split("|");
    var arrFormattedAddress = strFormattedAddress.split("|");
    var arrTitle = strTitle.split("|");
    var arrWebSites = strWeb.split("|");
    var arrPhone1 = strPhone1.split("|");
    var arrPhone2 = strPhone2.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], arrFormattedAddress[i], arrTitle[i], arrWebSites[i], arrPhone1[i], arrPhone2[i], i);
        }
    }
    
    geocoder.getLatLng(String(arrAddress[0]), function(point) { map.setCenter(point) });  
}

function createMarker(point, strAddress, strFormattedAddress, strTitle, strWebSites, strPhone1, strPhone2, i) 
{  
    //Create the marker and its properties
    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 lettered icon for this point using our icon class from above
    var intMarkerName = 1 + i;
    var icon = new GIcon(baseIcon);
    var domain = $(".JSDomain > input").val();
    
    //Set marker image 
    icon.image = "http://" + domain + "/images/numberedmarkers/marker" + String(intMarkerName) + ".png";
    var marker = new GMarker(point, icon);
    
    //Watch for click to redisplay the information
    GEvent.addListener(marker, "click", function() 
    {
        var strNewTitle = (strTitle != ' ') ? ('<strong>' + strTitle + '</strong><br />') : ('');
        var strNewFormattedAddress = (strFormattedAddress != ' ') ? (strFormattedAddress + '<br />') : ('');
        var strNewPhone1 = (strPhone1 != ' ') ? (strPhone1 + '<br />') : ('');
        var strNewPhone2 = (strPhone2 != ' ') ? (strPhone2 + '<br />') : ('');
        var strNewWebSites = (strWebSites != ' ') ? (strWebSites) : ('');
                
        if ( strNewWebSites == '' )
        {
            marker.openInfoWindowHtml('<div>' + strNewTitle + strNewFormattedAddress + strNewPhone1 + strNewPhone2
                + '</div>');
        }
        else
        {
            marker.openInfoWindowHtml('<div>' + strNewTitle + strNewFormattedAddress + strNewPhone1 + strNewPhone2
                + '<a href="' + strNewWebSites + '" title="View ' + strNewWebSites +'">' + strNewWebSites + '</a><br /></div>');
        }
    });
    return marker;
}

function showAddress(strAddress, strFormattedAddress, strTitle, strWebSites, strPhone1, strPhone2, 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, strFormattedAddress, strTitle, strWebSites, strPhone1, strPhone2, i);
            map.addOverlay(marker);
            if(i == 0)
            {
                var strNewTitle = (strTitle != ' ') ? ('<strong>' + strTitle + '</strong><br />') : ('');
                var strNewFormattedAddress = (strFormattedAddress != ' ') ? (strFormattedAddress + '<br />') : ('');
                var strNewPhone1 = (strPhone1 != ' ') ? (strPhone1 + '<br />') : ('');
                var strNewPhone2 = (strPhone2 != ' ') ? (strPhone2 + '<br />') : ('');
                var strNewWebSites = (strWebSites != ' ') ? (strWebSites) : ('');
                
                if ( strWebSites == '' )
                {
                    marker.openInfoWindowHtml('<div>' + strNewTitle + strNewFormattedAddress + strNewPhone1 + strNewPhone2
                        + '</div>');
                }
                else
                {
                    marker.openInfoWindowHtml('<div>' + strNewTitle + strNewFormattedAddress + strNewPhone1 + strNewPhone2
                        + '<a href="' + strNewWebSites + '" title="View ' + strNewWebSites +'">' + strNewWebSites + '</a><br /></div>');
                }
            }
        });
    }
}