var totFs = 4;
var curNum = 0;
var allFs = new Array(0, 1, 2, 3);
var fadeTimeout = null;
var lastNum = -1;
var hasStarted = false;

shuffle = function(o) {
 for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
 return o;
};

function startFade() {
 allFs = shuffle(allFs);
 if (allFs[0] == lastNum) {
  allFs[0] = allFs[2];
  allFs[2] = lastNum;
 }
 lastNum = allFs[3];
 curNum = 0;
 tmpPos = getPos(document.getElementById('missionStatement'));

 for (i=0;i<totFs;i++) {
  objName = 'f' + allFs[i];
  obj = document.getElementById(objName);
  obj.style['position'] = 'absolute';
  obj.style['top'] = tmpPos[1] + 'px';
  obj.style['left'] = '300px';
  obj.style['z-index'] = i;
  obj.style['filter'] = 'alpha(opacity=0)';
  obj.style['opacity'] = 0;
  obj.style['-moz-opacity'] = 0;
  obj.style['-khtml-opacity'] = 0;
  obj.style['visibility'] = 'visible';
 }

 allFs = shuffle(allFs);

 if (hasStarted) {
  fadeTimeout = setTimeout('fadeIn(0)', 1000);
 } else {
  objName = 'f' + allFs[curNum];
  obj = document.getElementById(objName);
  obj.style['filter'] = 'alpha(opacity=100)';
  obj.style['opacity'] = 1;
  obj.style['-moz-opacity'] = 1;
  obj.style['-khtml-opacity'] = 1;
  fadeTimeout = setTimeout('fadeOut(100)', 2000);
  hasStarted = true;
 }
}

function fadeIn(opac) {
 opac += 10;
 decOpac = opac / 100;
 objName = 'f' + allFs[curNum];
 obj = document.getElementById(objName);
 obj.style['filter'] = 'alpha(opacity=' + opac + ')';
 obj.style['opacity'] = decOpac;
 obj.style['-moz-opacity'] = decOpac;
 obj.style['-khtml-opacity'] = decOpac;

 if (opac >= 100)
  fadeTimeout = setTimeout('fadeOut(100)', 1000);
 else
  fadeTimeout = setTimeout('fadeIn(' + opac + ')', 100);
}

function fadeOut(opac) {
 opac -= 10;
 decOpac = opac / 100;
 objName = 'f' + allFs[curNum];
 obj = document.getElementById(objName);
 obj.style['filter'] = 'alpha(opacity=' + opac + ')';
 obj.style['opacity'] = decOpac;
 obj.style['-moz-opacity'] = decOpac;
 obj.style['-khtml-opacity'] = decOpac;

 if (opac <= 0) {
  curNum++;
  if (curNum > 3) {
   startFade()
  } else {
   fadeTimeout = setTimeout('fadeIn(0)', 500);
  }
 } else {
  fadeTimeout = setTimeout('fadeOut(' + opac + ')', 100);
 }
}