// Functions for use with the GLS Avenue system

var warnings=10;
var sfxMode = 0; // 0 = no SFX, 1 = horizontal scroll, 2 = vertical scroll

var scrollMenus=new Array;
var scrollMenuId = false;
function scrollMenu() {
  var i;
  var ok=0;
  for (i=0; i<scrollMenus.length; i++) {
    var menu=scrollMenus[i][0];
    var goal=scrollMenus[i][1]; // 0: done, 1: make visible, 2: make hidden
    var mode=scrollMenus[i][2]; // 1: positioning, 2: clipping
    var submode=scrollMenus[i][3]; // 0: go directly, 1: wander
    var speed=scrollMenus[i][4];
    var pos=scrollMenus[i][5];
    var el=scrollMenus[i][6];
    var el2=scrollMenus[i][7];
    var el3=scrollMenus[i][8]
    var size=scrollMenus[i][9];
    var sfxMode=scrollMenus[i][10];

    if (goal) {
      var el_style=(document.layers)?el:el.style;
      if (goal!=mode) {
        speed++;
        if (speed<0) speed++;
      } else if (submode==1) {
        speed++;
        if (speed<0) speed++;
        if (speed>5) submode=0;
      } else {
        speed--;
        if (speed>0) speed--;
      }
      pos+=speed;
      if (pos>=size) {
        pos=size;
        speed=-speed;
        submode=((mode==2 && goal!=2) ? 1 : 0);
        mode=goal;
      } else if (pos<=0) {
        pos=speed=submode=0;
        if (goal==mode) goal=0;
      }
      if (mode==1 || pos>=size) {
        el_style.clip='rect(0, 1000, 1000, 0)';
        el_style.visibility='visible';
      } else if (pos<=0) {
        el_style.visibility='hidden';
      } else {
        el_style.visibility='visible';
        if (sfxMode==1) {
          el_style.clip='rect(0, '+(pos+2)+', 1000, 0)';
        } else {
          el_style.clip='rect(0, 1000, '+(pos+2)+', 0)';
        }
      }
      if (el2) {
        var el2_style=(document.layers)?el2:el2.style;
        if (pos<=0) {
          el2_style.visibility = 'hidden';
        } else {
          if (sfxMode==1) {
            el2_style.left=pos;
          } else {
            el2_style.top=pos;
          }
          el2_style.visibility = 'visible';
        }
      }
      scrollMenus[i][1]=goal;
      scrollMenus[i][2]=mode;
      scrollMenus[i][3]=submode;
      scrollMenus[i][4]=speed;
      scrollMenus[i][5]=pos;
    }
    if (goal) ok = 1;
  }
  if (! ok && scrollMenuId) {
    clearInterval(scrollMenuId);
    scrollMenuId = false;
  }
}

function startScroll(menu, goal, height) {
  // Start scrolling to show/hide a menu
  var el=findObject(menu);
  if (! el) {
    // Menu does not exist
    return;
  }
  if (sfxMode==0) {
    // No special effects: just show/hide menu
    var el_style=(document.layers)?el:el.style;
    el_style.visibility=(goal==1?'visible':'hidden');
    return;
  }
  if (! scrollMenuId) scrollMenuId = setInterval('scrollMenu()', 40);
  for(i=0; i<scrollMenus.length; i++) {
    if (scrollMenus[i][0]==menu) {
      // menu is already in memory: just set its goal
      scrollMenus[i][1]=goal;
      return;
    }
  }
  // Make a new scrollMenu array entry for this menu
  var el2=findObject(menu+'SFXline');
  var el3=findObject(menu+'contents');
  var el2_style;
  if (el2) {
    el2_style=(document.layers)?el2:el2.style;
  }
  var width3=el3.offsetWidth;
  if (!width3 && el3.width) width3=el3.width;
  if (!width3 && el3.style) width3=el3.style.width;
  if (!width3) width3=110;
  var height3=el3.offsetHeight;
  if (!height3 && el3.height) height3=el3.height;
  if (!height3 && el3.style) height3=el3.style.height;
  if ((!height3 || height3<16) && arguments[2]) height3 = arguments[2];
  if (!height3) height3=110;
  if (sfxMode!=2) {
    scrollMenus[scrollMenus.length] =
        new Array(menu, goal, 2, 0, 0, 0, el, el2, el3, width3, 1);
        // push() doesn't work in Mac/IE
    if (el2) {
      el2_style.height=(height3?height3:'100%');
    }
  } else {
    scrollMenus[scrollMenus.length] =
        new Array(menu, goal, 2, 0, 0, 0, el, el2, el3, height3, 2);
        // push() doesn't work in Mac/IE
    if (el2) {
      el2_style.width=(width3?width3:'100%');
    }
  }
}


var hideDelay = 0;
var currentMenu=new Array;
var checkMenuId = false;
function showMenu(menu, level) {
  // drop down the selected menu
  // note that this menu has extra parameters denoting the menuitem count
  if (! level) level = 1;
  hideMenu(level-1); // retract other dropped-down menus
  if (!menu) {
    // no menu: return;
  }
  // calculate height
  var height=0;
  var i;
  for (i=2; i<arguments.length; i++) {
    height += arguments[i]*16;
  }
  if (height<16) {
    // no height: empty menu, so don't do anything
    return;
  }
  startScroll(menu, 1, height);
  currentMenu[currentMenu.length] = menu; // push() doesn't work in Mac/IE

  // if not doing so already, periodically check whether the mouse
  // is still over the area, and retract the menu if it isn't
  if (! checkMenuId) checkMenuId = setInterval('checkMenu()', 200);
}

function hideMenu(level) {
  // retract the currently dropped-down menu(s) above level, if any
  while (currentMenu.length > level) {
    startScroll(currentMenu[currentMenu.length-1], 2); // pop() doesn't work
    currentMenu.length = currentMenu.length-1;     // in Mac/IE
  }
  currentMenu.length = level;
  if (checkMenuId && currentMenu.length==0) {
    clearInterval(checkMenuId);
    checkMenuId = false;
  }
  hideTimer = hideDelay;
}


// The following 3 functions track and check the presence of the mouse over
// certain areas, f.i. the menubutton or the menu itself, such that the
// dropped menu should not be retracted while the mouse is over that area

var mouseInId = new Array();
var hideTimer = hideDelay;
function noteMouseIn(id, level) {
  // called if the mouse enters such an area
  mouseInId[level ? level : 1] = id;
}

function noteMouseOut(id, level) {
  // called if the mouse leaves such an area
  if (mouseInId[level ? level : 1] == id) {
    // Note that if mouseInId != id, then apparently the noteMouseIn of
    // another area went ahead in the execution queue
    mouseInId[level ? level : 1] = false;
  }
}

function checkMenu() {
  // check whether the mouse is in such an area, and retract the menu otherwise
  if ((currentMenu.length>0) && (! mouseInId[currentMenu.length])) {
    if (--hideTimer <= 0) {
      var level = 0;
      while (mouseInId[level+1]) level++;
      hideMenu(level);
    }
  } else {
    hideTimer = hideDelay;
  }
}

function setNavButton(btnName, image, className) {
  setButton(btnName, 'nav/'+image, className);
}

function toggleNavMenu(opener, state) {
  var list = opener;
  while (list && (!list.tagName || list.tagName != 'UL')) {
    list = list.nextSibling;
  }
  if (list) {
    var ls = (document.layers)?list:list.style;
    if (typeof(state)=='undefined') {
      state=(ls.display!='block');
    }
    if (state) {
      ls.display='block';
      opener.innerHTML = '<img src="'+wwwroot+'/images/admin/hoofdnav_minus.gif" border="0" alt="-">';
    } else {
      ls.display='none';
      opener.innerHTML = '<img src="'+wwwroot+'/images/admin/hoofdnav_plus.gif" border="0" alt="+">';
    }
  }
  return false;
}

// Functions to update the navigation when using partial page loads
// currentPage, currentButtonId and updateNavButton are declared and initialized
// in page templates
function updateNavPage(newPage) {
  // Update the navigation for a new page
  var id;
  if (newPage!=currentPage) {
    if (currentButtonId && updateNavButton) {
      id = currentButtonId;
      while(id) {
        updateNavButton(id, 0);
        id = findParentButtonId(id);
      }
    }
    currentButtonId=findButtonId(newPage);
    if (currentButtonId && updateNavButton) {
      id = currentButtonId;
      while(id) {
        updateNavButton(id, 1);
        id = findParentButtonId(id);
      }
    }
    currentPage=newPage;
  }
}
function replaceContainer(containerId, contentsId) {
  // Replace a container in the main template with new contents from a
  // hidden element in a partial-page-load
  var container=document.getElementById(containerId);
  var contents=document.getElementById(contentsId);
  if (container && contents) {
    container.parentNode.insertBefore(contents, container);
    container.parentNode.removeChild(container);
    contents.style.display='';
    contents.id=containerId;
  }
}

