// Prepare array of menues...
var AnimatedMenus = new Array();

/** Popup window class */

function AnimatedMenu(menu_id) {
  
  this.menuId = '';
  this.timer;
  this.direction = 1; // 1 for rollout, -1 for rollin
  this.locked = false;
  
};

AnimatedMenu.prototype.showMenu = function() {
  
  // Get menu and set it wisible...
  menu = this.getMenu();
  
  // Get width...
  width = 0;
  if(menu.style.width != '') width = parseInt( menu.style.width.substring(0, menu.style.width.length - 2) );
  
  // Set menu as visible and reset direction...
  if(width < 50) {
    
    // Prepare menu
    menu.style.width = '0px';
    menu.style.visibility = 'visible';
    this.direction = 1;
    
    // Bring menu in front
    menu_to_front(this.menuId);
    
    // Init animation after 0.1 seconds
    this.startAnimation(100); // delay was 100
  
  } // if
  
} // showMenu

AnimatedMenu.prototype.hideMenu = function() {
  
  // Get menu and width value
  menu = this.getMenu();
  
  // Get width
  width = 0;
  if(menu.style.width != '') width = parseInt( menu.style.width.substring(0, menu.style.width.length - 2) );
  
  if(width > 0) {
    this.direction = -1;
    this.startAnimation(500);
  } // if
  
  // Kill old timer...
  //if(this.timer) window.clearTimeout(this.timer);
  
} // hideMenu

AnimatedMenu.prototype.startAnimation = function(after) {
  
  // Submenu is locked? Do not rollin
  if((this.direction == -1) && this.locked) return;
  
  // Animate with delay or animate instantly
  if(after > 0) {
    this.timer = window.setTimeout("do_animate('" + this.menuId + "')", after);
  } else {
    do_animate(this.menuId);
  } // if
  
} // startAnimation
  
AnimatedMenu.prototype.getMenu = function() {
  return document.getElementById(this.menuId);
} // getMenu

function do_animate(menu_id) {
  
  // Get animation object and menu...
  animation_object = AnimatedMenus[menu_id];
  menu = animation_object.getMenu();
  
  // Get old width...
  old_width = 0;
  if(menu.style.width != '') old_width = parseInt( menu.style.width.substring(0, menu.style.width.length - 2) );
  
  // Have we reached 240px?
  
  if(animation_object.direction == 1) {

    if(old_width >= 240) {
      window.clearTimeout(animation_object.timer);
    } else {
      menu.style.width = (old_width + animation_object.direction * 20) + 'px';
      animation_object.timer = window.setTimeout("do_animate('" + menu_id + "')", 10);
    } // if
     
  } else if(animation_object.direction == -1) {
    
    if(animation_object.locked) {
      window.clearTimeout(animation_object.timer);
      return;
    } // if
    
    // Rolledout?
    if(old_width == 0) {
      window.clearTimeout(animation_object.timer);
      menu.style.visibility = 'hidden';
    } else {
      menu.style.width = (old_width + animation_object.direction * 10) + 'px';
      animation_object.timer = window.setTimeout("do_animate('" + menu_id + "')", 10);
    } // if
    
  }
  
} // do_animate

function menu_to_front(menu_id) {
  
  // Loop all menus...
  for(current_menu_id in AnimatedMenus) {
    
    // Get menu and animation object...
    animation_object = AnimatedMenus[current_menu_id];
    menu = animation_object.getMenu();
    
    // Move it to front or to back?
    if(current_menu_id == menu_id) {
      menu.className = 'toFront';
    } else {
      menu.className = 'toBack';
      animation_object.direction = -1;
      animation_object.startAnimation(500);
    } // if
    
  } // for
  
} // menu_to_front

//function hide_all_menus() {
//  
//  // Loop all menus...
//  for(current_menu_id in AnimatedMenus) {
//    
//    // Get menu and animation object...
//    animation_object = AnimatedMenus[current_menu_id];
//    menu = animation_object.getMenu();
//    
//    // Get old width...
//    width = 0;
//    if(menu.style.width != '') width = parseInt( menu.style.width.substring(0, menu.style.width.length - 2) );
//    
//    if(width > 0) {
//      animation_object.direction = -1;
//      animation_object.startAnimation(500);
//    } // if
//    
//  } // for
//  
//} // hideAllmenus

function show_submenu(menu_id) {
  animation_object = AnimatedMenus[menu_id];
  if(!animation_object) return;
  animation_object.showMenu();
} // show_submenu

function hide_submenu(menu_id) {
  animation_object = AnimatedMenus[menu_id];
  if(!animation_object) return;
  animation_object.hideMenu();
} // hide_submenu

function lock_submenu(menu_id) {
  animation_object = AnimatedMenus[menu_id];
  if(!animation_object) return;
  if(animation_object.getMenu().className != 'toBack') {
    animation_object.locked = true;
  } // if
} // lock_submenu

function unlock_submenu(menu_id) {
  animation_object = AnimatedMenus[menu_id];
  if(!animation_object) return;
  animation_object.locked = false;
} // unlock_submenu