/**
 * @class MenuRoot
 *
 * The MenuRoot represents the root of the menu.
 */
var MenuRoot = function()
{
  // ---- Constants ---------------------------------------------------

  /** The class name of the normal A element. */
  var CLASSNAME = "mainnavigation_level_0";
  /** The class name of the selected A element. */
  var CLASSNAME_SELECTED = "mainnavigation_level_0_selected";

  // ---- Fields ------------------------------------------------------

  /** Indicates whether this menu entry is selected. */
  this.selected = null;
  /** The child menu entries. */
  this.children = null;
  /** The A element that represents this menu entry. */
  var a = null;
  /** The DIV element that represents this menu entry in the content area. */
  this.contentEntryDiv = null;
  /** The DIV element that represents this menu entry in the navigation area. */
  this.menuEntryDiv = null;
  /** The DIV element where the child elements will be attached to in the navigation area. */
  this.menuContainerDiv = null;

  // ---- Constructors ------------------------------------------------

  /**
   * Creates a new MenuRoot. It is assumed that the menu root elements always exist in the HTML page.
   * @constructor
   */
  this.constructor = function()
  {
    this.initMenu();
  };

  // ---- Public functions --------------------------------------------

  /**
   * Returns the child with the given ID or null.
   *
   * @public
   * @param {String} id an ID
   * @return {MenuItemApp}
   * The child with the given ID or null.
   */
  this.getChild = function(id)
  {
    if(this.children != null)
    {
      for(var i = 0; i < this.children.length; i++)
      {
        var child = this.children[i];
        if(child.id == id)
        {
          return child;
        }
      }
    }
    return null;
  };

  /**
   * Adds a child to the menu item.
   *
   * @public
   * @param {MenuItemApp} child an application menu item
   */
  this.addChild = function(child)
  {
    if(this.children == null)
    {
      this.children = new Array();
    }
    this.children.push(child);
  };

  /**
   * Unselects this menu entry.
   *
   * @public
   */
  this.close = function()
  {
    if(this.selected)
    {
      a.removeClass(CLASSNAME_SELECTED);
      a.addClass(CLASSNAME);

      this.selected = false;
      this.activeContent = false;
    }
  };

  /**
   * Selected this menu entry.
   *
   * @public
   */
  this.open = function()
  {
    if(!this.activeContent)
    {
      for(var i = 0; i < this.children.length; i++)
      {
        var child = this.children[i];
        child.close();
      }

      if(this.contentEntryDiv == null)
      {
        this.buildContent();
      }

      a.removeClass(CLASSNAME);
      a.addClass(CLASSNAME_SELECTED);

      if(page.contentDiv != null)
      {
        page.contentDiv.animate({opacity:"hide"}, {duration:page.speed / 2, queue:"content"});
      }
      if(this.contentEntryDiv != null)
      {
        this.contentEntryDiv.animate({opacity:"show"}, {duration:page.speed / 2, queue:"content"});
      }
      page.contentDiv = this.contentEntryDiv;

      this.selected = true;
    }
  };

  // ---- Not so public functions -------------------------------------

  /**
   * Inits the menu area. It is assumed that it alwas exists and must not be created.
   */
  this.initMenu = function()
  {
    // the menu entry
    this.menuEntryDiv = $("div.mainnavigation_application_container:first");
    // the menu item
    a = this.menuEntryDiv.find("a:first");
    this.selected = a.hasClass(CLASSNAME_SELECTED);

    // add click
    a.attr("href", "javascript:openMenu();");

    // the child container
    this.menuContainerDiv = $("div.mainnavigation_container");
  };

  /**
   * Inits the content page if it exists.
   */
  this.initContent = function()
  {
    // the content entry
    this.contentEntryDiv = $("div.content_application_container");
    if(this.contentEntryDiv.length != 0)
    {
      var applicationDivs = this.contentEntryDiv.find("div.application");
      for(var i = 0; i < applicationDivs.length; i++)
      {
        var applicationDiv = applicationDivs.eq(i);
        applicationDiv.find("a").attr("href", "javascript:openMenu(\"" + this.children[i].id + "\");");
      }
    }
    else
    {
      this.contentEntryDiv = null;

      for(var i = 0; i < this.children.length; i++)
      {
        var child = this.children[i];
        if(child.selected)
        {
          child.initContent();
        }
      }
    }

    // the child container
    this.contentContainerDiv = $("div.content_container");
  };

  /**
   * Creates the HTML representation of this menu entry in content area.
   */
  this.buildContent = function()
  {
  };

  // ---- Constructor call --------------------------------------------

  this.constructor();
};

/**
 * The openMenu function gets the menu item with the given IDs and opens it. This method is designed to be used as link.
 *
 * The method comes in four flavours:
 * openMenu() - opens the root menu item
 * openMenu(applicationID) - opens an application menu item
 * openMenu(applicationID, typeID) - opens a type menu item
 * openMenu(applicationID, typeID, rangID, index) - opens a range menu item
 *
 * @param {String} application an application ID (optional)
 * @param {String} type a type ID (optional)
 * @param {String} range a range ID (optional)
 * @param {Number} index an index (optional)
 */
function openMenu(application, type, range, index)
{
  var menu = page.menuRoot;

  if(application != null)
  {
    var children = menu.children;
    for(var i = 0; i < children.length; i++)
    {
      var child = children[i];
      if(child.id == application)
      {
        menu = child;
      }
    }
  }

  if(type != null)
  {
    children = menu.children;
    for(i = 0; i < children.length; i++)
    {
      child = children[i];
      if(child.id == type)
      {
        menu.activeContent = false;
        menu = child;
      }
    }
  }
  else if (children != null)
  {
    for(i = 0; i < children.length; i++)
    {
      children[i].close();
    }
  }

  if(range != null && index != null)
  {
    children = menu.children;
    for(i = 0; i < children.length; i++)
    {
      child = children[i];
      if(child.id == range && child.index == index)
      {
        menu.activeContent = false;
        menu = child;
      }
    }
  }
  else if (menu != null)
  {
    children = menu.children;
    if (children != null)
    {
    for(i = 0; i < children.length; i++)
    {
      children[i].close();
    }
    }
  }

  menu.open();
}
