/**
* Javascript PopUp / PopUnder Class (visits-based)
*
* This popup class is based on Shaun Thomas' popup code. The only difference
* is that this version allows you to show popups every n visits, rather than
* having showings being based on amount of time passed. It retains the other
* options available in Shaun's original (cycling through multiple popups, optionally
* launching multiple popups, popunders, etc.
*
* Date: $Date$
* @author Shaun Thomas <sthomas@townnews.com
* @author Chris Edmonds <cedmonds@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 nVisits  Popup should show every "n" site visits
* @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, nVisits, sPath, sDomain, bSecure)
{
	this.sName = sName;
	this.nVisits = nVisits;
	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
*
* The value of the cookie being set it what's important here, since we'll
* later evaluate this value to determine how many times the user has been to
* the site and whether to show the popup. Therefore, the expiration date is set
* far into the future, since we don't want the cookie to expire according to date
*/
PopUp.prototype.setCookie = function(nVal)
{
	var sCookie = this.sName + "=" + nVal;
	sCookie += "; expires=Sat, 9 May 2099 12:00:00 UTC1";
	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.
*
* After the user has visited the required number of times, this function is
* called to remove the cookie from the user's system, so they'll see the popup
* on their next visit
*/

PopUp.prototype.deleteCookie = function()
{
	var sCookie = this.sName + "=1";
	sCookie += "; expires=Sat, 9 May 1998 12:00:00 UTC1";
	if (this.sPath) sCookie += "; path=" + this.sPath;
	if (this.sDomain) sCookie += "; domain=" + this.sDomain;
	if (this.bSecure) sCookie += "; secure";
	document.cookie = sCookie;
} // 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.
*
* If no cookie exists on the user's machine, the popup is shown and a cookie is set with a 
* value of "1". If a cookie IS found, then the cookie value is evaluated. As long as the number of 
* visits set by the administrator is greater than this value, that value is increased by 1 and the cookie
* is re-written to the user's system with this new value. If, however, the specified
* number of visits HAS been reached, the cookie is deleted from the user's system,
* meaning they'll see the popup on their next visit. This function also has the ability to cycle through
* 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)
{
	
	// If a cookie has been set...
	if (nCookieValue = this.getCookie())
	{
		// As long as the value of the cookie is less than the number of visits set by the admin...
		if(nCookieValue < (this.nVisits-2))
		{
			// Increase the value by 1
			nCookieValue++;
			// Re-set the cookie, with the new value
			this.setCookie(nCookieValue);			
		}
		// If the number of visits set by the admin has been reached...
		else
		{
			// Delete the cookie. The popup will show again on the next page load
			this.deleteCookie(this.sName);
		}
	}
	// If no cookie currently exists...
	else
	{
		// Set the cookie and show the popup(s)
		nCookieValue = 1;
		this.setCookie(nCookieValue);

		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
