//Add a function to create a marker for mapped points
function createMarker(point, name) 
{
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(name);});
    return marker;
}
function setupLocationsMap() 
{
	if (GBrowserIsCompatible()) 
	{
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
        
        var place = "";
        //see if there is a GET variable to indicate where the map should
        //be centred
        if ( 0 == window.location.search.indexOf("?centre=") ){
            place = window.location.search.substr(8);
            switch (place)
            {
            case "moss_side":
                map.setCenter(new GLatLng(53.4603, -2.2478), 13);
                break;
            case "mac":
                map.setCenter(new GLatLng(53.466335, -2.2330), 13);
                break;
            case "longford":
                map.setCenter(new GLatLng(53.44544584678435, -2.290499210357666), 13);
                break;
            case "stretford":
                map.setCenter(new GLatLng(53.458326,-2.292688), 13);
                break;
            case "velodrome":
                map.setCenter(new GLatLng(53.485594, -2.193103), 13);
                break;
            case "didsbury":
                map.setCenter(new GLatLng(53.419252, -2.231512), 13);
                break;
            case "quays":
                map.setCenter(new GLatLng(53.470845, -2.289319), 13);
                break;
            case "chorlton":
                map.setCenter(new GLatLng(53.424003,-2.266403), 13);
                break;
            case "bruntwood":
                map.setCenter(new GLatLng(53.379751,-2.210226), 13);
                break;
            default:
                //use default
                map.setCenter(new GLatLng(53.450, -2.2478), 13);
            }
        }
        else{
            //no location specified so use default value
            map.setCenter(new GLatLng(53.450, -2.2478), 13);
        }
        
		//Now add markers to the map for each training location
		//Moss side
		var point = new GLatLng(53.4603, -2.2478);
        var mossSideText = "<strong>Moss Side Leisure Centre</strong><br />Moss Lane East, M15 5NN<br />0161 226 5015<br />Swim and Turbo Sessions<br /><a href=\"Venues/moss-side.html\">Venue Details</a>";
        var mossSideMarker = createMarker(point, mossSideText);
		map.addOverlay(mossSideMarker);

		//Aquatics centre
		var point = new GLatLng(53.466335, -2.2330);
        var macText = "<strong>Manchester Aquatics Centre</strong><br />2 Booth Street East, M13 9SS<br />0161 275 9450<br />Swim and Core Stability Sessions<br /><a href=\"Venues/mac.html\">Venue Details</a>";
        var macMarker = createMarker(point, macText);
		map.addOverlay(macMarker);

		//Longford Park
		var point = new GLatLng(53.44544584678435, -2.290499210357666);
        var longfordText = "<strong>Longford Park Stadium</strong><br />Ryebank Road, M21 9TA<br />0161 881 4488<br />Track Sessions<br /><a href=\"Venues/longford.html\">Venue Details</a>";
        var longfordMarker = createMarker(point, longfordText);
		map.addOverlay(longfordMarker);

		//Stretford Leisure Centre
		var point = new GLatLng(53.458326,-2.292688);
        var stretfordText = "<strong>Stretford Leisure Centre</strong><br />Greatstone Road, Stretford, M32 0ZS<br />0161 875 1414<br />Swim and Turbo Sessions<br /><a href=\"Venues/stretford.html\">Venue Details</a>";
        var stretfordMarker = createMarker(point, stretfordText);
		map.addOverlay(stretfordMarker);

		//Manchester Velodrome
        /*
		var point = new GLatLng(53.485594, -2.193103);
        var velodromeText = "<strong>Manchester Velodrome</strong><br /> Stuart Street, Manchester, M11 4DQ<br />Bike/Core session";
        var velodromeMarker = createMarker(point, velodromeText);
		map.addOverlay(velodromeMarker);
        */

		//Didsbury Clock Tower
		var point = new GLatLng(53.419252, -2.231512);
        var didsburyText = "<strong>Didsbury Clock Tower</strong><br />Wilmslow Road, Didsbury<br />Sunday Bike Meeting Point";
        var didsburyMarker = createMarker(point, didsburyText);
		map.addOverlay(didsburyMarker);
        
		//Salford Quays
		var point = new GLatLng(53.470845, -2.289319);
        var quaysText = "<strong>Salford Watersports Centre</strong><br />15 The Quays, Salford, M50 3SQ<br />Open Water Swim Sessions (not run by Man Tri)";
        var quaysMarker = createMarker(point, quaysText);
		map.addOverlay(quaysMarker);
        
		//Chorlton Water Park
		var point = new GLatLng(53.424003,-2.266403);
        var chorltonText = "<strong>Chorlton Water Park</strong><br />Maitland Ave, Chorlton<br />Run Sessions";
        var chorltonMarker = createMarker(point, chorltonText);
		map.addOverlay(chorltonMarker);
        
		//Bruntwood Park
		var point = new GLatLng(53.379751,-2.210226);
        var bruntwoodText = "<strong>Bruntwood Park</strong><br />Bruntwood Lane, Cheadle<br />XC Run Sessions";
        var bruntwoodMarker = createMarker(point, bruntwoodText);
		map.addOverlay(bruntwoodMarker);
        
        //Open up the info window for the marker we are centred on (if any)
        switch(place){
        case "moss_side":
            mossSideMarker.openInfoWindowHtml(mossSideText);
            break;
        case "mac":
            macMarker.openInfoWindowHtml(macText);
            break;
        case "longford":
            longfordMarker.openInfoWindowHtml(longfordText);
            break;
        case "stretford":
            stretfordMarker.openInfoWindowHtml(stretfordText);
            break;
        case "velodrome":
            velodromeMarker.openInfoWindowHtml(velodromeText);
            break;
        case "didsbury":
            didsburyMarker.openInfoWindowHtml(didsburyText);
            break;
        case "quays":
            quaysMarker.openInfoWindowHtml(quaysText);
            break;
        case "chorlton":
            chorltonMarker.openInfoWindowHtml(chorltonText);
            break;
        case "bruntwood":
            bruntwoodMarker.openInfoWindowHtml(bruntwoodText);
            break;
        }
        
	}
	else
	{

		//Tell the user their browser doesnt support google maps
		document.getElementById("map").innerHTML = 
            "<p>Sorry, your browser doesn't support Google Maps so we can't show you the locations.</p><p>Try the latest version of IE or Firefox for the best results.</p><p>Meanwhile, you'll have to look up the MTC training locations yourself!</p><ul><li>Moss Side Leisure Centre, Moss Lane East, M15 5NN</li><li>Manchester Aquatics Centre, 2 Booth Street East, M13 9SS</li><li>Longford Park Stadium, Ryebank Road, M21 9TA</li><li>Stretford Leisure Centre, Greatstone Road, Stretford, M32 0ZS</li><li>Manchester Velodrome, Stuart Street, Manchester, M11 4DQ</li><li>Didsbury Clock Tower, Wilmslow Road, Didsbury</li><li>Salford Watersports Centre, 15 The Quays, Salford, M50 3SQ</li></ul>";

	}

}
