// What blinkenlichten site could be complete without lichten that blinken?

// Lichten globals
var size = 7;
var CurMap = new Array();
var NextMap = new Array();
var Licht = new Array();
var lastpop = 20;
var lastlastpop = 20;
var popvar = 100;
var dx = 1-(Math.floor(Math.random()*3));
var dy = 1-(Math.floor(Math.random()*3));

// Screenshot globals
var Screenshots = new Array();
var SelectedScreenshot = false;
var AutoAdvanceTime = 8000;
var OnScreenshot = 0;
var ScreenshotClicked = false;
var LoadingImg = false;
var GlowTimer = 0;

// Create lichten array
for (x=0; x<size; x++) {
  var s = 4;
  CurMap[x] = new Array();
  NextMap[x] = new Array();
  Licht[x] = new Array();
  for (y=0; y<size; y++) {
    CurMap[x][y] = (Math.random()>0.5)?true:false;
    NextMap[x][y] = (Math.random()>0.5)?true:false;
    px = 288 + s*(y-x);
    py = 5 + s*(y+x-2);

    if ((x>0)&&(y>0)&&(x<size-1)&&(y<size-1)) {
      document.write('<img style=\"position: absolute; width: 4px; height: 5px; top: '+py+'px; left: '+px+'px; visibility: hidden;" id="l'+x+'-'+y+'" src="images/licht.gif" onMouseOver="ToggleLicht(this)">');
      document.write('<div style=\"position: absolute; width: 4px; height: 5px; top: '+py+'px; left: '+px+'px; visibility: visible;" id="s'+x+'-'+y+'" onMouseOver="ToggleLicht(this)"></div>');
    }
  }
}

// Calls on document load
function StartLichten() {
  if (document.getElementById("ScreenshotDisplay")) {
    // Set up screenshot stuff, if screenshots exist
    LoadingImg = new Image();
    LoadingImg.src = "images/loading.gif";

    var n = 1;
    while (document.getElementById("shot"+n)) {
      var ss = Screenshots[n-1] = new Object;
      ss.link = document.getElementById("shot"+n);
      ss.thumb = document.getElementById("thumb"+n);
      ss.desc = ss.link.getAttribute("description");
      n += 1;
    }

    ShowScreenshot(0);
    window.setTimeout("AutoAdvance()", AutoAdvanceTime);
    window.setInterval("ScreenshotGlow()", 15);
  }

  // Set up lichten states
  for (x=0; x<size; x++) {
    for (y=0; y<size; y++) {
      if ((x>0)&&(y>0)&&(x<size-1)&&(y<size-1)) {
	Licht[x][y] = document.getElementById('l'+x+'-'+y);
	Licht[x][y].style.visibility = 'visible';
      } else {
	Licht[x][y] = false;
      }
    }
  }

  // Start the blinken!
  Blinken();
}

function ToggleLicht(i) {
  // Mouse-over action on lichts
  m = i.id.match(/^s(\d+)-(\d+)$/);
  if (m) {
    var x = m[1];
    var y = m[2];

    CurMap[x][y] = (CurMap[x][y])?false:true;
    NextMap[x][y] = CurMap[x][y];
    Licht[x][y].style.visibility = CurMap[x][y]?'visible':'hidden'; 
  }
}

function Blinken() {
  // Blink the lichts using a Game-Of-Life-like procedure
  window.setTimeout("Blinken()", 80);

  var pop = 0;
  var m = NextMap;
  NextMap = CurMap;
  CurMap = m;

  for (x=0; x<size; x++) {
    for (y=0; y<size; y++) {
      neighbors = 0;
      for (nx=-1; nx<=1; nx++) {
	sx = (x+nx+size-dx)%size;
	for (ny=-1; ny<=1; ny++) {
	  if ((nx != 0) || (ny != 0)) {
	    sy = (y+ny+size+dy)%size;
	    neighbors += CurMap[sx][sy]?1:0;
	  }
	}
      }
      if ((neighbors <= 1) || (neighbors >= 4)) {
	NextMap[x][y] = false;
      } else if (neighbors == 3) {
	NextMap[x][y] = true;
      } else {
	NextMap[x][y] = CurMap[x][y];
      }

      if (NextMap[x][y] == CurMap[x][y]) { // delayed action looks smooooth!
	if (Licht[x][y]) {
	  Licht[x][y].style.visibility = NextMap[x][y]?'visible':'hidden';
	}
      }

      if (popvar <= 1) {
	if ((x==0)||(x==size-1)||(y==0)||(y==size-1)) {
	  if (Math.random() < 0.4) {
	    NextMap[x][y] = true;
	  }
	}
      }

      pop += NextMap[x][y]?1:0
    }
  }

  if ((popvar <= 1) || (Math.random() < 0.004)) {
    dx = 1-(Math.floor(Math.random()*3));
    dy = 1-(Math.floor(Math.random()*3));
  }

  popvar = Math.floor(popvar*0.3);
  popvar += Math.abs(lastlastpop-pop);
  lastlastpop = lastpop;
  lastpop = pop;
}






function AutoAdvance() {
  // Automatically flip sreenshots
  if (ScreenshotClicked) {return;}
  OnScreenshot = OnScreenshot + 1
  if (OnScreenshot >= Screenshots.length) {
    if (OnScreenshot == 0) {return;}
    OnScreenshot = 0;
  }
  ShowScreenshot(OnScreenshot);
  window.setTimeout("AutoAdvance()", AutoAdvanceTime);
}

function ClickScreenshot(n) {
  // User clicks a screenshot
  ScreenshotClicked = true;
  ShowScreenshot(n-1);
  return false;
}

function ShowScreenshot(n) {
  // Show screenshot and description
  if (Screenshots[n]) {
    var ss = Screenshots[n];
    document.getElementById("ScreenshotDisplay").src = LoadingImg.src;
    document.getElementById("ScreenshotDesc").innerHTML = ss.desc;
    document.getElementById("ScreenshotDisplay").src = ss.link.href;

    if ((SelectedScreenshot) && (SelectedScreenshot != ss)) {
      ss.thumb.style.borderColor = SelectedScreenshot.thumb.style.borderColor;
      ss.thumb.style.borderWidth = SelectedScreenshot.thumb.style.borderWidth;
      SelectedScreenshot.thumb.style.borderColor = "";
      SelectedScreenshot.thumb.style.borderWidth = "";
    }
    SelectedScreenshot = ss;
  }

  return false;
}

function ScreenshotGlow() {
  // You see this glowing?  Not a lot of screenshots can do that.
  GlowTimer += 1;
  phase = (Math.sin(GlowTimer/15)+1)/2;
  aphase = 1-phase;
  r = Math.floor(200 * phase + 255 * aphase);
  g = Math.floor( 20 * phase + 230 * aphase);
  b = Math.floor(  0 * phase + 100 * aphase);
  SelectedScreenshot.thumb.style.borderColor = "rgb("+r+","+g+","+b+")";  
  SelectedScreenshot.thumb.style.borderWidth = "1px 4px";
}
