//////////////////////////////////////////
//                                      //
//                MENU                  //
//                                      //
//////////////////////////////////////////

Menu.MENU_PROPERTIES_XML_FILE_NAME = "menu_properties.xml";

Menu.MENUS_TAG_NAME = "menus";
Menu.MENU_TAG_NAME = "menu";
Menu.TITLE_TAG_NAME = "title";
Menu.DIR_TAG_NAME = "dir";

Menu.selectedMenuIndex = -1;
Menu.beginSelectedIndex = 0;
Menu.menuItemTextDefaultFontColor = "#EBEDF3";
Menu.menuItemTextClickFontColor = "#000000";

function Menu() 
{
  this.menuItems = new Array();
}  

Menu.init = function()
{ 
  // create system object //
  var menu = new Menu();
  var filePath = system.contentDirPath + Menu.MENU_PROPERTIES_XML_FILE_NAME;
  // import menu properties xml file //
  var xmlDoc = XmlTools.importXML(filePath);
  menu.parseXML(xmlDoc, filePath);  

  menu.paintMenuPanel();
  menu.setTreePanelWidth();
  menu.setAllMenuItemImgElements();  
    
  if(System.getMenuIndexTextFieldElem().value == "")
  {
    Menu.over(Menu.beginSelectedIndex);
    system.menu.select(Menu.beginSelectedIndex);
  }
  else
  {
    var index = System.getMenuIndexTextFieldElem().value;
    Menu.over(index);
    system.menu.select(index);  
  }
}

Menu.changeLanguage = function()
{
  system.menu.repaintMenuPanelText();
  Menu.over(Menu.selectedMenuIndex);
  Menu.click(Menu.selectedMenuIndex);  
}

Menu.prototype.parseXML = function(xmlDoc, filePath)
{
  var structureElements = xmlDoc.getElementsByTagName(System.STRUCTURE_TAG_NAME);

  if(XmlTools.isXMLFormatValid(structureElements, filePath))
  {
    var menusTag = structureElements[0].getElementsByTagName(Menu.MENUS_TAG_NAME);
    var menuTag = menusTag[0].getElementsByTagName(Menu.MENU_TAG_NAME);

    for(var i=0; i<menuTag.length; i++)
    {
      var titleTag = menuTag[i].getElementsByTagName(Menu.TITLE_TAG_NAME);
      var dirTag = menuTag[i].getElementsByTagName(Menu.DIR_TAG_NAME);
      var dir = XmlTools.getNodeValue(dirTag, 0);
      var menuItem = new MenuItem(titleTag, dir);     
      this.addMenuItem(menuItem);       
    }
  }
  system.menu = this;
}  


Menu.prototype.paintMenuPanel = function ()
{  
  var str = "";

  str += "<table border=0 cellpadding=0 cellspacing=0 width=100% height=100%>";
  str += "<tr>";

  var menuItemsLength = this.getMenuItems().length;  
  for(var i=1; i<menuItemsLength; i++)
  {
    var menuItem = this.getMenuItems()[i];
    var title = XmlTools.getLanguageNodeValue(menuItem.titleTag[0]);

    str += "<td class=\"menuTD\">";     
    str += "<table border=0 cellpadding=0 cellspacing=0 class=\"menuTABLE\"><tr>";

    str += "<td id=\"leftMenuTD"+i+"\" class=\"leftMenu\"><img src=\"img/spacer.gif\" width=9></td>";
    str += "<td id='centerMenuTD"+i+"' class='centerMenu' width=100% onclick='System.getNodeIdTextFieldElem().value = \"\";Menu.click("+i+")' onmouseover='Menu.over("+i+")' onmouseout='Menu.out("+i+")'>";      
    str += "<div id=\"textMenuDIV"+i+"\" class=\"menuText\">"+title+"</div>";     
    str += "</td>";      
    str += "<td id=\"rightMenuTD"+i+"\" class=\"rightMenu\"><img src=\"img/spacer.gif\" width=9></td>";      

    str += "</tr></table>";
    str += "</td>"; 
  }

  str += "</tr>";
  str += "</table>";

  System.getMenuPanelElem().innerHTML = str;
}

Menu.prototype.setTreePanelWidth = function ()
{
  if(document.body.offsetWidth < 800)
  {
    System.getTreePanelElem().style.width = System.treePanelElemWidthSmall + "px";
  }
  else
  {
    System.getTreePanelElem().style.width = System.treePanelElemWidthLarge + "px";  
  }
}


Menu.prototype.repaintMenuPanelText = function ()
{
  var menuItemsLength = this.getMenuItems().length;  
  for(var i=1; i<menuItemsLength; i++)
  {
    var menuItem = this.getMenuItems()[i];
    var title = XmlTools.getLanguageNodeValue(menuItem.titleTag[0]);
    menuItem.getTextElem().innerHTML = title;
  }  
}

Menu.prototype.setAllMenuItemImgElements = function ()
{
  var menuItemsLength = this.getMenuItems().length;  
  for(var i=1; i<menuItemsLength; i++)
  {
    var menuItem = this.getMenuItems()[i];
    menuItem.setLeftElem(document.getElementById("leftMenuTD" + i));
    menuItem.setCenterElem(document.getElementById("centerMenuTD" + i));
    menuItem.setTextElem(document.getElementById("textMenuDIV" + i)); 
    menuItem.setRightElem(document.getElementById("rightMenuTD" + i));   
  }
}

Menu.prototype.addMenuItem = function(menuItem)
{
  this.menuItems[this.menuItems.length] = menuItem;
}

Menu.prototype.getMenuItems = function() 
{
  return this.menuItems;
}

Menu.click = function(index)
{
  if(Menu.selectedMenuIndex != -1 && Menu.selectedMenuIndex != index && Menu.selectedMenuIndex != 0)
  {
    if(Menu.selectedMenuIndex != 0)
    {
      system.menu.convertMenuImageStylePath(Menu.selectedMenuIndex, false);
      system.menu.unselect(Menu.selectedMenuIndex);
    }
    System.getNodeIdTextFieldElem().value = "";
  }

  if(Menu.selectedMenuIndex != index)
  {
    System.getNodeIdTextFieldElem().value = "";
  }
  system.menu.select(index);
}

Menu.over = function(index)
{ 
  if(index != 0)
  {
    system.menu.convertMenuImageStylePath(index, true);
  }  
}

Menu.out = function(index)
{
  if(Menu.selectedMenuIndex != index && index != 0)
  {
    system.menu.convertMenuImageStylePath(index, false);
  }  
}

Menu.prototype.select = function(index)
{   
  Node.uniqueId = 0;
  if(index != -1)
  {
    var menuItem = this.getMenuItems()[index];
    var content = new Content(menuItem.dir);
    if(content.isValid)
    {
      this.content = content;
      content.paintTreeComponent();
      //if(System.getNodeIdTextFieldElem().value == 0 || System.getNodeIdTextFieldElem().value == "")
      if(System.getNodeIdTextFieldElem().value == "")
      {
        //System.getPageIndexTextFieldElem().value = "";
        PageNavigator.currentPageIndex = -1;
        content.selectFirstTreeNode();       
        PageNavigator.click(1);
      }
      else
      {
        var nodeId = System.getNodeIdTextFieldElem().value;
        var node = content.getTree().findNode(nodeId);
        try {
        nodeObject = eval(node.treeComponentNodeVarName)
        } catch(ex){
        alert("ERROR: " + nodeId)
        }
        nodeObject.select();
        var tempIndex = System.getPageIndexTextFieldElem().value;
        PageNavigator.currentPageIndex = -1;
        if(content.nodeClick(nodeId))
        {
          PageNavigator.click(tempIndex);      
        }  
      }
    }
    System.getMenuIndexTextFieldElem().value = index;
    Menu.selectedMenuIndex = index;
  }  
}

Menu.prototype.unselect = function(index)
{
  Menu.selectedMenuIndex = -1;
}

Menu.prototype.convertMenuImageStylePath = function(index, toClick)
{
  var menuItem = this.getMenuItems()[index];
  var leftElem = menuItem.getLeftElem();
  var centerElem = menuItem.getCenterElem();
  var rightElem = menuItem.getRightElem();
  var textElem = menuItem.getTextElem();

  if(toClick && textElem.style.color == Menu.menuItemTextClickFontColor)  
  {
    return; 
  }
  else if(!toClick && textElem.style.color == Menu.menuItemTextDefaultFontColor)
  {
    return;
  }

  if(toClick)
  {
    System.convertDefaultToClickImageStylePath(leftElem, MenuItem.leftElemBgImg);
    System.convertDefaultToClickImageStylePath(centerElem, MenuItem.centerElemBgImg);
    System.convertDefaultToClickImageStylePath(rightElem, MenuItem.rightElemBgImg);
    textElem.style.color = Menu.menuItemTextClickFontColor;
  }
  else
  {
    System.convertClickToDefaultImageStylePath(leftElem, MenuItem.leftElemBgImg);
    System.convertClickToDefaultImageStylePath(centerElem, MenuItem.centerElemBgImg);
    System.convertClickToDefaultImageStylePath(rightElem, MenuItem.rightElemBgImg);
    textElem.style.color = Menu.menuItemTextDefaultFontColor;
  }
}



//////////////////////////////////////////
//                                      //
//              MENU ITEM               //
//                                      //
//////////////////////////////////////////

MenuItem.leftElemBgImg = "img/leftMenuBg.gif";
MenuItem.centerElemBgImg = "img/centerMenuBg.gif";
MenuItem.rightElemBgImg = "img/rightMenuBg.gif";

function MenuItem(titleTag, dir) 
{
  this.titleTag = titleTag;
  this.dir = dir;
  this.leftElem = null;
  this.centerElem = null;
  this.rightElem = null;
  this.textElem = null;
}

MenuItem.prototype.setLeftElem = function(elem)
{
  this.leftElem = elem;
}

MenuItem.prototype.getLeftElem = function()
{
  return this.leftElem;
}

MenuItem.prototype.setCenterElem = function(elem)
{
  this.centerElem = elem;
}

MenuItem.prototype.getCenterElem = function()
{
  return this.centerElem;
}

MenuItem.prototype.setRightElem = function(elem)
{
  this.rightElem = elem;
}

MenuItem.prototype.getRightElem = function()
{
  return this.rightElem;
}

MenuItem.prototype.setTextElem = function(elem)
{
  this.textElem = elem;
}

MenuItem.prototype.getTextElem = function()
{
  return this.textElem;
}