﻿
var currentPopUpPanelDescriptor = null;
var closingTimerId = null;

function createPopUpPanel(id, cssClass) {
  var panelDescriptor = new Object();
  var panelElement = document.createElement('div');
  
  panelElement.setAttribute('id', id);
  setElementClassAttribute(panelElement, cssClass);
  attachEventHandler(panelElement, 'onmousemove', 'resetClosingTimer();');
  attachEventHandler(panelElement, 'onmouseout', 'setClosingTimer();');
  panelDescriptor.id = id;
  panelDescriptor.element = panelElement;
  return panelDescriptor;
}

function addChildElementHtml(panelDescriptor, html) {
  var childElement = document.createElement('div');

  childElement.innerHTML = html;
  addChildElement(panelDescriptor, childElement);
}

function addChildElement(panelDescriptor, childElement) {
  panelDescriptor.element.appendChild(childElement);
}

function showPopUpPanel(panelDescriptor, x, y) {
  if (currentPopUpPanelDescriptor != null)
    hidePopUpPanel();
  
  panelDescriptor.element.style.position = 'absolute';
  panelDescriptor.element.style.left = x;
  panelDescriptor.element.style.top = y;
  document.body.appendChild(panelDescriptor.element);
  currentPopUpPanelDescriptor = panelDescriptor;
  setClosingTimer(panelDescriptor.id);
}

function hidePopUpPanel() {
  if (currentPopUpPanelDescriptor == null || currentPopUpPanelDescriptor.element == null)
    return;
  
  document.body.removeChild(currentPopUpPanelDescriptor.element);
  currentPopUpPanelDescriptor = null;
  resetClosingTimer();
}

function getPopUpPanelPositionForElementById(elementId) {
  var elementRectangle = getElementAbsoluteRectangle(elementId);
  var position = new Object();

  position.x = elementRectangle.x + 'px';
  position.y = (elementRectangle.y + elementRectangle.height + 2) + 'px';
  return position;
}

function resetClosingTimer() {
  if (closingTimerId == null)
    return;
    
  window.clearTimeout(closingTimerId);
}

function setClosingTimer() {
  closingTimerId = window.setTimeout('hidePopUpPanel();', 1000);
}