﻿
//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();
                
                var oMarkerOptions = { icon: g_oIcon }; //Options array used to tell Google about our icon
		        
		        //14233 Interdrive W., Houston, TX 77032
		        g_oGMap.addOverlay(new google.maps.Marker(new GLatLng(29.926833, -95.345487), oMarkerOptions));
		        
		        //3500 Shelby Ln., Denton, TX 76207
		        g_oGMap.addOverlay(new google.maps.Marker(new GLatLng(33.1944986, -97.1795003), oMarkerOptions));
		        
		        //316 E. Gloria Switch Rd, Lafayette, LA 70507
		        g_oGMap.addOverlay(new google.maps.Marker(new GLatLng(30.2952777, -92.0174205), oMarkerOptions));
		        
		        //3912 4th St, Marrero, LA 70072
		        g_oGMap.addOverlay(new google.maps.Marker(new GLatLng(29.9053686, -90.0924114), oMarkerOptions));
		        
		        //561 W. 62nd St., Shreveport, LA 71106
		        g_oGMap.addOverlay(new google.maps.Marker(new GLatLng(32.4487979, -93.766915), oMarkerOptions));
		        
		        //401 South Hwy 342, Suite A,  Red Oak, TX 75154
		        g_oGMap.addOverlay(new google.maps.Marker(new GLatLng(32.507607, -96.806797), oMarkerOptions));
		        
		        g_oGMap.setCenter(new GLatLng(31.849933, -93.50595585), 7);
            }
        },

        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();
        }
    }
} ();