﻿// JScript File for showing / hiding popup panels
var m_curPopup=null;
var m_tmrPopup=null;
var m_intPopups
var m_arrPopups

function InitPopups(){
  // create counter & array
  m_intPopups = -1;
  m_arrPopups = new Array();

  // check all links
  var elementsFound = document.getElementsByTagName("a");
  for (var j = 0; j < elementsFound.length; j++){
    if(elementsFound.item(j).id){
      if (elementsFound.item(j).id.indexOf("link")==0){
        // find corresponding popup menu
        s = "popup" + elementsFound.item(j).id.substring(4)
        mnu = document.getElementById(s);
        if (mnu){
          // add combination
        	m_intPopups = m_intPopups + 1;
        	m_arrPopups[m_intPopups] = new PopupMenuItem(elementsFound.item(j), mnu);
        }
      }
    }
  }
}

// popup-link class constructor
function PopupMenuItem(lnk, mnu){
  // set properties
  this.linkItem = lnk;
  this.menuItem = mnu;
  // set reference to this object in document object
  this.linkItem.item = this;
  this.menuItem.item = this;
  // methods
  this.linkItem.onmouseover=PopupMenuMouseOver;
  this.linkItem.onclick=PopupMenuClick;
  this.linkItem.onmouseout=HidePopup;
  if (lnk.getAttribute("poptype")){
    if(lnk.getAttribute("poptype")!="1"){
      this.menuItem.onmouseover=ClearHideTimer;
      this.menuItem.onmouseout=HidePopup;    
    }
  }else{
    this.menuItem.onmouseover=ClearHideTimer;
    this.menuItem.onmouseout=HidePopup;
  }
}

function PopupMenuMouseOver(){
  var itm = this.item;
  PopupShow(itm.linkItem,itm.menuItem);
}

function PopupMenuClick(){
  var itm = this.item;
  PopupShow(itm.linkItem,itm.menuItem)
}

function PopupShow(lnk, mnu){
  // if popup allready visible exit, otherwise hide current
  if (mnu==m_curPopup){
    ClearHideTimer();
    return true;
  }else{
      HideCurrentPopup();
  }
  m_curPopup=mnu;
  
  // get window dimensions
  var posWin = getWindowDimensions();
  
  // show popup menu
  m_curPopup.style.position = "absolute";
  m_curPopup.style.display = "block";
  m_curPopup.style.visibility = "visible";

  // and get dimensions of link and popup-element
  var posLnk = getElementDimensions(lnk);
  var posPop = getElementDimensions(m_curPopup);
  
  // set position
  var l = t = 0
  l = posLnk[0];
  t = posLnk[1] + posLnk[2];
  m_curPopup.style.left = l;
  m_curPopup.style.top = t; 

  // check position; if popup layer too width to align with left lower coner of link
  // so align with right lower coner (link left + link width - popup width)
  // alert(posLnk[0]); alert(m_curPopup.clientWidth); alert(posWin[0]);
  if (posLnk[0] + m_curPopup.clientWidth > posWin[0]){
    l = posLnk[0] + posLnk[3] - m_curPopup.clientWidth;
    if (l>=0){
      m_curPopup.style.left = parseInt(l)+"px";
    }
  }
}

function HidePopup(){
  clearTimeout(m_tmrPopup);
  m_tmrPopup=setTimeout("HideCurrentPopup()",200);
}

function HideCurrentPopup(){
  if(m_curPopup){
    m_curPopup.style.visibility="hidden";
    m_curPopup.style.display="none";
    m_curPopup=null;
    clearTimeout(m_tmrPopup)
    m_tmrPopup=null;
  }
}

function ClearHideTimer(){
  clearTimeout(m_tmrPopup);
  m_tmrPopup=null;
}



