function initPopups()
{
  var s = document.getElementsByTagName("span");

  for (i = 0; i < s.length; i++)
    if (s[i].className == "popup_link")
      s[i].onclick = popupOnClick;

  var s = document.getElementsByTagName("a");

  for (i = 0; i < s.length; i++)
    if (s[i].className == "popup_link")
      s[i].onclick = popupOnClick;
}

function popupOnClick(e)
{
  var link  =  e ? e.target : window.event.srcElement;

  if (popupNotExists(link.id))
  {
    var popup = createPopup(link.id);
    mx = (e ? e.clientX : window.event.clientX);
    my = (e ? e.clientY : window.event.clientY);

    popup.style.display = "block";

    x = (mx + popup.clientWidth) > pageWidth() ? Math.max(0, mx - popup.clientWidth) : mx;
    y = (my + popup.clientHeight) > pageHeight() ? Math.max(0, my - popup.clientHeight) : my;

    popup.style.position = "absolute";
    popup.style.top = (y + getScrollPos()) + "px";
    popup.style.left = x + "px";

    document.getElementById("close_" + link.id).onclick = popupClose;
    document.getElementById("t_close_" + link.id).onclick = popupClose;
  }
}

function popupNotExists(name)
{
  return document.getElementById(name + "_win") == null;
}

function createPopup(name)
{
  var src   = document.getElementById(name + "_popup");
  var popup = document.createElement("div");

  popup.setAttribute("id", name + "_win");

  popup.style.width = src.style.width;
  popup.style.height = src.style.height;
  popup.style.zIndex = 1000;

  innerHtml = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" class=\"popupWin\">";
  innerHtml += "  <tr><td class=\"tl\"></td><td class=\"tm\"><div class=\"closeBtn\" id=\"close_" + name + "\"></div></td><td class=\"tr\"></td></tr>";
  innerHtml += "  <tr><td class=\"ml\"></td><td class=\"mm\">";
  innerHtml += src.innerHTML;
  innerHtml += "<p class='bottom_space'>&nbsp;</p></td><td class=\"mr\"></td></tr>";
  innerHtml += "  <tr><td class=\"bl\"></td><td class=\"bm\"><span id=\"t_close_" + name + "\">Close window</span></td><td class=\"br\"></td></tr>";
  innerHtml += "</table>";

  popup.innerHTML = innerHtml;
  document.body.appendChild(popup);
  return popup;
}

function popupClose(e)
{
  var me = e ? e.target : window.event.srcElement;
  var divName = (me.id.indexOf("t_") > -1) ?  me.id.substring(8) : me.id.substring(6);
  document.body.removeChild(document.getElementById(divName + "_win"));
}

function pageWidth()
{
  return (typeof( window.innerWidth ) == 'number')
    ? window.innerWidth
    : document.documentElement && document.documentElement.clientWidth
      ? document.documentElement.clientWidth
      : document.body != null ? document.body.clientWidth : null;
}

function pageHeight()
{
	return window.innerHeight != null
    ? window.innerHeight
    : document.documentElement && document.documentElement.clientHeight
      ?  document.documentElement.clientHeight
      : document.body != null ? document.body.clientHeight : null;
}

function getScrollPos()
{
  return window.pageYOffset ? window.pageYOffset : (document.body.scrollTop ? document.body.scrollTop : 0);
}
