var mode_array = [
  {id: 0, name: 'Taxi',            commutetype: 'taxi',         pounds: 1.51, image: 'images/icon_lg/taxi.gif', image2: 'images/icon_sm/taxi.gif', action: 'Riding in', promote: 0, explanatorytext: 'Taking a taxi is one of the most carbon-intensive ways to travel around New York City.  Current taxis -- 92% of which are big, heavy Crown Victorias -- emit about 1.5 pounds of carbon dioxide per mile.  Plus, they contribute to a host of other problems like air pollution and dangerous, congested streets.<br /><br />', factboxtext: 'New York City has recently enacted an important policy to improve the fuel economy of taxis.  Though this is a critical step, even the new, hybrid taxis will still not be nearly as efficient as riding public transportation or riding a bike.  '},
  {id: 1, name: 'SUV',             commutetype: 'SUV',          pounds: 1.60, image: 'images/icon_lg/suv.gif', image2: 'images/icon_sm/suv.gif', action: 'Driving', promote: 0, explanatorytext: 'An SUV is one of the most carbon-intensive modes of transportation in New York City.  Current, large SUVs release about 1.6 pounds of CO<sub>2</sub> per mile traveling in the City.  This carbon really adds up -- and doesn\'t even take into account the host of other problems caused by driving an SUV in the City, problems like increased air pollution, dangerous sight-lines, spatial inefficiency and a design that increases the severity of pedestrian injuries in crashes.<br /><br />', factboxtext: 'When Ken Livingstone was Mayor of London, he thought SUVs so environmentally damaging that he levied a $50 charge against their use in Central London.'},
  {id: 2, name: 'Car',             commutetype: 'car',          pounds: 1.22, image: 'images/icon_lg/car.gif', image2: 'images/icon_sm/car.gif', action: 'Driving', promote: 0, explanatorytext: 'An average car in New York City emits about 1.2 pounds of CO<sub>2</sub> per mile. And although 25% of car trips in New York City are one mile or less, 50% are three miles or less and 77% are five miles or less, all that CO<sub>2</sub> adds up. If just 10% of all people commuting by private car or taxi switched to public transit, bicycling, or walking just once per week, we would save 140 million pounds of CO<sub>2</sub> emissions per year.  That\'s the same amount of CO<sub>2</sub> emitted by the homes of almost 30,000 New Yorkers.<br /><br />', factboxtext: 'In some NYC neighborhoods, between 28% and 45% of traffic is cruising for a parking spot.'},
  {id: 3, name: 'Hybrid Car',      commutetype: 'hybrid car',   pounds: 0.42, image: 'images/icon_lg/hybrid.gif', image2: 'images/icon_sm/hybrid.gif', action: 'Driving', promote: 0, explanatorytext: 'By choosing a hybrid car, you\'ve taken an important step towards limiting your carbon footprint.  An average hybrid is 25-30% more efficient than a comparable gasoline vehicle, though probably even more efficient in city conditions. However, the use of any car, even a hybrid in an urban environment, imposes additional problems like traffic congestion, unsafe streets and spatial inefficiencies that make biking, walking and mass transit less alacritious.  Even daily use of a hybrid is not a sustainable transportation solution: If everyone drove one, city streets would still be jam-packed.<br /><br /> ', factboxtext: 'A single-occupancy hybrid car takes up as much street space as a dozen pedestrians, five bikes, or one fifth of a bus filled with 50 people.'},
  {id: 4, name: 'Bus',             commutetype: 'bus',          pounds: 0.52, image: 'images/icon_lg/bus.gif', image2: 'images/icon_sm/bus.gif', action: 'Riding the', promote: 1, explanatorytext: 'By taking the bus, you made a convenient and socially responsible decision. You not only avoided the hassles and costs of parking and driving, but also did something environmentally and civically responsible. If just 10% of drivers and taxi users took the bus one day a week, the City would emit 68.5 million fewer pounds of CO<sub>2</sub>.<br /><br />', factboxtext: 'New York\'s mass transit system moves approximately 40 percent of all people traveling by motorized transportation on a typical weekday (excluding heavy trucks), while resulting in only 12% of transportation CO<sub>2</sub> emissions, and 3 percent of overall CO<sub>2</sub> emissions.'},
  {id: 5, name: 'Subway/Train',    commutetype: 'subway or train', pounds: 0.25, image: 'images/icon_lg/subway.gif', image2: 'images/icon_sm/subway.gif', action: 'Riding the', promote: 1, explanatorytext: 'New York City\'s subway is not only one of the most extensive public transportation systems in the world, with 468 reported passenger stations and a 24 hour schedule, but also one of the easiest, cleanest, cheapest and carbon-efficient ways to commute. If just 10% of drivers and taxi users took the Subway one day a week, the City would emit 48 million fewer pounds of CO<sub>2</sub>', factboxtext: 'One in four transit trips made in the United States each day is made in New York City.'},
  {id: 6, name: 'Bike/Walk/Skate', commutetype: 'NMT',          pounds: 0,    image: 'images/icon_lg/bike.gif', image2: 'images/icon_sm/bike.gif', action: '', promote: 2, explanatorytext: 'About 11% of New Yorkers walk, bike, skate and scoot to work each day. Around 800,000 walk to work, and roughly 45,000 commute by bike. In fact, there has been a surge in the popularity of biking in NYC in recent years. Regular cycling in NYC has increased by about 33% over the past decade so that now there are roughly 143,000 people riding bikes everyday.  In the past few years, New York City has made sizeable efforts to boost biking, walking and non-motorized transit. The Bloomberg administration committed to installing 200 miles of bike lanes and 40 miles of greenway by 2009 and completing the 1,800 mile bike master plan by 2030. They\'ve also made efforts to put a pedestrian plaza in every New York City neighborhood and make intersections and sidewalks safer and simpler for every New Yorker.', factboxtext: ' If New York City had the same commute pattern as Copenhagen, where 35% of commuters ride a bike, 9% walk or telecommute, 33% use transit and 23% drive, the City\'s annual carbon footprint would be 900 million pounds less.'},
  ];
 
// tree planting factor: lbs of carbon per tree, based on 25 year lifespan 
var treefactor = 253.75;

var numberofmodes = mode_array.length;


function displaymode() {
  document.getElementById("modeicon").innerHTML = "<img src='" + mode_array[document.modeform.modetype.selectedIndex].image + "'>";
  if (document.getElementById("factboxtext").innerHTML == "") {
    document.getElementById("factboxtext").innerHTML = "A small change in your commute can make a big impact on your carbon footprint.";
  }  
  if (!document.modeform.miles.value) {
    document.getElementById("results").style.background = "none";
  }

}


function get_carbon_output($location) {
  var mode = "";
  if (($location == 'index') || ($location == 'calc')) {
    window.document.location.href = 'calc.html?mode=' + document.modeform.modetype.selectedIndex + '&distance=' + document.modeform.miles.value;
  }
 
  if (document.location.href.search(/\?/) != -1) {
    var length1 = document.location.href.length;
    var delim1 = document.location.href.indexOf('?');
    var arguments = document.location.href.substr(delim1+1,length1 - delim1); 
    var delim2 = arguments.indexOf('&');
    var length2 = arguments.length;
    var modechunk = arguments.substr(0, delim2);
    var distancechunk = arguments.substr(delim2+1, length2);
    var mode = modechunk.substr(modechunk.indexOf('=')+1, modechunk.length);
    var distance = distancechunk.substr(distancechunk.indexOf('=')+1, distancechunk.length);
    document.modeform.miles.value = distance;
    document.modeform.modetype[mode].selected = true;
  } else {
    return(0);
  }

  // clear previous results
  document.getElementById("results").innerHTML = "";
  document.getElementById("explanatorytext").innerHTML = ""; 
  document.getElementById("factboxtext").innerHTML = ""; 
  document.getElementById("trees").innerHTML = "";

  if ((isNaN(document.modeform.miles.value)) || (document.modeform.miles.value <= 0)) {
    document.getElementById("results").innerHTML = "Sorry, please enter a valid number.";
    document.getElementById("explanatorytext").innerHTML = "";	
    document.getElementById("results").style.background = "none"; 
   return (0)
  }
  
  // use Math.round to fix Javascript float multiplication errors; we multiply and then divide by 1000 in order to get 3 decimals of accuracy
  var carbon_output = Math.round((mode_array[document.modeform.modetype.selectedIndex].pounds * document.modeform.miles.value * 2)*1000)/1000;
  var treefrequency = Math.round(treefactor/carbon_output*1)/1;
  var treesperyear  = Math.round(365/(treefactor/carbon_output)*1)/1;

  var treefrequency_word = "days";
  var treesperyear_word = "trees";

  if (treefrequency == 1) {
    treefrequency_word = "day";
  }
  if (treesperyear == 1) {
    treesperyear_word = "tree";
  }

  if (mode_array[document.modeform.modetype.selectedIndex].promote == 0) {
    document.getElementById("results").innerHTML = "Your " + document.modeform.miles.value * 2 + " mile " + mode_array[document.modeform.modetype.selectedIndex].commutetype + " commute generates " + carbon_output.toString() + " pounds of carbon dioxide.<br /><br />";
    document.getElementById("results").innerHTML += "To offset all that carbon, you'd need to plant a tree every " + treefrequency + " " + treefrequency_word + ", or " + treesperyear + " " + treesperyear_word + " per year.<br />";
    document.getElementById("explanatorytext").innerHTML = mode_array[document.modeform.modetype.selectedIndex].explanatorytext;
    document.getElementById("factboxtext").innerHTML = mode_array[document.modeform.modetype.selectedIndex].factboxtext;
    document.getElementById("modeswitch").innerHTML = "To see how much carbon you would generate with another mode, click one of the icons below:<br />";
    for (var j=0; j < document.modeform.modetype.length; j++) {
      if (j != document.modeform.modetype.selectedIndex) {
        document.getElementById("modeswitch").innerHTML += "<a href='calc.html?mode=" + j+ "&distance="+ document.modeform.miles.value + "'><img src='" + mode_array[document.modeform.modetype[j].value].image2 + "'></a>";
      }
    }
    if (treesperyear > 50) {
      document.getElementById("trees").innerHTML = "<img src='images/forest.jpg'><br /><br />";
      document.getElementById("trees").innerHTML += "Looks like you've got to plant a whole forest!";
    } else {
      var tree_counter = 0;
      document.getElementById("trees").innerHTML = "";
      var tree_string = "";
      while (tree_counter < treesperyear) {
        tree_string += "<img src='images/tree.gif'><nbsp>";
        tree_counter++;
      } 
      document.getElementById("trees").innerHTML = tree_string;
    }
  } else {
    if (mode_array[document.modeform.modetype.selectedIndex].promote == 1) {
      document.getElementById("results").innerHTML = mode_array[document.modeform.modetype.selectedIndex].action + " " + mode_array[document.modeform.modetype.selectedIndex].commutetype + " today, you put " + carbon_output.toString() + " pounds of carbon dioxide into the atmosphere.<br />";
      document.getElementById("results").style.width = "350px";
      document.getElementById("results").style.background = "none";
      document.getElementById("trees").style.display = "none";


    } else if (mode_array[document.modeform.modetype.selectedIndex].promote == 2) {
      document.getElementById("results").innerHTML = "During your commute you generated no carbon dioxide. What's more, you probably enjoyed some sights and smells and got some exercise too.<br />";
      document.getElementById("results").style.width = "350px";
      document.getElementById("results").style.background = "none";
      document.getElementById("trees").style.display = "none";

    }
    document.getElementById("explanatorytext").innerHTML = mode_array[document.modeform.modetype.selectedIndex].explanatorytext;
    document.getElementById("factboxtext").innerHTML = mode_array[document.modeform.modetype.selectedIndex].factboxtext;

    document.getElementById("modeswitch").innerHTML = "<strong>To see how much carbon you would generate with another mode, click one of the icons below:</strong><br />";
    for (var j=0; j < document.modeform.modetype.length; j++) {
      if (j != document.modeform.modetype.selectedIndex) {
        document.getElementById("modeswitch").innerHTML += "<a href='calc.html?mode=" + j+ "&distance="+ document.modeform.miles.value + "'><img src='" + mode_array[document.modeform.modetype[j].value].image2 + "'></a>";
      }
    }
  }
}


