var timeplot1;
var timeplot2;
var tg1 = new Timeplot.ReverseTimeGeometry({ gridColor: "#660066", axisLabelsPlacement: "top", labeller: new Timeplot.MADateLabeller("en", SimileAjax.DateTime.getTimezone()),
  max: "0542-01-01", min: "0000-01-01", timeZone: 0});
var tg2 = new Timeplot.ReverseTimeGeometry({ gridColor: "#660066", axisLabelsPlacement: "top", labeller: new Timeplot.MADateLabeller("en", SimileAjax.DateTime.getTimezone()),
  max: "0542-01-01", min: "0000-01-01", timeZone: 0});

function onLoad() {

  var toggler;
  var toggler1;

  var esAngiosperms = new Timeplot.DefaultEventSource();
  var esCO2 = new Timeplot.DefaultEventSource();
  var esGymnosperms = new Timeplot.DefaultEventSource();
  var esO2 = new Timeplot.DefaultEventSource();
  var esPaleotemp = new Timeplot.DefaultEventSource();
  var esPteridophytes = new Timeplot.DefaultEventSource();
  var esIceage = new Timeplot.DefaultEventSource();
  var esImpEvents = new Timeplot.DefaultEventSource();
  var esPlantSpecies = new Timeplot.DefaultEventSource();
  var esMassExtinctions = new Timeplot.DefaultEventSource();
  var esLifeCycle = new Timeplot.DefaultEventSource();
  var esMajorGroups = new Timeplot.DefaultEventSource();
  var esCarbonBurial = new Timeplot.DefaultEventSource();
  var esCS = new Timeplot.DefaultEventSource();
  var esTectonicActivity = new Timeplot.DefaultEventSource();
  var esLeafEvo = new Timeplot.DefaultEventSource();
  var esStomata = new Timeplot.DefaultEventSource();
  var esrbcl = new Timeplot.DefaultEventSource();
  var esInsects = new Timeplot.DefaultEventSource();
  var esTetrapods = new Timeplot.DefaultEventSource();
  var esTectonicActivityXML = new Timeplot.DefaultEventSource();

  var dsAngiosperms = new Timeplot.ColumnSource(esAngiosperms,1);
  var dsCO2 = new Timeplot.ColumnSource(esCO2,1);
  var dsGymnosperms = new Timeplot.ColumnSource(esGymnosperms,1);
  var dsO2 = new Timeplot.ColumnSource(esO2,1);
  var dsPaleotemp = new Timeplot.ColumnSource(esPaleotemp,1);
  var dsPteridophytes = new Timeplot.ColumnSource(esPteridophytes,1);
  var dsCarbonBurial = new Timeplot.ColumnSource(esCarbonBurial,1);
  var dsCS = new Timeplot.ColumnSource(esCS,1);
  var dsTectonicActivity = new Timeplot.ColumnSource(esTectonicActivity,1);
  var dsInsects = new Timeplot.ColumnSource(esInsects, 1);
  var dsTetrapods = new Timeplot.ColumnSource(esTetrapods, 1);
  
  var gc = new Timeplot.Color("#555555");
  var g1 = new Timeplot.DefaultValueGeometry({ axisLabelsPlacement: "left", gridType: "short", max: 8000, min: 0, gridColor: gc});
  var g2 = new Timeplot.DefaultValueGeometry({ axisLabelsPlacement: "left", gridType: "short", max: 100, min: 0, gridColor: gc});
  var g3 = new Timeplot.DefaultValueGeometry({ axisLabelsPlacement: "right", gridType: "short", max: 32, min: 0, gridColor: gc});
  var g4= new Timeplot.InverseValueGeometry({ axisLabelsPlacement: "right", gridType: "short", max: 3.2, min: -3.2, gridColor: gc});
  var g5= new Timeplot.DefaultValueGeometry({ axisLabelsPlacement: "left", gridType: "short", max: 710, min: 706, gridColor: gc});
  var g6 = new Timeplot.DefaultValueGeometry({axisLabelsPlacement: "left", gridType: "short", max: 20, min: 0, gridColor: gc});
  var g7 = new Timeplot.DefaultValueGeometry({ axisLabelsPlacement: "left", gridType: "short", max: 710, min: 706, gridColor: gc});
  var g8 = new Timeplot.DefaultValueGeometry({ axisLabelsPlacement: "left", gridType: "short", max: 8, min: 0, gridColor: gc});
  var g9 = new Timeplot.DefaultValueGeometry({ axisLabelsPlacement: "right", gridType: "short", max: 21, min: 0, gridColor: gc});

  var piAngiosperms = Timeplot.createPlotInfo({id: "angiosperms", dataSource: dsAngiosperms, timeGeometry: tg2, valueGeometry: g2,
    lineColor: "rgba(201,18,134, 1)", dotColor: "rgba(201,18,134, 1)", showValues: false, fillColor: "rgba(201,18,134, 0.15)", fillGradient: false});
  var piCO2 = Timeplot.createPlotInfo({id: "co2", dataSource: dsCO2, timeGeometry: tg1, valueGeometry: g1,
    lineColor: "#e70e0e", dotColor: "#e70e0e", showValues: false, valuesBorder: "#ff0000", valuesColour: "#e70e0e", valuesText: "#ffffff"});
  var piGymnosperms = Timeplot.createPlotInfo({id: "gymnosperms", dataSource: dsGymnosperms, timeGeometry: tg2, valueGeometry: g2,
    lineColor: "rgba(18,201,83,1)", dotColor: "rgba(18,201,83, 1)", showValues: false, fillColor: "rgba(18,201,83, 0.15)", fillGradient: false});
  var piO2 = Timeplot.createPlotInfo({id: "o2", dataSource: dsO2, timeGeometry: tg1, valueGeometry: g3,
    lineColor: "#0813ef", dotColor: "#0813ef", showValues: false, valuesBorder: "#0000ff", valuesColour: "#0813ef", valuesText: "#ffffff"});
  var piPaleotemp = Timeplot.createPlotInfo({id: "paleotemp", dataSource: dsPaleotemp, timeGeometry: tg1, valueGeometry: g4,
    lineColor: "#ab520b", dotColor: "#ab520b", showValues: false, roundValues: false, valuesBorder: "#cb5a00", valuesColour: "#ab520b", valuesText: "#ffffff", valuesRight: false});
  var piPteridophytes = Timeplot.createPlotInfo({id: "pteridophytes", dataSource: dsPteridophytes, timeGeometry: tg2, valueGeometry: g2,
    lineColor: "rgba(21,184,199,1)", dotColor: "rgba(21,184,199, 1)", showValues: false, fillColor: "rgba(21,184,199, 0.15)", fillGradient: false});
  var piInsects = Timeplot.createPlotInfo({id: "insects", dataSource: dsInsects, timeGeometry: tg2, valueGeometry: g5,
    lineColor: "#c18e14", dotColor: "#c18e14", showValues: false});
  var piTetrapods = Timeplot.createPlotInfo({id: "tetrapods", dataSource: dsTetrapods, timeGeometry: tg2, valueGeometry: g5,
    lineColor: "#8418b7", dotColor: "#8418b7", showValues: false});
  var piCarbonBurial = Timeplot.createPlotInfo({id: "carbonburial", dataSource: dsCarbonBurial, timeGeometry: tg1, valueGeometry: g8, lineColor: "#5d5d5d", dotColor: "#5d5d5d", showValues: false, roundValues: false, valuesBorder: "#191919", valuesColour: "#5d5d5d", valuesText: "#ffffff"});
  var piCS = Timeplot.createPlotInfo({id: "cs", dataSource: dsCS, timeGeometry: tg1, valueGeometry: g6, lineColor: "#b5098a", dotColor: "#b5098a", showValues: false, roundValues: false, valuesBorder: "#62054b", valuesText: "#ffffff", valuesColour: "#b5098a"});
  var piTectonicActivity = Timeplot.createPlotInfo({id: "tectonicactivity", dataSource: dsTectonicActivity, timeGeometry: tg1, valueGeometry: g7, lineColor: "#157756", dotColor: "#157756", showValues: false, roundValues: false, valuesColour: "#157756", valuesBorder: "#28e3a4", valuesText: "#ffffff"});
  var piTectonicActivityXML = Timeplot.createPlotInfo({id: "tectonicactivityxml", timeGeometry: tg1, lineColor: "#157756", eventSource: esTectonicActivityXML, showLabel: true });
  var piLeafEvo = Timeplot.createPlotInfo({id: "leafevo", eventSource: esLeafEvo, timeGeometry: tg2, lineColor: "rgba(0,240,0,0.4)", showLabel: true, bubbleWidth: 510, bubbleHeight: 600});
  var piStomata = Timeplot.createPlotInfo({id: "Stomata", valueGeometry: g9, eventSource: esStomata, timeGeometry: tg1, lineColor: "rgba(0,240,0,0.4)"});
  var pirbcl = Timeplot.createPlotInfo({id: "rbcl", eventSource: esrbcl, timeGeometry: tg2, lineColor: "rgba(0,240,0,0.4)", showLabel: true, labelLeft: false, bubbleWidth: 600, bubbleHeight: 400});

  var piIceage = Timeplot.createPlotInfo({id: "iceage", eventSource: esIceage, timeGeometry: tg1, lineColor: "#86bbef", bubbleWidth: 300, bubbleHeight: 100});
  var piImpEvents = Timeplot.createPlotInfo({id: "impevents", eventSource: esImpEvents, timeGeometry: tg2, lineColor: "rgba(40,227,164,0.5)", bubbleWidth: 700, bubbleHeight: 350});
  var piPlantSpecies = Timeplot.createPlotInfo({id: "plantspecies", eventSource: esPlantSpecies, timeGeometry: tg2, lineColor: "#169a16", bubbleWidth: 700, bubbleHeight: 500, showLabel: true});
  var piMassExtinctions = Timeplot.createPlotInfo({id: "massextinctions", eventSource: esMassExtinctions, timeGeometry: tg2, lineColor: "rgba(255,0,0,0.4)", eventLineWidth: 10.0, bubbleWidth: 300, bubbleHeight: 100});
  var piLifeCycle = Timeplot.createPlotInfo({id: "lifecycle", eventSource: esLifeCycle, timeGeometry: tg2, lineColor: "rgba(22,154,208,0.5)", bubbleWidth: 650, bubbleHeight: 400, eventLineWidth: 10.0});
  var piMajorGroups = Timeplot.createPlotInfo({id: "majorgroups", eventSource: esMajorGroups, timeGeometry: tg2, lineColor: "rgba(15,195,87,0.4)", bubbleWidth: 650, bubbleHeight: 400, eventLineWidth: 10.0});

  var plotInfo1 = [
    piCO2, piO2, piPaleotemp, piIceage, piCarbonBurial, piCS, piTectonicActivity,
    piStomata, piTectonicActivityXML
  ];

  var plotInfo2 = [
    piAngiosperms, piGymnosperms, piPteridophytes, piImpEvents, piPlantSpecies, piMassExtinctions, piLifeCycle, piMajorGroups, piLeafEvo, pirbcl, piInsects, piTetrapods
  ];

  timeplot1 = Timeplot.create(document.getElementById("timeplot1"), plotInfo1);
  timeplot2 = Timeplot.create(document.getElementById("timeplot2"), plotInfo2);
  
  toggler1 = BN.Toggle.create("bar", timeplot1, 4, "250px");
  toggler2 = BN.Toggle.create("foo", timeplot2, 4, "250px"); 

  toggler1.addPlot("CO2", "http://www.ensemble.ac.uk/projects/plantsci/timeline/data/CO2data.txt", piCO2, dsCO2, ",", esCO2, "txt", "#e70e0e", source[1], true);
  toggler1.addPlot("O2", "http://www.ensemble.ac.uk/projects/plantsci/timeline/data/O2data.txt", piO2, dsO2, ",", esO2, "txt", "#0813ef", source[0], true);
  toggler1.addPlot("paleotemp", "http://www.ensemble.ac.uk/projects/plantsci/timeline/data/paleotemp.txt", piPaleotemp, dsPaleotemp, ",", esPaleotemp, "txt", "#ab520b", source[2], true);
  toggler1.addPlot("iceage", "http://www.ensemble.ac.uk/projects/plantsci/timeline/data/iceage.xml",piIceage, null,"",esIceage,"xml", "#86bbef", source[3]);
 
  toggler2.addPlots("anggympte", [
    BN.Toggle.createItem("http://www.ensemble.ac.uk/projects/plantsci/timeline/data/Angiosperms.txt", piAngiosperms, dsAngiosperms, ",", esAngiosperms, "txt"),
    BN.Toggle.createItem("http://www.ensemble.ac.uk/projects/plantsci/timeline/data/Gymnosperms.txt", piGymnosperms, dsGymnosperms, ",", esGymnosperms, "txt"), 
    BN.Toggle.createItem("http://www.ensemble.ac.uk/projects/plantsci/timeline/data/Pteridophytes.txt", piPteridophytes, dsPteridophytes, ",", esPteridophytes, "txt")], "#000000", source[4]);
  toggler2.addPlot("impevents", "http://www.ensemble.ac.uk/projects/plantsci/timeline/data/ImpEvents.xml", piImpEvents, null, "", esImpEvents, "xml", "#28e3a4", source[10]);
  toggler2.addPlot("plantspec", "http://www.ensemble.ac.uk/projects/plantsci/timeline/data/PlantSpecies.xml", piPlantSpecies, null, "", esPlantSpecies, "xml", "#169a16", source[7]);
  toggler2.addPlot("massex", "http://www.ensemble.ac.uk/projects/plantsci/timeline/data/MassExtinctions.xml",piMassExtinctions, null,"",esMassExtinctions,"xml","#ff0000", source[8]);
  toggler2.addPlot("lifecycle", "http://www.ensemble.ac.uk/projects/plantsci/timeline/data/lifecycle.xml",piLifeCycle, null,"",esLifeCycle,"xml","#169ad0",source[9]);
  toggler2.addPlot("majorgroups", "http://www.ensemble.ac.uk/projects/plantsci/timeline/data/MajorGroups.xml", piMajorGroups, null, "", esMajorGroups,"xml", "#0fc357", source[13]);
    toggler1.addPlot("CarbonBurial", "http://www.ensemble.ac.uk/projects/plantsci/timeline/data/CarbonBurial.txt", piCarbonBurial, dsCarbonBurial, ",", esCarbonBurial, "txt", "#5d5d5d", source[14], true);
    toggler1.addPlot("CS", "http://www.ensemble.ac.uk/projects/plantsci/timeline/data/CS.txt", piCS, dsCS, ",", esCS, "txt", "#b5098a", source[17], true);
    toggler1.addPlots("TectonicActivity", [
      BN.Toggle.createItem("http://www.ensemble.ac.uk/projects/plantsci/timeline/data/TectonicActivity.txt", piTectonicActivity, dsTectonicActivity, ",", esTectonicActivity, "txt", true),
      BN.Toggle.createItem("http://www.ensemble.ac.uk/projects/plantsci/timeline/data/TectonicActivity.xml", piTectonicActivityXML, null, "", esTectonicActivityXML, "xml")], "#157756", source[15]);
    toggler1.addPlot("Stomata", "http://www.ensemble.ac.uk/projects/plantsci/timeline/data/Stomata.xml", piStomata, null, "", esStomata, "xml", "#00f000", source[19]);
    toggler2.addPlot("LeafEvo", "http://www.ensemble.ac.uk/projects/plantsci/timeline/data/LeafEvo.xml", piLeafEvo, null, "", esLeafEvo, "xml", "#00f000", source[18]);
    toggler2.addPlot("rbcl", "http://www.ensemble.ac.uk/projects/plantsci/timeline/data/photosynthesis.xml", pirbcl, null, "", esrbcl, "xml", "#00f000", source[20]);
    toggler2.addPlots("instet", [
      BN.Toggle.createItem("http://www.ensemble.ac.uk/projects/plantsci/timeline/data/Insects.txt", piInsects, dsInsects, ",", esInsects, "txt"),
      BN.Toggle.createItem("http://www.ensemble.ac.uk/projects/plantsci/timeline/data/Tetrapods.txt", piTetrapods, dsTetrapods, ",", esTetrapods, "txt")], "#000000", source[12]);

  timeplot1.putText("title", "Phanerozoic", "", {
    left: 0,
    top: -10,
    width: 1000,
    textAlign: "center"
    });
  timeplot2.putText("title", "Phanerozoic", "", {
    left: 0,
    top: -10,
    width: 1000,
    textAlign: "center"
    });
}            

var resizeTimerID = null;
function onResize() {
  if (resizeTimerID == null) {
    resizeTimerID = window.setTimeout(function() {
      resizeTimerID = null;
      if (timeplot1) timeplot1.repaint();
    }, 0);
  }
}
