// R. Wagner 15 August 2007.  Based on concept/code from The Javascript Anthology by Edwards and Adams.
// Updated 4 March 2008.
// WinIE HACK code based on http://dotnetjunkies.com/WebLog/jking/archive/category/139.aspx
var CopyMsg = " This site and all images on this site are Copyright \xA9 1997-2008 by Richard and Marjory Wagner;  all rights are reserved. No form of reproduction, including the copying or saving of digital image files, or the alteration or manipulation of said image files is permitted, with the exception of comp usage by professional image researchers. Each image on this website is legally protected by U.S. and International Copyright laws and may not be copied or used for reproduction in any manner (except comp usage by professional image researchers) unless arranged for in writing. The reproduction or other use of any image must be negotiated. No image on this website may be used for any other purpose without the express written consent of WildNaturePhotos, L.L.C. In the event of infringement, the infringer will be charged a minimum of triple the industry-standard fee for usage and we will pursue prosecution for Copyright Infringement in U.S. Federal Court, where the infringer may be subject to a fine of U.S. $100,000 in statutory damages as well as court costs. Please indicate whether or not you agree to respect our copyrights:";
// var CopyMsg = " This site and all images on this site are Copyright \xA9 1997-2007 by Richard and Marjory Wagner";


function createDialog()
{
  var body = document.getElementsByTagName("body")[0];
  var pageDimensions = getPageDimensions();
  var viewportSize = getViewportSize();

  if (viewportSize[1] > pageDimensions[1])
  {
    pageDimensions[1] = viewportSize[1];
  }

  var dropSheet = document.createElement("div");

  dropSheet.setAttribute("id", "dropSheet");
  dropSheet.style.position = "absolute";
  dropSheet.style.left = "0";
  dropSheet.style.top = "0";
  dropSheet.style.width = pageDimensions[0] + "px";
  dropSheet.style.height = pageDimensions[1] + "px";
  body.appendChild(dropSheet);
  
  // HACK - z-index fix for WinIE
  if (isIE) {
  	HideWinIEObject(true);
  	}
 
  try
  {
    var dialog = document.createElement("div");
    dialog.className = "customDialog";
    dialog.style.visibility = "hidden";
    dialog.style.position = "absolute";

    var dialogTitle = document.createElement("h1");
    dialogTitle.appendChild(document.createTextNode("Copyright Warning"));
    dialog.appendChild(dialogTitle);

    var dialogMessage = document.createElement("p");
    dialogMessage.appendChild(document.createTextNode(CopyMsg));
    dialog.appendChild(dialogMessage);

    var dialogButton1 = document.createElement("input");
    dialogButton1.setAttribute("type", "button");
    dialogButton1.setAttribute("value", "I do not agree");
    attachEventListener(dialogButton1, "click", dialogClick, false);
    dialog.appendChild(dialogButton1);

    var dialogButton2 = document.createElement("input");
    dialogButton2.setAttribute("type", "button");
    dialogButton2.setAttribute("value", "I agree");
    attachEventListener(dialogButton2, "click", dialogClick, false);
    dialog.appendChild(dialogButton2);

    body.appendChild(dialog);

    var scrollingPosition = getScrollingPosition();
    // Safari gives erroneous values for dialog.offsetWidth and dialog.offsetHeight
    //   because the dialog is not completely rendered.  Workaround - check for 
    //   outrageous values and reset the variables to approximated defaults if 
    //   needed.  R.W.  5-1-06.
    var offsetWidth = dialog.offsetWidth;
    var offsetHeight = dialog.offsetHeight;
    
    if ((offsetWidth < 400) || (offsetWidth > 600)) {
    	offsetWidth = 500;
    }
    if ((offsetHeight < 300) || (offsetHeight > 400)) {
    	offsetHeight = 350;
    }
    
    var offsetLeft = scrollingPosition[0] + parseInt(viewportSize[0] / 2) - parseInt(offsetWidth / 2);
    var offsetTop = scrollingPosition[1] + parseInt(viewportSize[1] / 2) - parseInt(offsetHeight / 2);

    if (offsetLeft < 10 ) {
    	offsetLeft = 10;
    }
    if (offsetTop < 10 ) {
    	offsetTop = 10;
    }
    
    dialog.style.left = offsetLeft + "px";
    dialog.style.top = offsetTop + "px";
    
    dialog.style.visibility = "visible";

    dialogButton1.focus();
  }
  catch(error)
  {
    dropSheet.parentNode.removeChild(dropSheet);
  // HACK - z-index fix for WinIE
  if (isIE) {
  	HideWinIEObject(false);
  	}

    return true;
  }

    return false;
}

function dialogClick(event)
{
  if (typeof event == "undefined")
  {
    event = window.event;
  }

  var target = getEventTarget(event);

  while (target.nodeName.toLowerCase() != "input")
  {
    target = target.parentNode;
  }

  var value = target.getAttribute("value");

  if (value == "I agree")
  {
   writeCookie("CopyrightWarning", "accept", "", "/");
    var dialog = target;

    while (dialog.className != "customDialog")
    {
      dialog = dialog.parentNode;
    }

    closeDialog(dialog);
  }
  else
  {
   // window.location.href = "http://www.copyright.gov";
   	writeCookie("CopyrightWarning", "refuse", "", "/");
   // top.frames.location.href = "http://www.copyright.gov/title17/92chap5.html";
   window.location.href = "http://www.copyright.gov";
    }

  return true;
}

function closeDialog(dialog)
{
  var dropSheet = document.getElementById("dropSheet");

  dropSheet.parentNode.removeChild(dropSheet);
  dialog.parentNode.removeChild(dialog);
// HACK - z-index fix for WinIE
	if (isIE) {
  	HideWinIEObject(false);
  	}


  return true;
}

function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}

function attachEventListener(target, eventType, functionRef, capture)
{
  if (typeof target.addEventListener != "undefined")
  {
    target.addEventListener(eventType, functionRef, capture);
  }
  else if (typeof target.attachEvent != "undefined")
  {
    target.attachEvent("on" + eventType, functionRef);
  }
  else
  {
    eventType = "on" + eventType;

    if (typeof target[eventType] == "function")
    {
      var oldListener = target[eventType];

      target[eventType] = function()
      {
        oldListener();

        return functionRef();
      }
    }
    else
    {
      target[eventType] = functionRef;
    }
  }

  return true;
}

function getEventTarget(event)
{
  var targetElement = null;

  if (typeof event.target != "undefined")
  {
    targetElement = event.target;
  }
  else
  {
    targetElement = event.srcElement;
  }

  while (targetElement.nodeType == 3 && targetElement.parentNode != null)
  {
    targetElement = targetElement.parentNode;
  }

  return targetElement;
}

function getScrollingPosition()
{
  //array for X and Y scroll position
  var position = [0, 0];

  //if the window.pageYOffset property is supported
  if (typeof window.pageYOffset != 'undefined')
  {
    //store position values
    position = [
        window.pageXOffset,
        window.pageYOffset
    ];
  }

  //if the documentElement.scrollTop property is supported
  //and the value is greater than zero
  if (typeof document.documentElement.scrollTop != 'undefined'
      && document.documentElement.scrollTop > 0)
  {
    //store position values
    position = [
        document.documentElement.scrollLeft,
        document.documentElement.scrollTop
    ];
  }

  //if the body.scrollTop property is supported
  else if(typeof document.body.scrollTop != 'undefined')
  {
    //store position values
    position = [
        document.body.scrollLeft,
        document.body.scrollTop
    ];
  }

  //return the array
  return position;
}

function getViewportSize()
{
  var size = [0,0];

  if (typeof window.innerWidth != 'undefined')
  {
    size = [
    		// Use Top to account for frames.  R.W.
        //window.innerWidth,
        //window.innerHeight
        top.innerWidth,
        top.innerHeight
    ];
  }
  else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth != 'undefined'
      && document.documentElement.clientWidth != 0)
  {
    size = [
        document.documentElement.clientWidth,
        document.documentElement.clientHeight
    ];
  }
  else
  {
    size = [
        document.getElementsByTagName('body')[0].clientWidth,
        document.getElementsByTagName('body')[0].clientHeight
    ];
  }

  return size;
}

function getPageDimensions()
{
  var body = document.getElementsByTagName("body")[0];
  var bodyOffsetWidth = 0;
  var bodyOffsetHeight = 0;
  var bodyScrollWidth = 0;
  var bodyScrollHeight = 0;
  var pageDimensions = [0, 0];

  if (typeof document.documentElement != "undefined" &&
      typeof document.documentElement.scrollWidth != "undefined")
  {
    pageDimensions[0] = document.documentElement.scrollWidth;
    pageDimensions[1] = document.documentElement.scrollHeight;
  }

  bodyOffsetWidth = body.offsetWidth;
  bodyOffsetHeight = body.offsetHeight;
  bodyScrollWidth = body.scrollWidth;
  bodyScrollHeight = body.scrollHeight;

  if (bodyOffsetWidth > pageDimensions[0])
  {
    pageDimensions[0] = bodyOffsetWidth;
  }

  if (bodyOffsetHeight > pageDimensions[1])
  {
    pageDimensions[1] = bodyOffsetHeight;
  }

  if (bodyScrollWidth > pageDimensions[0])
  {
    pageDimensions[0] = bodyScrollWidth;
  }

  if (bodyScrollHeight > pageDimensions[1])
  {
    pageDimensions[1] = bodyScrollHeight;
  }
  return pageDimensions;
}


// HACK - z-index fix for WinIE
// Hides the SELECT popup menu when the dropSheet is down using DIV / iFRAME in 
//	rt_cp_incl.html code.
  function HideWinIEObject(state)
  {
   var DivRef = document.getElementById('PopupDiv');
   var IfrRef = document.getElementById('DivShim');
   var popupRef = document.getElementById('right_cp_cat-popup');
   if(state)
   {
    DivRef.style.display = "block";
    IfrRef.style.width = DivRef.offsetWidth;
    IfrRef.style.height = DivRef.offsetHeight;
    IfrRef.style.top = DivRef.style.top;
    IfrRef.style.left = DivRef.style.left;
    IfrRef.style.zIndex = DivRef.style.zIndex - 1;
    // z-index of right_cp_cat-popup needs to be Neg for IEWin to hide obj.
    popupRef.style.zIndex = -1;
    IfrRef.style.display = "block";
   }
   else
   {
    DivRef.style.display = "none";
    IfrRef.style.display = "none";
    popupRef.style.zIndex =  10;

   }
  }
