/***************************************************************************
 *   Copyright (C) 2008, Paul Lutus                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

if (typeof document.attachEvent!='undefined') {
  window.attachEvent('onload',hist_setup);
}
else {
  window.addEventListener('load',hist_setup,false);
}

var years = 24;
var bar_array = new Array(years+1);
var year_array = new Array(years+1);
var low_year = 2;
var min_hist_y = 8;
var neg_max = -50;
var year;
var duration = 1000;
var interval = 20;
var timer = null;
var timer_interval_milliseconds = 30;
var hist_title = document.createTextNode("");

function hist_setup() {
  title_div = document.getElementById("hist_title_span");
  title_div.appendChild(hist_title);
  var histogram = document.getElementById("histogram");
  for(i = low_year;i <= years;i++) {
    var div = document.createElement("div");
    div.className = "histogram_bar";
    var tn = document.createTextNode(i);
    var span = document.createElement("span");
    span.appendChild(tn);
    var img = document.createElement("img");
    img.src = "images/cicada_small.png";
    img.style.marginRight = "-37px";
    div.appendChild(span);
    div.appendChild(img);
    histogram.appendChild(div);
    bar_array[i] = div;
  }
  init_values();
}

function init_values() {
  year = 0;
  for(i = 0;i <= years;i++) {
    year_array[i] = min_hist_y;
  }
  update_hist_display();
}

function update_hist_display() {
  y = "" + year;
  while(y.length < 4) {
    y = "0" + y;
  }
  hist_title.nodeValue = "Year " + y;
  for(var i = low_year;i <= years;i++) {
    var v = year_array[i];
    v = ( v < min_hist_y)?min_hist_y:v;
    bar_array[i].style.width = v + "%"
  }
}

function callback_sim() {
  var cy,py,ty,y;
  var cicadas;
  // cy = cicada cycle year
  for(cy = 2; cy <= years;cy++) {
    cicadas = 0;
    // py = predator cycle year
    for(py = 2;py <= years;py++) {
      ty = year+interval;
      for(y = year;y < ty;y++) {
        // are cicadas and predators both present at once?
        if ((y % py == 0) && (y % cy == 0)) {
          // let predators eat a random number of cicadas
          cicadas -= (Math.random() * 200) + 25;
          // after the feast, set a minimum cicada count
          cicadas = (cicadas < neg_max)?neg_max:cicadas;
        }
        // let cicadas reproduce randomly
        cicadas += Math.random() * 4.5;
      }
    }
    // accumulate cicada results
    year_array[cy] += cicadas / ((cy+5) * interval);
  }
  year += interval;
  update_hist_display(year);
  if(year < duration) {
    timer = setTimeout("callback_sim()",timer_interval_milliseconds);
  }
}

function run_sim() {
  clearTimeout(timer);
  year = 0;
  init_values();
  callback_sim();
  return false;
}
function stop_sim() {
  clearTimeout(timer);
  init_values();
}
