var OPACITY_MAX = 100; // %
var DELTA_OPACITY = 5; // %
var DELTA_TIME = 25; // ms
var FADE_OUT_DELAY = 1500; // ms


function Popup(id)
{
  var fadeStatus = 0;
  /* 0 = ausgeblendet
     1 = wird gerade eingeblendet
     2 = wird gerade eingeblendet, Timer fürs Ausblendung bereits gesetzt
     3 = eingeblendet
     4 = eingeblendet, Timer fürs Ausblendung bereits gesetzt
     5 = wird gerade ausgeblendet
  */
  var hTimerOut = null;
  var hTimerIn = null;
  var id = id;
  
  this.getID = function()
  {
    return id;
  }

  this.BeginFadeIn = function()
  { 
    this.lock();
    if(fadeStatus == 1 || fadeStatus == 3) return;
    clearTimerOut();
    if(fadeStatus == 2) {
      fadeStatus = 1;
      return;
    }
    if(fadeStatus == 5)
      clearTimerOut()
     document.getElementById(id).style.visibility = '';
    fadeStatus = 1
    this.FadeIn(0);
  }

  this.BeginFadeOut = function()
  {
    if(fadeStatus != 1 && fadeStatus != 3)
      return;
    hTimerOut = window.setTimeout("getPopup('"+ id + "').FadeOut(" + OPACITY_MAX + ")", FADE_OUT_DELAY);
    fadeStatus = fadeStatus + 1;
  }

  this.FadeIn = function(opacity)
  {
    if(opacity >= OPACITY_MAX)
    {
      fadeStatus = fadeStatus + 2;
      return;
    }
    changeOpac(opacity + DELTA_OPACITY);
    var opacityNew = opacity + DELTA_OPACITY + 0;
    hTimerIn = window.setTimeout("getPopup('"+ id + "').FadeIn(" + opacityNew + ")", DELTA_TIME);
  }

  this.FadeOut = function(opacity)
  {
    if(opacity <= 0)
    {
        document.getElementById(id).style.visibility = 'hidden';
        fadeStatus = 0;
        clearTimerOut();
        return;
    }
    fadeStatus = 5;
    changeOpac(opacity - DELTA_OPACITY);
    var opacityNew = opacity - DELTA_OPACITY + 0;
    hTimerOut = window.setTimeout("getPopup('"+ id + "').FadeOut(" + opacityNew + ")", DELTA_TIME);
  }

  this.lock = function()
  {
    if(fadeStatus == 2 || fadeStatus == 4)
    {
      clearTimerOut();
      fadeStatus = fadeStatus - 1;
    }
  }

  function clearTimerOut()
  {
      if(hTimerOut)
        window.clearTimeout(hTimerOut);
      hTimerOut = null;
  }

  function changeOpac(opacity)
  { 
      var objStyle = document.getElementById(id).style;
      objStyle.opacity = (opacity / 100); 
      objStyle.MozOpacity = (opacity / 100);
      objStyle.KhtmlOpacity = (opacity / 100); 
      objStyle.filter = "alpha(opacity=" + opacity + ")";
  }
}

var arPopups = new Array();

function getPopup(id)
{
  for(var i = 0; i < arPopups.length; i++)
  {
    if(arPopups[i].getID() == id) return arPopups[i];
  }
  var p = new Popup(id)
  arPopups.push(p);
  return p;
}

function fadeIn(id)
{
  getPopup(id).BeginFadeIn();
}

function fadeOut(id)
{
  getPopup(id).BeginFadeOut();
}

function lock(id)
{
  getPopup(id).lock();
}