﻿var map; 
var geocoder;
var cpList = "";

var flagsPath = "images/flags/";
var princeRupert = new function() { // not actually prince rupert, we've changed the coords to skidgate to centre the map for the first stage
   this.lat = 53.2484504;
   this.lng = -131.9971045;
}

var CPs = new Array(
   {"cp":"S",     "ta":null,  "lat":53.2484504,      "lng":-131.9971045,    
      "name":"The racers start from this point but will return here two more times as it also serves as CP11 and CP21/TA4",
      "obj":null},
   {"cp":"1",     "ta":null,  "lat":53.1486482,      "lng":-132.2149512,    "name":"boat drop",               "obj":null},
   {"cp":"2",     "ta":null,  "lat":53.2148480,      "lng":-132.3929496,    "name":"<br/>",               "obj":null},
   {"cp":"3",     "ta":"1",   "lat":53.3075495,      "lng":-132.4218608,    "name":"trek to bike",               "obj":null},
   {"cp":"4",     "ta":"2",   "lat":53.6156660,      "lng":-132.3150078,    "name":"bike to boat",               "obj":null},
   {"cp":"5",     "ta":"3",   "lat":53.6876566,      "lng":-132.1865751,    "name":"boat drop bike pick up",               "obj":null},
   {"cp":"6",     "ta":"4",   "lat":54.0697686,      "lng":-131.7926734,    "name":"bike to trek",               "obj":null},
   {"cp":"7",     "ta":"4",   "lat":54.1535895,      "lng":-131.6684641,    "name":"unmanned trek",               "obj":null},
   {"cp":"8",     "ta":null,  "lat":54.1101294,      "lng":-131.6716219,    "name":"unmanned trek",               "obj":null},
   {"cp":"9",     "ta":null,  "lat":53.5112450,      "lng":-131.9577944,    "name":"unmanned trek",               "obj":null},
   {"cp":"11",    "ta":"5",   "lat":53.2484504,      "lng":-131.9971045,    "name":"CP11 is a bike drop.  This location is also served as the start of the race and will be revisited by the racers as CP21.",               "obj":null},
   {"cp":"12",    "ta":null,  "lat":53.1518077,      "lng":-132.0787863,    "name":"boat drop / trek",               "obj":null},
   {"cp":"13",    "ta":null,  "lat":53.0200445,      "lng":-132.0848604,    "name":"trek",               "obj":null},
   {"cp":"14",    "ta":null,  "lat":53.0520363,      "lng":-132.0277299,    "name":"trek to boat pick up",               "obj":null},
   {"cp":"14",    "ta":"6",   "lat":53.1178609,      "lng":-131.7005380,    "name":"boat to bike",               "obj":null},
   {"cp":"15",    "ta":null,  "lat":53.1476796,      "lng":-131.7603452,    "name":"boat to bike",               "obj":null},
   {"cp":"16",    "ta":null,  "lat":53.0961473,      "lng":-131.8540423,    "name":"nmanned bike",               "obj":null},
   {"cp":"17",    "ta":null,  "lat":53.1046997,      "lng":-131.9568481,    "name":"",               "obj":null},
   {"cp":"18",    "ta":null,  "lat":53.0799882,      "lng":-131.9802838,    "name":"unmanned bike",               "obj":null},
   {"cp":"19",     "ta":null,  "lat":53.0869217,      "lng":-131.9252773,    "name":"",               "obj":null},
   {"cp":"20",    "ta":"7",   "lat":53.2541785,      "lng":-131.8209972,    "name":"bike to boat",               "obj":null},
   {"cp":"21",    "ta":null,  "lat":53.2484504,      "lng":-131.9971045,    "name":"bike drop",               "obj":null},
   {"cp":"22",    "ta":null,  "lat":54.2868533,      "lng":-130.3600463,    "name":"",               "obj":null},
   {"cp":"23",    "ta":"8",   "lat":54.3129785,      "lng":-130.3333309,    "name":"",               "obj":null},
   {"cp":"24",    "ta":"9",   "lat":54.3021653,      "lng":-129.9892224,    "name":"",               "obj":null},
   {"cp":"25",    "ta":"10",  "lat":54.2481093,      "lng":-130.1990535,    "name":"",               "obj":null},
   {"cp":"26",    "ta":"11",  "lat":54.2977853,      "lng":-130.2531897,    "name":"",               "obj":null},
   {"cp":"27",    "ta":null,  "lat":54.2836696,      "lng":-130.3190932,    "name":"",               "obj":null},
   {"cp":"F",     "ta":null,  "lat":54.3171040,      "lng":-130.3215050,    "name":"Finish",               "obj":null}






);


function load() {
   if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("courseMap"));
      geocoder = new GClientGeocoder();
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      
      map.setCenter(new GLatLng(princeRupert.lat, princeRupert.lng), 7);
      loaded();
   }
}


function loaded() {
   map.setMapType(G_HYBRID_TYPE); 
   plotCPs();
   initLightbox();
}



function createIcon(urlImage, urlShadow) {
   var icon = new GIcon();
   icon.image = urlImage; 
   icon.shadow = urlShadow;
   icon.iconSize = new GSize(34, 56);
   icon.shadowSize = new GSize(34, 56);
   icon.iconAnchor = new GPoint(0, 56);
   icon.infoWindowAnchor = new GPoint(0, 56);
   return icon
}


function createMarker(point, content) {
   var marker = new GMarker(point);
   GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(content);
   });
   return marker;
}


function createMarker2(point, content, iconPath, shadowPath) {
   var icon = createIcon(iconPath, shadowPath);
   var marker = new GMarker(point, icon);
   GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(content);
   });
   return marker;
}

function plotCPs() {
   var iconPath;
   var iconShadowPath;
   var arCP = new Array();
   
   for (i=0; i<CPs.length; i++) {
      //cpList += "<option value\"" + CPs[i].cp + "\">" + "&nbsp;" + CPs[i].cp + "</option>";
      cpList += "<a href=\"#\" onclick=\"moveTo(" + i + ")\">" + CPs[i].cp + "</a> &nbsp;"
      arCP.push(CPs[i].cp);
      if (!isNaN(CPs[i].cp))
      {
         if (CPs[i].ta)
         {
            iconPath = flagsPath + "ta" + CPs[i].cp + ".png";
         }
         else
         {
            iconPath = flagsPath + "cp" + CPs[i].cp + ".png";
         }   
         iconShadowPath = flagsPath + "shadow.png";
      }
      else
      {
         switch(CPs[i].cp)
         {
            case "S":
               iconPath = flagsPath + "start.png";
               iconShadowPath = flagsPath + "shadow.png";
               break;
            case "F":
               iconPath = flagsPath + "finish.png";
               iconShadowPath = flagsPath + "shadow.png";
               break;
            default:
               iconPath = "http://labs.google.com/ridefinder/images/mm_20_red.png";
               iconShadowPath = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
               break;
         }
      }
      
      var icon = createIcon(iconPath, iconShadowPath);
      var point = new GLatLng(CPs[i].lat, CPs[i].lng);
      var content = "CP" + CPs[i].cp + " " + CPs[i].name + "<br/>" + ((CPs[i].ta != null) ? ("TA" + CPs[i].ta + "<br/>"): "") + "lat: " + CPs[i].lat + "<br/>" + "lng: " + CPs[i].lng;
      map.addOverlay(createMarker2(point, content, iconPath, iconShadowPath));
   }
   
   document.getElementById("cpList").innerHTML = cpList;
}


function moveTo(cp) {
   map.setCenter(new GLatLng(CPs[cp].lat, CPs[cp].lng), 13);
   document.getElementById("cpInfo").innerHTML = CPs[cp].name;
}

