﻿
//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,
        g_oAddrs = 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
    },

    FormatTemplate = function (iDx) {
        var sTmp = sTmp = g_oAddrs[iDx].url == null || g_oAddrs[iDx].url.length == 0 ? '<em>' + g_oAddrs[iDx].title + '</em>' : '<a href="' + g_oAddrs[iDx].url + '">' + g_oAddrs[iDx].title + '</a>';
        sTmp += '<br/>' + g_oAddrs[iDx].address + '<br/>';

        //Shows multiple sites on same location
        for (var iCtr = 0; iCtr < g_oAddrs.length; iCtr++) {
            if (iCtr == iDx) continue;
            if (g_oAddrs[iDx].latitude == g_oAddrs[iCtr].latitude && g_oAddrs[iDx].longitude == g_oAddrs[iCtr].longitude) {
                sTmp += "<br/>";
                sTmp += g_oAddrs[iCtr].url == null || g_oAddrs[iCtr].url.length == 0 ? '<em>' + g_oAddrs[iCtr].title + '</em>' : '<a href="' + g_oAddrs[iCtr].url + '">' + g_oAddrs[iCtr].title + '</a>';
                sTmp += '<br/>' + g_oAddrs[iCtr].address + '<br/>';
            }
        }
        return sTmp;
    },

    CreateMarker = function (latLng, markerOptions, iDx, UseHover) {
        var oGmarker = new google.maps.Marker(latLng, markerOptions);
        if (!UseHover) return oGmarker;

        var sHtml = FormatTemplate(iDx);
        oGmarker.bindInfoWindowHtml(sHtml);

        GEvent.bind(oGmarker, "mouseover", oGmarker, function () {
            oGmarker.openInfoWindowHtml(sHtml);
        });
        return oGmarker;
    };

    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, addresses) {
            if ((!latitude || latitude.length == 0 || !longitude || longitude.length == 0)
                && (!addresses || addresses.length == 0))
                return;

            var iIcoDim = 30, iIcoHalf = 15;

            if (GBrowserIsCompatible()) {
                //Local Variables
                var oLats = null, oLongs = null, iOverlayCnt = 1, UseHover = true;
                g_oAddrs = addresses;

                //When only latitude and longitude are passed without addresses hover will be disabled
                if (!addresses || addresses.length == 0) {
                    UseHover = false;
                    g_oLatLng = new GLatLng(latitude, longitude);
                }
                else {
                    //For a single overlay(With Hover) the address object will be passed instead of an array
                    if (addresses[0] == null) { g_oAddrs = addresses = [addresses]; }

                    g_oLatLng = new GLatLng(addresses[0].latitude, addresses[0].longitude);
                    iOverlayCnt = addresses.length;
                }

                //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
                }

                if (iOverlayCnt > 1) {
                    iIcoDim = 24;
                    iIcoHalf = 12;
                }

                //Create the dealer's icon
                g_oIcon = new google.maps.Icon();
                g_oIcon.image = icoUrl;
                g_oIcon.iconSize = new google.maps.Size(iIcoDim, iIcoDim);        //width,height of icon .png
                g_oIcon.iconAnchor = new google.maps.Point(iIcoHalf, iIcoHalf);       //top left to marker point
                g_oIcon.infoWindowAnchor = new google.maps.Point(iIcoHalf, iIcoHalf); //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,
                     oGmarker = null,
                     sHtml = null;

                for (var iCnt = 0; iCnt < iOverlayCnt; iCnt++) {
                    oLatLng = UseHover ? new GLatLng(addresses[iCnt].latitude, addresses[iCnt].longitude) : new GLatLng(latitude, longitude);
                    oGmarker = CreateMarker(oLatLng, oMarkerOptions, iCnt, UseHover);
                    g_oGMap.addOverlay(oGmarker);
                    olatLongBounds.extend(oLatLng);
                }

                //When there's only one location, we zoom out a little to give the use perspective,
                //  this isn't so much a problem when there are multiple locations because Google auto-zooms out
                if (iOverlayCnt == 1)
                    g_oGMap.setCenter(olatLongBounds.getCenter(), g_oGMap.getBoundsZoomLevel(olatLongBounds) - 6);
                else
                    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();
        }
    }
} ();
