/**
 * @class MenuItemRange
 *
 * The class MenuItemRange represents a range menu item in the main menu.
 */
var MenuItemRange = function(parentItem, htmlId, id, index, ippId, label, thumb)
{
  // ---- Constants ---------------------------------------------------

  /** The class name of the normal A element. */
  var CLASSNAME = "mainnavigation_level_2";
  /** The class name of the selected A element. */
  var CLASSNAME_SELECTED = "mainnavigation_level_2_selected";

  // ---- Fields ------------------------------------------------------

  /** The parent menu item. */
  this.parentItem = null;
  /** The HTML id of this menu entry. */
  this.htmlId = null;
  /** The ID of this menu entry. */
  this.id = null;
  /** The ID index of this menu entry. */
  this.index = -1;
  /** The IPP ID of this menu entry. */
  this.ippId = null;
  /** The local label of this menu entry. */
  this.label = null;
  /** The thumbnail path for this menu entry. */
  this.thumb = null;
  /** Indicates whether this menu entry is selected. */
  this.selected = false;
  /** The A element that represents this menu entry. */
  var a = null;
  /** The DIV element that represents this menu entry in the navigation area. */
  this.menuEntryDiv = null;

  // ---- Constructors ------------------------------------------------

  /**
   * Creates a new range menu item using the given information and existing HTML elements.
   *
   * @param {MenuItemType} parentItem the parent type menu item
   * @param {String} htmlId the ID of the existing HTML element
   * @param {String} id the ID of this menu entry
   * @param {Number} index the ID index of this menu entry
   * @param {String} ippId the IPP ID for this menu entry
   * @param {String} label a label in the local language
   * @param {String} thumb the thumbnail path of this image
   */
  this.constructor = function(parentItem, htmlId, id, index, ippId, label, thumb)
  {
    this.parentItem = parentItem;
    this.htmlId = htmlId;
    this.id = id;
    this.index = index;
    this.ippId = ippId;
    this.label = label;
    this.thumb = thumb;

    this.parentItem.addChild(this);

    this.initMenu();
    if(this.selected)
    {
      this.initContent();
    }
  };

  // ---- Public functions --------------------------------------------

  /**
   * Unselects this menu entry.
   *
   * @public
   */
  this.close = function()
  {
    if(this.selected)
    {
      a.removeClass(CLASSNAME_SELECTED);
      a.addClass(CLASSNAME);

      this.selected = false;
    }
  };

  /**
   * Selected this menu entry and closes all sibling menu entries.
   *
   * @public
   */
  this.open = function()
  {
    if(!this.selected)
    {
      for(var i = 0; i < this.parentItem.children.length; i++)
      {
        var child = this.parentItem.children[i];
        if(child != this)
        {
          child.close();
        }
      }

      a.removeClass(CLASSNAME);
      a.addClass(CLASSNAME_SELECTED);

      this.selected = true;

      showHighlights(page.dealerId, this.parentItem.id, this.id, this.ippId, 0, page.locale);
    }
  };

  // ---- Not so public functions -------------------------------------

  /**
   * Inits the menu area for this menu entry.
   */
  this.initMenu = function()
  {
    this.menuEntryDiv = $("#" + htmlId);
    if(this.menuEntryDiv.length == 0)
    {
      this.buildMenu();
    }
    else
    {
      a = this.menuEntryDiv.find("a:first");
    }

    // set javascript link
    a.attr("href", "javascript:openMenu(\"" + this.parentItem.parentItem.id + "\", \"" + this.parentItem.id + "\", \""
      + this.id + "\", " + this.index + ");");
    this.selected = a.hasClass(CLASSNAME_SELECTED);
  };

  /**
   * The method creates the HTML representation of this menu entry in the navigation area.
   */
  this.buildMenu = function()
  {
    this.menuEntryDiv = $("<div class=\"mainnavigation_entry_gray\"/>");
    a = $("<a class=\"mainnavigation_level_2\"/>");
    a.text(this.label);
    this.menuEntryDiv.append(a);
    this.parentItem.menuContainerDiv.append(this.menuEntryDiv);
  };

  /**
   * Inits the content area for this menu entry.
   */
  this.initContent = function()
  {
    var contentEntryDiv = $("div.content_range_container");
    if(contentEntryDiv.length != 0)
    {
      page.contentDiv = contentEntryDiv;

      // set javascript links
      var highlightDivs = contentEntryDiv.find("div.highlight");
      var type = this.parentItem.id;
      var range = this.id;
      var ippId = this.ippId;
      highlightDivs.each(function (i)
      {
        // overwrite internal urls only which don't have the attribute target set.
        var target = $(this).find("a").attr("target");
        if(target == null)
        {
          var url = "javascript:showHighlights(\"" + page.dealerId + "\", \"" + type + "\", \"" + range + "\", \"" + ippId + "\", " + i + ", \""
            + page.locale + "\");";
          $(this).find("a").attr("href", url);
        }
      });

    }
  };

  // ---- Constructor call --------------------------------------------

  this.constructor(parentItem, htmlId, id, index, ippId, label, thumb);
};

  /*
   :parent.div <div class="mainnavigation_type_contents">
   <c:forEach var="range" items="${type.subMenu}">
   <div class="mainnavigation_entry_gray">
   :a                <a id="${range.id}" class="mainnavigation_level_2<c:if test="${range.selected}">_selected</c:if>" href="${range.url}"><c:out value="${range.label}"/></a>
   </div>
   </c:forEach>
   </div>
   */
