function pushposition (positions, circles) {
  for (var i = positions.length; i < 5; i++) {
    var tmp = new Array();
    for (var j = 0; j < circles.length; j++) {
      tmp.push(j);
      for (var k = 0; k < positions.length; k++) {
        if (positions[k] == j) {
          tmp.pop();
          break;
        }
      }
    }
    positions[i] = tmp[Math.floor(Math.random() * tmp.length)];
  }
}

function changecircle (position, elm, newcircles) {
  var tmp = new Array();
  for (var j = 1; j < 38; j++) {
    tmp.push(j);
    for (var k = 0; k < newcircles.length; k++){
      if (newcircles[k] == j) {
        tmp.pop();
        break;
      }
    }
  }
  if (elm[position][0] == 'bol125menu' || elm[position][0] == 'bol180c') {
    for (var j = tmp.length-1; j >= 0; j--) {
      if(tmp[j] > 11 && !((elm[position][0] == 'bol180c' || elm[position][1] == 'bol180c') && tmp[j] == 25)) {
        tmp.splice(j, 1);
      }
    }
  }
  newcircles[position] = tmp[Math.floor(Math.random()*tmp.length)];
}

function updatecircle (position, elm, pld, newcircles) {
  circles[position] = newcircles[position];
  for (var j = 0; j < elm[position].length; j++) {
    document.getElementById(elm[position][j]).style.backgroundImage = 'url(' + pld[position][j].src + ')';
  }
}

function preloadcircle (position, circles, elm, src, pld) {
  var str = '';
  for (var j = 0; j < elm[position].length; j++) {
    str = src[position][j];
    for (var k = 0; k < circles.length; k++) {
       str = str.replace('#' + k + '#', newcircles[k]);
    }
    pld[position][j].src = str;
  }
}

function rotatecircle (positions, circles, elm, src, pld, newcircles) {
  updatecircle(positions.shift(), elm, pld, newcircles);
  pushposition(positions, circles);
  changecircle(positions[positions.length - 1], elm, newcircles);
  preloadcircle(positions[positions.length - 1], circles, elm, src, pld);
}

function rotate_all (circles, elm, src, pld, newcircles) {
  for (var i = elm.length - 1; i >= 0; i--) {
    updatecircle(i, elm, pld, newcircles);
    changecircle(i, elm, newcircles);
    preloadcircle(i, circles, elm, src, pld);
  }
}

function bodyOnload () {
  document.getElementById('witte_rand').style.height = Math.max(800, (20 + document.getElementById('berichten').offsetHeight)) + 'px';
  setInterval(IEbrowser ? 'rotate_all(circles, elm, src, pld, newcircles); ' : 'rotatecircle(positions, circles, elm, src, pld, newcircles); ', IEbrowser ? 40000 : 5000);
}

var IEbrowser = navigator.userAgent.search('MSIE')!= -1;
var elm = new Array();
var src = new Array();
var pld = new Array();
var newcircles = new Array();