/*
FFCG Map by Phil Freo 
http://www.philfreo.com

Thanks to: http://www.commchurch.freeserve.co.uk/, http://www.econym.demon.co.uk/googlemaps/

Required:
    GMap javascript
Optional:
    { prototype.lite.js, moo.fx.js } - adds animation to div#locations
        
*/

var ffcg_map = function() {
    var map;
    var locations_html = "";
    var gmarkers = [];
    var htmls = [];
    var i = 0;
	var showingAll = true;
	var oldHeight = 0;	
    var baseIcon;
    var locationsDivHead = '<h3>View Items of Interest</h3>';
    
    // A function to create the marker and set up the event window
    function createMarker(point,name,html,icon,tooltip) {
        var marker = new GMarker(point,{"icon":icon,"title":tooltip});
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(html);
            });
        gmarkers[i] = marker;
        htmls[i] = html;
        locations_html += '<li class="plus"><a onclick="ffcg_map.location_click('+i+')" href="#">' + name + '</a></li>';
        i++;
        return marker;
    }
    
    function getIcon(iconStr) {
        var icon;
        switch(iconStr) {
            case "golf":
                icon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon13.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon13s.png");
                break;
            case "hotel":
                icon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon28.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon28s.png");
                break;
            case "airport":
                icon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon56.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon56s.png");
                break;
            case "rentalcar":
                icon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon47.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon47s.png");
                break;
            case "restaurant":
                icon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon40.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon40s.png");
                break;
            default:
                icon = "";
        }
        return icon;
    }

    // A function to read the data
    function readMap(url) {
        var request = GXmlHttp.create();
        request.open("GET", url, true);
        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                var xmlDoc = request.responseXML;
                // obtain the array of markers and loop through it
                var markers = xmlDoc.documentElement.getElementsByTagName("marker");

                // hide the info window, otherwise it still stays open where the removed marker used to be
                map.getInfoWindow().hide();

                map.clearOverlays();

                // empty the array
                gmarkers = [];

                // reset the sidebar
                locations_html="";

                for (var j = 0; j < markers.length; j++) {
                    // obtain the attribues of each marker
                    var lat = parseFloat(markers[j].getAttribute("lat"));
                    var lng = parseFloat(markers[j].getAttribute("lng"));
                    var point = new GLatLng(lat,lng);
                    var html = markers[j].getAttribute("html");
                    var label = markers[j].getAttribute("label");
                    var iconStr = markers[j].getAttribute("icon");
                    var icon = getIcon(iconStr);
                    
                    // create the marker
                    var marker = createMarker(point,label,html,icon,label);
                    map.addOverlay(marker);
                }
				
                // put the assembled sidebar_html contents into the sidebar div
				var locationDiv = document.getElementById("locations");
				
				
				try { // Update div#locations with animation
				    var myEffect;
				    function updateLocationsHeight() {
				        locationDiv.style.height = "auto";
				        oldHeight = locationDiv.scrollHeight;//myEffect.el.scrollHeight;
				        locationDiv.style.height = oldHeight + "px";
				        locationDiv.style.overflow = "hidden";
                    }

				    if (showingAll) { // don't show any locations when default "all" is displayed
					    locations_html = "";
					    oldHeight = locationDiv.offsetHeight;
					    myEffect = new fx.Height(locationDiv, {duration: 500, onComplete: function() {locationDiv.innerHTML = locations_html; updateLocationsHeight() } });
					    myEffect.custom(oldHeight, 0);
					    oldHeight = 0;
				    } else {
					    locations_html = locationsDivHead + '<ul class="plus">' + locations_html + '</ul><p>&nbsp;</p>';
					    locationDiv.innerHTML = locations_html;
					    myEffect = new fx.Height(locationDiv, {duration: 500, onComplete: updateLocationsHeight});
					    myEffect.custom(oldHeight, myEffect.el.scrollHeight + 60);
				    }
                } catch(e) {    // If error, Update div#locations without animation
				    if (showingAll) { // don't show any locations when default "all" is displayed
					    locations_html = "";
					    document.getElementById("locations").innerHTML = locations_html;
				    } else {
					    locations_html = locationsDivHead + '<ul class="plus">' + locations_html + '</ul><p>&nbsp;</p>';
					    document.getElementById("locations").innerHTML = locations_html;
				    }
				}
            }
        };
        request.send(null);
    }
    
    return {

        init:function() {
            if (GBrowserIsCompatible()) {
                // set event handlers for sidebar links
                document.getElementById("ffcg_map_all").onclick = function() {showingAll = true; readMap("/resources/all.xml"); };
                document.getElementById("ffcg_map_courses").onclick = function() {showingAll = false; readMap("/resources/golf-courses.xml"); };
                document.getElementById("ffcg_map_hotels").onclick = function() {showingAll = false; readMap("/resources/hotels.xml"); };

                // create the map
                map = new GMap2(document.getElementById("map"));
                map.setCenter(new GLatLng( 29.995976,-81.602325), 9);
                map.addControl(new GMapTypeControl());
                map.addControl(new GLargeMapControl());
                
                // initialize baseIcon property used in function getIcon()
                baseIcon = new GIcon();
                baseIcon.iconSize=new GSize(32,32);
                baseIcon.shadowSize=new GSize(56,32);
                baseIcon.iconAnchor=new GPoint(16,32);
                baseIcon.infoWindowAnchor=new GPoint(16,0);

                // When initially loaded, use default data
                readMap("/resources/all.xml");
            
            } else {
                alert("Sorry, the Google Maps API is not compatible with this browser");
            }
        },
        
        location_click:function(i) {
            gmarkers[i].openInfoWindowHtml(htmls[i]);
        }
    };
    

}();



function addEventHandler(node, type, f) {
    if (node.addEventListener) {
        node.addEventListener(type, f, false); 
    } else if (node.attachEvent) {       
        node.attachEvent("on" + type, f); 
    } else {
        node["on" + type] = f;
    } 
}

addEventHandler(window, "load", ffcg_map.init);
addEventHandler(window, "unload", GUnload);
