﻿var outerContainer;

function closePopup(imgId, divId)
{
  // Reposition at top of page so Scrollbar doesn't flicker when show again
  var div = document.getElementById(outerContainer)
  div.style.left = 0;
  div.style.top = 0;
  div.style.width = 1;
  div.style.height = 1;
  div.style.display = "none";
  return false;
}

function getAbsoluteLeft(oElement)
{
    var iReturnValue = 0;
    while (oElement != null)
    {
        iReturnValue += oElement.offsetLeft;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

function getAbsoluteTop(oElement)
{
    var iReturnValue = 0;
    while (oElement != null)
    {
        iReturnValue += oElement.offsetTop;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

function popupMouseDown(elem, e)
{
  var evt = e || window.event;
  var win = elem.offsetParent;
  var startWinX = win.offsetLeft;
  var startWinY = win.offsetTop;
  var startMouseX = evt.clientX;
  var startMouseY = evt.clientY;
  
  var move = function(e)
  {
    var evt = e || window.event;

    win.style.left = (startWinX - (startMouseX - evt.clientX)) + 'px';
    win.style.top = (startWinY - (startMouseY - evt.clientY)) + 'px';

    if (document.all)
    {
        evt.cancelBubble = true;
        evt.returnValue = false;
    }
    if (evt.preventDefault)
    {
        evt.preventDefault();
    }
  }

  var up = function(e)
  {
    document.onmousemove = null;
    document.onmouseup = null;
  }

  document.onmousemove = move;
  document.onmouseup = up;
}

function setOpacity(elem, value)
{
  elem.style.opacity = value / 10;
  elem.style.filter = 'alpha(opacity=' + value * 10 + ')';
}

function showPopup(divId, divHeight, divWidth, sourceElement)
{
  outerContainer = divId;

  var screenWidth = 0
  var screenHeight = 0;

  if (typeof (window.innerWidth) == 'number')
  {
    //Non-IE
    screenWidth = window.innerWidth;
    screenHeight = window.innerHeight;
  }
  else if (document.documentElement &&
           (document.documentElement.clientWidth ||
            document.documentElement.clientHeight))
  {
    //IE 6+ in 'standards compliant mode'
    screenWidth = document.documentElement.clientWidth;
    screenHeight = document.documentElement.clientHeight;
  }
  else if (document.body &&
           (document.body.clientWidth ||
            document.body.clientHeight))
  {
    //IE 4 compatible
    screenWidth = document.body.clientWidth;
    screenHeight = document.body.clientHeight;
  }

  var element = document.getElementById(divId);

  var newTop = Math.round(((screenHeight - divHeight) / 2), 0);
  var newLeft = Math.round(((screenWidth - divWidth) / 2), 0);

  newTop = (newTop < 0) ? 0 : newTop;
  newLeft = (newLeft < 0) ? 0 : newLeft;

  var scrollTop = document.documentElement.scrollTop >= document.body.scrollTop ?
                        document.documentElement.scrollTop : document.body.scrollTop

  var startLeft = (sourceElement) ? getAbsoluteLeft(sourceElement) : (screenWidth / 2);
  var startTop = (sourceElement) ? getAbsoluteTop(sourceElement) : (screenHeight / 2);
  var startHeight = (sourceElement) ? sourceElement.offsetHeight : 1;
  var startWidth = (sourceElement) ? sourceElement.offsetWidth : 1;

  // Set size before showing to reduce flicker
  element.style.position = 'absolute';
  element.style.left = startLeft;
  element.style.top = startTop;
  element.style.offsetWidth = startWidth;
  element.style.offsetHeight = startHeight;
  setOpacity(element, 0);
  element.style.display = "";

  var anm = new Animator({ duration: 300 });
  anm.addSubject(new NumericalStyleSubject(element, 'left', startLeft, newLeft));
  anm.addSubject(new NumericalStyleSubject(element, 'top', startTop, newTop + scrollTop));
  anm.addSubject(new NumericalStyleSubject(element, 'width', startWidth, divWidth));
  anm.addSubject(new NumericalStyleSubject(element, 'height', startHeight, divHeight));
  anm.addSubject(new NumericalStyleSubject(element, 'opacity', 0, 1));

  anm.play();

  return false;
}

function showPopupError(divId, errorMessage)
{
  if (errorMessage == null ||
      errorMessage.length == 0)
  {
    errorMessage = "An error occurred loading the Image. Please reload the page and try again";
  }

  var div = document.getElementById(divId);

  div.innerHTML =
    "<div id=\"divCaption\" class=\"largeImageCaption\" onmousedown=\"popupMouseDown(this, event)\">" +
      "<table cellpadding=\"2\" cellspacing=\"0\" width=\"796\">" +
        "<tr>" +
          "<td style=\"height: 24px; width: 35px;\"></td>" +
          "<td style=\"color: #eaeae8; font-weight: 600; text-align: center; vertical-align: middle; width: 714px;\">" +
            "&nbsp;</td>" +
          "<td style=\"cursor: pointer; vertical-align: middle; width: 35px;\">" +
            "<img alt=\"Close this window\" src=\"../Images/close.gif\" onclick=\"closePopup()\" /></td>" +
        "</tr>" +
      "</table>" +
    "</div>" +
        "<span class=\"errorLabel\">" + errorMessage + "</span>";

  showPopup(divId, 600, 800);
}

function showSearchingPopup(sender, divId, contentElementId)
{
  var div = document.getElementById(divId);

  if (div == null)
  {
    return;
  }

  var screenWidth = 0
  var screenHeight = 0;

  if (typeof (window.innerWidth) == 'number')
  {
    //Non-IE
    screenWidth = window.innerWidth;
    screenHeight = window.innerHeight;
  }
  else if (document.documentElement &&
           (document.documentElement.clientWidth ||
            document.documentElement.clientHeight))
  {
    //IE 6+ in 'standards compliant mode'
    screenWidth = document.documentElement.clientWidth;
    screenHeight = document.documentElement.clientHeight;
  }
  else if (document.body &&
           (document.body.clientWidth ||
            document.body.clientHeight))
  {
    //IE 4 compatible
    screenWidth = document.body.clientWidth;
    screenHeight = document.body.clientHeight;
  }
  
  var newLeft = Math.round(((screenWidth - 400) / 2), 0);
  var newTop;

  var pageContainer = null;

  if (contentElementId != null)
  {
    pageContainer = document.getElementById(contentElementId);
  }

  // If PageContainer is taller than screen height, center in Screen, otherwise center in PageContainer
  if (pageContainer == null ||
      pageContainer.offsetHeight > screenHeight)
  {
      newTop = Math.round(((screenHeight - 130) / 2), 0);
  }
  else
  {
    newTop = Math.round(((pageContainer.offsetHeight - 70) / 2), 0);
  }

  newTop = (newTop < 0) ? 0 : newTop;
  newLeft = (newLeft < 0) ? 0 : newLeft;

  var scrollTop = document.documentElement.scrollTop >= document.body.scrollTop ?
                        document.documentElement.scrollTop : document.body.scrollTop
  
  // Use sender's position as the starting position
  var parentleft = getAbsoluteLeft(sender);
  var parenttop = getAbsoluteTop(sender);
  var parentheight = sender.offsetHeight;
  var parentwidth = sender.offsetWidth;

  div.style.position = "absolute";
  div.style.left = parentleft;
  div.style.top = parenttop;
  div.style.height = parentheight;
  div.style.width = parentwidth;
  div.style.display = "";
  
  var anm = new Animator({ duration: 300 });
  anm.addSubject(new NumericalStyleSubject(div, 'left', parentleft, newLeft));
  anm.addSubject(new NumericalStyleSubject(div, 'top', parenttop, newTop + scrollTop));
  anm.addSubject(new NumericalStyleSubject(div, 'width', parentwidth, 400));
  anm.addSubject(new NumericalStyleSubject(div, 'height', parentheight, 70));

  anm.play();

  return false;
}