﻿// JScript File

cm_IE = (document.all) ? true : false;

var cm_CurMenu = null;
// array that ontains all the top menus
var cm_RootTrees = [];
var cm_RootTreesCnt = 0;

var cvMenu_OtherOnLoad;


//===========================================================================
// 
//===========================================================================
function cvMenuStart() {
  var sMsg = '';

  //CreateDbg();

  //alert('menu start');
  //alert('xslDoc: ' + xslDoc );

  cm_f_CreateMenuDivs(xmlSideDoc);

  if (xmlDoc == null)
    return;

  //---------------------------------------------------------------------------
  // first create the menu divs	
  //---------------------------------------------------------------------------
  cm_f_CreateMenuDivs(xmlDoc);


  //---------------------------------------------------------------------------
  // now go through the root nodes
  //---------------------------------------------------------------------------
  cm_CurMenu = null;

  var rn = xmlDoc.getElementsByTagName("rootnode");
  for (i = 0; i < rn.length; i++) {
    var sID = rn[i].attributes.getNamedItem("id").nodeValue

    cm_CurMenu = cm_f_getRefToDiv('f' + sID, document);
    var oBut = cm_f_getRefToDiv('imgPopMenu' + sID, document);

    var bPersist = (sID > 1);
    var iDelay;

    try {
      var iType = '' + rn[i].attributes.getNamedItem("type").nodeValue;
    }
    catch (err) {
      iType = '0'
    }

    switch (iType) {
      case ('7'):
        {
          iDelay = 250;
          break;
        }
      case ('6'):
        {
          iDelay = 250;
          break;
        }
      case ('5'):
        {
          iDelay = 250;
          break;
        }
      case ('4'):
        {
          iDelay = 250;
          break;
        }
      case ('2'):
        {
          iDelay = 250;
          break;
        }
      case ('1'):
        {
          iDelay = 250;
          break;
        }
      default: // type 0
        {
          iDelay = 1200;
          break;
        }
    }

    //DbgWriteLine( cm_CurMenu.id + ' type: ' + iType );


    var tData =
		{
		  index: sID,
		  RootMenu: cm_CurMenu,
		  bOverButton: false,
		  bOverMenu: false,
		  oTimerID: null,
		  oBut: oBut,
		  closeDelay: iDelay,
		  bPersist: bPersist,
		  left: 0,
		  top: 0
		};

    if (oBut) {
      oBut.menu = cm_CurMenu;
      oBut.tData = tData;
      oBut.onmouseover = cm_f_RollOver;
      oBut.onclick = cm_f_ShowMenu;
      oBut.onmouseout = cm_f_ButtonOut;
    }

    if (cm_CurMenu) {
      var PosParams = eval('CV_Pos' + iLayoutStyle + '[' + (sID - 1) + ']');
      if (PosParams) {
        if (bPersist) {
          iLeft = cm_f_EvalParameters(PosParams.left, null, "delayed");
          iTop = cm_f_EvalParameters(PosParams.top, null, "delayed");
          iWidth = cm_f_EvalParameters(PosParams.width, null, "delayed");
        }
        else {
          iLeft = 0;
          iTop = 0;
          iWidth = cm_f_EvalParameters(PosParams.width, null, "delayed");
        }

        tData.top = eval(iTop);
        tData.left = eval(iLeft);

        with (cm_CurMenu) {
          with (style) {
            left = eval(iLeft);
            top = eval(iTop);
            width = eval(iWidth);
          }
        }
      }
      cm_CurMenu.root = true;
      cm_CurMenu.tData = tData;
    }


    //cm_RootTrees[cm_RootTrees.length] = tData;
    cm_RootTrees[sID] = tData;


  }

  // now set up the links
  cm_f_SetLinks(xmlSideDoc);


  // now set up the links
  cm_f_SetLinks(xmlDoc);

  cm_CurMenu = null;
  //cvMenu_OtherOnLoad;
}

// chain the on load events
//alert('window.onload: ' + window.onload );

//cvMenu_OtherOnLoad = (window.onload) ? window.onload :  new Function;
//window.onload = function(){cvMenuStart();};
//window.onload = function(){setTimeout("cvMenuStart()",10)};
cvAddLoadEvent(cvMenuStart);


//---------------------------------------------------------------------------
// CreateMenuDivs
//---------------------------------------------------------------------------

function cm_f_CreateMenuDivs(oXmlDoc) {
  if (oXmlDoc) {
    var tree = oXmlDoc.getElementsByTagName("tree");
    for (t = 0; t < tree.length; t++) {
      //DbgWriteLine( 'tree: ' + tree[t].attributes.getNamedItem("id").nodeValue + ' type: ' + tree[t].attributes.getNamedItem("type").nodeValue )

      switch (tree[t].attributes.getNamedItem("type").nodeValue) {
        case ('1'):
          {
            cm_CurMenu = cm_f_getRefToDiv('f' + tree[t].attributes.getNamedItem("id").nodeValue, document);
            if (!cm_CurMenu) {
              cm_CurMenu = cm_f_MakeElement('f' + tree[t].attributes.getNamedItem("id").nodeValue);
              //DbgWriteLine( 'creating: ' + 'f'+tree[t].attributes.getNamedItem("id").nodeValue );
              var xml = tree[t].getElementsByTagName("folder")[0];

              if (window.ActiveXObject) {
                ex = xml.transformNode(xslDoc);
                cm_CurMenu.innerHTML = ex;
              }
              // code for Mozilla, Firefox, Opera, etc.
              else if (document.implementation && document.implementation.createDocument) {
                var xsltProcessor = new XSLTProcessor();
                xsltProcessor.importStylesheet(xslDoc);
                var resultDocument = xsltProcessor.transformToFragment(xml, document);
                cm_CurMenu.appendChild(resultDocument);
              }
            }
            cm_f_SetMenuParams(cm_CurMenu);

            break;
          }
        default: // type 0
          {
            var fld = tree[t].getElementsByTagName("folder");

            for (i = 0; i < fld.length; i++) {
              cm_CurMenu = cm_f_getRefToDiv('f' + fld[i].attributes.getNamedItem("id").nodeValue, document);

              if (!cm_CurMenu) {
                cm_CurMenu = cm_f_MakeElement('f' + fld[i].attributes.getNamedItem("id").nodeValue);
                //DbgWriteLine( 'creating: ' + 'f'+fld[i].attributes.getNamedItem("id").nodeValue )

                var xml = fld[i];

                if (window.ActiveXObject) {
                  ex = xml.transformNode(xslDoc);
                  cm_CurMenu.innerHTML = ex;
                }
                // code for Mozilla, Firefox, Opera, etc.
                else if (document.implementation && document.implementation.createDocument) {
                  var xsltProcessor = new XSLTProcessor();
                  xsltProcessor.importStylesheet(xslDoc);
                  var resultDocument = xsltProcessor.transformToFragment(xml, document);
                  cm_CurMenu.appendChild(resultDocument);
                }
              }
              cm_f_SetMenuParams(cm_CurMenu);
            }
          }
      }
    }
  }
}

function cm_f_SetMenuParams(oMenu) {
  //DbgWriteLine( 'setting params: ' + oMenu.id );

  oMenu.tree = null;
  oMenu.parent = null;
  oMenu.root = false;

  oMenu.showIt = cm_f_Show;
  oMenu.hideKids = cm_f_HideKids;
  oMenu.displayKid = cm_f_ShowKid;
  oMenu.bOn = false;
  oMenu.hideTimer = null;
  oMenu.currentLink = null;
  oMenu.visibleKid = null;
  oMenu.bKidsVisible = false;
}



//---------------------------------------------------------------------------
// Set up the links
//---------------------------------------------------------------------------

function cm_f_SetLinks(oXmlDoc) {
  var fld = oXmlDoc.getElementsByTagName("folder");
  var sMsg = '';
  for (i = 0; i < fld.length; i++) {
    //DbgWriteLine( 'folder f' + fld[i].attributes.getNamedItem("id").nodeValue + ' type: ' + fld[i].attributes.getNamedItem("type").nodeValue )

    var cm_CurMenu = cm_f_getRefToDiv('f' + fld[i].attributes.getNamedItem("id").nodeValue, document);
    var kids = fld[i].getElementsByTagName("node");

    cm_CurMenu.style.zIndex = 5000;

    cm_CurMenu.onmouseover = cm_f_OverMenu;
    cm_CurMenu.onmouseout = cm_f_MenuOut;

    if (fld[i].attributes.getNamedItem("type").nodeValue == '0') {
      for (k = 0; k < kids.length; k++) {
        var oLink = cm_f_getRefToDiv('l' + kids[k].attributes.getNamedItem("id").nodeValue, cm_CurMenu);

        if (oLink) {
          oLink.menu = cm_CurMenu;

          if (kids[k].attributes.getNamedItem("kids").nodeValue == 'True') {
            var oKidMenu = cm_f_getRefToDiv('f' + kids[k].attributes.getNamedItem("id").nodeValue, document);
            if (oKidMenu) {
              if (!cm_CurMenu.tData) {
                alert(cm_CurMenu.id + ' has no tdata');

              }
              oKidMenu.parentmenu = cm_CurMenu;
              oKidMenu.tData = cm_CurMenu.tData;

              oLink.menu = cm_CurMenu;
              oLink.tData = cm_CurMenu.tData;
              oLink.onmouseover = cm_f_OverLink;
              oLink.onmouseout = cm_f_OutLink;
              oLink.displayKid = cm_f_ShowKid;
              oLink.kid = oKidMenu;
            }
          }
          else {
            oLink.kid = null;
            oLink.onmouseover = cm_f_OverLink;
          }
        }
      }
    }
  }
}


//---------------------------------------------------------------------------
//  Create a div attached to oAllScreen
//---------------------------------------------------------------------------
function cm_f_MakeElement(menuid) {
  var MenuObject;
  var divs;

  MenuObject = document.createElement("DIV");
  with (MenuObject) {
    id = menuid;
    with (style) {
      position = "absolute";
      display = "none";
      /*			left = "0px";
      top = "0px";
      width = "0px";
      height = "0px"; */
    }
  }

  var oAll = cm_f_getRefToDiv('oAllScreen', document);

  if (oAll != null) {
    oAll.appendChild(MenuObject);
  }

  return MenuObject;
}


//===========================================================================
// menu functions
//===========================================================================

function cm_f_HideKids(oLink) {
  // get the current Visible Kid
  var oTemp = this.visibleKid;
  // and loop down turning them all off
  //alert(oTemp.id + ' ' + oTemp.bKidsVisible + ' ' + this.visibleChild != oLink.child);
  //alert(this.visibleKid != oLink.kid);

  //this.menu.bKidsVisible = true;

  while (oTemp.bKidsVisible) {
    oTemp.visibleKid.showIt(false);
    oTemp.bKidsVisible = false;
    oTemp = oTemp.visibleKid;
  }
  if ((oLink && (this.visibleKid != oLink.kid)) || (!oLink && !this.bOn)) {
    this.visibleKid.showIt(false);
    this.bKidsVisible = false;
  }

}


function cm_f_ShowKid() {
  this.menu.bKidsVisible = true;
  this.menu.visibleKid = this.kid;
  this.kid.showIt(true);
}


//---------------------------------------------------------------------------
//  Mouse over event handler for menu
//---------------------------------------------------------------------------

function cm_f_OverMenu() {
  if (!cm_CurMenu) {
    cm_CurMenu = this.tData.RootMenu;
  }
  //DbgWriteLine('over menu ' + this.tData.RootMenu.id + ' cur: ' + cm_CurMenu.id);
  if (this.tData.RootMenu.id != cm_CurMenu.id) {
    cm_CurMenu.bOn = false;
    if (cm_CurMenu.bKidsVisible) {
      cm_CurMenu.hideKids();
    }
    if (cm_CurMenu.tData.oBut) {
      cm_CurMenu.showIt(false);
      cm_CurMenu.tData.oBut.src = obj_boff[cm_CurMenu.tData.index - 2].src;
    }
    cm_CurMenu = this.tData.RootMenu;
  }



  // flag over the menu
  this.tData.bOverMenu = true;
  // flag not over the button
  this.tData.bOverButton = false;

  // if the timer is running clear it
  if (this.tData.oTimerID > 0) clearTimeout(this.tData.oTimerID);
  this.tData.oTimerID = null;
}

//---------------------------------------------------------------------------
//  Mouse out event handler for menu
//---------------------------------------------------------------------------

function cm_f_MenuOut() {

  if (cm_IE && event.srcElement.contains(event.toElement)) {
    return;
  }

  // flag not over the menu
  this.tData.bOverMenu = false;
  // if the timer is running clear it
  if (this.tData.oTimerID > 0) clearTimeout(this.tData.oTimerID);
  // start the timer for the delayed close of the menu
  this.tData.oTimerID = setTimeout("cm_f_MenuClose(" + this.tData.index + ")", this.tData.closeDelay);


}

//---------------------------------------------------------------------------
//  Set the display stae of a div
//---------------------------------------------------------------------------

function cm_f_Show(bVisible) {
  this.style.display = (bVisible) ? "block" : "none";
}

//===========================================================================
// link functions
//===========================================================================

//---------------------------------------------------------------------------
//  Mouse over event handler for over a link
//---------------------------------------------------------------------------

function cm_f_OverLink() {
  // this link is now the current item
  this.menu.currentLink = this;
  // does this menu have anything open already ?
  if (this.menu.bKidsVisible) {
    // yes, so is this link the current link and does its child 
    if (this.menu.visibleKid == this.child && this.menu.visibleKid.bKidsVisible) {
      this.menu.visibleKid.hideKids(this);
    }
    else {
      //alert( 'hide' );
      this.menu.hideKids(this);
    }
  }

  if (this.kid) {

    var x = parseInt(this.menu.style.left) - 10 + this.offsetWidth;
    var y = parseInt(this.menu.style.top) + this.offsetTop;

    cm_f_moveDivTo(x, y, this.kid);

    this.displayKid();

    cm_f_KeepOnScreen(x, y, this.kid, this);

  }

}

function cm_f_KeepOnScreen(x, y, oKid, oPar) {
  var ExtraSpace = 20;
  var WindowLeftEdge = (HM_IE) ? document.body.scrollLeft : window.pageXOffset;
  var WindowTopEdge = (HM_IE) ? document.body.scrollTop : window.pageYOffset;
  var WindowWidth = (HM_IE) ? document.body.clientWidth : window.innerWidth - ExtraSpace;
  var WindowHeight = (HM_IE) ? document.body.clientHeight : window.innerHeight - ExtraSpace;
  var WindowRightEdge = (WindowLeftEdge + WindowWidth);
  var WindowBottomEdge = (WindowTopEdge + WindowHeight);

  var MenuLeftEdge = x;
  var MenuRightEdge = MenuLeftEdge + oKid.offsetWidth;
  var MenuBottomEdge = y + oKid.offsetHeight;

  //DbgWriteLine('');

  //DbgWriteLine('Enter keep on Sreen ' + oKid.id + ' x: ' + x + ' y: ' + y);

  if (MenuRightEdge > WindowRightEdge) {
    {
      dif = MenuRightEdge - WindowRightEdge;
      x -= dif;
    }
    x = Math.max(5, x);
  }

  if (MenuBottomEdge > WindowBottomEdge) {
    //DbgWriteLine('MenuBottomEdge ' + MenuBottomEdge + ' WindowBottomEdge ' +WindowBottomEdge);

    //alert(this.hasParent);
    dif = MenuBottomEdge - WindowBottomEdge;
    y -= (dif);
  }

  cm_f_moveDivTo(x, y, oKid);

  //DbgWriteLine('Exit keep on Sreen ' + oKid.id + ' x: ' + x + ' y: ' + y);


}


//---------------------------------------------------------------------------
//  Mouse out event handler for over a link
//---------------------------------------------------------------------------

function cm_f_OutLink() {
  if (cm_IE && (event.srcElement.contains(event.toElement)
	  || (event.fromElement.tagName == "IMG" && (event.toElement && event.toElement.contains(event.fromElement)))))
    return;
}



//===========================================================================
// Button functions
//===========================================================================

//---------------------------------------------------------------------------
// Mouse over event handler for buttons
//---------------------------------------------------------------------------

function cm_f_ShowMenu() {
  //DbgWriteLine('over button ' + this.id );

  // Clear the timer
  if (this.tData.oTimerID > 0) clearTimeout(this.tData.oTimerID);
  this.tData.oTimerID = null;


  this.src = obj_bon[this.tData.index - 2].src;

  //for(var i=0; i<cm_RootTrees.length; i++)
  for (var i in cm_RootTrees) {

    var oTopMenu = cm_RootTrees[i].RootMenu;

    // we have to do this as the mootools-release-1.11.js attachs extra functionality to the array, very nice... not!
    if (oTopMenu) {
      if (oTopMenu == this.tData.RootMenu) {
        if (oTopMenu.bKidsVisible) {
          oTopMenu.hideKids(oTopMenu);
        }
        continue;
      }
      else {
        oTopMenu.bOn = false;
        if (oTopMenu.bKidsVisible) {
          oTopMenu.hideKids();
        }
        if (oTopMenu.tData.oBut) {
          oTopMenu.showIt(false);
          oTopMenu.tData.oBut.src = obj_boff[oTopMenu.tData.index - 2].src;
        }
      }
    }
  }

  this.tData.bOverButton = true;

  cm_CurMenu = this.tData.RootMenu;
  if (cm_CurMenu) {
    cm_CurMenu.bOn = true;
    this.tData.RootMenu.showIt(true);

    cm_f_KeepOnScreen(this.tData.left, this.tData.top, this.tData.RootMenu, null);

    //DbgWriteLine('current: ' + cm_CurMenu.id );
  }
}

//---------------------------------------------------------------------------
// Mouse out event handler for buttons
//---------------------------------------------------------------------------

function cm_f_ButtonOut() {
  //DbgWriteLine( 'Out: ' + this.tData.index);
  // If over this button and not over the menu
  if (this.tData.bOverButton && !this.tData.bOverOverMenu) {
    // check for a menu
    if (this.tData.RootMenu && this.tData.RootMenu.bOn) {
      // start the timer for the delayed close of the menu
      //if(  this.tData.RootMenu.bKidsVisible )
      this.tData.oTimerID = setTimeout("cm_f_MenuClose(" + this.tData.index + ")", this.tData.closeDelay);
      //else
      //	this.src = obj_boff[this.tData.index-2].src;
    }
    else {
      // no menu just turn off	
      this.src = obj_boff[this.tData.index - 2].src;
    }
  }
}

//---------------------------------------------------------------------------
// Mouse out event handler for buttons
//---------------------------------------------------------------------------

function cm_f_RollOver() {
  // If over this button and not over the menu
  //	if( this.tData.bOverButton && !this.tData.bOverOverMenu )
  {
    //alert('hello');
    //DbgWriteLine( 'Over: ' + this.tData.index );

    // check for a menu
    if (this.tData.RootMenu) {
      if (this.tData.RootMenu.bOn) {
        this.tData.bOverButton = true;
        // Clear the timer
        if (this.tData.oTimerID > 0) clearTimeout(this.tData.oTimerID);
        this.tData.oTimerID = null
      }
      else {
        //DbgWriteLine( 'Show it: ' + this.tData.index );
        this.src = obj_bover[this.tData.index - 2].src;
        this.tData.bOverButton = true;
      }
    }
    else {
      this.src = obj_bover[this.tData.index - 2].src;
      this.tData.bOverButton = true;
    }
  }
}


//---------------------------------------------------------------------------
// Close the tree down - called via timer
//---------------------------------------------------------------------------

function cm_f_MenuClose(iId) {
  var tData = cm_RootTrees[iId];
  //alert('close the menu: "' + tData +'" '+ iId);
  //eek
  //return;
  // Clear the timer
  if (tData.oTimerID > 0) clearTimeout(tData.oTimerID);
  tData.oTimerID = null;

  // flag not over the button
  tData.bOverMenu = false;

  // flag not over the menu
  tData.bOverButton = false;
  if (tData.oBut)
    tData.oBut.src = obj_boff[tData.index - 2].src;

  // is there a menu...
  if (tData.RootMenu) {
    // hide the menu
    if (tData.RootMenu.bKidsVisible)
      tData.RootMenu.hideKids(tData.RootMenu);

    if (tData.oBut)
      tData.RootMenu.showIt(false);

    tData.RootMenu.bOn = false;
  }
}


//===========================================================================
// Support functions
//===========================================================================


//---------------------------------------------------------------------------
// evaluate params
//---------------------------------------------------------------------------

function cm_f_EvalParameters(valuenew, valueold, valuetype) {
  var TestString, ParPosition;

  if (typeof valuenew == "undefined" || valuenew == null || (valuenew.isString && valuenew.length == 0)) {
    return valueold;
  }

  if (valuetype != "delayed") {
    while (valuenew.isString) {
      ParPosition = valuenew.indexOf("(");
      if (ParPosition != -1) {
        TestString = "window." + valuenew.substr(0, ParPosition);
        if (typeof eval(TestString) != "undefined" && eval(TestString).isFunction) {
          valuenew = eval(valuenew);
        }
      }
      else break
    }
  }

  while (valuenew.isFunction) { valuenew = valuenew() }

  switch (valuetype) {
    case "number":
      while (valuenew.isString) { valuenew = eval(valuenew) }
      break;
    case "boolean":
      while (!valuenew.isBoolean) {
        valuenew = (valuenew.isNumber) ? valuenew ? true : false : eval(valuenew);
      }
      break;
  }

  return valuenew;
}



