/*
Splash

copyright: B-Lex Information Technologies 2010. All rights reserved.

   public api:

   showsplash(settings)
     settings.
       imageurl
       linkurl
       showonce
*/

function showsplash(settings)
{
  // When cookies are disabled, don't display splashpage
  // because we otherwise would get a splashpage on every pageview.
  if (settings.showonce)
  {
    toddWriteCookie("cookiesenabled", "yep", 1);
    if (toddReadCookie("cookiesenabled") != "yep")
      return;

    if (toddReadCookie("seensplash") == settings.imageurl)
      return;

    // make the splash screen not reappear for about two months
    toddWriteCookie("seensplash", settings.imageurl, 7*8);
  }

  var splashanchor = document.createElement("a");
  splashanchor.href = settings.linkurl;
  splashanchor.target = "_blank";
  splashanchor.onclick = function()
    {
      splashanchor.parentNode.removeChild(splashanchor);
      removeGreyoutOverlay();
    };

  var splashimage = document.createElement("img");
  splashimage.src = settings.imageurl;
  splashimage.onload = positionsplash;
  splashanchor.appendChild(splashimage);

  window.blex_splash = splashanchor;
}

function positionsplash()
{
  var splash = window.blex_splash;

  // force browser to calculate layout and size of the splash
  splash.style.position = "absolute";
  splash.style.zIndex = "999";
  splash.style.visibility = "hidden";
  splash.style.display = "block";

  document.body.appendChild(splash);

  var dim = toddGetViewPortDimensions();

  var viewportoffsetx = document.body.scrollLeft+document.documentElement.scrollLeft;
  var viewportoffsety = document.body.scrollTop+document.documentElement.scrollTop;

  var centerposx = parseInt(viewportoffsetx + (dim.width - splash.offsetWidth) / 2)
  var centerposy = parseInt(viewportoffsety + (dim.height - splash.offsetHeight) / 2)

  splash.style.left = centerposx+"px";
  splash.style.top = centerposy+"px";

  showGreyoutOverlay();

  splash.style.visibility = "visible";
}

function closesplash()
{
  var splash = document.getElementById("splash");

  splash.parentNode.removeChild(splash);

  removeGreyoutOverlay();
}

function showGreyoutOverlay()
{
  var viewportsize = toddGetPageDimensions();

  if(!window.tolliumWF_ModalOverlay)
  {
    tolliumWF_ModalOverlay = document.createElement('div');
    tolliumWF_ModalOverlay.style.cssText = 'position: absolute; top: 0; left: 0; background-color: #000000; opacity: 0.7; -moz-opacity: 0.7; filter: alpha(opacity=70);';
    document.body.appendChild(tolliumWF_ModalOverlay);
  }

  tolliumWF_ModalOverlay.style.height = viewportsize.height + 'px';
  tolliumWF_ModalOverlay.style.width = viewportsize.width + 'px';
  tolliumWF_ModalOverlay.style.zIndex = '101';
  tolliumWF_ModalOverlay.style.display = 'block';
}

function removeGreyoutOverlay()
{
  if (window.tolliumWF_ModalOverlay)
  {
    if (tolliumWF_ModalOverlay)
      tolliumWF_ModalOverlay.style.display = 'none';
  }
}

// Cookie code based on http://www.quirksmode.org/js/cookies.html
function toddWriteCookie(name, value, days)
{
  var expires = "";
  if (days)
  {
    var date = new Date();
    date.setTime(date.getTime() + (days*24*60*60*1000));
    expires = "; expires=" + date.toGMTString();
  }
  document.cookie = name + "=" + value + expires + "; path=/";
}

function toddReadCookie(name)
{
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0; i < ca.length; i++)
  {
    var c = ca[i];
    while (c.charAt(0)==' ')
      c = c.substring(1, c.length);
    if (c.indexOf(nameEQ) == 0)
      return c.substring(nameEQ.length, c.length);
  }
  return null;
}

function toddDeleteCookie(name)
{
  toddWriteCookie(name, "", -1);
}

/** @short returns the size of the visual area of window
    @return object with width and height
*/
function toddGetViewPortDimensions()
{
  // all except Explorer
  if (window.innerHeight)
    return { width:  window.innerWidth
           , height: window.innerHeight
           };

  // Explorer 6 Strict Mode
  if (document.documentElement && document.documentElement.clientHeight)
    return { width:  document.documentElement.clientWidth
           , height: document.documentElement.clientHeight
           };

  return { width:  document.body.clientWidth
         , height: document.body.clientHeight
         };
}

/** @short returns the full size of the document
    @return size of the document
*/
function toddGetPageDimensions()
{
  var htmlwidth = document.documentElement.scrollWidth;
  var htmlheight = document.documentElement.scrollHeight;

  // IE6-QuirksMode and SF (all versions? tested with 3.2.1) give page height in <BODY>
  // All other browsers give the size in documentElement (<HTML>)
  var bodywidth = document.body.scrollWidth;
  var bodyheight = document.body.scrollHeight;

  // return largest values
  return { width:  htmlwidth  > bodywidth  ? htmlwidth  : bodywidth
         , height: htmlheight > bodyheight ? htmlheight : bodyheight
         };
}
