var aZasobnik = new Array();
var aAutomat = new Array();
var iObrazku = 56;
var iOnLoad = 0;

function fInitAutomat() {
  var objObr;

  for (i = 1; i <= iObrazku; i++) {
    if(i < 10) {
      sSoubor = "0" + i + ".gif";
    } else {
      sSoubor = "" + i + ".gif";
    };
    

    objObr = new Image(132,132);
    objObr.onload = fPoNahraniObrazku;
    objObr.src = "./obr/zasobnik/" + sSoubor;

    aZasobnik.push(objObr);
  };

  aAutomat.push(new clsPolicko(document.p1, aZasobnik));
  aAutomat.push(new clsPolicko(document.p2, aZasobnik));
  aAutomat.push(new clsPolicko(document.p3, aZasobnik));
  aAutomat.push(new clsPolicko(document.p4, aZasobnik));
}

function fPoNahraniObrazku() {
  if(++iOnLoad >= iObrazku) {
    for(i in aAutomat) {
      fAutomatVymena(i);
    };
  };
}

function fAutomatVymena(iPolicko) {
  aAutomat[iPolicko].fVymena();
  setTimeout("fAutomatVymena(" + iPolicko + ")", aAutomat[iPolicko].fVratTimeOut());
}

function clsPolicko(objObr, aObrBuf) {
  this.objObrazek = objObr;
  this.aZasobnik = aObrBuf
}

clsPolicko.prototype.fVymena = function () {
  var iObrazek;

  iObrazek = Math.round(Math.random() * (this.aZasobnik.length - 1));
  this.objObrazek.src = this.aZasobnik[iObrazek].src;
}

clsPolicko.prototype.fVratTimeOut = function () {
  var iTimeOut;

  iTimeOut = Math.round(Math.random() * 8000);
  if(iTimeOut < 1000) {
    iTimeOut = 1000;
  };

  return iTimeOut;  
}
