/*
 * Menuscript by Joachim Jansen
 * JDI-ICT ( 2007 )
 */

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

  this.AnimationOpenTime = 0;
  this.AnimationCloseTime = 0;

  this.minCPUResolution = 20;
 
  this.StandaardOpenTime = 1000;
  this.StandaardSubMenuOpenTime = 500;
  this.MenuItemUpPostFix = 'Up';
  this.zIndex = 10;
}

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

  if ( OnMouseOverStyle ){ 
    this.OnMouseOverStyle = OnMouseOverStyle;
    for( var i in OnMouseOverStyle ){
      this.StyleToRestore[ i ] = this.Element.style[ i ];
    }
  }


  this.NormalClass = element.className;

  this.MouseOn     = false;

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

  this.ToOpenID   = idToOpen;
  this.ToOpen     = null;
  if ( this.ToOpenID ){
    var List = new JJMenuList( menu, idToOpen, this );
    this.ToOpen     = List;
    if ( this.ToOpen ){

      List.FindElement();
   
      if ( List.Element ){
        List.Element.onmouseout = function (){
          List.MouseOut();
        }

        List.Element.onmouseover = function (){
          List.MouseOver();
        }
      }
    }
  }
  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;
    }

    this.StyleChangeOnEnter();

  } else {
    Class = this.NormalClass;
 
    this.StyleChangeOnLeave();
  }

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

JJMenuItem.prototype.StyleChangeOnEnter = function(){
  for( var i in this.OnMouseOverStyle ){
    this.Element.style[ i ] = this.OnMouseOverStyle[ i ];
  }
}

JJMenuItem.prototype.StyleChangeOnLeave = function(){
  for( var i in this.StyleToRestore ){
    this.Element.style[ i ] = this.StyleToRestore[ i ];
  }
}

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.MouseOn     = false;

  this.accelOpenConst  = 0.0004;
  this.idref       = idref;

  this.FindElement();

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

JJMenuList.prototype.FindElement = function( ){
  if ( ! this.Element ){
    this.Element = document.getElementById( this.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';
    }
  }
}

JJMenu.prototype.RegisterToMenu = function( element, idToOpen, idToKeepOpen, OnMouseOverStyle ){

  var menuitem = new JJMenuItem( this, element, idToOpen, idToKeepOpen, OnMouseOverStyle );

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

    this.AddToMenuOrder( menuitem );

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

    element.onmouseout = function(){
      menuitem.MouseOut();
    }

    element.onmouseover();

    return true;
  } else {
    element.onmouseover = function(){ 
      menuitem.CloseOtherThanMe( menuitem.Index );
    };
    element.onmouseover();
    return true;
  }
}

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.IDReference == MenuItem.KeepOpenID ){ 
    MenuItem.Parent = this;

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

JJMenuList.prototype.MouseOut = function(){
  this.MouseOn     = false;
  this.CheckPossibleClose();
}

JJMenuList.prototype.MouseOver = function(){
  this.MouseOn     = true;
  this.CheckPossibleClose();
}


JJMenuItem.prototype.MouseOut = function( ){
  this.MouseOn     = false;
  if ( this.ToOpen ){ 
    this.ToOpen.CheckPossibleClose();
  }
  this.StyleChangeOnLeave();
}

JJMenuItem.prototype.MouseMove = function ( ){ 
  this.StyleChangeOnEnter();

  this.MouseOn     = true;

  if ( this.ToOpen != null ){
    this.ToOpen.StartOpenMenu();
  }

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

  if ( ! this.KeepOpen && ! this.ToOpen ){
    this.CloseOtherThanMe( this.Index );
  }

  if ( this.ToOpen ){
    this.ToOpen.CheckPossibleClose();
  }

}

JJMenuList.prototype.CheckPossibleClose = function(){

  this.FindElement();

  if ( this.MouseOn == true ) { 
    this.ClearCloseTimer();
    return false;
  }

  if ( this.Opener != null && this.Opener.MouseOn == true ){
    this.ClearCloseTimer();
    return false;
  }

  this.ResetCloseTimer();

  return true;
}

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();
        MenuList.EndCloseMenuList();
      }
    }
  } else {
    this.Menu.CloseOtherThanMe( this.Index );
  }
}

JJMenuList.prototype.StartOpenMenu = function () {

  this.FindElement();
 
  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 ){ 
    this.Element.style.display = 'block';

    Top = this.Opener.ParentY;
    Left = this.Opener.ParentX;
    if ( this.Opener.Parent ){
      Left += this.Opener.Element.offsetWidth;
    }

    this.Element.style.top = Top + 'px';
    this.Element.style.left = Left + 'px';
    this.Opener.UpdateClassName( true );
    this.EndOpenMenuList();
  }
}

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;
  Left = this.Opener.ParentX;

  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;
}

JJMenuList.prototype.ClearCloseTimer = function ( ToKidsTo ){
  if ( this.AutoCloseTimer ){
    clearInterval( this.AutoCloseTimer );
  }
}

JJMenuList.prototype.ResetCloseTimer = function ( ToKidsTo ){


  this.ClearCloseTimer();
  TimeoutTime = this.Opener.KeepOpen ? this.Menu.StandaardSubMenuOpenTime : this.Menu.StandaardOpenTime;

  this.AutoCloseTimer = setTimeout( "JJMenu_AutoCloseMenu( " + this.Index + " )", TimeoutTime );
   
  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 ){
    MenuItemList = this.Childs[ idx ].ToOpen;
    if ( MenuItemList ){
      MenuItemList.StartCloseMenuList();
    }
  }

  clearTimeout( this.AutoCloseTimer );
  if ( this.Open || this.Menu.Opening[ this.Index ] == true ) {
    this.EndCloseMenuList();
  }
}

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

Menu = new JJMenu();

function ClickList( id ){
  var e = document.getElementById( id );
  if ( e ){ 
    e.style.display = e.style.display == 'block' ? 'none' : 'block';
  }
}


function ShowFlash( url, width, height ){
  document.write( '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '">' );
  document.write( '<param name="allowScriptAccess" value="sameDomain" />' );
  document.write( '<param name="movie" value="' + url + '">' );
  document.write( '<param name="quality" value="high">' );
  document.write( '<PARAM NAME=wmode VALUE=transparent>' );
  document.write( '<embed src="' + url + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '" wmode="transparent" ></embed>' );
  document.write( '</object>');
}


