<!-- 
     //  elektronengehirn.net  
     //  matrix.js - Version 1.11
     //  thomas.graf@elektronengehirn.net
// -->

var debug = false;

function Matrix(numOfLeds, matrixWidth, matrixHeight) {
  this.numOfLeds = numOfLeds;
  this.width = matrixWidth;
  this.height = matrixWidth;
  
  this.img_PRE = "led";
  this.img_POST= ".gif";
  this.imgFile_PRE = "m";
  this.imgFile_MID = "_";
  this.imgFile_POST = "";
  this.imgpath;
  
  this.distribution = new Array(this.numOfLeds);
  this._distrSum = 0;
  this._distrTmp = new Array();
  
  this.timer;
  
  this.init = initMatrix;
  this.getDistribution = getDistribution;
  this.setDistribution = setDistribution;
  this.setValueInDistribution = setValueInDistribution;
  this.changeDistribution = changeDistribution;
  this.incDistribution = incDistribution;
  this.decDistribution = decDistribution;
  this.incFlux = incFlux;
  this.decFlux = decFlux;
  this.getFluxDisplay = getFluxDisplay;

  this.changeRandImg = changeRandImg;
  
  this.buildImgFilename  = buildImgFilename;
  this.buildimgTagnameXY = buildimgTagnameXY;
  
  this.init();
}


function initMatrix() {
  // find path to led-images
  _imgName = this.imgFile_PRE + '0' + this.imgFile_MID + '0' + this.imgFile_POST;
  //alert(imgName);
  if (document.images[_imgName]) {
    this.imgpath = document.images[_imgName].src;
    //alert(this.imgpath);
    slashIndex = this.imgpath.lastIndexOf("/");
    if (slashIndex>1  &&  slashIndex!=this.imgpath.length-1)
    	this.imgpath = this.imgpath.substring(0,slashIndex+1);
  } else {
    this.imgpath = "images/";
  }
  //alert(this.imgpath);

  // init distribution
  _distr = new Array(this.numOfLeds);
  for (i=0; i<this.numOfLeds; i++) 
    _distr[i]=1;
  this.setDistribution( _distr );
}

function getDistribution() { 
  return this.distribution; 
}
function setDistribution(array) { 
  if (array.length != this.numOfLeds)
    return false;
    
  this.distribution = array;
  this._distrSum = 0;
  for (i=0; i<this.numOfLeds; i++)
    this._distrSum += this.distribution[i];

  this._distrTmp = new Array(this._distrSum);
  n = 0;
  for (i=0; i<this.numOfLeds; i++)
    for (j=0; j<this.distribution[i]; j++)
      this._distrTmp[n++] = i;
  //alert(this._distrTmp);
  return true;
}
function setValueInDistribution(index, value) {
  var _distr = new Array();
  _distr = this.getDistribution();       
  if (index <= this.distribution.length ) { // &&  this.distribution[index] > 0) {
    this.distribution[index] = value;    
    if (this.distribution[index] < 0) 
      this.distribution[index] = 0;
    this.setDistribution( _distr );
  }
  statusdebugtext( this.distribution );
}
function changeDistribution(index, value) {
  if (index <= this.distribution.length ) { // &&  this.distribution[index] > 0) {
    currentVal = this.distribution[index];
    this.setValueInDistribution(index, currentVal + value);
  }
}
function incDistribution(index) {
  this.changeDistribution( index, 1 );
}
function decDistribution(index) {
  this.changeDistribution( index, -1 );
}
function incFlux() {
  var newVal;
  current = this.distribution[0];
  if (current<=4)
    newVal = current-1;
  else if (current<=6)
    newVal = 4;
  else
    newVal = Math.round(current/2);
  if (newVal<0) newVal=0;
  this.setValueInDistribution( 0, newVal );
  statustext(this.getFluxDisplay());// + this.distribution);
}
function decFlux() {
  var newVal;
  current = this.distribution[0];
  
  if (current<4)
    newVal = current+1;
  else if (current<6) //==4
    newVal = 6;
  else
    newVal = current*2;
  /*
  switch(current)
  {
    case 0:  
    case 1:  
    case 2:  
    case 3:  newVal=current+1; break;
    case 4:  newVal=6; break;
    default:  newVal=current*2; break;
    //default: newVal=current*current;
  } 
  */ 
  if (newVal > 192) newVal=192;
  this.setValueInDistribution( 0, newVal );
  statustext(this.getFluxDisplay());// + this.distribution);
}

function getFluxDisplay() {
  max = 11;
  display = "";
  on  = "||||";
  off = "....";
  spacer = "";
  currentFlux = this.distribution[0];
  for (flux=0; flux<currentFlux; ) {
     display += off + spacer;
     if (flux<4)
       flux += 1;
     else if (flux<6) //==4
       flux = 6;
     else
       flux *= 2;
  }
  for (i=display.length; i<max*on.length; i+=on.length)
     display = on + spacer + display;

  //return "neuronic flux  (" + currentFlux + ")  " + display;
  return "neuronic flux  " + display;
}


function buildImgFilename( imgNr ) {
  return (this.imgpath +  this.img_PRE + imgNr + this.img_POST);
}
function buildimgTagnameXY( x, y ) {
  return (this.imgFile_PRE +  y  + this.imgFile_MID +  x  + this.imgFile_POST);
}


function changeRandImg() {
   x = getRandInt(this.width);
   y = getRandInt(this.height);
   imgNr = this._distrTmp[ getRandInt(this._distrSum) ];
   
   imgTagname = this.buildimgTagnameXY(x,y);
   imgFilename = this.buildImgFilename(imgNr);
   //statustext(imgTagname + " = " + imgFilename);
   if ((img=document.images[imgTagname]))
        img.src = imgFilename;
}

/////////////////////////////////////////////////////////////////////////////////

function getRandInt(n) {      //  0 <= rand < n
   return Math.floor( n * Math.random() );
}


function statustext(text)
{
 window.defaultStatus = text;
 //status=text;
 //if(window.statusbar && window.statusbar.visible == true) window.defaultStatus = text;
 //else alert(text);
}
function statusdebugtext(text)
{
 if (debug)
 statustext( text + window.defaultStatus );
}

