﻿
//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 (GBrowserIsCompatible()) {
                //Create the dealship locations coordinate location
                g_oLatLng = new GLatLng(latitude, longitude);

                //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(30, 30);          //width,height of icon .png
                g_oIcon.iconAnchor = new google.maps.Point(15, 15);       //top left to marker point
                g_oIcon.infoWindowAnchor = new google.maps.Point(15, 15); //top left to where the infobox starts

                //Draw the default map
                CreateDefaultMap();
            }
        },

        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();
        }
    }
} ();