// lib_popup.js - Special javscript library for popup window detector/notification

// Requires: lib_core.js
if (!LIB_CORE_LOADED) {
  // Here is where you _could_ do something to alert the user to a problem, but we don't want to do that.
}

var notifyTimer = null; // Timer object used to let the popup window call back to the parent
var alertTimer = null;  // Timer object used to determine when to show the alert to the user
var popTimer = false;   // Flag that indicates if the alertTimer expired or not

// Starts the process that detects if the popup really opened or not.
function detectPopupBlocker() {
  clearPopupBlocker(); // Kill any prior timers
  alertTimer = setTimeout("alertPopupBlocker()",6000);
  return true; // Use this to set popTimer to TRUE
}

// Clears all timers.
function clearPopupBlocker() {
  clearTimeout(alertTimer); // Ensures the timeout has been stopped
  popTimer = false; // Prevents anything else from firing later
}

// Alerts the user that a popup window was blocked, or never opened.
function alertPopupBlocker() {
  try {
    if (newWin.name) {
      // This isn't foolproof, but it may work in some cases, in most browsers.
      // This should be a last-ditch effort to detect that our window opened.
      clearPopupBlocker();
      return true;
    }
  }
  catch(e) { /* Die Quietly */ }
  if (popTimer == true) {
    // We attempted to open a new window, but you may be using a popup blocker...blah blah blah...
    alert(popupMessage);
    clearPopupBlocker();
  }
}

var checkInterval = false; // Timer used on the target page to determine if it loaded or not
var winLoaded = false;     // Flag to indicate that the target page actually loaded

// Main function that opens the popup window and starts the blocker-detection.
function openPopup(newURL,winParams) {
  var optName = "newWin";
  if (arguments.length > 2 && arguments[2] != "") {
    optName = arguments[2]; // Optional 3rd argument: window name
  } else {
    optName = "newWin";
  }
  popTimer = detectPopupBlocker();
  pageTarget = newURL; // Save the target to be called later by blank.html
  // We load blank.html first, because it loads the quickest of all pages.
  newWin = window.open("blank.html?stoken=" + SessionID + "|" + HitID + "&amp;ts=" + new Date().getTime(),optName,winParams);
  winLoaded = false; // Window is not loaded yet, duh!
  try {
    newWin.focus(); // Open a new window with the parameters passed, then focus it
    checkInterval = setTimeout("checkStatus()",100); // Is window loaded yet?
  } catch(e) { /* Die Quietly */ }
  return newWin; // Return the handle to the window itself
}

// Backup mechanism that checks if the new window itself was loaded with content yet.
function checkStatus() {
  try {
    var myStatus = newWin.document.readyState;
    if (myStatus == "complete")  {
      clearPopupBlocker();  // Ensure this is turned off
      winLoaded = true;     // Flag this window as loaded
    } else { 
      checkInterval = setTimeout("checkStatus()",100); // Keep trying
    }
  } catch(e) {
    clearTimeout(checkInterval); // Just die already
  }
}

// Cancel popup blocker notification
function cancelPBNotify() {
  // Notify the calling window that this opened
  if (window.opener) {
    window.opener.clearPopupBlocker();
  }
}

// Set a global flag that indicates this library has been loaded by the browser.
var LIB_POPUP_LOADED = true;

// End library