// Version 1.05 22-01-2010 12:19        

var MENU = undefined;

         parseMenu = function( pParentId, pMenu, pCMSMenu )
         {
               MENU = new MenuItem(pMenu, -1, "", null, null, "", "", false, pCMSMenu);
         }


         var MenuItem = Class.create(
         {
            initialize : function(pSubMenu, pId, pLabel, pParent, pRoot, pUrl, pModule, pOpen, pIsCMSMenu, pDoNotParseChildren)
            {
               this.id = pId;
               this.label = pLabel;
               this.url = pUrl;
               this.module = pModule;
               this.parent = pParent;
               this.cntr = undefined;
               this.labelCntr = undefined;
               this.cntCntr = undefined;
               this.open = false;
               this.active = pOpen;
               this.cmsMenu = pIsCMSMenu;
               this.root = pRoot;
			   this.autoHide = false;
			   this.isHeader = false;
               // timer only in root to close the currently open node if no action is used and the mouse cursor is not on the MenuItem.
               this.timer = null;
               this.subMenuItems = new Array();
               this.childNodes = -1;
               if ( pSubMenu != undefined && pSubMenu != null )
                  this.childNodes = pSubMenu.length;

               var root = pRoot;
               if ( pParent != null ) // Do stuff when it isn't the root node.
               {                 
                  //DEBUG("Adding [" + pLabel + "] to [" + this.getParent().getLabel() + " " + this.getParent().getId() + "]"); 			   
                  var element = this.createElement();
                  if ( element != null)
                  {
                     if ( this.getParent() != null )
					 {
					    if ( !this.getParent().isRoot() )
                        {
                           this.getParent().getContentCntr().appendChild(element);
                        }
						else
						{
						   //DEBUG("=== ERROR 101 ==="); 
						   //DEBUG("Error adding [" + this.getId() + " - " + this.getLabel() + "] parent is not root element");
						   //DEBUG("parent [" + this.getParent().getId() + "] - [" + this.getParent().getLabel() + "]");
						   //DEBUG("================="); 
						}
				     }
					 //else
					    //DEBUG("Error adding [" + this.getId() + " - " + this.getLabel() + "] parent is null");
				  }
                  //else
                     //DEBUG("Error adding [" + this.getId() + " - " + this.getLabel() + "] element is null"); 				  
               }
               else // Set root to be THIS node.
               {
			      //DEBUG(pLabel + " is root menu element");
                  root = this;
                  this.cntr = document.getElementById("menu");

                  this.cntCntr = this.cntr.childNodes[0];
                  var self = this;

                 // Check else IE may crash if this.cntCntr is undefined.
                 if ( typeof(this.cntCntr) != 'undefined' )
                 {
				    // Add an onMouseOut for closing menu, but only if the feature is turned on.
					if ( this.autoHide )
					{
                    // onMouseOut for closing menu when it looses focus.
                    this.cntCntr.onmouseout = function(e)
                    {
                       if (!e) 
                          var e = window.event;
	                 var tg = (window.event) ? e.srcElement : e.target;

                       var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	                 while (reltg != self.cntr && reltg.nodeName != 'BODY')
		              reltg = reltg.parentNode;
	
                       if (reltg== self.cntr) 
                          return;

                       self.startTimer();
                    };
					}
                  }
                  else
                  {
                     // Menu will not close automatically when it looses focus.
                  }
               }


               //this.cntCntr.onmouseover = new Function() { alert('test'); };

               // Parse the children of this node.
               if (pSubMenu.length > 0 )
               {   
			      
			      if ( typeof(pDoNotParseChildren) == 'undefined' || !pDoNotParseChildren)
				  {
			      // TODO if this is a submenu the item itself is a menu option to, but since it is the parent
				  // of child MenuItems it can't be clickable, so a sub MenuItem of itself is needed.
				  //var item = new MenuItem( "", pId, pLabel, this, pRoot, pUrl, pModule, pOpen, pIsCMSMenu, true );
                  //this.subMenuItems[i] = item;
				  this.isHeader = true;
				  
                  for ( var i = 0; i < pSubMenu.length; i++)
                  {
				if ( pSubMenu[i].enabled )
                     {     
                     var item = new MenuItem( pSubMenu[i].childPages, pSubMenu[i].id, pSubMenu[i].label, this, root, pSubMenu[i].url, pSubMenu[i].module, pSubMenu[i].active );
                     this.subMenuItems[i] = item;
}
                  }
				  }
				  
               }
               
            },



                    isRoot : function()
                    {
                       if ( this.root == null )
                          return true;
                       else
                          return false;
                    },

                    getRoot : function()
                    {
                       if ( this.isRoot() )
                          return this;
                       else
                          return this.root;
                    },

                    isCMSMenu : function()
                    {
                       return this.getRoot().cmsMenu;
                    },



                    createElement : function()
                    {
					   var isMainMenuItem = false;
                       if (this.root == this.parent)
                       {
					      //DEBUG(this.getLabel() + " retrieve component");
                          this.cntr = document.getElementById("mi_" + this.id);
                          this.labelCntr = document.getElementById("mi_" + this.id + "_lbl");
						  isMainMenuItem = true;
                       }
                       else
                       {
                          this.cntr = document.createElement("li");
                          this.labelCntr = document.createElement("div");
						  // FIX Menu parents become links
						  // TODO how to avoid including links of children that are only frames.
                          if ( this.childNodes != -1 && this.childNodes != 0)
                          {
                             this.labelCntr.innerHTML = "[" + this.label + "]";
                          }
                          else
                          {
                             if ( this.isCMSMenu() )
                             {
                             // TODO need a way to tell if the CMS url or webpage url should be used.
                             var url = "<a href='../";
                             if ( this.module != "" )
                                url += this.module + "/";
                             url += this.url + "'>" + this.label + "</a>";
                             this.labelCntr.innerHTML = url;
                             }
                             else
                                this.labelCntr.innerHTML = "<a href='index.jsp?id=" + this.id + "'>" + this.label + "</a>";
                          }
                          this.labelCntr.id = "mi_" + this.id;
                       
                          this.cntr.appendChild(this.labelCntr);
                       }

                       if ( this.cntr != null )
                       {

                       var self = this;

					   // Add "open submenu" onclick handler if this menu has childs ( a submenu)
					   if ( this.childNodes > 0 )
                       {					   
                          this.cntr.onclick = function(e)
                          {
                             if (!e) 
                                var e = window.event;

                             var event = e;
                             setTimeout(function()
                             {
                                self.test(event);
                             }, 400);
                          };
					   }
					   else // turn this menu item into a link itself.
					   {
					      // TODO need to do sth else if this is the CMS menu.
					      this.labelCntr.innerHTML = "<a href='index.jsp?id=" + this.id + "'>" + this.label + "</a>";
					   }

                       this.labelCntr.onmouseover = function()
                       {
					      if ( !self.isHeader )
                          {
						     self.labelCntr.style.cursor = "pointer";
                             self.cntr.style.listStyleImage = "url(../images/menu-mo.png)";
						  }	
                          if ( self.getParent() != undefined && self.getParent() != null )
                          {
                             self.getParent().deHighlight();
                          }
                       }

                       this.labelCntr.onmouseout = function()
                       {
                          self.cntr.className = "no-hover";
                          self.cntr.style.listStyleImage = "none";
                       };

                       this.cntCntr = document.createElement("ul");
                       
                       if ( this.active == "true" || !this.autoHide)
                       {
                          this.cntCntr.className = "smenu";
                       }
                       else
                          this.cntCntr.className = "smenu-hidden";

                       this.cntr.appendChild( this.cntCntr );

                          return this.cntr;
                       }
                       else
                          return null;
                    },



                    test : function( pEvent )
                    {
                       this.showSubMenu();
                       this.cntr.className = "hover";
                    },

                    getParent : function()
                    {
                       return this.parent;
                    },

                    deHighlight : function()
                    {
                       this.cntr.style.listStyleImage = "none";
                    },

                    equals : function( pMenuItem)
                    {
                       if ( pMenuItem == undefined || pMenuItem == null )
                          return false;
 
                       if ( pMenuItem.getId() == this.getId() )
                          return true;

                       return false;
                    },



                    showSubMenu : function()
                    {
                       this.stopTimer();
					   // Close other nodes, but only if autoHide of inactive submenus is enabled.
					   if ( this.autoHide )
					   {
                       if ( this.root != undefined && this.root != null )
                       {
                          var menu = this.root.getSubMenu();
                       
                             for ( var i = 0; i < menu.length; i++ )
                             {
                                if ( menu[i].isOpen())
                                {
                                   menu[i].close();
                                }
                             }
                       }
                       }
                       this.cntCntr.className = "smenu";
                       this.open = true;
                    },



                    close : function()
                    {
                       if ( !this.isRoot() )
                       {
                          this.open = false;
                          this.cntCntr.className = "smenu-hidden";
                       }

                       if ( this.subMenuItems.length > 0 )
                       {
                          for ( var i = 0; i < this.subMenuItems.length; i++ )
                          {
                             if ( this.subMenuItems[i].isOpen() )
                                this.subMenuItems[i].close();
                          }
                       }
                    },

                    getContainer : function()
                    {
                       return this.cntr;
                    },

                    getId : function()
                    {
                       return this.id;
                    },

                    getContentCntr : function()
                    {
                       return this.cntCntr;
                    },

                    addSubMenuItem : function( pMenuItem )
                    {
                       //this.subMenuItems[ this.subMmenuItems.length ] = pMenuItem;
                       this.cntCntr.appendChild( pMenuItem.getContainer() );
                    },

                    hideSubMenu : function( pHide )
                    {
                       if ( pHide )
                          this.cntCntr.className = "smenu-hidden";
                       else
                          this.cntCntr.className = "smenu";
                    },

                    getSubMenu : function()
                    {
                       return this.subMenuItems;
                    },

                    isOpen : function()
                    {
                       return this.open;
                    },

                    stopTimer : function()
                    {
                       if ( this.isRoot() )
                       {
                          if ( this.timer != null )
                          {
                             clearTimeout(this.timer);
                             this.timer = null;
                          }
                       }
                       else
                            this.root.stopTimer();
                    },

                    getTimer : function()
                    {
                       if ( this.isRoot() )
                          return this.timer;
                       else
                          return this.root.getTimer();
                    },

                    setTimer : function( pTimerId )
                    {
                       if ( this.isRoot() )
                          this.timer = pTimerId;
                       else
                          this.root.setTimer( pTimerId );
                    },

                    startTimer : function()
                    {
                       var self = this;
                       var timerId = setTimeout( function() { self.close() }, 5000);
                       this.setTimer(timerId);
                    },

                    resetTimer : function()
                    {
                       this.stopTimer();
                       this.startTimer();
                    },
					
					getLabel : function()
					{
					   return this.label;
					}
                 });