﻿
//Requires Google Maps API
google.load('maps', '2');

YAHOO.namespace("CS");
YAHOO.CS.MapFxns = function () {
    var g_oGMap = null,
        g_oDirections = null,
        g_oLatLng = null,
        g_oIcon = null;

    //Creates a map with the default zoom centered on the dealership 
    //  with its icon loaded
    var CreateDefaultMap = function () {
        var oMarkerOptions = { icon: g_oIcon }; //Options array used to tell Google about our icon
        g_oGMap.addOverlay(new google.maps.Marker(g_oLatLng, oMarkerOptions)); //Tell Google about our location icon
        g_oGMap.setCenter(g_oLatLng, 13); //Center the map on the dealer's location
    };

    return {
        //Creates the Dealer's Latitude and Longitude object, 
        //  the map, directions retriever, and icon, then
        //  calls to the default map creator
        InitializeMap: function (targetID, latitude, longitude, icoUrl, width, height, useSmallControl, targetDirDiv) {
            if (!latitude || latitude.length == 0 || !longitude || longitude.length == 0) return;

            if (GBrowserIsCompatible()) {
                var oLats = latitude.split("^"),
                    oLongs = longitude.split("^"),
                    iOverlayCnt = oLats.length;

                g_oLatLng = new GLatLng(oLats[0], oLongs[0])

                //Create a reference to the map div so that google can draw it for us
                g_oGMap = new google.maps.Map2(YAHOO.util.Dom.get(targetID), { size: new GSize(width, height) });

                if (useSmallControl) { g_oGMap.addControl(new GSmallMapControl()); }
                else { g_oGMap.addControl(new GLargeMapControl()); }

                //Create a reference to the Directions div so that google can load them
                if (targetDirDiv) {
                    g_oDirections = new google.maps.Directions(g_oGMap, YAHOO.util.Dom.get(targetDirDiv));
                    google.maps.Event.addListener(g_oDirections, 'error', YAHOO.CS.MapFxns.HandleErrors); //Error handler
                }

                //Create the dealer's icon
                g_oIcon = new google.maps.Icon();
                g_oIcon.image = icoUrl;
                g_oIcon.iconSize = new google.maps.Size(17, 17);          //width,height of icon .png
                g_oIcon.iconAnchor = new google.maps.Point(8, 8);       //top left to marker point
                g_oIcon.infoWindowAnchor = new google.maps.Point(8, 8); //top left to where the infobox starts

                //If there is a directions div, only one overlay is mapped
                if (targetDirDiv) iOverlayCnt = 1;

                var oMarkerOptions = { icon: g_oIcon },
                     olatLongBounds = new GLatLngBounds(),
                     oLatLng = null;

                for (var iCnt = 0; iCnt < iOverlayCnt; iCnt++) {
                    oLatLng = new GLatLng(oLats[iCnt], oLongs[iCnt]);
                    g_oGMap.addOverlay(new google.maps.Marker(oLatLng, oMarkerOptions));
                    olatLongBounds.extend(oLatLng);
                }
                g_oGMap.setCenter(olatLongBounds.getCenter(), g_oGMap.getBoundsZoomLevel(olatLongBounds) - 1);
            }
        },

        RegisterUnload: function (bodyID) {
            YAHOO.util.Event.addListener(document.getElementById(bodyID), "unload", GUnload);
        },

        GetDirections: function (sAddr) {
            var oWayPoints = new Array();
            if (sAddr != null) {
                oWayPoints[0] = sAddr;
                oWayPoints[1] = g_oLatLng;
                g_oDirections.loadFromWaypoints(oWayPoints);
            }
        },

        //Tells the user we can't help and creates a default map
        HandleErrors: function () {
            alert('We are sorry.  The address you provided could not be located.  You might try making your search more general.');
            CreateDefaultMap();
        }
    }
} ();