  var fps = 10;

function fade(img, time, dir)
{
  img = document.getElementById(img);
  var steps = time * fps;

  if (typeof img.style.opacity != 'undefined')
  {
    var otype = 'w3c';
  }
  else if (typeof img.style.MozOpacity != 'undefined')
  {
    otype = 'moz';
  }
  else if (typeof img.style.KhtmlOpacity != 'undefined')
  {
    otype = 'khtml';
  }
  else if (typeof img.filters == 'object')
  {
    otype = (img.filters.length > 0
        && typeof img.filters.alpha == 'object'
        && typeof img.filters.alpha.opacity == 'number')
        ? 'ie' : 'none';
  }
  else { otype = 'none'; }

  if (otype != 'none')
  {
    if (dir == 'out') { dofade(steps, img, 1, false, otype); }
    else { dofade(steps, img, 0, true, otype); }
  }
}

  function dofade(steps, img, value, targetvisibility, otype)
{
  value += (targetvisibility ? 1 : -1) / steps;
  if (targetvisibility ? value > 1 : value < 0)
      value = targetvisibility ? 1 : 0;

  setfade(img, value, otype);

  if (targetvisibility ? value < 1 : value > 0)
  {
    setTimeout(function()
    {
      dofade(steps, img, value, targetvisibility, otype);
    }, 1000 / fps);
  }
}

  function setfade(img, value, otype)
{
  switch(otype)
  {
    case 'ie':
      img.filters.alpha.opacity = value * 100;
      break;

    case 'khtml':
      img.style.KhtmlOpacity = value;
      break;

    case 'moz':
      img.style.MozOpacity = (value == 1 ? 0.9999999 : value);
      break;

    default:
      img.style.opacity = (value == 1 ? 0.9999999 : value);
  }
}
 
 var timeout=6500;
  window.setTimeout('pass()', timeout);
 function pass(){
  fade('before', 5, 'out');
  fade('after', 5, 'in');
   window.setTimeout('pass1()', timeout);
  }
  
  function pass1(){
   fade('after', 5, 'out');
     fade('after1', 5, 'in');
   window.setTimeout('pass2()', timeout);
}
function pass2(){
   fade('after1', 5, 'out');
     fade('after2', 5, 'in');
   window.setTimeout('pass3()', timeout);
}
function pass3(){
   fade('after2', 5, 'out');
     fade('after3', 5, 'in');
   window.setTimeout('pass4()', timeout);
}
  function pass4(){
    fade('after3', 5, 'out');
  fade('before', 5, 'in');
  window.setTimeout('pass()', timeout);  
  }
  