

 
    function getRandomLat(){
        return parseInt(Math.floor(Math.random() * 360))
    }
    function getRandomLon(){
        return -1 * parseInt(Math.floor(Math.random() * 360))
    }
    var map;
    var gdir;
    var baseIcon;
    var alphabetArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ];
    var lat;
    var lon;
    var ddFrom;
    var markers = new Array();
    function loadMap(canvasId) {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById(canvasId));
        map.enableContinuousZoom();      
        if(hybridEnabled){
          var mapControl = new GMapTypeControl();
          map.addControl(mapControl);
         //map.addMapType(G_PHYSICAL_MAP);
        //map.addMapType(G_SATELLITE_3D_MAP);  //Add google earth
        }
        map.addControl(new GSmallZoomControl());
        
        var bounds = new GLatLngBounds();
        //Add markers to map
        for(i = 0; i < locations.length; i++){
            map.setCenter(new GLatLng(locations[i][0],locations[i][1]), 13);  
            baseIcon = new GIcon(G_DEFAULT_ICON);
            bounds.extend(new GLatLng(locations[i][0],locations[i][1]));
            var markerText = "<div style='text-align:left;font-size:12px'>";
            markerText += "<span style='font-weight:bold;'>" + locations[i][2] + "</span>";
            markerText += "<br>"
            markerText += "<span style='font-weight:normal;'>";
            markerText += locations[i][3] + "<br>" + locations[i][4] + ", " + locations[i][5] + " " + locations[i][6];
            markerText += "</span>";
            if(document.getElementById("fl_route") != null){
                markerText += "<br><br>"; 
                markerText += "<input id='bubbleDirections' type='text' size='40'><br><input type=\"button\" onclick=\"lat=" + locations[i][0]+";lon=" + locations[i][1] + ";getDirections(document.getElementById('bubbleDirections').value)\" value=\"Get Directions\">";
            }
            markerText += "</div>";
            addMarker(new GLatLng(locations[i][0],locations[i][1]), markerText, locations.length == 1?1:i, locations[i][7]);
        }
        if(locations.length == 1){
            lat = locations[0][0];
            lon = locations[0][1];
            if(ddLat != null){
                getDirections(ddLat + ", " + ddLon);
            }
        }
        map.setZoom(map.getBoundsZoomLevel(bounds)-1);
        map.setCenter(bounds.getCenter());
      }
    }
    function getBubbleTabs(container){
        var tabs = new Array();
        var divs = container.getElementsByTagName("div");
        for(var i = 0; i < divs.length; i++){
            if(divs[i].className == "bubble-tab"){
                tabs.push(divs[i]);
            }
        }
        return tabs;
    }
    function addMarker(point,text,index,iconType){
            var customIcon = new GIcon(baseIcon);
            customIcon.image =  iconBase + iconName + "-" + iconType + (numbered?index+1:alphabetArray[index]) + ".png";
            if(iconWidth != null && iconHeight != null && iconXAlign != null && iconWidth > 0 && iconHeight > 0 && iconXAlign >= 0){
                customIcon.iconSize = new GSize(iconWidth,iconHeight);
                customIcon.iconAnchor = new GPoint(iconXAlign,iconHeight);
            }
            
            markerOptions = {icon: customIcon };
            var marker = new GMarker(point,markerOptions);
            map.addOverlay(marker);
            markers[markers.length] = marker;
            
            var locBubble = document.getElementById("bubble-loc" + index);
            if(locBubble != null){
                var tabs = getBubbleTabs(locBubble);
                if(tabs.length > 0){
                    var infoTabs = new Array();
                    for(var i = 0; i < tabs.length; i++){
                        infoTabs.push(new GInfoWindowTab(tabs[i].getAttribute("tab-header"),tabs[i]));
                    }
                    GEvent.addListener(marker, "click", function(){
		                marker.openInfoWindowTabs(infoTabs);
                    });
                }
                else{
                    GEvent.addListener(marker, "click", function(){
                                marker.openInfoWindow(locBubble);
                    });
                }
                
            }
            else{
                GEvent.addListener(marker, "click", function(){
                            marker.openInfoWindow(text);
                });
            }
            
    }
        function getDirectionsWithDetails(street,city,state,postalCode,countryAbbr){
        
        ddFrom = street + " " + city + ", " + state + " " + postalCode + " " + countryAbbr;
        if(gdir != null){
            gdir.clear();
        }
        directionsPanel = document.getElementById("fl_route");
        gdir = new GDirections(map, directionsPanel);
        G_START_ICON.image = "http://www.titanicons.com/files/http://rossstores.findlocation.com/Images/Map Icons/blueletter-a.png";
        G_END_ICON.image = "http://www.titanicons.com/files/http://rossstores.findlocation.com/Images/Map Icons/blueletter-a.png";
        
        var address = "";
        if(street != null && street.length > 0){
         address += street;
            if(city != null && city.length > 0){
                address += ", ";
            }
            else{
                address += " ";
            }
        }
        if(city != null  && city.length > 0){
            address += city;
            if(state != null && state.length > 0){
                address += ", ";
            }
            else{
                address += " ";
            }
        }
        if(state != null  && state.length > 0){
            address += state = " ";
        }
        if(postalCode != null  && postalCode.length > 0){
            address += postalCode + " ";
        }
        if(countryAbbr != null  && countryAbbr.length > 0){
            address += countryAbbr;
        }
        gdir.load("from: " + address + " to: " + lat + ", " + lon,{ "locale": locale});
       
        GEvent.addListener(gdir, "error", handleErrors);
    }  
    function getDirections(from){
        if(from == null){
            from = document.getElementById("directionInput").value;
        }
        ddFrom = from;
        if(gdir != null){
            gdir.clear();
        }
        directionsPanel = document.getElementById("fl_route");
        gdir = new GDirections(map, directionsPanel);
        //G_START_ICON.image = "http://www.titanicons.com/files/http://rossstores.findlocation.com/Images/Map Icons/blueletter-a.png";
        //G_END_ICON.image = "http://www.titanicons.com/files/http://rossstores.findlocation.com/Images/Map Icons/blueletter-a.png";
        gdir.load("from: " + from + " to: " + lat + ", " + lon,{ "locale": locale});
        GEvent.addListener(gdir, "error", handleErrors);
    }    
    
    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	   else alert("An unknown error occurred.");
	}

