// JavaScript Document

//========================================================================================================================================
//========================================================================================================================================
//========================================================================================================================================


//change these two to change the speed of the transition and the interval between each transition noob
var switchInterval = 12000;
var switchSpeed = 50;

//========================================================================================================================================
//========================================================================================================================================
//========================================================================================================================================

var fadeCount = 0;
var fadeCountOut = 0;
var intervalOut = 20;
var interval = 20;

function fadeInObject(object, speed ){
  fadeCount = 0;
  interval = speed;
  faderIn(object);
  document.getElementById(object).style.display = 'block';
}
function faderIn(object){
  if(fadeCount < 10){
    document.getElementById(object).style.opacity = fadeCount/10;
    document.getElementById(object).style.filter = 'alpha(opacity='+fadeCount*10+');';
    
    fadeCount = fadeCount + .5;
    t = setTimeout("faderIn('"+object+"')",interval);
  }
}

function fadeOutObject(object, speed ){
  fadeCountOut = 10;
  intervalOut = speed;
  faderOut(object);
}
function faderOut(object){
  if(fadeCountOut > 0){
    document.getElementById(object).style.opacity = fadeCountOut/10;
    document.getElementById(object).style.filter = 'alpha(opacity='+fadeCountOut*10+');';
    
    fadeCountOut = fadeCountOut - .5;
    t = setTimeout("faderOut('"+object+"')",intervalOut);
  }else{
    document.getElementById(object).style.display = 'none';
  }
}

var objectList = new Array();
var length = 0;
var current = 0;

function start(){
  var object;
  var count = 0;
  
  while(object = document.getElementById('block'+count)){
    objectList[count] = 'block'+count;
    count ++;
  }
  
	/* randomise objectList sequence */
	objectList.sort(function() {return 0.5 - Math.random()});

  var objectID = 0;
  length = objectList.length;

  loopThrough();

}

function loopThrough(){
  fadeInObject(objectList[current],switchSpeed);
  var lastItem = current - 1;

  if(lastItem != -1){
    fadeOutObject(objectList[lastItem],switchSpeed);
  }else{
    fadeOutObject(objectList[length-1],switchSpeed);
  }

  current ++;
  if(current == length){
    current = 0;
  }
  
  t = setTimeout("loopThrough()",switchInterval);
}