var isIE = document.all ? true : false;
var isNN = document.layers ? true : false;

var timer_showPopup;
var timer_hidePopup;
var timer_showWindows;
var timer_hideWindows;
var timer_trackMouse;

var iCurrentSpanId, iCurrentHeight, iCurrentWidth, iCurrentLength, iCurrentPass, iCurrentMenuTypeId;
var objCurrentLink, objCurrentEvent;
var bPopupOpened;
var bMouseEntered;
var bDelaySuccess;
var iCurrentclientX, iCurrentclientY;

var iPass1Height = 25; var iPass2Height = 50;
var iPass1Width = 25; var iPass2Width = 50;

if (typeof(document.attachEvent) != 'undefined') 
{
  document.attachEvent('onmousemove', trackMouse);
}
else 
{
  document.addEventListener('mousemove', trackMouse, false);
}

function trackMouse(e)
{
  var objEvent = isIE ? event : e;
  var spanPopup = util_getSpanObject(iCurrentSpanId);
  var tabPopup = util_getTableObject(iCurrentSpanId)
 
  iCurrentclientX = parseInt(objEvent.clientX);
  iCurrentclientY = parseInt(objEvent.clientY);
  if(!bPopupOpened) { return; }

  var ileft = parseInt(spanPopup.style.left);
  var itop = isIE ? (parseInt(spanPopup.style.top) - document.body.scrollTop) : (parseInt(spanPopup.style.top) - window.pageYOffset);
  var iwidth = parseInt(tabPopup.style.width);
  //var iheight = parseInt(tabPopup.style.height);
  var iheight = parseInt(spanPopup.clientHeight);
  var bXpos = (iCurrentclientX >= ileft) && (iCurrentclientX <= (ileft + iwidth));
  var bYpos = (iCurrentclientY >= itop) && (iCurrentclientY <= (itop + iheight));
  //window.status = iCurrentclientX + " : " + iCurrentclientY + " _ " + " t:" + itop + " l:" + ileft + " w:" + iwidth + " h:" + iheight
  //+ " bMouseEntered:" + bMouseEntered + " bXpos:" + bXpos + " bYpos:" + bYpos;
  if( bXpos && bYpos )
  {
    bMouseEntered = true;
    util_clearTimeout(timer_hidePopup);
    return;
  }

  if( bMouseEntered && (!bXpos || !bYpos) )
  {
    bMouseEntered = false;
    util_clearTimeout(timer_hidePopup);
    timer_hidePopup = setTimeout("hidePopup()", 500);
  }
}

function checkMousePos()
{
  var spanPopup = util_getSpanObject(iCurrentSpanId);
  var tabPopup = util_getTableObject(iCurrentSpanId)
  var ileft = parseInt(spanPopup.style.left);
  var itop = isIE ? (parseInt(spanPopup.style.top) - document.body.scrollTop) : (parseInt(spanPopup.style.top) - window.pageYOffset);
  var iwidth = parseInt(tabPopup.style.width);
  var iheight = parseInt(spanPopup.clientHeight);
  var bXpos = (iCurrentclientX >= ileft) && (iCurrentclientX <= (ileft + iwidth));
  var bYpos = (iCurrentclientY >= itop) && (iCurrentclientY <= (itop + iheight));
  return (bXpos && bYpos);
}

function settrackMouse()
{
  util_clearTimeout(timer_trackMouse);
  bPopupOpened = true;
  bMouseEntered = true;
}

function func_mouseout()
{
  if(iCurrentMenuTypeId == 3 && bPopupOpened && checkMousePos())
  {
    bMouseEntered = true;
    return;
  }
  util_clearTimeout(timer_hidePopup);
  util_clearTimeout(timer_trackMouse);
  //timer_trackMouse = setTimeout("settrackMouse()", 1000);
  timer_hidePopup = setTimeout("hidePopup()", 3000);
  util_clearTimeout(timer_showPopup);
  timer_showPopup = null;
  bDelaySuccess = false;
}

function showPopup(iSpanId, iHeight, iWidth, iLength, iMenuTypeId, iPopupDelay, objLink, e)
{
  objCurrentLink = objLink;
  objCurrentEvent = e;
  if(timer_showPopup == null)
  {
    bDelaySuccess = true;
    timer_showPopup = setTimeout("showPopup(" + iSpanId + ", " + iHeight + ", " + iWidth + ", " + iLength + ", " + iMenuTypeId + ", " + iPopupDelay + ", objCurrentLink, objCurrentEvent)", 500);
    return;
  }

  if(!bDelaySuccess)
  {
    return;
  }

  util_clearAllTimers();
  var sarrSpan = document.getElementsByTagName("span");
  var iSpan;
  for(iSpan = 0; iSpan < sarrSpan.length; ++iSpan)
  {
    var sPopupIdOld = sarrSpan[iSpan].id;
    if(sPopupIdOld.substring(0, 10) == "spanPopup_")
    {
      var tabLinksOld = document.getElementById("tabPopup_" + sPopupIdOld.substr(10));
      tabLinksOld.style.visibility = 'hidden';
      sarrSpan[iSpan].style.visibility = 'hidden';
    }
  }
  iCurrentSpanId = iSpanId;
  iCurrentMenuTypeId = iMenuTypeId;
  bPopupOpened = false;
  bMouseEntered = false;
  var objEvent = isIE ? event : e;
  var iclientX = util_getX(objLink);
  var iclientY = util_getY(objLink);
  /*
  var iExtraWidth = (iclientX + iWidth) - util_getScreenWidth();
  if( iExtraWidth > 0 )
  {
    iclientX -= (iExtraWidth + 0);
  }
  */
  iCurrentHeight = iHeight;
  iCurrentWidth = iWidth;
  //iCurrentLength = iLength;
  iCurrentLength = objLink.offsetWidth;
  iCurrentPass = ((iCurrentMenuTypeId==3) ? 2 : 3);
  util_clearTimeout(timer_showWindows);
  timer_showWindows = setTimeout("showWindows(" + iclientX + ", " + iclientY + ")", iPopupDelay);
}

function showWindows(iclientX, iclientY)
{
  var spanPopup = util_getSpanObject(iCurrentSpanId);
  var tabPopup = util_getTableObject(iCurrentSpanId)
  if(spanPopup && tabPopup)
  {
    spanPopup.style.left = util_getLayerLeft(iclientX);
    spanPopup.style.top = util_getLayerTop(iclientY);
    spanPopup.style.overflow = "hidden";
    spanPopup.style.backgroundColor = "transparent";
    spanPopup.style.display = "inline";
    if(iCurrentPass == 1)
    {
      spanPopup.style.height = iPass1Height;
      spanPopup.style.width = iPass1Width;
      tabPopup.style.height = iPass1Height;
      tabPopup.style.width = iPass1Width;
      ++iCurrentPass;
    }
    else if(iCurrentPass == 2)
    {
      spanPopup.style.height = iPass2Height;
      spanPopup.style.width = iPass2Width;
      tabPopup.style.height = iPass2Height;
      tabPopup.style.width = iPass2Width;
      ++iCurrentPass;
    }
    if (timer_showWindows != null && iCurrentPass >= 3)
    {
      util_clearTimeout(timer_showWindows);
      util_hideAllObjects("select");
      util_hideAllObjects("object");
      iCurrentHeight = (spanPopup.scrollHeight > iCurrentHeight) ? spanPopup.scrollHeight : iCurrentHeight;
      iCurrentWidth = (spanPopup.scrollWidth > iCurrentWidth) ? spanPopup.scrollWidth : iCurrentWidth;
      var iExtraWidth = (iclientX + iCurrentWidth) - util_getScreenWidth();
      if( iExtraWidth > 0 )
      {
        iclientX -= (iExtraWidth + 0);
      }
      var iTop = isIE ? document.body.scrollTop : window.pageYOffset;
      var iExtraHeight = (iclientY + iCurrentHeight) - util_getScreenHeight() - iTop;
      if( iExtraHeight > 0 )
      {
        iclientY -= (iExtraHeight + 0);
      }
      spanPopup.style.left = util_getLayerLeft(iclientX);
      spanPopup.style.top = util_getLayerTop(iclientY);
      spanPopup.style.height = iCurrentHeight;
      spanPopup.style.width = iCurrentWidth;
      tabPopup.style.height = iCurrentHeight;
      tabPopup.style.width = iCurrentWidth;
      //spanPopup.style.border = "none";
      spanPopup.style.backgroundColor = "#FFFFFF";
      spanPopup.style.overflow = "visible";
      tabPopup.style.visibility = "visible";
      bPopupOpened = true;
      util_clearTimeout(timer_trackMouse);
      //timer_trackMouse = setTimeout("settrackMouse()", 2000);
      iCurrentPass = 3;
    }
    else
    {
      util_clearTimeout(timer_showWindows);
      timer_showWindows = setTimeout("showWindows(" + iclientX + ", " + iclientY + ")", 1);
    }
    spanPopup.style.visibility = "visible";
  }
}

function hidePopup()
{
  if(bPopupOpened)
  {
    util_clearTimeout(timer_hidePopup);
    bPopupOpened = false;
    bMouseEntered = false;
    iCurrentPass = 0;
    util_clearTimeout(timer_hideWindows);
    timer_hideWindows = setTimeout("func_hideWindows()", 75);
  }
}

function func_hideWindows()
{
  var spanPopup = util_getSpanObject(iCurrentSpanId);
  var tabPopup = util_getTableObject(iCurrentSpanId)
  if(spanPopup && tabPopup)
  {
    tabPopup.style.visibility = "visible";
    spanPopup.style.overflow = "hidden";
    spanPopup.style.visibility = "visible";
    if(iCurrentPass == 1)
    {
      spanPopup.style.height = iPass1Height;
      spanPopup.style.width = iPass1Width;
      tabPopup.style.height = iPass1Height;
      tabPopup.style.width = iPass1Width;
      --iCurrentPass;
    }
    else if(iCurrentPass == 2)
    {
      spanPopup.style.height = iPass2Height;
      spanPopup.style.width = iPass2Width;
      tabPopup.style.height = iPass2Height;
      tabPopup.style.width = iPass2Width;
      --iCurrentPass;
    }
    if (timer_hideWindows != null && iCurrentPass <= 0)
    {
      util_clearTimeout(timer_hideWindows);
      tabPopup.style.visibility = "hidden";
      spanPopup.style.visibility = "hidden";
      util_showAllObjects("select");
      util_showAllObjects("object");
      bPopupOpened = false;
      bMouseEntered = false;
      iCurrentPass = 0;
      iCurrentPass = 0;
      util_clearAllTimers();
    }
    else
    {
      util_clearTimeout(timer_hideWindows);
      timer_hideWindows = setTimeout("func_hideWindows()", 75);
    }
  }
}

function func_nonpopuplinks()
{
  util_clearTimeout(timer_showWindows);
}

function func_nav_dynmenu(ipages, istart, iid)
{
  for(var i=istart; i<(istart+ipages+5); ++i)
  {
    var objTabPopupLinks = document.getElementById("tabPopup_links_" + i);
    if(objTabPopupLinks)
    {
      objTabPopupLinks.style.display = "none";
    }
  }
  var spanPopup = document.getElementById("spanPopup_1");
  var tabPopup = document.getElementById("tabPopup_1");
  objTabPopupLinks = document.getElementById("tabPopup_links_" + iid);
  if(objTabPopupLinks)
  {
    objTabPopupLinks.style.display = isIE ? "block" : "table";
    if(window.opera)
    {
      objTabPopupLinks.style.display = "table";
    }
    if(spanPopup && tabPopup)
    {
      spanPopup.style.display = "inline";
      var iCurrentHeightC = isNaN(tabPopup.style.height) ? parseInt(tabPopup.style.height) : 0;
      var iCurrentWidthC = isNaN(tabPopup.style.width) ? parseInt(tabPopup.style.width) : 0;
      iCurrentHeightC = (tabPopup.scrollHeight > iCurrentHeightC) ? tabPopup.scrollHeight : iCurrentHeightC;
      iCurrentWidthC = (tabPopup.scrollWidth > iCurrentWidthC) ? tabPopup.scrollWidth : iCurrentWidthC;
      iCurrentHeightC = (isIE && !window.opera) ? tabPopup.scrollHeight+2 : tabPopup.scrollHeight;
      iCurrentWidthC = (isIE && !window.opera) ? tabPopup.scrollWidth+2 : tabPopup.scrollWidth;
      spanPopup.style.height = iCurrentHeightC;
      spanPopup.style.width = iCurrentWidthC;
      tabPopup.style.height = iCurrentHeightC;
      tabPopup.style.width = iCurrentWidthC;
      if(window.opera)
      {
        spanPopup.style.border = "none";
        spanPopup.scrollWidth = 700;
      }
    }
  }
}

//-------Utility function-----------------
function util_clearTimeout(objTimer) { if (objTimer != null) { clearTimeout(objTimer); } }
function util_getSpanObject(iSpanId) { return document.getElementById("spanPopup_" + iSpanId); }
function util_getTableObject(iSpanId) { return document.getElementById("tabPopup_" + iSpanId); }
function util_hideAllObjects(sTagName) 
{ 
  var sarrObject = document.getElementsByTagName(sTagName);
  var iCtr;
  for(iCtr = 0; iCtr < sarrObject.length; ++iCtr)
  {
    if(iCurrentMenuTypeId != 1)
    {
      if(sarrObject[iCtr].name == "selGlassesType" || sarrObject[iCtr].name == "selGender" || sarrObject[iCtr].name == "selMaterial")
      {
        continue;
      }
    }
    if( sTagName == "object" && !(isIE && !window.opera) )
    {
      continue;
    }
    sarrObject[iCtr].style.visibility = 'hidden';
  }
}
function util_showAllObjects(sTagName) 
{ 
  var sarrObject = document.getElementsByTagName(sTagName);
  var iCtr;
  for(iCtr = 0; iCtr < sarrObject.length; ++iCtr) { sarrObject[iCtr].style.visibility = 'visible'; }
}
function util_getX(obj)
{
  var iReturn = 0;
  while( obj != null ) 
  {
    iReturn += obj.offsetLeft;
    obj = obj.offsetParent;
  }
  iReturn = isIE ? (iReturn - document.body.scrollLeft) : iReturn;
  return iReturn;
}
function util_getY(obj)
{
  var iReturn = 0;
  while( obj != null ) 
  {
    iReturn += obj.offsetTop;
    obj = obj.offsetParent;
  }
  //iReturn = isIE ? (iReturn - document.body.scrollTop) : iReturn;
  return iReturn;
}
function util_clearAllTimers()
{
  util_clearTimeout(timer_showWindows);
  util_clearTimeout(timer_hidePopup);
  util_clearTimeout(timer_hideWindows);
  util_clearTimeout(timer_trackMouse);
}
function util_getScreenWidth() { return (isIE ? document.body.clientWidth-10 : self.innerWidth-25); }
function util_getScreenHeight() { return (isIE ? document.body.clientHeight-10 : self.innerHeight-25); }
function util_getLayerLeft(iclientX)
{
  if(iCurrentMenuTypeId == 1) { return iclientX + (iCurrentLength + 2); }
  else if(iCurrentMenuTypeId == 2) { return iclientX; }
  else if(iCurrentMenuTypeId == 3) 
  {
    var iLeft = isIE ? document.body.scrollLeft : window.pageXOffset;
    return (util_getScreenWidth()/2) - (iCurrentWidth/2) + iLeft; 
  }
}
function util_getLayerTop(iclientY)
{
  if(iCurrentMenuTypeId == 1) { return iclientY; }
  else if(iCurrentMenuTypeId == 2) { return iclientY + 15; }
  else if(iCurrentMenuTypeId == 3)
  {
    var iTop = isIE ? document.body.scrollTop : window.pageYOffset;
    return (util_getScreenHeight()/2) - (iCurrentHeight/2) + iTop; 
  }
}
