﻿// StoreInfo Object
var storeList = new Array();
var googleMap = null;
var geocoder = null;

function StoreInfo(title, description, street, city, state, zip, titleLabelId, lat, lng, phone)
{
    this.title = title;
    this.description = description;
    this.address = street;
    this.city = city;
    this.state = state;
    this.zipCode = zip;
    this.titleLabelId = titleLabelId;
    this.lat = lat;
    this.lng = lng;
    this.phone = phone;
    
    this.getGeocodeAddress = 
        function ()
        {
            return this.address + ', ' + this.city + ', ' + this.state + ', ' + this.zipCode;
        };
    this.getFormattedAddress =
        function ()
        {
            return '<b>' + this.title + '</b><br />' + this.address + '<br />' + this.city + ', ' + this.state + '  ' + this.zipCode;
        };
    this.getInfoBubble =
        function()
        {
            var bubbleInfo = "";
            //bubbleInfo = bubbleInfo + "<table border='0' cellpadding='2' cellspacing='2'>";
            bubbleInfo = bubbleInfo + "<div style='width: 250px;'>";
            bubbleInfo = bubbleInfo + "<b>" + this.title + "</b><br />"
            bubbleInfo = bubbleInfo + 
                this.address + "<br />" + this.city + ", " + this.state + "&nbsp;&nbsp;" + this.zipCode + "<br />";
            bubbleInfo = bubbleInfo + "Phone: " + this.phone + "<br />";
            bubbleInfo = bubbleInfo + this.description;
            bubbleInfo = bubbleInfo + "</div>";

            //bubbleInfo = bubbleInfo + "</table>";
            
            return bubbleInfo;
        }
}

function AddStore(storeInfo)
{
    storeList[storeList.length] = storeInfo;
}

// Initialize google maps variables
function initMapping(mapElementId, mapWidth, mapHeight, titlePre)
{
    geocoder = new GClientGeocoder();
    googleMap = new GMap2(document.getElementById(mapElementId),
        { size: new GSize(mapWidth,mapHeight) } );
    var customUI = googleMap.getDefaultUI();
    // Remove MapType.G_HYBRID_MAP
    customUI.maptypes.hybrid = false;
    customUI.maptypes.physical = false;
    googleMap.setUI(customUI);
    titlePrefix = titlePre;
}

// Map Centering Function
function centerMapByAddress(address, zoom)
{
    geocoder.getLatLng(address, function(point) {googleMap.setCenter(point, zoom);});
}
function centerMapByLatLng(lat, lng, zoom)
{
    googleMap.setCenter(new GLatLng(lat,lng), zoom);
}

// Map marker creation
function CreateMapMarkers()
{
    for (x in storeList)
        CreateMarker(storeList[x])
}
function CreateMarker(storeInfo)
{
    /*var searchAddr = storeInfo.getGeocodeAddress();
    if (storeInfo.lat != "" && storeInfo.lng != "")
    {*/
    if (storeInfo.lat != 0 && storeInfo.lng  != 0)
    {
        var point = new GLatLng(storeInfo.lat, storeInfo.lng);
        var marker = new GMarker(point);
        var storeLabel = document.getElementById(storeInfo.titleLabelId);
        
        AddBubbleDisplayEvent(googleMap, marker, point, storeInfo.getInfoBubble(), true);
        if (storeLabel)
        {
            AddBubbleDisplayEvent(googleMap, storeLabel, point, storeInfo.getInfoBubble(), false);
        }
        googleMap.addOverlay(marker);
    }
    /*}
    else
        geocoder.getLatLng(searchAddr, 
            function(point)
            {
                if (point)
                {
                    var marker = new GMarker(point);
                    var storeLabel = document.getElementById(storeInfo.titleLabelId);
                    
                    AddBubbleDisplayEvent(googleMap, marker, point, storeInfo.getInfoBubble(), true);
                    if (storeLabel)
                    {
                        AddBubbleDisplayEvent(googleMap, storeLabel, point, storeInfo.getInfoBubble(), false);
                    }
                    googleMap.addOverlay(marker);
                }
            });*/
}

// Show Map Information
function AddBubbleDisplayEvent(mp, obj, point, bubbleInfo, useEvent)
{
    if (useEvent)
        GEvent.addListener(obj, "click", function(){mp.openInfoWindowHtml(point, bubbleInfo);});
    else
        obj.onclick = function(){mp.openInfoWindowHtml(point, bubbleInfo);};
}

