var TimerRan;
var TimerShow;
var Shown;
var PopupNames;

function MenuInit()
{
  TimerShow = 500;
  Shown = 0;
  PopupNames = new Array(ItemsCount);
  for (i=0; i<ItemsCount; i++)
  {
    PopupNames[i] = 'divPopupTopMenu' + i;
  }
  for (i=0; i<ItemsCount; i++)
  {
    PopupCreate(i);
  }
}

function PopupCreate(i)
{
  try
  {
    idName = PopupNames[i];
    idDivForName = DivForPopupNames[i];
    if (screen.width < 600)
        str = '<div style="width:100px;" class="dynamicMenu" id="'+idName+'" name="'+idName+'">';
    else    
        str = '<div class="dynamicMenu" id="'+idName+'" name="'+idName+'">';
    for (j=0; j<ItemsNames[i].length; j++)
    {
    if (ItemsLinks[i][j] != '') 
    {
      if (j == 0)
      {
        str += '<div class="dynamicMenuItem" style="z-index:100; border-bottom: 1px solid;"';
      }
      else if (j != ItemsNames[i].length-1 && ItemsLinks[i][j] != '' && ItemsLinks[i][j+1] != '')
      {
        str += '<div class="dynamicMenuItem" style="z-index:100; border-bottom: 1px solid;"';
      }
      else
      {
        str += '<div class="dynamicMenuItem" style="z-index:100;"';
      }
    } 
    else 
    {
      str += '<div class="dynamicMenuItem" style="z-index:100;"';
    }
    if (ItemsLinks[i][j] != '') 
    {
      str += ' onMouseOver="this.className=\'dynamicMenuItemHover\';Shown=1;status=\''+''+ItemsLinks[i][j]+'\'" onMouseOut="this.className=\'dynamicMenuItem\';MenuHide(\''+i+'\');status=\'\'" onClick="location.href=\''+ItemsLinks[i][j]+'\';event.cancelBubble=true"';
    }
    str += '>'+'&nbsp;'+ItemsNames[i][j]+'&nbsp;'+'</div>';
  }
  str += '</div>';
  document.getElementById(idDivForName).innerHTML += str;
  document.getElementById(DivForPopupNames[i]).style.display = "none";
  }
  catch (err)
  {
    // alert('MenuCreate Error');
  }
}

function MenuShow(i)
{
  Shown = 1;
  HideAllMenu();
  try
  {
    document.getElementById(PopupNames[i]).style.display = "";
    var el = document.getElementById(StaticMenuItemNames[i]);  
    var tgtPos = posit(el);
    var o = document.getElementById(PopupNames[i]);
    o.style.left = (tgtPos[0]-10) +'px';
    o.style.top = (tgtPos[1]-3) +'px';
    if (screen.width < 600)
      o.style.top = (parseInt(tgtPos[1])+parseInt(PopupMarginTopForAndriod[i])) +'px';
    document.getElementById(DivForPopupNames[i]).style.display = ""; 
    document.getElementById(PopupNames[i]).style.visibility = "visible";    
  }
  catch (err)
  {
    // alert('MenuShow Error');
  }
}

function MenuHide(i)
{
  Shown = 0;
  if (TimerRan) 
  {
    clearTimeout(TimerRan);
    TimerRan = null;
  }
  try
  {
    TimerRan = setTimeout("MenuHideTimer(" + i + ")",TimerShow);
  }
  catch (err)
  {
    // alert('MenuHide Error');
  }
}

function HideAllMenu()
{
  for (i=0; i<ItemsCount; i++)
  {
    MenuClose(i);
  }
}

function MenuHideTimer(i)
{
  if (!Shown)
  {
    MenuClose(i);
  }
}

function MenuClose(i)
{
  try
  {
    document.getElementById(DivForPopupNames[i]).style.display = "none";
    document.getElementById(PopupNames[i]).style.visibility = "hidden";
    document.getElementById(PopupNames[i]).style.display = "none";
  }
  catch (err)
  {
    // alert('MenuHideTimer Error');
  }
}

function posit(element) 
{
  var valueT = 0, valueL = 0;
  do 
  {
    valueT += element.offsetTop  || 0;
    valueL += element.offsetLeft || 0;
    element = element.offsetParent;
  } 
  while (element);
  return [valueL, valueT];
}
