//this file is deprecated

/*
locations[i][0]=latitude
locations[i][1]=longitude
locations[i][2]=name
locations[i][3]=address1
locations[i][4]=city
locations[i][5]=state
locations[i][6]=postal
locations[i][7]=icon url
locations[i][8]=icon width
locations[i][9]=icon height
locations[i][10]=icon xanchor
locations[i][11]=icon yanchor
locations[i][12]=icon shadow url
locations[i][13]=shadow width
locations[i][14]=shadow height
*/


    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 searching center point icon (if desired)
        if (displayCenter) {
            var customIcon = new GIcon(baseIcon);
            customIcon.image = centerIcon;
            if (centerWidth != null && centerHeight != null && centerXAlign != null && centerWidth > 0 && centerHeight > 0) {// && centerXAlign >= 0) {
                customIcon.iconSize = new GSize(centerWidth, centerHeight);
                customIcon.iconAnchor = new GPoint(centerXAlign, centerYAlign);
            }
            markerOptions = { icon: customIcon };
            var marker = new GMarker(new GLatLng(searchLat * 57.2957795, searchLon * 57.2957795), markerOptions);
            map.addOverlay(marker);
            bounds.extend(new GLatLng(searchLat * 57.2957795, searchLon * 57.2957795));

            GEvent.addListener(marker, "click", function() {
                map.openInfoWindow(new GLatLng(searchLat * 57.2957795, searchLon * 57.2957795), "Search Point");
            });
        }
        
        //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' onkeypress = 'doButtonClickOnEnter(event, document.getElementById(\"bubblebtn\"))' size='40'><br><input id =\"bubblebtn\" type=\"button\" onclick=\"lat=" + locations[i][0]+";lon=" + locations[i][1] + ";getDirections(document.getElementById('bubbleDirections').value)\" value=\"Get Directions\">";
            }
            markerText += "</div>";
            //var iconNumber = i;
            //if (typeof displayB != "undefined" && displayB) iconNumber = 1; //display a B or 2 instead of A or 1 for map.aspx
            //addMarker(new GLatLng(locations[i][0],locations[i][1]), markerText, iconNumber, locations[i][7]);
            addMarker(new GLatLng(locations[i][0],locations[i][1]), markerText, locations[i], i);
        }
        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());
      }
    }
    
//This function clicks the input button if the input event was the enter key
function doButtonClickOnEnter(inputEvent, inputButton){
    if(inputEvent.keyCode==13){
        inputButton.click();
    }
}

    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,locationArray, index){
            var customIcon = new GIcon(baseIcon);
            //customIcon.image =  iconBase + iconName + "-" + iconType + (numbered?index+1:alphabetArray[index]) + ".png";
            customIcon.image = locationArray[7];
            customIcon.iconSize = new GSize(locationArray[8],locationArray[9]);
            customIcon.iconAnchor = new GPoint(locationArray[10], locationArray[11]);
            customIcon.transparent = "";
            customIcon.infoWindowAnchor = new GPoint(locationArray[8] / 2, locationArray[9] / 2);
            if (locationArray[13] > 0 && locationArray[14] > 0) {
                customIcon.shadow = locationArray[12];
                customIcon.shadowSize = new GSize(locationArray[13], locationArray[14]);
            } else {
                customIcon.shadow = null;
                customIcon.shadowSize = null;
            }
            
            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();
        }
        //directionsIconsSetup();
        directionsPanel = document.getElementById("fl_route");
        gdir = new GDirections(map, null);
        //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, "getSteps":true});
        GEvent.addListener(gdir, "error", handleErrors);
        GEvent.addListener(gdir, "load", handleDirLoad);
    }

    function handleDirLoad() {
        var marker = gdir.getMarker(0);
        var customIcon = marker.getIcon();
        customIcon.image = ddIconStart;
        customIcon.iconSize = new GSize(ddIconWidth, ddIconHeight);
        customIcon.iconAnchor = new GPoint(ddIconXAlign, ddIconYAlign);
        customIcon.transparent = "";
        if (ddShadowX > 0 && ddShadowY > 0) {
            customIcon.shadow = ddShadow;
            customIcon.shadowSize = new GSize(ddShadowX, ddShadowY);
        } else {
            customIcon.shadow = null;
            customIcon.shadowSize = null;
        }
        
        marker = gdir.getMarker(1);
        customIcon = marker.getIcon();
        customIcon.image = ddIconEnd;
        customIcon.iconSize = new GSize(ddIconWidth, ddIconHeight);
        customIcon.iconAnchor = new GPoint(ddIconXAlign, ddIconYAlign);
        customIcon.transparent = "";
        if (ddShadowX > 0 && ddShadowY > 0) {
            customIcon.shadow = ddShadow;
            customIcon.shadowSize = new GSize(ddShadowX, ddShadowY);
        } else {
            customIcon.shadow = null;
            customIcon.shadowSize = null;
        }
        displayRoute();
    }

    function displayRoute() {
        //copied and shortened from tripplanner
        var numRoutes = gdir.getNumRoutes();
        if (numRoutes == 0) return;
        var dirHtml;
        for (var i = 0; i < numRoutes; i++) {
            var curRoute = gdir.getRoute(i);
            var numSteps = curRoute.getNumSteps();
            for (var j = 0; j < numSteps; j++) {
                var curStep = curRoute.getStep(j);
                if (j == 0 && i == 0) {//start of the first section of the trip
                    dirHtml = "<table width=\"100%\">";
                    dirHtml += "<tr onclick='stepBlowup(" + i + "," + j + ")' style='cursor:pointer'><td colspan=\"3\">";
                    dirHtml += "<div class=\"trip-stop\"><img src=\"" + ddIconStart + "\" alt=\"start\"><b>" + curRoute.getStartGeocode().address + "</b></div>";
                    dirHtml += "</td></tr>";
                    dirHtml += "<tr><td></td><td colspan=\"2\" align=\"right\">";
                    dirHtml += gdir.getRoute(0).getDistance().html + " (About " + gdir.getRoute(0).getDuration().html + ")";
                    dirHtml += "</td></tr>";
                    dirHtml += "<tr><td colspan=\"3\"><hr/></td></tr>";
                }

                dirHtml += "<tr onclick='stepBlowup(" + i + "," + j + ")' style='cursor:pointer'>";
                dirHtml += "<td width=\"1%\" style=\"font-weight:bold;\" valign=\"top\">";
                dirHtml += (j + 1) + ". ";
                dirHtml += "</td>";
                dirHtml += "<td>";
                dirHtml += curStep.getDescriptionHtml();
                dirHtml += "</td>";
                dirHtml += "<td nowrap valign=\"top\" align=\"right\">";
                dirHtml += " (" + curStep.getDistance().html + ")";
                dirHtml += "</td>";
                dirHtml += "</tr>";
                dirHtml += "<tr><td colspan=\"3\"><hr/></td></tr>";
            }
        }

        var lastRoute = gdir.getRoute(numRoutes - 1);
        var lastSteps = lastRoute.getNumSteps();

        dirHtml += "<tr onclick='stepBlowup(" + (numRoutes - 1) + "," + (lastSteps - 1) + ")' style='cursor:pointer'><td colspan=\"3\">";
        dirHtml += "<div class=\"trip-stop\"><img src=\"" + ddIconEnd + "\" alt=\"end\">" + lastRoute.getEndGeocode().address + "</div>";
        dirHtml += "</td></tr></table>";

        document.getElementById("fl_route").innerHTML = dirHtml;
    }

    function stepBlowup(route, step) {
        //copied from tripplanner
        var curStep = gdir.getRoute(route).getStep(step);
        map.showMapBlowup(curStep.getLatLng());
    }
    
    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 if(gdir.getStatus().code == G_GEO_UNKNOWN_DIRECTIONS)
	     alert("The GDirections object could not compute directions between the points mentioned in the query. This is usually because there is no route available between the two points, or because we do not have data for routing in that region. \n Error code: " + gdir.getStatus().code);
	   else alert("An unknown error occurred.\nError code: "+ gdir.getStatus().code);
	}

