/* Add to your javascript.js

//# Defined sizes for popupdiv types
var aPopupDivSizes = new Array;
aPopupDivSizes['popup'] = new Array;
aPopupDivSizes['popup']['width']  = 641;
aPopupDivSizes['popup']['height'] = 392;
document['aPopupDivSizes'] = aPopupDivSizes;

if ('function' == typeof WHITE_AddLoadEvent)
{
  //# Create sunglassdiv and add onclick events
  if ('function' == typeof CreateSunglassPopup)
  {
    WHITE_AddLoadEvent(CreateSunglassPopup);
  }

  //# Create close popup links
  if ('function' == typeof InitClosePopupWindow)
  {
    WHITE_AddLoadEvent(InitClosePopupWindow);
  }
}

*/

//#############################################################################
//# Popup div with sunglass background
//#############################################################################
//### Write div to HTML content
function CreateSunglassPopup()
{
  //# Check for elements with class PopupDiv
  var aPopupLinks = WHITE_GetElementsByClass(document, 'popupLink', 'a');
  var oPopupIFrame = WHITE_GetElementFlex('popupIFrame');
  var oSunglassDiv = WHITE_GetElementFlex('SunglassDiv');

  if ('object' == typeof oPopupIFrameDiv
      && 'object' != typeof oSunglassDiv)
  {
    //# Place SunGlassHTML before Popup Div
    var sSunglassDivHTML = '<div id="SunglassDiv"></div>\n';
    WHITE_InsertBeforeElement(oPopupIFrameDiv, sSunglassDivHTML);
    oSunglassDiv = WHITE_GetElementFlex('SunglassDiv');
  }

  //# Style SunglassDiv
  if ('object' == typeof oSunglassDiv)
  {
    //# Hide sunglass with a single click outside the popup (on the sunglass)
    //WHITE_AddEventToElement(oSunglassDiv, 'click', SunglassPopupHide);
    oSunglassDiv.style.position          = 'absolute';
    oSunglassDiv.style.left              = '0px';
    oSunglassDiv.style.top               = '0px';
    oSunglassDiv.style.zIndex            = '1500';
    oSunglassDiv.style.visibility        = 'hidden';
    oSunglassDiv.style.backgroundColor   = '#000000';
    oSunglassDiv.style.opacity           = '0.70';
    oSunglassDiv.style.filter            = 'Alpha(Opacity=70)';
    WHITE_AddClassName(oSunglassDiv,'hidden');
  }

  if ('object' == typeof oPopupIFrame)
  {
    if ('undefined' != typeof oPopupIFrame.src)
    {
      oPopupIFrame['sEmptySrc'] = oPopupIFrame.src;
    }
  }

  for (var iEl=0;iEl<aPopupLinks.length;iEl++)
  {
    var oPopupLink = aPopupLinks[iEl];
    if ('object' == typeof oPopupLink)
    {
      //# Add function EventSunglassPopupShow to openbuttons
      WHITE_AddEventToElement(oPopupLink, 'click', EventSunglassPopupShow);
    }
  }

}

//### Show SunglassDiv and PopupDiv
function EventSunglassPopupShow(oEvent)
{
  //# Get vars from object
  var sPopupUrl  = this.href;
  var sPopupType = this.id.split(/-/)[0];

  SunglassPopupShowType(sPopupUrl,sPopupType);

  //# Cancel all other (link) events
  WhiteForm_CancelEvent(oEvent);
  return false;
}

function SunglassPopupShowType(sPopupUrl,sPopupType)
{
  var iWidth = false;
  var iHeight = false;
  var aPopupDivSizes = new Array();
  if (document.aPopupDivSizes)
  {
    aPopupDivSizes = document.aPopupDivSizes;
  }
  if ('object' == typeof aPopupDivSizes[sPopupType])
  {
    iWidth = aPopupDivSizes[sPopupType]['width'];
    iHeight = aPopupDivSizes[sPopupType]['height'];
  }
  SunglassPopupShow(sPopupUrl,iWidth,iHeight);
}

function SunglassPopupShow(sPopupUrl,iWidth,iHeight)
{
  var oSunglassDiv = WHITE_GetElementFlex('SunglassDiv');
  var oPopupIFrame = WHITE_GetElementFlex('popupIFrame');
  var oPopupDiv = WHITE_GetElementFlex('popupIFrameDiv');

  if ('object' == typeof oPopupIFrame
      && 'object' == typeof oPopupDiv)
  {
    //# Open url in popup iframe
    if ('undefined' != typeof oPopupIFrame.sEmptySrc)
    {
      oPopupIFrame.src = oPopupIFrame.sEmptySrc; // clear previous content
      //oPopupIFrame.contentWindow.document.body.innerHTML = ('<div style="background-color: black;">EMPTY&nbsp;</div>');
    }

    //# Show sunglass div
    if ('object' == typeof oSunglassDiv)
    {
      var aWebsiteSize = GetWebsiteSize();
      oSunglassDiv.style.width      = aWebsiteSize[0] + 'px';
      oSunglassDiv.style.height     = aWebsiteSize[1] + 'px';
      oSunglassDiv.style.visibility = 'visible';
      WHITE_RemoveClassName(oSunglassDiv,'hidden');
      //Hide selects
      WHITE_EXTRA_SelectHide();
    }

    //# Show popup div
    if ('undefined' != typeof iWidth  && iWidth)  oPopupDiv.style.width = iWidth + 'px';
    if ('undefined' != typeof iHeight && iHeight) oPopupDiv.style.height = iHeight + 'px';
    setTimeout('SUNGLASS_PopupUrlTimer(\''+sPopupUrl+'\');',1); // by using timer this function is completed first
  }

  return false; // for  onclick="return SunglassPopupShow(...)"
}

function SUNGLASS_PopupUrlTimer(sPopupUrl)
{
  var oPopupIFrame = WHITE_GetElementFlex('popupIFrame');
  if ('object' == typeof oPopupIFrame)
  {
    oPopupIFrame.src = sPopupUrl;
    setTimeout('SUNGLASS_PopupShowTimer();',1); // by using timer this function is completed first
  }
}

function SUNGLASS_PopupShowTimer()
{
  var oPopupDiv = WHITE_GetElementFlex('popupIFrameDiv');
  var oPopupIFrame = WHITE_GetElementFlex('popupIFrame');

  oPopupDiv.style.visibility = 'visible';
  WHITE_RemoveClassName(oPopupDiv,'hidden');
  WHITE_RemoveClassName(oPopupIFrame,'hidden');

  // unhide first to make offsetWidth & offsetHeight work
  var aPopupLeftTop   = GetWindowCenterLeftTop(oPopupDiv.offsetWidth, oPopupDiv.offsetHeight);
  oPopupDiv.style.left       = aPopupLeftTop[0] + 'px';
  oPopupDiv.style.top        = aPopupLeftTop[1] + 'px';
}


//### Hide SunglassDiv and PopupDiv
function SunglassPopupHide(oEvent)
{
  //Show selects
  WHITE_EXTRA_SelectShow();
  var oPopupDiv = WHITE_GetElementFlex('popupIFrameDiv');
  if ('object' == typeof oPopupDiv)
  {
    oPopupDiv.style.visibility = 'hidden';
    WHITE_AddClassName(oSunglassDiv,'hidden');
  }
  var oSunglassDiv = WHITE_GetElementFlex('SunglassDiv');
  if ('object' == typeof oSunglassDiv)
  {
    oSunglassDiv.style.visibility = 'hidden';
    WHITE_AddClassName(oSunglassDiv,'hidden');
  }
/*
  ### Changing the src of the iframe kills this current running script, at least in FF.
  var oPopupIFrame = WHITE_GetElementFlex('popupIFrame');
  if ('object' == typeof oPopupIFrame)
  {
    oPopupIFrame.src = "/images/spacer.gif";
  }
*/
  WhiteForm_CancelEvent(oEvent);
  return false;
}

//### Calculate size of website
function GetWebsiteSize()
{
  var iWindowWidth  = 0;
  var iWindowHeight = 0;

  //# Get width of fullscreen div (if exists)
  //# and height of site div (if exists)
  var oFullscreen = WHITE_GetElementFlex('fullscreen');
  if ('object' == typeof oFullscreen)
  {
    iWindowWidth = oFullscreen.offsetWidth;
  }
  var oSiteDiv = WHITE_GetElementFlex('site');
  if ('object' == typeof oSiteDiv)
  {
    iWindowHeight = oSiteDiv.offsetHeight;
  }
  if (0 == iWindowWidth)
  {
    iWindowWidth = window.innerWidth;
  }
  if (0 == iWindowHeight)
  {
    iWindowHeight = window.innerHeight;
  }

  //# Check if fullscreen div isn't smaller than body
  var iBodyWidth  = f_clientWidth();
  var iBodyHeight = f_clientHeight();
  if (iWindowWidth < iBodyWidth)
  {
    iWindowWidth = iBodyWidth;
  }
  if (iWindowHeight < iBodyHeight)
  {
    iWindowHeight = iBodyHeight;
  }

  //# Return values
  var aResult = new Array(2);
  aResult[0] = iWindowWidth;
  aResult[1] = iWindowHeight;
  return aResult;
}

//### Calculate center position of website
function GetWindowCenter()
{
  var iBodyWidth  = f_clientWidth();
  var iBodyHeight = f_clientHeight();
  var iScrollLeft = f_scrollLeft();
  var iScrollTop  = f_scrollTop();
  //# Calculate center position of website
  var aResult = new Array(2);
  aResult[0] = (iBodyWidth  / 2) + iScrollLeft.valueOf();
  aResult[1] = (iBodyHeight / 2) + iScrollTop.valueOf();
  return aResult;
}

//### Calculate x,y pos of PopupDiv from center of active window
function GetWindowCenterLeftTop(iObjectWidth,iObjectHeight)
{
  var aWindowCenter = GetWindowCenter();
  var aResult = new Array(2);

  //# Calculate position
  aResult[0] = Math.floor(aWindowCenter[0].valueOf() - iObjectWidth.valueOf() / 2);
  aResult[1] = Math.floor(aWindowCenter[1].valueOf() - iObjectHeight.valueOf() / 2);

  //# Position starts in visible window (min scrollTop and scrollLeft)
  var iScrollLeft = f_scrollLeft();
  var iScrollTop  = f_scrollTop();
  if (iScrollLeft > aResult[0])
  {
    aResult[0] = iScrollLeft;
  }
  if (iScrollTop > aResult[1])
  {
    aResult[1] = iScrollTop;
  }

  //# Position is within website size (max WebsiteSize - iObjectSize)
  var aWebsiteSize = GetWebsiteSize();
  if ((aWebsiteSize[0]- iObjectWidth) < aResult[0])
  {
    aResult[0] = (aWebsiteSize[0]- iObjectWidth);
  }
  if ((aWebsiteSize[1]- iObjectHeight) < aResult[1])
  {
    aResult[1] = (aWebsiteSize[1]- iObjectHeight);
  }

  return aResult;
}

//#############################################################################
//# Get body size and scrollbar positions functions
//#############################################################################
//http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
function f_clientWidth()
{
  return f_filterResults (
    window.innerWidth ? window.innerWidth : 0,
    document.documentElement ? document.documentElement.clientWidth : 0,
    document.body ? document.body.clientWidth : 0
  );
}
function f_clientHeight()
{
  return f_filterResults (
    window.innerHeight ? window.innerHeight : 0,
    document.documentElement ? document.documentElement.clientHeight : 0,
    document.body ? document.body.clientHeight : 0
  );
}
function f_scrollLeft() {
  return f_filterResults (
    window.pageXOffset ? window.pageXOffset : 0,
    document.documentElement ? document.documentElement.scrollLeft : 0,
    document.body ? document.body.scrollLeft : 0
  );
}
function f_scrollTop()
{
  return f_filterResults (
    window.pageYOffset ? window.pageYOffset : 0,
    document.documentElement ? document.documentElement.scrollTop : 0,
    document.body ? document.body.scrollTop : 0
  );
}
function f_filterResults(n_win, n_docel, n_body)
{
  var n_result = n_win ? n_win : 0;

  if (n_docel && (!n_result || (n_result > n_docel)))
  {
    n_result = n_docel;
  }

  return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

//#############################################################################
//# Hide and show select lists in form
//#############################################################################
function WHITE_EXTRA_SelectHide()
{
  var aSelects = document.getElementsByTagName("select");
  if (0 < aSelects.length)
  {
    for (iTel = 0; iTel != aSelects.length; iTel++)
    {
      oSelect = aSelects[iTel];
      if ('object' == typeof oSelect)
      {
        oSelect.style.visibility = "hidden";
      }
    }
  }
}

function WHITE_EXTRA_SelectShow()
{
  var aSelects = document.getElementsByTagName("select");
  if (0 < aSelects.length)
  {
    for (iTel = 0; iTel != aSelects.length; iTel++)
    {
      oSelect = aSelects[iTel];
      if ('object' == typeof oSelect)
      {
        oSelect.style.visibility = "visible";
      }
    }
  }
}

//#############################################################################
//# Call SunglassPopupHide from parent window
//#############################################################################
function InitClosePopupWindow()
{
  var aApplyToElements = new Array('a','img');
  var aCloseButtons = WHITE_GetElementsByClass(document, 'closelink', aApplyToElements);
  for (var iElC=0;iElC<aCloseButtons.length;iElC++)
  {
    WHITE_AddEventToElement(aCloseButtons[iElC], 'click', EventCloseThisPopupWindow);
  }
}

function CloseThisPopupWindow()
{
  if (parent && 'function' == typeof parent.SunglassPopupHide)
  {
     parent.SunglassPopupHide();
  }
  else
  {
    history.back(); // Fix for if opened in new tab/window
  }
}

function EventCloseThisPopupWindow(oEvent)
{
  CloseThisPopupWindow();
  WhiteForm_CancelEvent(oEvent);
  return false;
}

