
jQuery(document).ready(function(){ //Start script when ready
    if (document.getElementById("map_canvas")) {//Checks to see that the page element exists and to prevent conflict with other maps
	
		// Create our "tiny" marker icon
		var blueIcon = new GIcon(G_DEFAULT_ICON);
		blueIcon.image = "images/icon_ymarker.png";
		blueIcon.iconSize = new GSize(25, 25);
		blueIcon.shadowSize = new GSize(40, 26);
		// Set up our GMarkerOptions object
		markerOptions = { icon:blueIcon };

        //Default map center location
        var defaultLat = 30.268735;
        var defaultLon = -97.855209;
        var markers = new Array(); // For Lat & Long use http://itouchmap.com/latlong.html
        markers[0] = new Array(new GMarker(new GLatLng(30.26883,-97.757152),markerOptions), "TownLake Branch", "<strong>TownLake Branch</strong><br />1100 W. Cesar Chavez<br />Austin, TX 78703<br />Phone: 512-542-9622<br />Fax: 512-476-3548<br /><a href='http://www.austinymca.org/branches/loc.php?loc=1'>Branch Website</a><br /><a href='http://maps.google.com/maps?q=1100+W.+Cesar+Chavez+Austin+TX+78703' target='_blank'>Get Directions to Branch</a>");
        markers[1] = new Array(new GMarker(new GLatLng(30.446271,-97.734174),markerOptions), "Northwest Family Branch", "<strong><a href=''>Northwest Family Branch</a></strong><br />5807 McNeil Dr.<br />Austin, TX 78729<br />Phone: 512-335-9622<br />Fax: 512-335-1615<br /><a href='http://www.austinymca.org/branches/loc.php?loc=4'>Branch Website</a><br /><a href='http://maps.google.com/maps?q=5807+McNeil+Dr+Austin+TX+78729' target='_blank'>Get Directions to Branch</a>");
        markers[2] = new Array(new GMarker(new GLatLng(30.100231,-97.879986),markerOptions), "Hays Communities Family Branch", "<strong><a href=''>Hays Communities Family Branch</a></strong><br />465 Buda Sportsplex Drive<br />Buda, TX 78610<br />Phone: 512-523-0099<br />Fax: 512-523-0066<br /><a href='http://www.austinymca.org/branches/loc.php?loc=5'>Branch Website</a><br /><a href='http://maps.google.com/maps?q=465+Buda+Sportsplex+Drive+Buda+TX+78610' target='_blank'>Get Directions to Branch</a>");
        markers[3] = new Array(new GMarker(new GLatLng(30.236324,-97.853689),markerOptions), "Southwest Branch", "<strong><a href=''>Southwest Branch</a></strong><br />6219 Oakclaire Dr.<br />Austin, TX 78735<br />512-891-9622<br />Fax: 512-892-1557<br /><a href='http://www.austinymca.org/branches/loc.php?loc=6'>Branch Website</a><br /><a href='http://maps.google.com/maps?q=6219+Oakclaire+Dr+Austin+TX+78735' target='_blank'>Get Directions to Branch</a>");
        markers[4] = new Array(new GMarker(new GLatLng(30.291928,-97.662591),markerOptions), "East Communities Branch", "<strong><a href=''>East Communities Branch</a></strong><br />5315 Ed Bluestein<br />Austin, TX 78723<br />512-933-9622<br />Fax: 512-933-1225<br /><a href='http://www.austinymca.org/branches/loc.php?loc=7'>Branch Website</a><br /><a href='http://maps.google.com/maps?q=5315+Ed+Bluestein+Austin+TX+78723' target='_blank'>Get Directions to Branch</a>");
        markers[5] = new Array(new GMarker(new GLatLng(30.366636,-97.695794),markerOptions), "North Park Branch", "<strong><a href=''>North Park Branch</a></strong><br />9616 N. Lamar Blvd, Suite 130<br />Austin, TX 78753<br />512-973-9622<br />fax 512-491-0909<br /><a href='http://www.austinymca.org/branches/loc.php?loc=8'>Branch Website</a><br /><a href='http://maps.google.com/maps?q=9616+N+Lamar+blvd+Austin+TX+78753' target='_blank'>Get Directions to Branch</a>");
        markers[6] = new Array(new GMarker(new GLatLng(30.183434,-98.085733),markerOptions), "Springs Family Branch", "<strong><a href=''>Springs Family Branch</a></strong><br />27216 Ranch Road 12<br />Dripping Springs, TX 78620<br />512-894-3309<br />Fax: 512-858-1789<br /><a href='http://www.austinymca.org/branches/loc.php?loc=11'>Branch Website</a><br /><a href='http://maps.google.com/maps?q=27216+Ranch+Road+12+Dripping Springs+TX+78620' target='_blank'>Get Directions to Branch</a>");
        markers[7] = new Array(new GMarker(new GLatLng(30.260222,-97.718446),markerOptions), "Program Services", "<strong><a href=''>Program Services</a></strong><br />2121 E. 6th St. Suite 203<br />Austin, TX 78702<br />Phone: 512-236-9622<br />Fax: 512-478-8065<br /><a href='http://www.austinymca.org/branches/loc.php?loc=10'>Branch Website</a><br /><a href='http://maps.google.com/maps?q=2121+E+6th+St+Suite+203+Austin+TX+78702' target='_blank'>Get Directions to Branch</a>");
        var map = new google.maps.Map2(jQuery("#map_canvas").get(0));//Initialise google maps
		map.setMapType(G_NORMAL_MAP);
        map.setCenter(new GLatLng(defaultLat, defaultLon), 10);//Set location to the default and zoom level to 8
		var mapControl = new GMapTypeControl();
			map.addControl(mapControl);
		map.addControl(new GLargeMapControl());

        jQuery(markers).each(function(i,marker){
           map.addOverlay(marker[0]);
            jQuery("<li />")
                .html(markers[i][1])//Use list item label from array
                .click(function(){
                    displayPoint(marker[0], i);
                    setActive(this);//Show active state
                    if (jQuery('#map_message').is(':hidden')) {//Allow toggling of active state
                        setActive();
                    }
                })
                .appendTo("#map_list");
            
            GEvent.addListener(marker[0], "click", function(){
                displayPoint(marker[0], i);
                setActive(i);//Show active location
                if (jQuery('#map_message').is(':hidden')) {//Allow toggling of active state
                    setActive();
                }
            });
        });
        
        jQuery("#map_list").css("opacity","0.2").animate({opacity: 1}, 1100);//Fade in menu
        jQuery("#map_message").appendTo(map.getPane(G_MAP_FLOAT_SHADOW_PANE));
                        
        function displayPoint(marker, index){
            if (jQuery('#map_message').is(':hidden')) {//Allow toggling of markers
                jQuery('#map_message').fadeIn();
            }
            else{//Remove all .active classes and hide markers
                jQuery('#map_message').hide();
                jQuery(".active").removeClass();
            }
            //jQuery("#map_message").hide();//Default behaviour, doesn't allow toggling
            
            var moveEnd = GEvent.addListener(map, "moveend", function(){
                var markerOffset = map.fromLatLngToDivPixel(marker.getLatLng());
                jQuery("#map_message")
                    .html(markers[index][2])//Use information from array
                    .fadeIn()
                    .css({ top:markerOffset.y, left:markerOffset.x });
            GEvent.removeListener(moveEnd);
            });
            map.panTo(marker.getLatLng());
			//map.setZoom(10);
        }	
        
        function setActive(el){
            jQuery(".active").removeClass();//Remove all .active classes
            jQuery("#map_list").find('li').eq(el).addClass('active');//Find list element equal to index number and set active
            jQuery(el).addClass('active');//Set active if list element clicked directly
        }
    }//End if map_canvas exists
}); //End onReady
