var activeMenu = null;
var currentMenu = null;
var timeOutVal = null;
var timeOutVal2 = null;
var imagesLoaded = false;
var currHigh = '';
var delayTime = 500;
var onMain = false;

if (isNavigator && isDHTML)
{ onResize = reloadPage(true);
  document.captureEvents(Event.MOUSEOVER, Event.MOUSEOUT);
}

function initMouse()
{ for (thisLayer in myLayers)
    if (thisLayer.indexOf("Menu") != -1)
    { with (myLayers[thisLayer])
      { currLayer.onmouseout = checkActive;
        currLayer.onmouseover = setActive;
        if (objGetZIndex() == 0)
          objSetZIndex(30 + menusList[thisLayer].menuLevel);
        else
          objSetZIndex(objGetZIndex() +  menusList[thisLayer].menuLevel);
      }
    }
    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);
		
    personas_off = loadImage(img_folder + "bot_nuestropuerto.gif");
    personas_on = loadImage(img_folder + "bot_nuestropuerto_f2.gif");

    empresas_off = loadImage(img_folder + "bot_concesion.gif");
    empresas_on = loadImage(img_folder + "bot_concesion_f2.gif");

    migente_off = loadImage(img_folder + "bot_entorno.gif");
    migente_on = loadImage(img_folder + "bot_entorno_f2.gif");

    datos_off = loadImage(img_folder + "bot_preguntas.gif");
    datos_on = loadImage(img_folder + "bot_preguntas_f2.gif");

    quienessomos_off = loadImage(img_folder + "bot_turismo.gif");
    quienessomos_on = loadImage(img_folder + "bot_turismo_f2.gif");

    imagesLoaded = true;
  }
}

function changeImage(imageIn, imageSrc)
{if (!imagesLoaded || !document.images)
    return;

  currImage = eval("document." + imageIn);
  if (!currImage)
  { if (isNavigator)
    { var foundImage = false;
      for (i = 0; 1 < document.layers.length; i++)
      { currImage = eval("document.layers[" + i + "].document." + imageIn);
        if (currImage)
        { foundImage = true;
          break
        }
      }
      if (!foundImage)
        return;
    }
    else
      return;
  }
  currImage.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)
{
  currentMenu = new initMenu(myLayers[whichLayer]);

  if (menusList[whichLayer].left != currentMenu.id.objGetLeft())
  { currentMenu.id.objSetLeft(menusList[whichLayer].left);
    currentMenu.id.objSetTop(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();
}

