/*

Scripts for Batchworth Dragons mapping page(s)
Copyright Batchworth Dragons 2007
Ideas and samples taken from http://www.google.com/apis/maps/documentation
  
*/

  //<![CDATA[

  var map;
  var mmgr;
  var marks = [];
  var dbmarkers = [];
  var ptCenter = new GLatLng(52.776186, -1.472168);
  
  var ALL_ZOOM_LEVEL   = 7;

  var IDX_TYPE         = 0;
  var IDX_POINT        = 1;
  var IDX_NAME         = 2;
  var IDX_INFO         = 3;
  var IDX_ICON         = 4;

  var ID_TYPE_RACE     = 1;
  var ID_TYPE_CLUB     = 2;
  var ID_TYPE_BOTH     = 3;
  

  function initMarkers() {
    // Race/event geographical locations and info
    // Races
    dbmarkers[dbmarkers.length] = [ID_TYPE_RACE, new GLatLng(53.392407, -2.985956), "Liverpool race", "Queens Dock, 3 May 2009", "redblue"];
    dbmarkers[dbmarkers.length] = [ID_TYPE_RACE, new GLatLng(50.717774, -3.529408), "Exeter race", "The Quay, 17 May 2009", ""];
    dbmarkers[dbmarkers.length] = [ID_TYPE_RACE, new GLatLng(51.448390, -2.533190), "Bristol race", "Bristol Docks, 31 May 2009", ""];
    dbmarkers[dbmarkers.length] = [ID_TYPE_RACE, new GLatLng(51.507500,  0.030000), "London race (RVD)", "Royal Victoria Docks, 13 June 2009", ""];
    dbmarkers[dbmarkers.length] = [ID_TYPE_RACE, new GLatLng(52.936000, -1.136300), "Nottingham race", "County Hall, Nottingham<br />28 June 2009", ""];
    dbmarkers[dbmarkers.length] = [ID_TYPE_RACE, new GLatLng(53.760839, -2.735467), "Preston race", "26 July 2009<br />This is NOT necessarily the actual race location.<br />Just guessing, we don't know where it is.", ""]; // Don't know the venue location
    dbmarkers[dbmarkers.length] = [ID_TYPE_RACE, new GLatLng(52.195661, -2.232367), "Worcester race", "Grandstand Road, 2 August 2009", ""];
    //dbmarkers[dbmarkers.length] = [ID_TYPE_RACE, new GLatLng(51.507630,  0.048960), "Caldecotte race", "Caldecotte, 13 September 2009", ""];
    dbmarkers[dbmarkers.length] = [ID_TYPE_RACE, new GLatLng(51.507630,  0.048960), "London race (RAD)", "Royal Albert Dock, 27 September 2009", ""];
    dbmarkers[dbmarkers.length] = [ID_TYPE_RACE, new GLatLng(52.941985, -1.090604), "National finals", "National Water Sports Centre<br />Holme Pierrepont<br />Nottingham<br />October 2009", ""];
    // Clubs
    dbmarkers[dbmarkers.length] = [ID_TYPE_CLUB, new GLatLng(51.605449, -0.499288), "Batchworth Dragons", "Troy Lake, Rickmansworth<br /><a href='http://www.batchworthdragons.org.uk/'>Web site</a>", "blue"];
    dbmarkers[dbmarkers.length] = [ID_TYPE_CLUB, new GLatLng(53.392407, -2.985956), "Amathus DBC", "Liverpool Watersports Centre, Liverpool<br /><a href='http://www.amathusdbc.pwp.blueyonder.co.uk/main.htm'>Web site</a><br />&nbsp;<br /><strong>Liverpool race</strong><br />3 May 2009", "redblue"];
    dbmarkers[dbmarkers.length] = [ID_TYPE_CLUB, new GLatLng(51.447000, -0.548000), "BA Hurricanes", "Silverwing Lake, Wraysbury<br /><a href='http://www.ba-hurricanes.org/'>Web site</a>", "blue"];
    dbmarkers[dbmarkers.length] = [ID_TYPE_CLUB, new GLatLng(51.447900, -2.538100), "Bristol Empire", "River Avon, Bristol<br /><a href='http://www.bristolempiredragons.com/'>Web site</a>", "blue"];
    dbmarkers[dbmarkers.length] = [ID_TYPE_CLUB, new GLatLng(51.476572, -3.165867), "Cardiff Powerblades", "Atlantic Wharf, Cardiff<br /><a href='http://www.cardiffpowerblades.org.uk/'>Web site</a>", "blue"];
    dbmarkers[dbmarkers.length] = [ID_TYPE_CLUB, new GLatLng(51.535759, -0.896137), "Henley Dragons", "Eyot Boat Centre, Henley-on-Thames<br /><a href='http://www.henleydragons.com/'>Web site</a>", "blue"];
    dbmarkers[dbmarkers.length] = [ID_TYPE_CLUB, new GLatLng(51.423324, -0.308555), "Kingston Royals", "Royal Canoe Club, Trowlock Way, Teddington<br /><a href='http://www.kingstonroyals.com/'>Web site</a>", "blue"];
    dbmarkers[dbmarkers.length] = [ID_TYPE_CLUB, new GLatLng(51.507614,  0.043552), "Thames Dragons", "London Regatta Centre, Royal Albert Dock, London<br /><a href='http://www.thamesdragons.com/'>Web site</a>", "blue"];
    dbmarkers[dbmarkers.length] = [ID_TYPE_CLUB, new GLatLng(51.439813, -0.542679), "Wraysbury Dragons", "Wraysbury Skiff & Punting Club, Egham, Surrey<br /><a href='http://www.wraysburydragons.co.uk/'>Web site</a>", "blue"];
    dbmarkers[dbmarkers.length] = [ID_TYPE_CLUB, new GLatLng(52.188500, -2.228100), "Worcester Dragons", "Worcester<br /><a href='http://www.worcester-dragons.org.uk/'>Web site</a><br />&nbsp;<br /><strong>Worcester race</strong><br />2 August 2009", "redblue"];
    
    // Old ones
    //dbmarkers[dbmarkers.length] = [ID_TYPE_RACE, new GLatLng(51.476572, -3.165867), "Cardiff race", "Atlantic Wharf, 18 May 2008", "redblue"];
    //dbmarkers[dbmarkers.length] = [ID_TYPE_RACE, new GLatLng(54.566200, -1.302000), "Teesside race", "14 June 2008", ""];       
  }
  
  function showCenter() {
    var pt = map.getCenter();
    var lat = pt.lat();
    var lng = pt.lng();
    alert("Center: Lat: " + lat + "; Lng: " + lng);
  }
  
  function listLocations() {
    var txt = "";
    var rtxt = "";
    var ctxt = "";
    
    // Add links for all locations
    for (i = 0; i < dbmarkers.length; i++) {
      txt = "<a href='javascript:showLocation(" + i + ")'>" + dbmarkers[i][IDX_NAME] + "</a><br />"
      if (dbmarkers[i][IDX_TYPE] == ID_TYPE_RACE) {
        rtxt = rtxt + txt;
      } else if (dbmarkers[i][IDX_TYPE] == ID_TYPE_CLUB) {
        ctxt = ctxt + txt;
      }
    }

    txt = "<div id=\"pins\">"
        + "<div id=\"racepins\"><strong>Races</strong><br />" + rtxt + "</div>"
        + "<div id=\"clubpins\"><strong>Clubs</strong><br />" + ctxt + "</div>" 
        + "<div id=\"showpins\"><a href='javascript:showLocation(-1)'>Show all pins</a></div>"
        + "</div>";
        
    // For testing purposes only:
    //txt = txt + "<br /><a href='javascript:showCenter()'>coords</a>";
    
    document.getElementById("allpins").innerHTML = txt; 
  }


  function showLocation(index) {  
    map.closeInfoWindow();
    if ((index >= 0) && (index < dbmarkers.length)) {
      map.setCenter(dbmarkers[index][IDX_POINT]);
      showMarkerInfo(index);
    } else { 
      map.setCenter(ptCenter, ALL_ZOOM_LEVEL);
    }
  }
    
  function showMarkerInfo(index) {   
    var infotxt = "<strong>" + dbmarkers[index][IDX_NAME] + "</strong><br />&nbsp;<br />" + dbmarkers[index][IDX_INFO];
    marks[index].openInfoWindowHtml(infotxt);
  }
    
  function createMarker(index) {
    var ibase = dbmarkers[index][IDX_ICON];
    if ((ibase == "") || (ibase == null)) {
      ibase = "red";
    }
    var micon = new GIcon(G_DEFAULT_ICON);
    micon.image = "/images/td" + ibase + ".png";

    var marker = new GMarker(dbmarkers[index][IDX_POINT], { icon : micon});
    GEvent.addListener(marker, "click", function() {
      showMarkerInfo(index);
    });
    return marker;
  }

  function getMarkers() {
    for (i = 0; i < dbmarkers.length; i++) {
      marks.push(createMarker(i));
    }
    return marks;
  }
  
  function setupMarkers() {
    // Stopped using the marker manager 'cos it seemed to cause flicker 
    // with non-default icons. Simple .addOverlay doesn't seem to have 
    // this problem, especially with the small number we are dealing with.
//    mmgr = new GMarkerManager(map);
//    mmgr.addMarkers(getMarkers(), 1);
//    mmgr.refresh();

    var m = getMarkers();
    for (i = 0; i < m.length; i++) {
      map.addOverlay(m[i]);
    }  
  }

  function setupMap() {
    if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map"));
      map.addControl(new GMapTypeControl());
      map.addControl(new GLargeMapControl());
      map.addControl(new GOverviewMapControl());
      map.addControl(new GScaleControl());
      try
        {
          map.setCenter(ptCenter, ALL_ZOOM_LEVEL);
        }
      catch (e)
        {
          // For some reason Opera was failing big time due to 
          // an error somewhere inside the Google API. Adding an empty try...catch
          // "cures" the problem
        }
      window.setTimeout(setupMarkers, 0);        
    } else {
      document.getElementById("map").innerHTML = "This web browser is not compatible with the mapping functionality, sorry."
    }
  }

  function load() {
    initMarkers();
    setupMap();
    listLocations();
  }

  //]]>
