var img_folder = "/set-x1-new/images/";
var activeMenu = null;
var currentMenu = null;
var timeOutVal = null;
var timeOutVal2 = null;
var imagesLoaded = false;
var currHigh = '';
var delayTime = 700;
var onMain = false;

if (is_nav4 && isDHTML)
{ onResize = reloadPage(true);
  document.captureEvents(Event.MOUSEOVER, Event.MOUSEOUT);
}

function initMouse()
{ for (thisLayer in myLayers)
    if (thisLayer.indexOf("Menu") != -1)
    { myLayers[thisLayer].currLayer.onmouseout = checkActive;
      myLayers[thisLayer].currLayer.onmouseover = setActive;
    }
  menusLoaded = true;
}

function loadImage(fileName)
{ newImage = new Image();
  newImage.src = fileName;
  return newImage;
}

function indicatorObj(on, off, width, height)
{ this.on = on;
  this.off = off;
  this.width = width;
  this.height = height;
}

//function preloadImages()
//{ if(document.images)
//  { ind_on = loadImage(img_folder + indicator.on);
//    ind_off = loadImage(img_folder + indicator.off);
//
//    shop_off = loadImage(img_folder + "buttons/shop_off.gif");
//    shop_on = loadImage(img_folder + "buttons/shop_on.gif");
//
//    manage_off = loadImage(img_folder + "buttons/manage_off.gif");
//    manage_on = loadImage(img_folder + "buttons/manage_on.gif");
//
//    about_off = loadImage(img_folder + "buttons/about_off.gif");
//    about_on = loadImage(img_folder + "buttons/about_on.gif");
//
//    custcare_off = loadImage(img_folder + "buttons/custcare_off.gif");
//    custcare_on = loadImage(img_folder + "buttons/custcare_on.gif");
//
//    imagesLoaded = true;
//  }
//}

function changeImage(imageIn, imageSrc)
{ if (!imagesLoaded || !document.images)
    return;

  currImage = eval("document." + imageIn);
  if (!currImage)
    if (is_nav4)
    { var foundImage = false;
      for (i = 0; i < document.layers.length; i++)
      { currImage = eval("document.layers[" + i + "].document." + imageIn);
        if (currImage)
        { foundImage = true;
          break
        }
      }
      if (!foundImage) return;
    }
    else return;
  if (is_nav4){
    currImage.src = eval(imageSrc + ".src");
  } else {
    currImage.setAttribute("src", eval(imageSrc + ".src"));
  }
}

function initMenu(id)
{ this.id = id;
  this.prev = null;
  this.next = null;
}	

function CTO(timeIn)
{ clearTimeout(timeIn);
  return null;
}

function showMenu(whichLayer)
{ if (activeMenu != null)
    if (isInActiveList(whichLayer))
    { timeOutVal2 = CTO(timeOutVal2);
      clearCurrent();
      clearSubs(whichLayer);
      currentMenu = activeMenu;
      activeMenu = activeMenu.prev;
      currentMenu.prev = null;
      if (activeMenu != null){
        activeMenu.next = null;
      }
    }
    else
    {
      timeOutVal2 = CTO(timeOutVal2);
      clearCurrent();
      if (menusList[whichLayer].menuLevel == 0){
        clearSubs();
	  }
      createCurrent(whichLayer);
    }
  else
  { if (currentMenu != null)
      if (currentMenu.id.name == whichLayer){
        timeOutVal = CTO(timeOutVal);
      } else {
        clearCurrent();
      }
    createCurrent(whichLayer);
  }
}

function createCurrent(whichLayer)
{
  //alert (whichLayer);
  currentMenu = new initMenu(myLayers[whichLayer]);

  if (is_nav4)
    if (menusList[whichLayer].left != currentMenu.id.objGetLeft() || menusList[whichLayer].top != currentMenu.id.objGetTop())
      currentMenu.id.objMoveAbsolute(menusList[whichLayer].left, menusList[whichLayer].top);
  currentMenu.id.objShow();
}

function initCheckMenu()
{ timeOutVal = setTimeout("clearCurrent()", delayTime);
}

function clearCurrent()
{ timeOutVal = CTO(timeOutVal);
  if (currentMenu != null)
  { currentMenu.id.objHide();
    currentMenu = null;
  }
  if (activeMenu == null && !onMain)
    clearHighlight();
}

function clearCurrent2(whichLayer)
{ if (currentMenu == null)
    return;

  if (currentMenu.id.name == whichLayer)
    timeOutVal = CTO(timeOutVal);
  else
    clearCurrent();
}

function checkActive()
{ if (!onMain)
    timeOutVal2 = setTimeout("hideMenu()", 20);
}

function setActive()
{ if (timeOutVal2 != null)
    timeOutVal2 = CTO(timeOutVal2);

  if (activeMenu != null && this.id == activeMenu.id.name)
    return;

  if (isInActiveList(this.id))
  { if (currentMenu != null)
      clearCurrent();
    clearSubs(this.id);
  }
  else
  { if (currentMenu != null)
      if (currentMenu.id.name == this.id)
      { timeOutVal = CTO(timeOutVal);
        if (activeMenu != null)
        { activeMenu.next = currentMenu;
          currentMenu.prev = activeMenu;
          activeMenu = currentMenu;
          currentMenu = null;
        }
        else
        { activeMenu = currentMenu;
          currentMenu = null;
        }
      }
  }
}

function isInActiveList(checkThis)
{ var isInList = false;

  tempList = activeMenu;
  if (tempList == null)
    return isInList;

  while(tempList.next != null)
    tempList = tempList.next;

  while(tempList != null)
  { if (tempList.id.name == checkThis)
    { isInList = true;
      break;
    }
    tempList = tempList.prev;
  }
  return isInList;
}

function hideMenu(keepShowing)
{ timeOutVal2 = CTO(timeOutVal2);

  if (currentMenu != null)
    clearCurrent();

  if (activeMenu != null)
    clearSubs(keepShowing);
  
  if (activeMenu == null)
    clearHighlight();
}

function clearHighlight(keepHigh)
{ if (currHigh != "" && currHigh != null && currHigh != keepHigh)
    changeImage(currHigh, currHigh + "_off");
  currHigh = keepHigh;
}

function clearSubs(clearTo)
{ if (activeMenu != null)
  { while (activeMenu.next != null)
      activeMenu = activeMenu.next;
  
    while (activeMenu != null && activeMenu.id.name != clearTo)
    { activeMenu.id.objHide();
      activeMenu = activeMenu.prev;
      if (activeMenu != null)
        activeMenu.next = null;
    }  
  }
}

function reloadPage(init)
{  //reloads the window if Nav4 resized
  if (init == true)
  { document.pageW = innerWidth;
    document.pageH = innerHeight;
    onresize = reloadPage;
  }
  else if (innerWidth != document.pageW || innerHeight != document.pageH){
    location.reload();
  }
}

