﻿var location_map = {
    map: "",
    locations: [],
    points: [],
    getIndex: function(address) {
        for (var i = 0; i < location_map.locations.length; i++) {
            if (address == location_map.locations[i]) return i;
        }
    },
    marker: function() {
        texIcon = new GIcon(G_DEFAULT_ICON);

        // custom image for our map markers
        //texIcon.image = "/fsw/Portals/_default/Skins/FSW/images/map_dot.png";
        //texIcon.iconSize = new GSize(32, 32);
        texMarkerOptions = { icon: texIcon };
        return texMarkerOptions;
    },
    init: function() {
        if (!document.getElementById("map_canvas")) return false;

        // cycle through all of the immediate child list items of the locations list
        $("ul#locations>li").each(function(i) {

            // pull the lat and long of each location item
            // (note: these items are hidden via css)
            var latitude = $(this).children("ul").children(".latitude").html();
            var longitude = $(this).children("ul").children(".longitude").html();

            // create a location object from the lat & long
            var location = {
                "latitude": latitude,
                "longitude": longitude
            };

            // add the locations object to the locations array for mapping
            location_map.locations.push(location);

            // create the "Zoom on Map" link, hook up its behavior and add it to the html
            var directions = $(this).children("ul").children("li.directions");
            var map_zoom = $('<li class="map_zoom"><a href="#">&larr; Zoom on Map</a></li>');
            map_zoom.insertBefore(directions);
            map_zoom.click(function() {
                var index = $("ul#locations>li ul li.map_zoom").index($(this));
                location_map.centerOnOnePoint(index);
            });

        });

        if (GBrowserIsCompatible()) {
            location_map.map = new GMap2(document.getElementById("map_canvas"));

            // set the map zoom level and center point based on how many items we have to show
            if (location_map.locations.length > 18)
                location_map.map.setCenter(new GLatLng(47.9799, -110.2188), 3);
            else if (location_map.locations.length == 2)
                location_map.map.setCenter(new GLatLng(28.0425, -82.2216), 6);
            else if (location_map.locations.length > 1)
                location_map.centerOnArea(0);
            else if (location_map.locations.length == 1)
                location_map.centerOnOnePoint(0);
            else
                location_map.map.setCenter(new GLatLng(47.9799, -110.2188), 3);

            location_map.map.addControl(new GLargeMapControl3D());
            location_map.placeAllMarkers();
        }
    },
    placeAllMarkers: function() {
        for (var i = 0; i < location_map.locations.length; i++) {

            // use the lat & long to create a point and then construct a marker using our custom settings
            point = new GPoint(location_map.locations[i].longitude, location_map.locations[i].latitude);
            location_map.points.push(point);
            marker = new GMarker(point, location_map.marker());

            // create the map marker popup bubble using the content from the locations list
            var bubble_content = "";

            $("ul#locations>li").eq(i).children('ul').children('li.fn,li.adr,li.tel,li.directions').each(function() {
                bubble_content += $(this).html() + "<br />";
            })

            marker.bindInfoWindowHtml("<ul>" + bubble_content + "</ul>");

            location_map.map.addOverlay(marker);
        }
    },
    centerOnOnePoint: function(index) {
        address = location_map.locations[index];
        point = new GLatLng(address.latitude, address.longitude);
        location_map.map.setCenter(point, 15);

        var bubble_content = "";

        $("ul#locations>li").eq(index).children('ul').children('li.fn,li.adr,li.city,li.state,li.zip,li.tel').each(function() {
		if($("span",this).hasClass("locality") || $("span",this).hasClass("region")){
	            bubble_content += $(this).html();
		}else{
	            bubble_content += $(this).html() + "<br />";
		}
        })

        location_map.map.openInfoWindowHtml(point, '<ul class="googleSingleItem">' + bubble_content + '</ul>', { pixelOffset: new GSize(0, -32) });
    },
    centerOnArea: function(index) {
        point = new GLatLng(location_map.locations[index].latitude, location_map.locations[index].longitude);
if (location_map.locations.length > 5)
        location_map.map.setCenter(point, 5);
else
        location_map.map.setCenter(point, 6);

    }
};

google.load("maps", "2.x");
google.setOnLoadCallback(location_map.init);