$(document).ready(function() {
  initializeSelectAlls();

  //round corners
  $('#footer').corner("bottom");

  var map;
  if (shouldInitializeMap()) {
    //initializeMap(); //TODO: uncomment once all pages stop doing their own map init
  } else {
    //google_map_tag not included on this page, or browser not supported
  }

  displayMapObjectsFor(getCurrentRoute());
});

function shouldInitializeMap() {
  return 'function' == typeof GBrowserIsCompatible && GBrowserIsCompatible();
}

function initializeMap() {
  map = new GMap2(document.getElementById("map_canvas"));
  map.addControl(new GLargeMapControl());
  map.setCenter(new GLatLng(41.875696,-87.624207), 11); 
  map.addControl(new GLargeMapControl());
}

function getCurrentRoute() {
  // http://database.chicagorun.org/foo/bar => foo/bar
  var route = window.location.href.split('/').slice(3).join('/')
  // console.log("current route is " + route);
  return route;
}

function displayMapObjectsFor(route) {
  switch (route) {
  case "": //home page
    //console.log("home page");
    initializeMap(); //TODO: remove this line once all pages stop doing their own map init
    showMarathonRoute();
    break;
  //TODO: other pages. currently, each page does its own map initialization, mixed with
  // all the junk it needs to display.
  default : //console.log("no route matched")
  }
}

function showMarathonRoute() {
  geoXml = new GGeoXml("http://atlasrider.com/KMZ/2009ChicagoMarathon.kmz");
  map.addOverlay(geoXml);
} 

/*
  Update the miles field with data from the lap field
  Will be performed on the onclick action?

  NOTE: this function is ridiculously fragile and needs to be rewritten
*/
function convertLapToMiles(){
  var lap = document.forms[0].lap.value;
  if (lap){}
  else{
    document.forms[0].lap.value = 1;
    lap = 1;
  }
  var ran = document.forms[0].laps_ran.value;
  var miles = ran/lap;
  document.forms[0].entry_distance.value = miles;
}

/*
  Update the miles field with data from the lap field
  Will be performed on the onclick action?
*/
function convertGroupLapToMiles(){
  var lap = document.forms[0].lap.value;
  if (lap){}
  else{
    document.forms[0].lap.value = 1;
    lap = 1;
  }
  
  var year = document.forms[0].bulk_date_1i.value
  var month = document.forms[0].bulk_date_2i.value
  var day = document.forms[0].bulk_date_3i.value
  var dteDate
  thisform = document.forms[0];
  dteDate=new Date(year,month-1,day);
  if ((day!=dteDate.getDate()) || ((month-1)!=dteDate.getMonth()) || (year!=dteDate.getFullYear())){
    alert("the date entered is not a valid date, please correct")
    return false
  }
  
  

  for (i=7; i<=thisform.elements.length-2; i=i+5)
  {
    if ((thisform.elements[i].value)&&(thisform.elements[i+1].value))
    {      
      if (isNaN(thisform.elements[i].value) || isNaN(thisform.elements[i+1].value)){
        alert("One of the entries you entered is not a number")
        return false
      }
      if ((thisform.elements[i].value.to_i <= 0) || (thisform.elements[i+1].value.to_i <= 0)){
        alert("One of the entries you entered is not positive")
        return false
      }
      if (thisform.elements[i].value && thisform.elements[i+1].value){
        thisform.elements[i+3].value=(thisform.elements[i].value)/lap;
        thisform.elements[i+2].value=""+year+"-"+month+"-"+day;
      }
    }
  }
  return true
}

/*
  Select All checkboxes in tables
*/
function initializeSelectAlls() {
  $("table").each(function() {
    var $thisTable = $(this);

    // 'select all' functionality for checkbox in thead
    $thisTable.find("thead :checkbox").click(function() {
      var selectAll = this.checked;
      $thisTable.find('tbody :checkbox').each(function() {
	this.checked = selectAll;
      });
    });
  });
}
