/**
* Javascript PopUp / PopUnder Class
*
* This is a popup class that should replace all of the random popup scripts
* we've been using on all of our sites.  This makes sure popups always work
* as expected, and we don't accidently infringe on any cut-and-paste code.
*
* Date: $Date$
* @author Shaun Thomas <sthomas@townnews.com
* @version $Release$
* @package newsys
*/

/**
* Sets all of the basic popup control data.
*
* Only the first parameter is required.  If one or more of the other options
* are set, the system will define a cookie which will only show these popup
* windows if the cookie is not set or expired.
*
* @param string sName    Name for this group of popup windows.
* @param number nDays    How long a cookie should last, if one is set.
* @param string sPath    Which part of the website a possible cookie is valid.
* @param string sDomain  Which subdomain cookie is good for.
* @param string bSecure  Is this a secure popup: true or false.
*/
function PopUp(sName, nDays, sPath, sDomain, bSecure)
{
  this.sName = sName;

  this.oExpire = null;

  if (nDays)
  {
    this.oExpire = new Date();
    this.oExpire.setTime(this.oExpire.getTime() + nDays * 86400000);
  }

  this.sPath = (sPath) ? sPath : '/';
  this.sDomain = (sDomain) ? sDomain : null;
  this.bSecure = (bSecure) ? true : false;

  this.aURLs = new Array();

}// End PopUp Constructor


/**
* Gets the current value of the status cookie we've set.
*
* This method should only be used by the class structure.  Getting the cookie
* value will not mean anything to the end user.
*
* @return string
* Whatever value found in the cookie.
*/
PopUp.prototype.getCookie = function()
{
  var nIndex = document.cookie.indexOf(this.sName + "=");
  if (nIndex == -1)
    return null;

  nIndex = document.cookie.indexOf("=", nIndex) + 1;

  var nEnd = document.cookie.indexOf(";", nIndex);
  if (nEnd == -1)
    nEnd = document.cookie.length;

  return unescape(document.cookie.substring(nIndex, nEnd));

} // End method getCookie


/**
* Sets the popup cookie so we know not to show these popups again.
*/
PopUp.prototype.setCookie = function()
{
  var sCookie = this.sName + "=1";

  if (this.oExpire) sCookie += "; expires=" + this.oExpire.toGMTString();
  if (this.sPath) sCookie += "; path=" + this.sPath;
  if (this.sDomain) sCookie += "; domain=" + this.sDomain;
  if (this.bSecure) sCookie += "; secure";

  document.cookie = sCookie;

} // End method setCookie


/**
* Forecefully expires a cookie so we show popups again.
*
* If this method is called by the end user, the cookie telling us not to
* display a popup will be expired, so popups can display again.
*/
PopUp.prototype.deleteCookie = function()
{
  // Expire works by the number of seconds past a certain year - so we'll
  // just set it to January 1, 2000.

  oDate = new Date(2000, 1, 1);
  document.cookie = this.sName + "=1; expires=" + oDate.toGMTString();

} // End method deleteCookie


/**
* Add an URL to our list of URLs to use for popup code.
*
* This method must be called before an URL will display in a popup.  URLS
* entered here will be selected randomly before being displayed in a popup.
*
* @param string sURL  URL to add to list of known links.
*/
PopUp.prototype.addURL = function(sURL)
{
  this.aURLs[this.aURLs.length] = sURL;

} // End method addURL


/**
* Display one or more registered popups as popups or popunders.
*
* Sets display cookies if appropriate, and shows one or more URLs as defined
* by addURL.  These can be popunders as well.
*
* @param number nCount      Number of popups to display, empty for all.
* @param string sOptions    Options to use for the popup window.
* @param bool   bPopUnder   Windows are popunders: true or false.
*/
PopUp.prototype.display = function(nCount, sOptions, bPopUnder)
{
  // Before doing anything, check to see if the cookie is set.  If so, this
  // popup has already been seen - skip.

  if (this.oExpire)
  {
    if (sVal = this.getCookie())
      return null;
    else
      this.setCookie();
  }

  nCount = (nCount) ? nCount : this.aURLs.length;

  // Now that the flag for display is under control, create a random number
  // and display that popup element.  To avoid duplication, track which ones
  // we've already shown.  If these are popunders, blur them to the background
  // and focus on the parent when we're done.

  var aShown = new Array();

  nCount = (nCount > this.aURLs.length) ? this.aURLs.length : nCount;

  for (i = 0; i < nCount; i++)
  {
    do
    {
      nRandom = Math.floor(Math.random() * this.aURLs.length);

    } while (aShown[nRandom]);

    aShown[nRandom] = true;

    oWindow = window.open(this.aURLs[nRandom], this.sName + i, sOptions);

    if (bPopUnder)
      oWindow.blur();
  }

  if (bPopUnder)
    window.focus();

} // End method display

