/* Script by: www.jtricks.com
* Version: 20060925
* Latest version:
* www.jtricks.com/javascript/window/box_alone.html
*/
// Moves the box object to be directly beneath an object.
function EPW_Window_move_box(an, box, Width, Height, DisplayMode)
{
   var cleft = 0;
   var ctop = 0;
   var obj = an;

   if (DisplayMode == 0) // Under Link
   {
      while (obj.offsetParent)
      {
         cleft += obj.offsetLeft;
         ctop += obj.offsetTop;
         obj = obj.offsetParent;
      }
      ctop += an.offsetHeight + 8;

      // Handle Internet Explorer body margins,
      // which affect normal document, but not
      // absolute-positioned stuff.
      if (document.body.currentStyle &&
			document.body.currentStyle['marginTop'])
      {
         ctop += parseInt(document.body.currentStyle['marginTop']);
      }
   }
   else if (DisplayMode == 1) // Centered
   {
      var viewPortWidth = EPW_getWidth();
      var viewPortHeight = EPW_getHeight();
      var horizontalScroll = EPW_getScrollX();
      var verticalScroll = EPW_getScrollY();

      cleft = Math.round((viewPortWidth - Width) / 2) + horizontalScroll;
      ctop = Math.round((viewPortHeight - Height) / 2) + verticalScroll;
      cleft = (cleft < horizontalScroll) ? horizontalScroll : cleft;
      ctop = (ctop < verticalScroll) ? verticalScroll : ctop;
   }

   box.style.left = cleft + 'px';
   box.style.top = ctop + 'px';
}

// Hides other alone popup boxes that might be displayed
function EPW_Window_hide_other_alone(obj)
{
   if (!document.getElementsByTagName)
      return;
   var window_div = document.getElementById('EPW_Window');
   if (window_div != null)
   {

      EPW_grayOut(false);
      document.body.removeChild(window_div);
      return;
   }
}

// Shows a box if it wasn't shown yet or is hidden
// or hides it if it is currently shown
function EPW_Window_show_hide_box(an, Title, URL, width, height, borderStyle, DisplayMode, pathToCloseButton)
{
   var href = URL;
   var boxdiv = null;

   EPW_grayOut(true);

   EPW_Window_hide_other_alone(null);

   // Create box object through DOM
   boxdiv = document.createElement('div');

   // Assign id equalling to the document it will show
   boxdiv.setAttribute('id', 'EPW_Window');

   boxdiv.setAttribute('class', 'EPW_Text');

   // Add object identification variable
   boxdiv.alonePopupBox = 1;

   var viewPortWidth = EPW_getWidth() - 80;
   var viewPortHeight = EPW_getHeight() - 80;


   // Because the scroll bar is taken from the inside of the viewport
   // adjust the width if the content scrolls off the bottom of the viewport
   // to keep a scroll bar from appearing along the bottom of the viewport.
   var widthOffset = 0;
   if (height > viewPortHeight)
      widthOffset = 17;

   height = Math.min(viewPortHeight, height);
   width = Math.min(viewPortWidth, width) + widthOffset;

   //boxdiv.style.position = 'absolute';// DDD
   boxdiv.style.width = width + 'px';
   boxdiv.style.height = height + 'px';
   boxdiv.style.zIndex = EPW_getHighestZindex() + 2;
   //alert( " boxdiv.style.height: "  + boxdiv.style.height);
   var offsetX = 0;
   var offsetY = 0;

   boxdiv.innerHTML = "<table name='EPW_Window_Titlebar' id='EPW_Window_Titlebar' cellpadding='0' cellspacing='0'>"
                     + "<tr>"
                     + "<td width='90%' align='left'>"
                     + Title
                     + "</td>"
                     + "<td width='10%' align='right' vertical-align='top' onclick='EPW_Window_hide_other_alone()'"
                     + " style='cursor:pointer;background-position:right; background-repeat:no-repeat;background-image:url("
                     + pathToCloseButton
                     + ");'>"
                     + "</td>"
                     + "</tr>"
                     + "</table>";

   // Show the box now instead of earlier, so the innerHTML is exists in Safari for the following getElementbyID call
   document.body.appendChild(boxdiv);

   // prevent hyperlink navigation.
   boxdiv.style.display = 'block';

   oEPW_Window_Titlebar = document.getElementById('EPW_Window_Titlebar');

   offsetX = 0;

   // Added a check here for NaN because if the first step blows up the offsetY will always be NaN
   offsetY = isNaN(parseInt(EPW_Window_getStyle(oEPW_Window_Titlebar, "height"))) ? 0 : parseInt(EPW_Window_getStyle(oEPW_Window_Titlebar, "height"));

   offsetY += isNaN(parseInt(EPW_Window_getStyle(oEPW_Window_Titlebar, "border-top-width"))) ? 0 : parseInt(EPW_Window_getStyle(oEPW_Window_Titlebar, "border-top-width"));
   offsetY += isNaN(parseInt(EPW_Window_getStyle(oEPW_Window_Titlebar, "border-bottom-width"))) ? 0 : parseInt(EPW_Window_getStyle(oEPW_Window_Titlebar, "border-bottom-width"));

   // JMR Had to add 2 pixels here so it wouldn't eat the bottom of the windows display box
   offsetY += 2;

   // JMR approximate/punt if the above fails
   if (offsetY == 2)
   {
      offsetY = 22;
   }

   var contents = document.createElement('iframe');
   contents.scrolling = 'auto';
   contents.overflowX = 'hidden';
   contents.overflowY = 'scroll';
   contents.frameBorder = '0';
   contents.style.width = oEPW_Window_Titlebar.style.width = (width - offsetX) + 'px';
   contents.style.height = (height - offsetY) + 'px';

   boxdiv.appendChild(contents);

   EPW_Window_move_box(an, boxdiv, width, height, DisplayMode);

   if (contents.contentWindow)
      contents.contentWindow.document.location.replace(href);
   else
      contents.src = href;

   return false;
}

function EPW_Window_getStyle(element, prop)
{

   if (typeof element == 'string') element = document.getElementById(element);

   if (prop == null)
   {
      return null;
   }
   else if (element.style[prop])
   {
      // inline style property
      return element.style[prop];
   }
   else if (element.currentStyle)
   {
      // external stylesheet for Explorer
      return element.currentStyle[prop];
   }
   else if (document.defaultView && document.defaultView.getComputedStyle && document.defaultView.getComputedStyle(element, ""))
   {
      // external stylesheet for Mozilla and Safari 1.3+
      prop = prop.replace(/([A-Z])/g, "-$1");
      prop = prop.toLowerCase();
      return document.defaultView.getComputedStyle(element, "").getPropertyValue(prop);
   }
   else
   {
      return null;
   }
}


function EPW_grayOut(vis)
{
   // Pass true to gray out screen, false to ungray
   // options are optional.  This is a JSON object with the following (optional) properties
   // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
   // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
   // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
   // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
   // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
   // in any order.  Pass only the properties you need to set.

   var zindex = 50;
   var opacity = 50;
   //var opaque = (opacity / 100);
   var bgcolor = '#000000';
   var dark = document.getElementById('EPWdarkenScreenObject');
   if (!dark)
   {
      // The dark layer doesn't exist, it's never been created.  So we'll
      // create it here and apply some basic styles.
      // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
      var tbody = document.getElementsByTagName("body")[0];
      var tnode = document.createElement('div');           // Create the layer.
      // DDD tnode.style.position = 'absolute';                 // Position absolutely
      tnode.style.position = 'absolute';
      // DDD tnode.style.top = '0px';                           // In the top
      tnode.style.top = '0';
      // DDD tnode.style.left = '0px';                          // Left corner of the page
      tnode.style.left = '0';
      tnode.style.width = '100%';
      tnode.style.height = '100%';
      tnode.style.overflow = 'hidden';                   // Try to avoid making scroll bars            
      tnode.style.display = 'none';                      // Start out Hidden
      tnode.id = 'EPWdarkenScreenObject';                   // Name it so we can find it later
      tbody.appendChild(tnode);                            // Add it to the web page
      dark = document.getElementById('EPWdarkenScreenObject');  // Get the object.
   }
   
   if (vis)
   {
      /* DDD
      // Calculate the page width and height 
      var x, y;
      if (typeof (window["innerWidth"]) != "undefined" && typeof (window["scrollMaxX"]) != "undefined")
      {
         x = innerWidth + scrollMaxX + 2 * (innerWidth - outerWidth) - 1;
         y = innerHeight + scrollMaxY;
      } else
      {
         y = document.body.scrollHeight + 24;
         x = document.body.scrollWidth + 16;
      }

      var pageHeight = y + 'px';
      var pageWidth = x + 'px';

      //set the shader to cover the entire page and make it visible.
      dark.style.width = pageWidth;
      dark.style.height = pageHeight;
      dark.style.zIndex = zindex;
      */
      
      dark.style.backgroundColor = bgcolor;
      dark.style.display = 'block';

      EPW_Animate(dark, 0, opacity, 1, 550, EPW_set_opacity, null);
      dark.style.zIndex = "" + (EPW_getHighestZindex() + 1);
   }
   else
   {
      //EPW_Animate(dark, opacity, 0, -1, 25, EPW_set_opacity, EPWclearblock);
      dark.style.display = 'none';
   }
}

function EPWclearblock()
{
   dark = document.getElementById('EPWdarkenScreenObject');  // Get the object.
   dark.style.display = 'none';
}

function EPW_nop()
{

}

function EPW_pausefive()
{
   dark = document.getElementById('EPWdarkenScreenObject');  // Get the object.
   Animate(dark, 0, 1000, 200, 5000, flash, fade_in);
}

function EPW_fade_in()
{
   dark = document.getElementById('EPWdarkenScreenObject');  // Get the object.
   Animate(dark, 70, 0, -2, 1000, _set_opacity, clearblock);
   Animate(dark, 0, 255, 8, 500, _set_bg, null);
}

function EPW_set_opacity(obj, opacity)
{
   opacity = (opacity == 100) ? 99.999 : opacity;

   // IE/Win
   obj.style.filter = "alpha(opacity:" + opacity + ")";

   // Safari<1.2, Konqueror
   obj.style.KHTMLOpacity = opacity / 100;

   // Older Mozilla and Firefox
   obj.style.MozOpacity = opacity / 100;

   // Safari 1.2, newer Firefox and Mozilla, CSS3
   obj.style.opacity = opacity / 100;
}


function EPW_Animate(prop, start, end, step, ms, setval, callback)
{

   if (prop == null || setval == null)
      return;

   var delay = 0;
   while (delay < 50)
   {
      delay = Math.abs(ms / ((start - end) / step));
      ((start - end) < 0) ? step++ : step--;
   }
   ((start - end) < 0) ? step-- : step++;

   if ((start > end) ? (step > 0) : (step < 0))
   {
      setval(prop, end);
      return;
   }

   var current = start;
   setval(prop, current);

   function stepfunc()
   {
      current += step;
      if ((start > end) ? (current > end) : (current < end))
      {
         setval(prop, current);
         setTimeout(stepfunc, delay);
      } else
      {
         setval(prop, end);
         //register[mycount]=null;
         if (callback)
            callback(prop, start, end, step, ms, setval, callback);
      }
   }

   setTimeout(stepfunc, delay);
}

function EPW_getDocHeight()
{
   if (document.body.scrollHeight && navigator.appVersion.indexOf("Win") != -1)
   {
      // body.scrollHeight gets the correct value on WIN IE6, but non on MAC
   }
   else if (document.documentElement.scrollHeight)
   {
      return document.documentElement.scrollHeight;
   }
   else if (document.documentElement.offsetHeight)
   {
      return document.documentElement.offsetHeight;
   }
}

var EPW_highestzindex = null;
function EPW_getHighestZindex()
{
   if (EPW_highestzindex != null && EPW_highestzindex != 0)
      return EPW_highestzindex;

   var allElems = document.getElementsByTagName ? document.getElementsByTagName("*") : document.all; // or test for that too
   var maxZIndex = 0;


   for (var i = 0; i < allElems.length; i++)
   {
      var elem = allElems[i];
      var cStyle = null;
      if (elem.currentStyle) { cStyle = elem.currentStyle; }
      else if (document.defaultView && document.defaultView.getComputedStyle)
      {
         cStyle = document.defaultView.getComputedStyle(elem, "");
      }
      var sNum;
      if (cStyle)
      {
         sNum = Number(cStyle.zIndex);
      } else
      {
         sNum = Number(elem.style.zIndex);
      }
      if (!isNaN(sNum))
      {
         maxZIndex = Math.max(maxZIndex, sNum);
      }
   }
   maxZIndex += 100;
   EPW_highestzindex = maxZIndex;
   return maxZIndex;
}

function EPW_getScrollX()
{
   if (self.pageXOffset)
      return self.pageXOffset;
   else if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollLeft;
   else if (document.body)
      return document.body.scrollLeft;
}

function EPW_getScrollY()
{
   if (self.pageYOffset)
      return self.pageYOffset;
   else if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
   else if (document.body)
      return document.body.scrollTop;
}

function EPW_getWidth()
{
   if (self.innerWidth)
      return (self.innerWidth);
   else if (document.documentElement && document.documentElement.clientWidth)
      return document.documentElement.clientWidth;
   else if (document.body)
      return document.body.clientWidth;
}

function EPW_getHeight()
{
   if (self.innerHeight)
      return (self.innerHeight);
   else if (document.documentElement && document.documentElement.clientHeight)
      return document.documentElement.clientHeight;
   else if (document.body)
      return document.body.clientHeight;
}
