/** *************************************************************************************
 * "Pop-up window" emulator using Javascript and CSS.
 *
 * @author Justin Spargur
 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
 ************************************************************************************* **/


/*********************************************************************
 * Below are the variables you can edit for this script. If you'd    *
 * like to modify the script more, feel free but do so at your own   *
 * risk.                                                             *
 *********************************************************************/

// Content for the pop-up window goes here...
var popUpContent = "<p>You are using an older version of Internet Explorer, which may cause this webpage not to appear as it should. We strongly recommend you to update to a newer version, both because of this and for security reasons.<br /><br /><a href=\"http://www.microsoft.com/windows/downloads/ie/getitnow.mspx\" target=\"_blank\">Click HERE to update</a></p>";


var maskOpacity        = 80;                  // in percent. 90 = 90% visible. Default is 90.
var popUpDelay           = 250;                 // In milliseconds. Default is 500.
var maskBGColor        = "#333333";           // Default is gray
var popUpWindowBGColor   = "#ffffff";           // Default is white
var popUpWindowFontColor = "#1f1108";           // Default is black
var popUpWindowBorder    = "3px solid #a4799c"; // Leave blank for no border.


/** *******************************************************************
 *                    DO NOT EDIT BELOW THIS LINE                     *
 ******************************************************************* **/

var viewableScreenWidth;
var viewableScreenHeight;
var myMask;
var popWindow;

// Variable used to disable/enable scrolling
// without jumping all over the page.
var lastYVal;

/*******************************
 * Function to initialize the  *
 * necessary variables and to  *
 * create the elements used in *
 * the "pop-up".               *
 *******************************/
function initPopUpElements()
{
   // Set defaults for required variables.
   if(empty(maskOpacity))
      maskOpacity = 90;
   if(empty(popUpDelay))
      popUpDelay = 500;
   if(empty(maskBGColor))
      maskBGColor = "#333";
   if(empty(popUpWindowBGColor))
      popUpWindowBGColor = "#fff";
   if(empty(popUpWindowFontColor))
      popUpWindowFontColor = "#000";

   lastYVal = 0;

   // Create the mask
   myMask = document.createElement( "div" );
   setWindowDimensions();

   // Set permanent mask attributes
   myMask.id = "myMask";
   myMask.style.backgroundColor = maskBGColor;
   myMask.style.position = "absolute";
   myMask.style.display = "none";
   opacity("myMask", 100, 0, 1)

   // Set permanent pop-up attributes
   popWindow = document.createElement( "div" );
   popWindow.id = "popUpWindow";
   popWindow.style.backgroundColor = popUpWindowBGColor;
   popWindow.style.color = popUpWindowFontColor;
   popWindow.style.position = "absolute";
   popWindow.style.padding = "2%";
   popWindow.style.overflow = "auto";
   popWindow.style.display = "none";

   if(!empty(popUpWindowBorder))
      popWindow.style.border = popUpWindowBorder;

   // Set dynamic attributes
   // for new elements
   setMaskAttributes();

   // Add content to the pop-up window.
   popWindow.innerHTML = "<div style=\"text-align:right;" +
                         "\"><a href=\"#" +
                         "\" onclick=\"hidePopUp(); return false\">Close</a></div>" +
                         popUpContent;

   // Add elements to the DOM
   document.body.appendChild(myMask);
   document.body.appendChild(popWindow);
}

/****************************
 * Function to check for    *
 * uninitialized variables. *
 ****************************/
function empty(variable)
{
   if(variable == "" || variable == undefined)
      return true;
   return false;
}

/****************************
 * Function to get viewable *
 * window dimensions.       *
 ****************************/
function setWindowDimensions()
{
   if (document.documentElement)
   {
      var dombody = document.body;
      if ((document.body.clientHeight == document.body.offsetHeight) &&
          (document.body.offsetHeight == document.body.scrollHeight))
      {
         dombody = document.documentElement;
      }
      if ((document.body.clientHeight == 0) &&
          (document.documentElement.clientHeight > 0))
      {
         dombody = document.documentElement;
      }

      viewableScreenWidth = dombody.clientWidth;
      viewableScreenHeight = dombody.clientHeight;
   }
   else
   {
      viewableScreenWidth = window.screen.width;
      viewableScreenHeight = window.screen.height;
   }
   return true;
}

/****************************
 * Function to set dynamic  *
 * mask attributes.         *
 ****************************/
function setMaskAttributes()
{
   myMask.style.height =  viewableScreenHeight + "px";
   myMask.style.width = viewableScreenWidth+30 + "px";

   myMask.style.top = getCurrentTop() + "px";

   myMask.style.left = "0px";

   setPopupAttributes();
}

/****************************
 * Function to set dynamic  *
 * pop-up attributes.       *
 ****************************/
function setPopupAttributes()
{
   popWindow.style.width = (parseInt(viewableScreenWidth/3)) + "px";
   popWindow.style.height = (parseInt(viewableScreenHeight/2)) + "px";

   popWindow.style.top = (parseInt(getCurrentTop() +
                         (viewableScreenHeight/4))) + "px";
   //popWindow.style.top = (parseInt((viewableScreenHeight/4))) + "px";

   popWindow.style.left = (parseInt(viewableScreenWidth/3)) + "px";
}

/*******************************
 * Function to get the current *
 * scrolled position.          *
 *******************************/
function getCurrentTop()
{
   var currentTop = 0;
   if( typeof( window.pageYOffset ) == 'number' )
      currentTop = window.pageYOffset;
   else if( document.body && ( document.body.scrollLeft ||
                               document.body.scrollTop ) )
      currentTop = document.body.scrollTop;
   else if( document.documentElement &&
            ( document.documentElement.scrollLeft ||
              document.documentElement.scrollTop
            )
          )
      currentTop = document.documentElement.scrollTop;
   return currentTop;
}

/*******************************
 * Function to display the     *
 * pop-up window.              *
 *******************************/
function showPopUp()
{
var popcookies = document.cookie;
  if (popcookies.indexOf(popWindow) == -1){ // cookie not found 
   	lastYVal = getCurrentTop();
   	setMaskAttributes()
   	opacity("myMask", 0, maskOpacity, popUpDelay);
   	document.body.style.overflow = "hidden";
   	window.scrollTo(0,lastYVal);
   	myMask.style.top = getCurrentTop() + "px";
   	//myMask.style.top = 0 + "px";

   	myMask.style.display = "block";
   	popWindow.style.display = "block";
   	window.scrollTo(0,lastYVal);

    document.cookie=popWindow+"=used"
	return;
    }
}

/*******************************
 * Function to hide the        *
 * pop-up window.              *
 *******************************/
function hidePopUp()
{

   popWindow.style.display = "none";
   opacity("myMask", maskOpacity, 0, popUpDelay);
   myMask.style.display = "none";

   document.body.style.overflow = "auto";
   window.scrollTo(0,lastYVal);

   return;
}

/************************************
 * Function to adjust for resizing. *
 ************************************/
function resetPageDimensions()
{
   if(setWindowDimensions())
      setMaskAttributes();
}

/*********************************
 * Function to fade in the mask. *
 *********************************/
function opacity(id, opacStart, opacEnd, millisec)
{
   //speed for each frame
   var speed = Math.round(millisec / 100);
   var timer = 0;

   //determine the direction for the blending, if start and end are the same nothing happens
   if(opacStart > opacEnd)
   {
      for(i = opacStart; i >= opacEnd; i--)
      {
         setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
         timer++;
      }
   }
   else if(opacStart < opacEnd)
   {
      for(i = opacStart; i <= opacEnd; i++)
      {
         setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
         timer++;
      }
   }
}

/**************************************
 * Function to help fade in the mask. *
 **************************************/
function changeOpac(opacity, id)
{
   var object = document.getElementById(id).style;
   object.opacity = (opacity / 100);
   object.MozOpacity = (opacity / 100);
   object.KhtmlOpacity = (opacity / 100);
   object.filter = "alpha(opacity=" + opacity + ")";
}
