var moodlink;

function init() {
  moodlink = new Moodlink();
  return true;
}

function exit() {
  if (moodlink.TI) {
    window.clearInterval(moodlink.TI);
  }
  return true;
}

function Moodlink() {
  this.moodlink = document.getElementById('moodlink');
  this.moodlink.style.width  = '138px';
  this.moodlink.style.margin = '0.2em 1em 0 0';

  this.pictures = new Array();
  var pic = document.createElement('img');
  with (pic) {
    width  = '138';
    height = '92';
  }
  for (var n = 0; n < moodpicFiles.length; n++) {
    this.moodlink.appendChild(this.pictures[n] = pic.cloneNode(true));
    with (this.pictures[n]) {
      src   = moodpicFiles[n];
      title = moodpicNames[n];
      with (style) {
        float    = 'left';
        position    = 'relative';
        zIndex      = n;
        opacity     = ((n  < moodpicFiles.length - 1) ? 0 : 1);
        MozOpacity  = ((n  < moodpicFiles.length - 1) ? 0 : 1);
        filter      = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + ((n  < moodpicFiles.length - 1) ? 0 : 100) + ')';
        margin      = ((n  < moodpicFiles.length - 1) ? '0 -138px 0 0' : '0');
      }
    }
  }

  this.active         = moodpicFiles.length - 1;
  this.moodlink.href  = moodpicTargets[this.active];
  this.moodlink.title = moodpicNames[this.active];

  if (this.pictures[this.active].style.opacity || this.pictures[this.active].style.MozOpacity || this.pictures[this.active].style.filter) {
    this.TI = window.setInterval('switchMoodlink()', 5000);
  }
}

function switchMoodlink() {
  moodlink.active = (moodlink.active + 1) % moodpicFiles.length;
  if (moodlink.pictures[moodlink.active].style.opacity) {
    moodlink.pictures[moodlink.active].style.opacity = 0;
  } else if (moodlink.pictures[moodlink.active].style.MozOpacity) {
    moodlink.pictures[moodlink.active].style.MozOpacity = 0;
  } else if (moodlink.pictures[moodlink.active].style.filter) {
    moodlink.pictures[moodlink.active].style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=0)';
  }
  moodlink.pictures[moodlink.active].style.zIndex = moodlink.pictures[moodlink.active].style.zIndex + moodpicFiles.length;
  if (moodlink.pictures[moodlink.active].style.opacity || moodlink.pictures[moodlink.active].style.MozOpacity) {
    window.setTimeout('blendIn(0.05)', 80);
  } else if (moodlink.pictures[moodlink.active].style.filter) {
    window.setTimeout('blendIn(5)', 80);
  }

  moodlink.moodlink.href  = moodpicTargets[moodlink.active];
  moodlink.moodlink.title = moodpicNames[moodlink.active];
}

function blendIn(opacity) {
  if (moodlink.pictures[moodlink.active].style.opacity) {
    moodlink.pictures[moodlink.active].style.opacity = opacity;
    if (opacity < 1) {
      window.setTimeout('blendIn(' + (opacity + 0.05) + ')', 80 - opacity * 50);
    }
  } else if (moodlink.pictures[moodlink.active].style.MozOpacity) {
    moodlink.pictures[moodlink.active].style.MozOpacity = opacity;
    if (opacity < 1) {
      window.setTimeout('blendIn(' + (opacity + 0.05) + ')', 80 - opacity * 50);
    }
  } else if (moodlink.pictures[moodlink.active].style.filter) {
    moodlink.pictures[moodlink.active].style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + opacity + ')';
    if (opacity < 100) {
      window.setTimeout('blendIn(' + (opacity + 5) + ')', 80 - opacity * 0.5);
    }
  }
}