﻿var map;
var geocoder = new GClientGeocoder();


var GetLatLngAttempted = false;
function GetLatLngForAddress(SaveCID, LatCID, LngCID, AddressString)
{
    if (GetLatLngAttempted)
    {
        return true;
    }
    else
    {
        GetLatLngAttempted = true;

        if (GBrowserIsCompatible())
        {
            geocoder.getLocations(AddressString,
                                function(response)
                                {
                                    if (!response || response.Status.code != 200 || response.Placemark.length == 0)
                                    {
                                        //alert("Unable to find address");
                                        document.getElementById(LatCID).value = "0";
                                        document.getElementById(LngCID).value = "0";
                                    }
                                    else
                                    {
                                        document.getElementById(LatCID).value = response.Placemark[0].Point.coordinates[1];
                                        document.getElementById(LngCID).value = response.Placemark[0].Point.coordinates[0];
                                    }

                                    document.getElementById(SaveCID).click();
                                });
        }
        return false;
    }
}

function InitMap()
{
    if (GBrowserIsCompatible())
    {
        map = new GMap2(document.getElementById("MapCanvas"));
        map.addControl(new GSmallMapControl());

        StoreMarkers = new Array();
        StoreCounts = 0;
    }
}

function SetHome(homeLat, homeLng, homeAddress, homeZoom)
{
    var point = new GLatLng(homeLat, homeLng);

    map.setCenter(point, homeZoom);

/*    var houseIcon = new GIcon(G_DEFAULT_ICON);
    houseIcon.image = 'images/houseicon.png';
    houseIcon.iconSize = new GSize(32, 32);
    houseIcon.imageMap = [0, 0, 32, 0, 32, 32, 0, 32];

    var marker = new GMarker(point, { icon: houseIcon });
    marker.bindInfoWindowHtml(homeAddress);
    map.addOverlay(marker);*/

    GEvent.addListener(map, "zoomend", ChangeZoom);     // ChangeZoom is in pickstore.aspx
}

function GeocodeHome(homeAddress, homeAddressHTML, homeZoom)
{
    if (GBrowserIsCompatible())
    {
        geocoder.getLocations(homeAddress,
                                function(response)
                                {
                                    if (!response || response.Status.code != 200 || response.Placemark.length == 0)
                                    {
                                        //alert("Unable to find address");
                                    }
                                    else
                                    {
                                        SetHome(response.Placemark[0].Point.coordinates[1],
                                            response.Placemark[0].Point.coordinates[0],
                                            homeAddressHTML, homeZoom);
                                    }
                                });
    }
}


var StoreMarkers = new Array();
var StoreCounts = 0;

function AddStore(storeLat, storeLng, storeAddress, iconPath, iconWidth, iconHeight)
{
    var point = new GLatLng(storeLat, storeLng);

    var storeIcon = new GIcon(G_DEFAULT_ICON);
    storeIcon.image = iconPath
    storeIcon.iconSize = new GSize(iconWidth, iconHeight);
    storeIcon.imageMap = [0, 0, iconWidth, 0, iconWidth, iconHeight, 0, iconHeight];
    storeIcon.iconAnchor = new GPoint(iconWidth / 2, iconHeight / 2);

    var marker = new GMarker(point, { icon: storeIcon });
    marker.bindInfoWindowHtml(storeAddress);
    map.addOverlay(marker);
    
    StoreCounts++;
    StoreMarkers.length = StoreCounts;
    StoreMarkers[StoreCounts-1] = marker;
}


function ShowStorePopup(storeMarkerIndex, storeAddress)
{
    var StoreMarker = StoreMarkers[storeMarkerIndex];
    StoreMarker.openInfoWindow(storeAddress);
}























/*	This work is licensed under Creative Commons GNU LGPL License.

License: http://creativecommons.org/licenses/LGPL/2.1/
Version: 0.9
Author:  Stefan Goessner/2006
Web:     http://goessner.net/ 
*/
function json2xml(o, tab)
{
    var toXml = function(v, name, ind)
    {
        var xml = "";
        if (v instanceof Array)
        {
            for (var i = 0, n = v.length; i < n; i++)
                xml += ind + toXml(v[i], name, ind + "\t") + "\n";
        }
        else if (typeof (v) == "object")
        {
            var hasChild = false;
            xml += ind + "<" + name;
            for (var m in v)
            {
                if (m.charAt(0) == "@")
                    xml += " " + m.substr(1) + "=\"" + v[m].toString() + "\"";
                else
                    hasChild = true;
            }
            xml += hasChild ? ">" : "/>";
            if (hasChild)
            {
                for (var m in v)
                {
                    if (m == "#text")
                        xml += v[m];
                    else if (m == "#cdata")
                        xml += "<![CDATA[" + v[m] + "]]>";
                    else if (m.charAt(0) != "@")
                        xml += toXml(v[m], m, ind + "\t");
                }
                xml += (xml.charAt(xml.length - 1) == "\n" ? ind : "") + "</" + name + ">";
            }
        }
        else
        {
            xml += ind + "<" + name + ">" + v.toString() + "</" + name + ">";
        }
        return xml;
    }, xml = "";
    for (var m in o)
        xml += toXml(o[m], m, "");
    return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, "");
}

