function JJMenu (){
  this.Childs = [];
  this.MenuItems = [];
  this.MenuLists = [];
 
  this.Closing = [];
  this.Opening = [];

  this.AnimationOpenTime = 500;
  this.AnimationCloseTime = 250;

  this.minCPUResolution = 20;
 
  this.StandaardOpenTime = 3000;
  this.MenuItemUpPostFix = 'Up';
  this.zIndex = 1;
}

function JJMenuItem( menu, element, idToOpen , idToKeepOpen ){
  this.Menu       = menu;
  this.Parent     = null;
  this.Index      = null;
  this.Element    = element;

  this.NormalClass = element.className;

  this.ParentY    = element.offsetTop;
  this.ParentX    = element.offsetLeft;

  this.ToOpenID   = idToOpen;
  this.ToOpen     = null;
  if ( this.ToOpenID ){
    this.ToOpen     = new JJMenuList( menu, idToOpen, this );
  }
  this.KeepOpenID = idToKeepOpen;
  if ( this.KeepOpenID ){
    this.KeepOpen   = this.Menu.SearchMenuList( this.KeepOpenID );
    this.Parent = this.KeepOpen;
  }

  this.Open = false;
 
  this.Menu.AddMenuItem( this );

}

JJMenuItem.prototype.UpdateClassName = function( ChildOpen ){
  Class = "";

  if ( ChildOpen ){ 
    Class = this.NormalClass;
    ML = this.Menu.MenuItemUpPostFix.length;
    CL = Class.length;
    if ( Class.substring( CL - ML, CL ) != this.Menu.MenuItemUpPostFix ){ 
      Class += this.Menu.MenuItemUpPostFix;
    }
  } else {
    Class = this.NormalClass;
  }

  if ( this.Element.className != Class ){ 
    this.Element.className = Class;
  }
}

JJMenu.prototype.SearchMenuList = function ( idToSearch ){
  for( idx in this.MenuLists ){  
    MenuList = this.MenuLists[ idx ];
    if ( MenuList.IDReference == idToSearch ){ 
      return MenuList;
    }
  }
  return null;
}

function JJMenuList( menu, idref, opener ){
  this.IDReference = idref;
  this.Opener      = opener;
  this.Menu        = menu;

  this.Childs      = [];
  this.DropDownX   = 0;
  this.DropDownY   = 0;
  this.Open        = false;
  this.Direction   = "open";


  this.accelOpenConst  = 0.0004;
  this.Element     = document.getElementById( idref );
 
  if ( this.Element ){ 
    this.DropDownY = this.Element.offsetHeight;
    this.DropDownX = this.Element.offsetWidth;
    this.Element.style.left = this.Opener.ParentX + 'px';
    this.Element.style.top  = ( this.Opener.ParentY - this.DropDownY ) + 'px';
    this.BerekenAccelConst( this.DropDownY );
  } 

  this.StartTime            = null;
  this.ActionIntervalRunner = null;
  this.AutoCloseTimer       = null;
 
  this.Menu.AddMenuList( this );
}

JJMenuList.prototype.BerekenAccelConst = function( ) {
  Space = ( parseInt( this.Element.style.top ) - this.Opener.ParentY ) * -1;

  this.accelOpenConst = Space / this.Menu.AnimationOpenTime / this.Menu.AnimationOpenTime;
  this.accelCloseConst = Space / this.Menu.AnimationCloseTime / this.Menu.AnimationCloseTime;
}

JJMenu.prototype.RegisterToMenu = function( element, idToOpen, idToKeepOpen ){
  var menuitem = new JJMenuItem( this, element, idToOpen, idToKeepOpen );

  if ( menuitem.ToOpen || menuitem.KeepOpen ){ 

    this.AddToMenuOrder( menuitem );

    element.onmouseover = function (){
      menuitem.MouseMove();
    }

    element.onmouseover();

    return true;
  } else {
    element.onmouseover = function(){ };
    return false;
  }
}

JJMenu.prototype.GetNewZIndex = function (){
  this.zIndex++;
  return this.zIndex;
}

JJMenu.prototype.AddMenuItem = function ( MenuItem ){
  var index = ( this.MenuItems.push( MenuItem ) ) - 1;
  MenuItem.Index = index;
}

JJMenu.prototype.AddMenuList = function ( MenuList ){
  var index = ( this.MenuLists.push( MenuList ) ) - 1;
  this.Closing[ index ] = false;
  this.Opening[ index ] = false;
  MenuList.Index = index;
}

JJMenu.prototype.AddToMenuOrder = function ( MenuItem ){

  ChildIndex = null;

  if ( MenuItem.KeepOpen ){ 
    for( var i in this.Childs ){ 
      if ( ChildIndex == null && this.Childs[ i ].ToOpen ){
        ChildIndex = this.Childs[ i ].ToOpen.AddToMenuOrder( MenuItem );
      }
    }
  } else { 
    ChildIndex = ( this.Childs.push( MenuItem ) ) - 1;
  }

  if ( ChildIndex ){
    this.MenuItems[ MenuItem.Index ].ChildIndex = ChildIndex;
  }

}

JJMenuList.prototype.AddToMenuOrder = function( MenuItem ){
  ChildIndex = null;
  if ( this.ToOpen == MenuItem.KeepOpen ){ 
    MenuItem.Parent = this;
    return this.Childs.push( MenuItem );
  }
  for( var i in this.Childs ){
    ChildIndex = this.Childs[ i ].AddToMenuOrder( MenuItem );
    if ( ChildIndex ){ 
      return ChildIndex;
    }
  }
  return null;
}

JJMenuItem.prototype.MouseMove = function ( ){ 
  if ( this.ToOpen != null ){
    this.ToOpen.StartOpenMenu();
  }

  if ( this.KeepOpen != null && this.Menu.Closing[ this.KeepOpen.Index ] == false ){
    this.KeepOpen.StartOpenMenu();
  }
}

JJMenu.prototype.CloseOtherThanMe = function( index ){
  for( var idx in this.Childs ){ 
    MenuItem = this.Childs[ idx ];
    cIndex = MenuItem.Index;
    if ( cIndex != index ){
      if ( MenuItem.ToOpen ){
        lIndex = MenuItem.ToOpen.Index;
        if ( this.Closing[ lIndex ] == false && ( MenuItem.ToOpen.Open || this.Opening[ lIndex ] == true ) ){ 
          MenuItem.ToOpen.StartCloseMenuList();
        } 
      }
    }
  }
}

JJMenuItem.prototype.CloseOtherThanMe = function( Index ){
  if ( this.Parent ) { 
    for( var idx in this.Childs ){ 
      MenuList = this.Childs[ idx ];
      if ( MenuList.Index != Index ){ 
        MenuList.StartCloseMenuList();
      }
    }
  } else {
     this.Menu.CloseOtherThanMe( this.Index );
  }
}

JJMenuList.prototype.StartOpenMenu = function () {
 
  this.Opener.CloseOtherThanMe( this.Index );

  this.Element.style.zIndex = this.Menu.GetNewZIndex();

  if ( this.Menu.Opening[ this.Index ] == true ){
    return;
  }

  if ( this.Open == false || this.Menu.Closing[ this.Index ] == true ){ 
//    if ( this.Menu.Closing[ this.Index ] == false ){
//      this.Element.style.top = ( this.Opener.ParentY - this.DropDownY ) + 'px';
      this.StartTime = (new Date()).getTime();
//    } else { 
  //    var elapsed = (new Date()).getTime() - this.StartTime;
//      this.StartTime = (new Date()).getTime() - ( this.Menu.AnimationOpenTime - elapsed );
  //  }
    this.Opener.UpdateClassName( true );

    this.Element.style.visibility = 'visible';
    this.Menu.Closing[ this.Index ] = false;
    this.Menu.Opening[ this.Index ] = true;
    this.Direction = "open";

    clearInterval( this.ActionIntervalRunner );
    this.ActionIntervalRunner = setInterval( "JJMenu_SlideMenuList( " + this.Index + " )", this.Menu.minCPUResolution );
  } else { 
    if ( this.Open == true ){
      this.ResetCloseTimer();
    }
  }
}

JJMenu_SlideMenuList = function ( index ){
  if ( Menu.MenuLists[ index ] ){ 
    Menu.MenuLists[ index ].SlideMenuList();
  }
}

JJMenu_AutoCloseMenu = function ( index ){
  if ( Menu.MenuItems[ index ] ){
    Menu.MenuLists[ index ].StartCloseMenuList();
  }
}


JJMenuList.prototype.SlideMenuList = function(){
  var elapsed = (new Date()).getTime() - this.StartTime;


  Top = this.Opener.ParentY;
  if ( this.Direction == "open" ){ 
    var d = Math.round(Math.pow(this.Menu.AnimationOpenTime-elapsed, 2) * this.accelOpenConst);
    Top = this.Opener.ParentY - d;
  } else { 
    var d = Math.round(Math.pow(elapsed, 2) * this.accelCloseConst );

    Top = this.Opener.ParentY - d ; 
  }

  this.Element.style.top  = ( Top ) + 'px';

  if ( elapsed > ( this.Direction == "open" ? this.Menu.AnimationOpenTime : this.Menu.AnimationCloseTime) ) {
    if ( this.Direction == "open" ){
      this.EndOpenMenuList();
    } else { 
      this.EndCloseMenuList();
    }
  }
}

JJMenuList.prototype.EndOpenMenuList = function(){
  clearInterval( this.ActionIntervalRunner );
  this.Open = true;
  this.Element.style.top  = this.Opener.ParentY + 'px';
  this.Menu.Opening[ this.Index ] = false;
  this.ResetCloseTimer( false );
}

JJMenuList.prototype.ResetCloseTimer = function ( ToKidsTo ){
  clearInterval( this.AutoCloseTimer );
  this.AutoCloseTimer = setTimeout( "JJMenu_AutoCloseMenu( " + this.Index + " )", this.Menu.StandaardOpenTime );
  if ( ToKidsTo ){ 
    HasKids = false;
    for( var idx in Childs ){ 
      MenuItem = Childs[ idx ];
      if ( MenuItem.ToOpen ){  
        if ( MenuItem.ToOpen.Open ){
          MenuItem.ToOpen.ResetCloseTimer( ToKidsTo );
          ToKidsTo = true;
        }
      }
    }
    ToKidsTo = HasKids;
  }
  if ( ! ToKidsTo ){
    if ( this.Opener.Parent ){ 
      this.Opener.Parent.ResetCloseTimer( false );
    }
  }
}

JJMenuList.prototype.StartCloseMenuList = function () {

  for( var idx in this.Childs ){
    MenuItem = this.Childs[ idx ].ToOpen;
    if ( MenuItem.ToOpen ){
      MenuItem.ToOpen.StartCloseMenuList();
    }
  }

  clearTimeout( this.AutoCloseTimer );
  if ( this.Open || this.Menu.Opening[ this.Index ] == true ) {
//    if ( this.Menu.Opening[ this.Index ] == false ){
      this.StartTime = (new Date()).getTime();
//    } else {
  //    var elapsed = (new Date()).getTime() - this.StartTime;
    //  this.StartTime = (new Date()).getTime() - ( this.Menu.AnimationCloseTime - elapsed );
//    }

    this.Direction  = "close";
    this.Menu.Closing[ this.Index ] = true;
    this.Menu.Opening[ this.Index ] = false;
    clearInterval( this.ActionIntervalRunner );
    this.ActionIntervalRunner = setInterval( "JJMenu_SlideMenuList( " + this.Index + " )", this.Menu.minCPUResolution );
  }
}

JJMenuList.prototype.EndCloseMenuList = function(){
  clearInterval( this.ActionIntervalRunner );
  this.Open = false;
  this.Opener.UpdateClassName( false );
  this.Element.style.visibility = 'hidden';
  this.Menu.Closing[ this.Index ] = false;
}

Menu = new JJMenu();

function RunPlayer( url, width, height ){
  document.write( '<embed ');
  document.write( '  type="application/x-mplayer2"' );
  document.write( '  SRC="' + url + '"' );
  document.write( '  width="' + width + '"' );
  document.write( '  height="' + height + '"' );
  document.write( '  autostart="1"' );
  document.write( '  showcontrols="1"' );
  document.write( '  showstatusbar="1"' );
  document.write( '  autorewind="1"' );
  document.write( '  showdisplay="0"' );
  document.write( '> </embed>' );
}

timeoutFade = null;

function FadeFrontImage(){
   md = document.getElementById( 'MainDiv' );  
   md.style.display = 'block';
   timeoutFade = setTimeout( 'StartFading()', 10 * 1000 );
}

var fader = null;

function StartFading(){
  il = document.getElementById( "IntroLayer" );

  if ( timeoutFade != null && il.style.display != 'none' ){ 
    clearTimeout( timeoutFade );
    try {
      fader = new Fadomatic( il, 2, 100, 0, 100 );
      fader.fadeOut();
    } catch( e ){ 
      RemoveIntroLayer();
    }
  }
}



function RemoveIntroLayer( ){
  clearTimeout( timeoutFade );
  timeoutFade = null;
  if ( fader ){
    fader.haltFade();
    fader.hide();
  }
  il = document.getElementById( "IntroLayer" );
  if ( il ){ 
    il.style.display = 'none';
  }
}
