var zIndex = 1000;
var hideTimeout = 500;
      
      //var menuObj = new Menu();
        
      function Menu(menu, menuID)
      {
         // menu properties
         this.DHTMLMenu = [];
         if(!(menu==undefined))
         {
            for(var i = 0; i < menu.length-1; i++)
            {
               var childs = [];
               for(var j = 0; j < menu.length-1; j++)
               {
                  if(menu[i][0] == menu[j][2])
                  {
                     childs[childs.length] = j;
                  }
               }
               
               this.DHTMLMenu[i] = [];
               this.DHTMLMenu[i][0] = menu[i][0];
               this.DHTMLMenu[i][1] = menu[i][1];
               this.DHTMLMenu[i][2] = childs;
               this.DHTMLMenu[i][3] = menu[i][3];
               this.DHTMLMenu[i][4] = menu[i][4];
               this.DHTMLMenu[i][5] = menu[i][5];
               this.DHTMLMenu[i][6] = menu[i][6];
            }
         }
         
         this.menuID = menuID;
         this.parentID = '';
         this.menuBorderCssClass = '';
         this.menuItemCssClass = '';
         this.menuItemHoverCssClass = '';
         this.menuImageCssClass = '';
         this.dividerCssClass = '';
         this.isLinkedMenu = false;
         this.isShowImages = false;
         this.dividerImagePath = '';
         this.firstLevelFilterDuration = 0.3;
         this.firstLevelFilterTransition = 0;
         this.filterDuration = 0.0;
         this.filterTransition = 0;
         
         //private varibles
         this.activeIndex = -2;
         
         //methods
         this.show = ShowMenu;
         this.getIndex = GetIndex;
         this.getParentIndex = GetParentIndex;
         this.renderItem = RenderItem;
         this.topPosition = GetTopPosition;
         this.leftPosition = GetLeftPosition;
         this.hideAll = RemoveElement;
         this.destroy = Destroy;
         this.parsePosition = ParsePosition;
         this.getParentsDiv = GetParentsDiv;
         this.hideChildren = RemoveChildren;
         this.countOfChildren = GetCountOfChildren;
         this.deactivate = Deactivate;
         this.isChild = IsChild;
         this.addClass = AddClass;
         this.removeClass = RemoveClass;
         this.changeClass = ChangeClass;
      }
      
   
   function GetCountOfChildren(parentID)
   {
      itemNumber = this.getIndex(parentID);
      var count = 0;
      if(itemNumber == -1)
      {
         for(var i = 0; i < this.DHTMLMenu.length; i++)     
         {
            if(this.DHTMLMenu[i][1] == (itemNumber+1))
            { 
               count++;
            }
         }
      }
      else
      {
         count = this.DHTMLMenu[itemNumber][2].length;   
      }
      
      return count;
   }
      
   function IsChild(parentIndex, sonIndex, direct)
   {
      var isChild = false;
      if(direct)
      {
         if(sonIndex > -1)
         {
            if(parentIndex == -1)
            {
               return (this.DHTMLMenu[sonIndex][1] == 0);
            }
            else
            {
               for(var i = 0; i < this.DHTMLMenu[parentIndex][2].length; i++)  
               {
                  if(this.DHTMLMenu[parentIndex][2][i] == sonIndex)
                  {
                     return true;
                  }
               }
            }
         }
      }
      else
      {
         //TODO should be implmented if need it
      }
      return false;
   }
           
   function ShowMenu(parentID)
   {
      
      if(this.DHTMLMenu)
      {
         //Hide all submenu on the same level
         itemNumber = this.getIndex(parentID);
         this.activeIndex = itemNumber;
         var level = 0;
         if(itemNumber > 0)
         {
            level = this.DHTMLMenu[itemNumber][1];
         }
         
         for(var i = 0; i < this.DHTMLMenu.length; i++)
         {
            if(this.DHTMLMenu[i][1] == level)
            {
               this.hideChildren(this.menuID + "_" + this.DHTMLMenu[i][0]);
            }
         }

         if(this.countOfChildren(parentID) > 0)
         {
               divElement = CreateElement("div", window.document.body);
               divElement.className = this.menuBorderCssClass;
               if(itemNumber > -1)
               {
                  divElement.id = "div_" + parentID;
               }
               else
               {
                  divElement.id = "div_" + this.menuID + "_" + parentID;
               }
               divElement.style.zIndex = zIndex;
               try
               {
                  var filterDuration  = this.filterDuration;
                  var filterTransition  = this.filterTransition;
                  if(itemNumber == -1)
                  {
                     filterDuration = this.firstLevelFilterDuration;
                     filterTransition = this.firstLevelFilterTransition;
                  }
                     
                  if(filterTransition > 0)
                  {
                     divElement.style.filter = "revealTrans(Duration='"+filterDuration+"', Transition='"+filterTransition+"')";
                     divElement.filters[0].Apply();
                     divElement.filters[0].Play();
                  }
               }
               catch(e){}
               divElement.style.visibility = 'visible';
               divElement.style.position = 'absolute';
               innerHtml = "<table border='0' cellpadding='0' cellspacing='0' onmouseleave='"+this.menuID+".activeIndex = -2; setTimeout(\""+this.menuID+".deactivate()\","+hideTimeout.toString()+");'>";
                 
               itemRendering = "";
               if(itemNumber == -1)
               {
                  this.parentID = parentID;
                  for(var i = 0; i < this.DHTMLMenu.length; i++)     
                  {
                     if(this.DHTMLMenu[i][1] == (itemNumber+1))
                     {
                        if(this.DHTMLMenu[i][3] == 0)
                        {
                           itemRendering += "<tr onmouseover=\""+this.menuID+".show('"+this.menuID + "_" + this.DHTMLMenu[i][0]+"')\">";
                           itemRendering += "<td>";
                           //render item
                           itemRendering += this.renderItem(i);
                           itemRendering += "</td>";
                           itemRendering += "</tr>";
                        }
                        else
                        {
                           itemRendering += "<tr>";
                           itemRendering += "<td>";
                           if(this.dividerImagePath.length == 0)
                           {
                              itemRendering += "<hr class='"+this.dividerCssClass+"' />";
                           }
                           else
                           {
                              itemRendering += "<img src='"+this.dividerImagePath+"' class='"+this.dividerCssClass+"' />";
                           }
                           itemRendering += "</td>";
                           itemRendering += "</tr>"; 
                        }
                     }
                  } 
               }
               else
               {
                  for(var i = 0; i < this.DHTMLMenu[itemNumber][2].length; i++)
                  {
                     if(this.DHTMLMenu[this.DHTMLMenu[itemNumber][2][i]][3] == 0)
                     {
                        itemRendering += "<tr onmouseover=\""+this.menuID+".show('"+this.menuID + "_" + this.DHTMLMenu[this.DHTMLMenu[itemNumber][2][i]][0]+"')\" >";
                        itemRendering += "<td>";
                        //render item
                        itemRendering += this.renderItem(this.DHTMLMenu[itemNumber][2][i]);
                        itemRendering += "</td>";
                        itemRendering += "</tr>"; 
                     }
                     else
                     {
                        itemRendering += "<tr>";
                        itemRendering += "<td>";
                        if(this.dividerImagePath.length == 0)
                        {
                           itemRendering += "<hr class='"+this.dividerCssClass+"' />";
                        }
                        else
                        {
                           itemRendering += "<img src='"+this.dividerImagePath+"' class='"+this.dividerCssClass+"' />";
                        }
                        itemRendering += "</td>";
                        itemRendering += "</tr>"; 
                        
                     }   
                  }
               }
               innerHtml += itemRendering;         
               innerHtml += "</table>";
               divElement.innerHTML = innerHtml;
               
               //position
               divElement.style.top = this.topPosition(parentID, divElement)+'px';
               divElement.style.left = this.leftPosition(parentID, divElement)+'px';
         }
      }
   }
  
   function RenderItem(itemIndex)
   {
      var item = this.DHTMLMenu[itemIndex];
      var itemRendering = "";
      if(item != null)
      {
         var clickActivity = '';
         clickActivity = "onclick = 'window.location.href=\""+item[5]+"\"'";
         var tblID = this.menuID + "_" + item[0];
         itemRendering += "<table>";
         itemRendering += "<tr id='"+tblID+"' class='"+this.menuItemCssClass+"' onmouseover=\""+this.menuID+".changeClass('"+tblID+"', '"+this.menuItemHoverCssClass+"', '"+this.menuItemCssClass+"');\" "+clickActivity+">";
         if(this.isShowImages)
         {
            itemRendering += "<td>";
            if(item[6].length > 0)
            {
               itemRendering += "<img src='"+item[6]+"' class='"+this.menuImageCssClass+"'>";
            }
            itemRendering += "</td>";
         }
         itemRendering += "<td>";
         if(this.isLinkedMenu)
         {
            itemRendering += "<a href='"+item[5]+"' class='"+this.menuItemCssClass+"'>";
            itemRendering += item[4];
            itemRendering += "</a>";
         }
         else
         {
            itemRendering += item[4];
         }
         itemRendering += "</td>";
         itemRendering += "</tr>";
         itemRendering += "</table>";
      }
      return itemRendering;
   }

//************************************************ Behavior ************************************************
     
   function CreateElement(type, parent) 
   {
      var el = null;
      if(window.self.document.createElementNS)
      // use the XHTML namespace; IE won't normally get here unless
      // _they_ "fix" the DOM2 implementation.</SPAN>
         el = window.self.document.createElementNS("http://www.w3.org/1999/xhtml", type);
      else
         el = window.self.document.createElement(type);
      if(typeof parent != "undefined")
         parent.appendChild(el);
      return el;
   };

   function Deactivate()
   {
      if(this.activeIndex == -2)
      {
         for(var i = 0; i < this.DHTMLMenu.length; i++)
         {
            if(this.DHTMLMenu[i][1] == 0)
            {
               this.hideAll(this.menuID + "_" + this.DHTMLMenu[i][0]);
            }
         }
      }
   }   
   
   function RemoveChildren(id)
   {
      var itemIndex = this.getIndex(id);
      if(itemIndex > -1)
      {
         for(var i = 0; i < this.DHTMLMenu[itemIndex][2].length; i++)
         {
            this.hideChildren(this.menuID + "_" + this.DHTMLMenu[this.DHTMLMenu[itemIndex][2][i]][0]);
         }
         
         var itemObj = window.document.getElementById("div_"+this.menuID+"_"+this.DHTMLMenu[itemIndex][0]);
         //alert("div_"+this.menuID+"_"+this.DHTMLMenu[itemIndex][0]);
         this.destroy(itemObj);
      }
      
   }
   
   function RemoveElement(id)
   {
      var itemIndex = this.getIndex(id);
      if(itemIndex > -1)
      {
         //Delete all children
          for(var i = 0; i < this.DHTMLMenu[itemIndex][2].length; i++)
         {
            this.hideAll(this.menuID + "_" + this.DHTMLMenu[this.DHTMLMenu[itemIndex][2][i]][0]);
         }
         
         parentIndex = this.getParentIndex(itemIndex);
         if(parentIndex > -1)
         {
            var itemObj = window.document.getElementById("div_"+this.menuID + "_" + this.DHTMLMenu[parentIndex][0]);
            this.destroy(itemObj);
         }
         else
         {
            var itemObj = window.document.getElementById("div_"+this.menuID + "_" + this.parentID);
            this.destroy(itemObj);
         }
      }
   }

   function Destroy(el) 
   {
      if(el && el.parentNode)
         el.parentNode.removeChild(el);
   };

   function IsExist(id)
   {
      if(window.document.getElementById(id))
         return true;
      
      return false;
   }
   
   function IsMozilla()
   {
  	   var ua=navigator.userAgent.toLowerCase() 
  	   var isGecko = (ua.indexOf('gecko') != -1);
	   if (isGecko && ua.indexOf("gecko/") + 14 == ua.length) 
	   return true;

	   return false;
   }
  
  //************************************** Menu Structure ***************************************************
  function GetParentIndex(index)
   {
         if(this.DHTMLMenu)
         {
            for(var i = 0; i < this.DHTMLMenu.length; i++)
            {
               for(var j = 0; j < this.DHTMLMenu[i][2].length; j++)
               {
                  if(this.DHTMLMenu[i][2][j] == index)
                     return i;
               }
            }
         }
         return -1
   }
   
   function GetIndex(parentID)
   {
      menu = this.DHTMLMenu;
      for(var i = 0; i < menu.length; i++)
      {
         //get name of the menu item
         if(this.menuID + "_" + menu[i][0] == parentID)
            return i;
      }
      // Level 0
      return -1;
   }
  
  //************************************** Menu Position ****************************************************
  
   function ParsePosition(position)
   {
      var pos = 0;
      try
      {
         var p = position.toString();         
         if(p.indexOf('px') > -1)
         {
            pos = parseInt(p.substring(0, p.indexOf('px')), 10);
            return pos;
         }
         pos = parseInt(p, 10);
         return pos
      }
      catch(e)
      {
         return 0;
      }
   }
   
    function GetParentsDiv(parentID)
   {
      var parentIndex = this.getIndex(parentID);
      var obj = null;
      if(parentIndex > -1)
      {
         var divIndex = this.getParentIndex(parentIndex);
         if(divIndex > -1)
         {
            obj = window.document.getElementById('div_'  + this.menuID + "_" + this.DHTMLMenu[divIndex][0]);
         }
         else
         {
            obj = window.document.getElementById('div_' + this.menuID + "_" + this.parentID);
         }
      }
      else
      {
         obj = window.document.getElementById(parentID);
      }
      return obj;
   }
   
   function GetParentsDivTop(parentID)
   {
      var obj = this.getParentsDiv(parentID);
      if(obj)
      {
         return this.parsePosition(obj.style.top);      
      }
      return 0;
   }
   
   function GetParentsDivLeft(parentID)
   {
      var obj = this.getParentsDiv(parentID);
      if(obj)
      {
         return this.parsePosition(obj.style.left);      
      }
      return 0;
   }
      
   function GetTopPosition(parentID, control)
   {  
      if(window.document.getElementById(parentID))
      {
         var parentControl = window.document.getElementById(parentID);
         var parentIndex = this.getIndex(parentID);
         var top;
         if(parentIndex > -1)
         {
            top = this.parsePosition(parentControl.getBoundingClientRect().top);
         }
         else
         {
            top = this.parsePosition(parentControl.getBoundingClientRect().bottom);
         }
         if((this.parsePosition(window.screen.availHeight) - top) < this.parsePosition(control.clientHeight))
         {
            return this.parsePosition(window.screen.availHeight) - this.parsePosition(control.clientHeight);
         }
         else
         {
            return top-2;
         }
      }
      return 0;
   } 
   
   function GetLeftPosition(parentID, control) 
   {
      if(window.document.getElementById(parentID))
      {
         var parentControl = window.document.getElementById(parentID);
         var obj = this.getParentsDiv(parentID);
         var left;
         var width;
         var parentIndex = this.getIndex(parentID)
         if(obj)
         {
         
            left = this.parsePosition(obj.getBoundingClientRect().left);
            width = this.parsePosition(obj.getBoundingClientRect().right) - this.parsePosition(obj.getBoundingClientRect().left);
         }
         else
         {
            left = this.parsePosition(parentControl.getBoundingClientRect().left);
            width = this.parsePosition(parentControl.getBoundingClientRect().right) - this.parsePosition(parentControl.getBoundingClientRect().left);
         }
         
         if((this.parsePosition(window.screen.availWidth) -  left) < this.parsePosition(control.clientWidth))
         {
            if((left-this.parsePosition(control.clientWidth)) > 0)
            {
               return left-this.parsePosition(control.clientWidth);
            }
            else
               return 0;
         }
         else
         {
            if(parentIndex > -1)
            {
               return left + width-2;
            }
            else
            {
               return left-1;
            }
         }
      }
      return 0;
   }
   
   /************************************************* Style *************************************************************/
   function ChangeClass(id, className, oldClassName)
   {
      var el = window.document.getElementById(id);
      
      var itemIndex = this.getIndex(id);
      for(var i = 0; i < this.DHTMLMenu.length; i++)         
      {
         if(this.DHTMLMenu[i][1] == this.DHTMLMenu[itemIndex][1])
         {
            this.removeClass(this.menuID + "_" + this.DHTMLMenu[i][0]);   
            this.addClass(this.menuID + "_" + this.DHTMLMenu[i][0], oldClassName);
         }
      }
      this.addClass(id, className);
   }
   
   function AddClass(id, className)
   {
      var el = window.document.getElementById(id);
      if(el && (className != null) && (className.length > 0))
      {
         this.removeClass(id);
         el.className = className;
      }
   }
   
   function RemoveClass(id)
   {
      var el = window.document.getElementById(id);
      if(!(el && el.className)) 
      {
         return;
      }
      el.className = '';
   }

