// Copyright © 2004 - 2012 Interact Computer Services Ltd.
// All rights reserved. Use is subject to license terms.
// For more information contact Interact at info@interact-uk.com

function AL_MenuStrip (parent, oid, cname, cdiv)
{
  this.getMenuStripClass     = function (obj, oid) {return (null);}
  this.getMenuStripContainer = function (obj, oid) {return (null);}
  
  this.parent  = parent ? parent : this;
  this.oid     = oid    ? oid    : 0;
  this.cname   = cname  ? cname  : this.parent.getMenuStripClass     (this, oid);
  this.div     = cdiv   ? cdiv   : this.parent.getMenuStripContainer (this, oid);
  this.id      = menuSequence++;
  this.popup   = null;
  this.hframe  = null;
  this.timer   = null;
  this.stimer  = null;
  this.items   = new Array ();
  this.ml      = 0;
  
  this.menuDiv = null;
  this.menuSL  = null;
  this.menuSR  = null;
  
  if (window.createPopup)
  {
//  this.popup = window.createPopup ();
  }
  
  menuIndex.put (this.id, this);
  
  if (this.cname) if (this.cname.length == 0)
  {
    this.cname = null;
  }
  
  if (this.cname == null)
  {
    this.cname = "mstrip";
  }
  
  this.configure = function (doc, augment, keep, array)
  {
    if (!augment)
    {
      this.clear (keep ? true : false);
    }
    
    if (this.menuDiv == null)
    {
      this.menuDiv = document.createElement ("DIV");
      this.menuSL  = document.createElement ("DIV");
      this.menuSR  = document.createElement ("DIV");
      
      this.menuDiv.className = "mstrip";
      this.menuSL.className  = "mstripNavigation mstripScroll";
      this.menuSR.className  = "mstripNavigation mstripScroll";
      
      this.menuSL.style.left  = "0px";
      this.menuSR.style.right = "0px";
      
      this.menuSL.innerHTML = "&laquo";
      this.menuSR.innerHTML = "&raquo";

      this.menuSL.onmouseover = alHighlight;
      this.menuSL.onmouseout  = alHighlight;
      this.menuSL.onmousedown = Menu_Mouse;
      this.menuSL.onmouseup   = Menu_Mouse;
      
      this.menuSR.onmouseover = alHighlight;
      this.menuSR.onmouseout  = alHighlight;
      this.menuSR.onmousedown = Menu_Mouse;
      this.menuSR.onmouseup   = Menu_Mouse;
      
      this.menuSL.sdir = -1;
      this.menuSR.sdir =  1;
      
      this.menuDiv.mid = this.id;
      this.menuSL.mid  = this.id;
      this.menuSR.mid  = this.id;
      
      this.div.appendChild (this.menuDiv);
      this.div.appendChild (this.menuSL);
      this.div.appendChild (this.menuSR);
    
      this.menuDiv.onclick       = Menu_Click;
      this.menuDiv.oncontextmenu = Menu_Click;
      this.menuDiv.ondblclick    = Menu_Click;
      
      this.menuDiv.onmouseover   = Menu_Highlight;
      this.menuDiv.onmouseout    = Menu_Highlight;
      
      if (!this.parent.resizeEvent)
      {
        addResizeListener (this);
      }
    }
    
    if (array)    {      var items = doc;      var midx  = -1;      var icnt  = 0;            while (++midx < items.length)      {        icnt = items[midx].length;                if (icnt > 0)        {          this.addItem ((icnt > 0) ? items[midx][0] : null,                        (icnt > 1) ? items[midx][1] : null,                        (icnt > 2) ? items[midx][2] : null,                        (icnt > 3) ? items[midx][3] : null,                        (icnt > 4) ? items[midx][4] : null,                        (icnt > 5) ? items[midx][5] : null,                        (icnt > 6) ? items[midx][6] : null, true);        }      }    }    else    {      var items = doc.selectNodes ("./items/item");
      var midx  = -1;
    
      while (++midx < items.length)
      {
        this.addItem (getSingleNode (items[midx], "./Name", null, true, null, true),
                      getSingleNode (items[midx], "./Text", null, true, null, true),
                      getSingleNode (items[midx], "./Title", ""),
                      items[midx].selectNodes ("./items/item"),
                      getSingleNode (items[midx], "./ActionLeft",   null, true, null, true),
                      getSingleNode (items[midx], "./ActionRight",  null, true, null, true),
                      getSingleNode (items[midx], "./ActionDouble", null, true, null, true));
      }
    }
    
    if (this.pidx)
    {
      var sobj  = null;
      
      while (this.pidx.length > 0)
      {
        sobj = this.pidx.pop ();
        midx = this.items.length;
        
        while (midx-- > 0)
        {
          if (this.items[midx].mname == sobj.mname)
          {
            mobj = this.items[midx];
            
            if (mobj.smenu)
            {
              mobj.sbase = mobj.smenu.length + 1;
              
              while (sobj.smenu.length > 0)
              {
                mobj.smenu.push (sobj.smenu.splice (0, 1)[0]);
              }
            }
            else
            {
              mobj.sbase = 1;
              mobj.smenu = sobj.smenu;
            }

            sobj.smenu = null;
          }
        }
        
        this.remSubMenu (sobj);
      }
      
      this.clearPidx ();
    }
    
    this.windowResized ();
  }
  
  this.clearPidx = function ()
  {
    if (this.pidx)
    {
      while (this.pidx.length > 0)
      {
        this.remSubMenu (this.pidx.pop ());
      }
    }
  }
  
  this.clear = function (keep)
  {
    var mobj  = null;
    var sobj  = null;
    
    if (this.pidx)
    {
      this.clearPidx ();
    }
    
    while (this.items.length > 0)
    {
      mobj = this.items.pop ();
      
      if (keep)
      {
        if (mobj.sbase && mobj.smenu) if (mobj.sbase <= mobj.smenu.length)
        {
          if (!this.pidx)
          {
            this.pidx = new Array ();
          }
          
          sobj = new Object ();
          
          sobj.mname = mobj.mname;
          sobj.smenu = mobj.smenu.splice ((mobj.sbase - 1), (mobj.smenu.length - mobj.sbase + 1));
          
          this.pidx.push (sobj);
        }
      }
      
      this.remSubItem (mobj.smenu);
      
      this.menuDiv.removeChild (mobj);
      
      mobj.actions = null;
      mobj         = null;
    }
    
    this.ml = 0;
  }
  
  this.destroy = function ()
  {
    this.clear ();

    if (this.menuDiv)
    {
      this.div.removeChild (this.menuDiv);
      this.div.removeChild (this.menuSL);
      this.div.removeChild (this.menuSR);
      
      this.menuDiv = null;
      this.menuSL  = null;
      this.menuSR  = null;
      
      if (!this.parent.resizeEvent)
      {
        remResizeListener (this);
      }
    }
    
    if (this.hframe)
    {
      freeIEOver (this.hframe);
    }

    this.popup  = null;
    this.hframe = null;
  }
    this.setItems = function (items)  {    var marr  = null;        if (items) if (items.length > 0)    {      var msub  = null;      var midx  = -1;      var icnt  = 0;      marr = new Array ();            while (++midx < items.length)      {        msub = new Object ();        icnt = items[midx].length;                msub.name  = (icnt > 0) ? items[midx][0] : null;        msub.text  = (icnt > 1) ? items[midx][1] : null;        msub.title = (icnt > 2) ? items[midx][2] : null;                msub.smenu   = this.setItems ((icnt > 3) ? items[midx][3] : null);        msub.actions = new Object ();                msub.actions.lclick = (icnt > 4) ? items[midx][4] : null;        msub.actions.rclick = (icnt > 5) ? items[midx][5] : null;        msub.actions.dclick = (icnt > 6) ? items[midx][6] : null;                marr.push (msub);      }    }        return (marr);  }    this.setItems = function (items)  {    var marr  = null;        if (items) if (items.length > 0)    {      var msub  = null;      var midx  = -1;      var icnt  = 0;      marr = new Array ();            while (++midx < items.length)      {        msub = new Object ();        icnt = items[midx].length;                msub.name  = (icnt > 0) ? items[midx][0] : null;        msub.text  = (icnt > 1) ? items[midx][1] : null;        msub.title = (icnt > 2) ? items[midx][2] : null;                msub.smenu   = this.setItems ((icnt > 3) ? items[midx][3] : null);        msub.actions = new Object ();                msub.actions.lclick = (icnt > 4) ? items[midx][4] : null;        msub.actions.rclick = (icnt > 5) ? items[midx][5] : null;        msub.actions.dclick = (icnt > 6) ? items[midx][6] : null;                marr.push (msub);      }    }        return (marr);  }  
  this.parseItems = function (items)
  {
    var marr  = null;
    
    if (items) if (items.length > 0)
    {
      var msub  = null;
      var midx  = -1;

      marr = new Array ();
      
      while (++midx < items.length)
      {
        msub = new Object ();
        
        msub.name  = getSingleNode (items[midx], "./Name", null, true, null, true);
        msub.text  = getSingleNode (items[midx], "./Text", null, true, null, true);
        msub.title = getSingleNode (items[midx], "./Title", "");
        msub.smenu = this.parseItems (items[midx].selectNodes ("./items/item"));
        
        msub.actions = new Object ();
        
        msub.actions.lclick = getSingleNode (items[midx], "./ActionLeft",   null, true, null, true);
        msub.actions.rclick = getSingleNode (items[midx], "./ActionRight",  null, true, null, true);
        msub.actions.dclick = getSingleNode (items[midx], "./ActionDouble", null, true, null, true);
        
        marr.push (msub);
      }
    }
    
    return (marr);
  }

  this.resetSubItemText = function (mname,
                                    name,
                                    text,
                                    title)
  {
    var midx  = -1;
    var mobj  = null;
    var msub  = null;
    
    while ((mobj == null) && (++midx < this.items.length))
    {
      if (this.items[midx].mname == mname)
      {
        mobj = this.items[midx];
      }
    }
    
    if (mobj) if (mobj.smenu)
    {
      midx = mobj.smenu.length;
      
      while ((msub == null) && (midx-- > 0))
      {
        if (mobj.smenu[midx].name == name)
        {
          msub = mobj.smenu[midx];
        }
      }
      
      if (msub)
      {
        msub.text    = text  ? text  : null;
        msub.title   = title ? title : null;
      }
    }
    
    mobj = null;
    msub = null;
  }
  
  this.resetSubItem = function (mname,
                                name,
                                text,
                                title,
                                items,
                                lclick,
                                rclick,
                                dclick,
                                create,
                                sorted)
  {
    var midx  = -1;
    var mobj  = null;
    var msub  = null;
    
    while ((mobj == null) && (++midx < this.items.length))
    {
      if (this.items[midx].mname == mname)
      {
        mobj = this.items[midx];
      }
    }
    
    if ((mobj == null) && create)
    {
      this.addSubItem (mname,
                       name,
                       text,
                       title,
                       items,
                       lclick,
                       rclick,
                       dclick,
                       sorted);
    }
    else
    {
      if (mobj) if (mobj.smenu)
      {
        midx = mobj.smenu.length;
        
        while ((msub == null) && (midx-- > 0))
        {
          if (mobj.smenu[midx].name == name)
          {
            msub = mobj.smenu[midx];
          }
        }
        
        if (!msub && create)
        {
          msub         = new Object ();
          msub.actions = new Object ();
          msub.smenu   = null;
          msub.name    = name;
          
          if (!mobj.sbase)
          {
            mobj.sbase = mobj.smenu.length + 1;
          }
         
          if (sorted)
          {
            var got = false;
            
            midx = mobj.smenu.length;
            
            while (!got && (midx > mobj.sbase))
            {
              if (mobj.smenu[--midx].name < name)
              {
                midx += 1;
                got   = true;
              }
            }
            
            mobj.smenu.splice (midx, 0, msub);
          }
          else
          {
            mobj.smenu.push (msub);
          }
        }
        
        if (msub)
        {
          msub.text    = text  ? text  : null;
          msub.title   = title ? title : null;
          
          msub.actions.lclick = lclick;
          msub.actions.rclick = rclick;
          msub.actions.dclick = dclick;
        }
      }
    }
  }
  
  this.addSubItem = function (mname,
                              name,
                              text,
                              title,
                              items,
                              lclick,
                              rclick,
                              dclick,
                              sorted)
  {
    var midx  = -1;
    var mobj  = null;
    
    while ((mobj == null) && (++midx < this.items.length))
    {
      if (this.items[midx].mname == mname)
      {
        mobj = this.items[midx];
      }
    }
    
    if (mobj == null)
    {
      mobj = this.addItem (mname);
    }
    
    if (mobj.smenu == null)
    {
      mobj.smenu = new Array ();
    }
    
    var msub  = new Object ();
    
    msub.name    = name;
    msub.text    = text  ? text  : null;
    msub.title   = title ? title : null;
    msub.smenu   = null;
    msub.actions = new Object ();
    
    msub.actions.lclick = lclick;
    msub.actions.rclick = rclick;
    msub.actions.dclick = dclick;
          
    if (!mobj.sbase)
    {
      mobj.sbase = mobj.smenu.length + 1;
    }
   
    if (sorted)
    {
      var got = false;
      
      midx = mobj.smenu.length;
      
      while (!got && (midx > mobj.sbase))
      {
        if (mobj.smenu[--midx].name < name)
        {
          midx += 1;
          got   = true;
        }
      }
      
      mobj.smenu.splice (midx, 0, msub);
    }
    else
    {
      mobj.smenu.push (msub);
    }
  }
  
  this.remSubItem = function (mname, name)
  {
    var midx  = -1;
    var mobj  = null;
    
    while ((mobj == null) && (++midx < this.items.length))
    {
      if (this.items[midx].mname == mname)
      {
        mobj = this.items[midx];
      }
    }
    
    if (mobj)
    {
      midx = mobj.smenu.length;
      
      while (midx-- > 0)
      {
        if (mobj.smenu[midx].name == name)
        {
          this.remSubMenu (mobj.smenu[midx]);
          mobj.smenu.splice (midx, 1);
          
          midx = 0;
        }
      }
    }
  }
  
  this.addItem = function (name,
                           text,
                           title,
                           items,
                           lclick,
                           rclick,
                           dclick,
                           array)
  {
    var mobj  = this.menuDiv.appendChild (document.createElement ("SPAN"));
    
    mobj.mid        = this.id;
    mobj.iid        = menuItemSeq++;
    mobj.mname      = name  ? name : "Option " + mobj.iid;
    mobj.innerHTML  = text  ? text  : name;
    mobj.title      = title ? title : "";
    mobj.mtop       = true;
    mobj.actions    = new Object ();
    
    mobj.style.left = this.ml + "px";
    this.ml        += 66;
    
    mobj.className  = "";
    mobj.hln        = "";
    mobj.hlc        = "mstrip_hl";
    
    this.items.push (mobj);

    if (items) if (items.length > 0)
    {
      if (array)      {        mobj.smenu = this.setItems (items);      }      else      {        mobj.smenu = this.parseItems (items);      }    }
    
    mobj.actions.lclick = lclick ? lclick : null;
    mobj.actions.rclick = rclick ? rclick : null;
    mobj.actions.dclick = dclick ? dclick : null;
    
    return (mobj);
  }
  
  this.remSubMenu = function (mobj)
  {
    if (mobj.smenu)
    {
      var smenu   = mobj.smenu;
      var sidx    = smenu.length;
      
      while (sidx-- > 0)
      {
        this.remSubMenu (smenu[sidx]); smenu[sidx] = null;
      }
    }

    mobj.actions = null;
    mobj.smenu   = null;
  }
  
  this.remItem = function (idx)
  {
    this.menuDiv.removeChild (this.items[idx]);
    this.remSubMenu          (this.items[idx]);
    
    this.items.splice (idx, 1);
    
    this.ml = idx * 66;
    
    while (idx < this.items.length)
    {
      this.items[idx].style.left = this.ml + "px";
      
      ml  += 66;
      idx += 1;
    }
    
    this.windowResized ();
  }
  
  this.remItemByID = function (iid)
  {
    var idx = this.items.length;
    var ok  = false;
    
    while (idx-- > 0)
    {
      if (this.items[idx].iid == iid)
      {
        this.remItem (idx);
        
        idx = 0;
        ok  = true;
      }
    }
    
    return (ok);
  }
  
  this.remItemByName = function (name)
  {
    var idx = this.items.length;
    var ok  = false;
    
    while (idx-- > 0)
    {
      if (this.items[idx].mname == name)
      {
        this.remItem (idx);
        
        idx = 0;
        ok  = true;
      }
    }
    
    return (ok);
  }

  this.highlighter = function (msin, evt)
  {
    if (evt)
    {
      if (evt.target.mtop)
      {
        if ((this.showing != evt.target) && evt.target.smenu)
        {
          this.subMenu (evt.target);
        }
      }
      else if (evt.target.msub)
      {
        evt = null;
      }
    }
    
    if (!evt)
    {
      if (this.stimer)
      {
        window.clearTimeout (this.stimer);
        this.stimer = null;
      }
      
      if (!msin)
      {
        this.stimer = window.setTimeout ("Menu_Cancel ('s', " + this.id + ")", 3000);
      }
    }
  }
  
  this.cancel = function (which)
  {
    if (which == 's')
    {
      window.clearTimeout (this.stimer);
      this.stimer = null;
      
      this.showSub (null);
    }
  }

  this.subMenu = function (mobj)
  {
    if (this.stimer)
    {
      window.clearTimeout (this.stimer);
      this.stimer = null;
    }

    if (mobj.smenu)
    {
      this.showing = mobj;
      
      if (!this.sdiv)
      {
        this.sdiv = document.createElement ("UL");
        this.sul  = this.sdiv;
        
        this.sdiv.className     = "mstripMenu";
        this.sdiv.style.display = "none";
        this.sdiv.style.zIndex  = 9999;
        
        document.body.appendChild (this.sdiv);
//      this.sdiv.appendChild     (this.sul);
        
        this.sul.onclick       = Menu_Click;
        this.sul.oncontextmenu = Menu_Click;
        this.sul.ondblclick    = Menu_Click;
        
        this.sul.onmouseover = Menu_Highlight;
        this.sul.onmouseout  = Menu_Highlight;
      }
      
      if (mobj.nodeName.toString () == "SPAN")
      {
        var dims  = getDimensions (mobj);

        this.sdiv.style.left = (2 + dims[0]) + "px";
        this.sdiv.style.top  = (2 + dims[1] + dims[3]) + "px";
      }
      
      this.showSub (mobj.smenu);
      this.highlighter (false, null);
    }
  }
  
  this.showSub = function (smenu)
  {
    var kids  = this.sul.childNodes;
    var kidx  = kids ? kids.length : 0;
    var kid   = null;
    
    while (kidx-- > 0)
    {
      kids[kidx].actions = null;
      kids[kidx].smenu   = null;
      kids[kidx].mname   = null;
      kids[kidx].mtext   = null;
      
      this.sul.removeChild (kids[kidx]);
    }
    
    if (smenu)
    {
      kidx = -1;
      
      while (++kidx < smenu.length)
      {
        kid = this.sul.appendChild (document.createElement ("LI"));
        
        kid.mid        = this.id;
        kid.iid        = menuItemSeq++;
        kid.mname      = smenu[kidx].name  ? smenu[kidx].name  : "Option " + kid.iid;
        kid.mtext      = smenu[kidx].text  ? smenu[kidx].text  : kid.mname;
        kid.title      = smenu[kidx].title ? smenu[kidx].title : kid.mtext;
        kid.innerHTML  = kid.mtext
        kid.msub       = true;
        kid.smenu      = smenu[kidx].smenu;
        
//      kid.className  = "";
        kid.actions    = new Object ();
        kid.mcancel    = 's';
        
        kid.hln        = this.cname;
        kid.hlc        = this.cname + " liOver";
        
        kid.actions.lclick = smenu[kidx].actions.lclick;
        kid.actions.rclick = smenu[kidx].actions.rclick;
        kid.actions.dclick = smenu[kidx].actions.dclick;
      }

      if (this.popup)
      {
        this.sdiv.style.display = "block";
        
        var px  = parseInt (this.sdiv.style.left, 10);
        var py  = parseInt (this.sdiv.style.top,  10);
        var pw  = this.sdiv.scrollWidth  + 10;
        var ph  = this.sdiv.scrollHeight + 10;
        
        this.sdiv.style.top                = "0px";
        this.sdiv.style.left               = "0px";
        this.sdiv.style.display            = "none";
        this.popup.document.body.innerHTML = "<ul>" + this.sdiv.innerHTML + "</ul>";
        
        this.popup.show (px, py, pw, ph);
      }
      else
      {
        this.sdiv.style.display = "block";
        
        alMakeTop (this.sdiv);

        if (AL_Browser.ie)
        {
          if (this.hframe == null)
          {
            this.hframe = getIEOver ();
          }
          
          this.hframe.show (this.sdiv);
        }
      }
      
//    this.sdiv.style.width  = (this.sdiv.scrollWidth  + 2) + "px";
//    this.sdiv.style.height = (this.sdiv.scrollHeight + 2) + "px";
    }
    else
    {
      if (this.popup)
      {
        this.popup.hide ();
      }
      else
      {
        if (this.hframe)
        {
          freeIEOver (this.hframe);
          this.hframe = null;
        }
        
        this.sdiv.style.display = "none";
      }
      
      this.showing = null;
    }
  }
  
  this.windowResized = function ()
  {
    var dw  = this.div.clientWidth;
    
    if (this.ml > dw)
    {
      this.menuDiv.style.left   = "0px";
      
      this.menuSL.style.display = "block";
      this.menuSR.style.display = "block";
    }
    else
    {
      this.menuDiv.style.left = Math.floor ((dw - this.ml) / 2) + "px";
      
      this.menuSL.style.display = "none";
      this.menuSR.style.display = "none";
    }
  }

  this.scroll = function (sdir)
  {
    var pp  = parseInt (this.menuDiv.style.left) + sdir;
    
    if (sdir < 0)
    {
   // if ((pp + (64 - this.div.clientWidth)) >= -12)
      {
        this.menuDiv.style.left = pp + "px";
        this.timer              = window.setTimeout ("Menu_Scroll (" + this.id + ", " + sdir + ")", 10);
      }
    }
    else
    {
      if (pp <= 12)
      {
        this.menuDiv.style.left = pp + "px";
        this.timer              = window.setTimeout ("Menu_Scroll (" + this.id + ", " + sdir + ")", 10);
      }
    }
  }
}

