var x_max = 8;
var y_max = 14;
var x_line;
var y_line;
var h_step;
var v_step;
var entered = 0;
var picprefix = "pics/b/p_";
var event = 0;

function init() {
  setTimeout("document.getElementById(\"side\").style.backgroundImage=\"url(pics/index.jpg)\"",5000);
  // setInterval("randomEvent()",6000);
}



function dc(name) 
{
  entered = 1;
  var point = name.match(/\d+/g);
  var images = new Array();
  images = document.getElementsByTagName('img');
  for (var i = 0 ; i<images.length ; i++) {
    var scanpoint = images[i].name.match(/\d+/g);
    if (Math.sqrt(Math.pow(Math.max(scanpoint[0],point[0])*20 - Math.min(scanpoint[0],point[0])*20,2) + Math.pow(Math.max(scanpoint[1],point[1])*32 - Math.min(scanpoint[1],point[1])*32,2)) <= 40) {
      images[i].src = "pics/transparent.gif";
    }
  }
}

function c(name) 
{
  entered = 0;
  var point = name.match(/\d+/g);
  var images = new Array();
  images = document.getElementsByTagName('img');
  for (var i = 0 ; i<images.length ; i++) {
    var scanpoint = images[i].name.match(/\d+/g);
    if (Math.sqrt(Math.pow(Math.max(scanpoint[0],point[0])*20 - Math.min(scanpoint[0],point[0])*20,2) + Math.pow(Math.max(scanpoint[1],point[1])*32 - Math.min(scanpoint[1],point[1])*32,2)) <= 40) {
      images[i].src = picprefix+scanpoint[0]+"_"+scanpoint[1]+".jpg";	
    } 
  }
}

function randomEvent() {
  event = Math.floor(Math.random()*4);
  initLine();
}

function initLine()
{ 
  if (entered == 1) {return;}
  // Startkoordinaten fuer die Linie raten
  x_line = Math.floor(Math.random()*x_max);
  y_line = Math.floor(Math.random()*y_max);
  // Startkoordinaten an den Rand schieben
  switch (event) {
    // Auf horizonaler Ebene
    case 0:
    case 1:
      x_line = Math.round(x_line/x_max)*(x_max-1);
      break;
    // Auf vertikaler Ebene
    case 2:
    case 3:
      y_line = Math.round(y_line/y_max)*(y_max-1);
      break;
  }
  // Horizontale Bewegung festlegen
  switch (event) {
    // Keine Bewegung
    case 3:
      h_step = 0;
      break;
    // Je nach Naehe zum Rand - rechts naeher, nach links...
    default:
      h_step = Math.round(x_line/x_max)*(-2) + 1;
  }
  // Vertikale Bewegung festlegen
  // Je nach Naehe zum Rand - rechts naeher, nach links...
  v_step = Math.round(y_line/y_max)*(-2) + 1;
  // Und malen
  drawLine();
}

function drawLine()
{
  if (entered == 1) {undrawLine();return;}
  // Ende, wenn die neuen Koordinaten keine Veraenderung mehr im Bild erzeugen koennen
  if (x_line > x_max+1 || x_line < -2 || y_line > y_max+1 || y_line < -2 ) {return;}
  // Alle "Pixel" anschauen
  var images = new Array();
  images = document.getElementsByTagName('img');
  for (var i = 0 ; i<images.length ; i++) {
    var scanpoint = images[i].name.match(/\d+/g);
    // Aufdecken
    if (scanpoint[0] == x_line && scanpoint[1] == y_line) {
      images[i].src = "pics/transparent.gif";
    }
    /*
    if ((scanpoint[0] == x_line && scanpoint[1] == y_line) ||
        (scanpoint[0] == x_line-h_step && scanpoint[1] == y_line-v_step) ||
        (scanpoint[0] == x_line+h_step && scanpoint[1] == y_line+v_step)) {
      images[i].src = "pics/transparent.gif";
    }
    else if (h_step != 0 && 
             ((scanpoint[0] == x_line+h_step && scanpoint[1] == y_line) ||
              (scanpoint[0] == x_line && scanpoint[1] == y_line-v_step) ||
              (scanpoint[0] == x_line+2*h_step && scanpoint[1] == y_line+v_step))) {
      images[i].src = "pics/transparent.gif";
    }
    */
    // Zudecken
    else if ((scanpoint[0] == x_line-1*h_step && scanpoint[1] == y_line-1*v_step) ||
             (scanpoint[0] == x_line+1*h_step && scanpoint[1] == y_line+1*v_step)) {
      images[i].src = picprefix+scanpoint[0]+"_"+scanpoint[1]+".jpg";
    }
    /*
    else if ((scanpoint[0] == x_line-2*h_step && scanpoint[1] == y_line-2*v_step) ||
             (scanpoint[0] == x_line+2*h_step && scanpoint[1] == y_line+2*v_step)) {
      images[i].src = picprefix+scanpoint[0]+"_"+scanpoint[1]+".jpg";
    }
    else if (h_step != 0 && 
             ((scanpoint[0] == x_line-h_step && scanpoint[1] == y_line-2*v_step) ||
              (scanpoint[0] == x_line+h_step && scanpoint[1] == y_line+2*v_step))) {
      images[i].src = picprefix+scanpoint[0]+"_"+scanpoint[1]+".jpg";
    }
    */
  }
  // Naechsten Schritt vorbereiten
  x_line = x_line + h_step;
  y_line = y_line + v_step;
  setTimeout("drawLine()",31);
}

function undrawLine()
{
  var images = new Array();
  images = document.getElementsByTagName('img');
  for (var i = 0 ; i<images.length ; i++) {
    var scanpoint = images[i].name.match(/\d+/g);
    if ((scanpoint[0] == x_line && scanpoint[1] == y_line) ||
        (scanpoint[0] == x_line-h_step && scanpoint[1] == y_line-v_step)) {
      images[i].src = picprefix+scanpoint[0]+"_"+scanpoint[1]+".jpg";
    }	
    /*
    if ((scanpoint[0] == x_line && scanpoint[1] == y_line) ||
        (scanpoint[0] == x_line-h_step && scanpoint[1] == y_line-v_step) ||
        (scanpoint[0] == x_line-2*h_step && scanpoint[1] == y_line-2*v_step)) {
      images[i].src = picprefix+scanpoint[0]+"_"+scanpoint[1]+".jpg";
    }	
    else if (h_step != 0 && 
             ((scanpoint[0] == x_line+h_step && scanpoint[1] == y_line) ||
              (scanpoint[0] == x_line && scanpoint[1] == y_line-v_step) ||
              (scanpoint[0] == x_line-h_step && scanpoint[1] == y_line-2*v_step))) {
      images[i].src = picprefix+scanpoint[0]+"_"+scanpoint[1]+".jpg";
    }
    */
  }
}
