/*
	POP-UNDER WINDOW CLASS
	Launches pop-under window as specified with cookie checking and 
	automatic expiration option. No library dependencies.
	7/15/2010 ~BV
	
	INSTANCE:
	sitePopunder(url, expire, cookieDays, width, height, windowOptions);
	
	PARAMETERS:
	url				Web address of page to open in pop-under window		Required
	
	expire			Date pop-under should stop happening [null]			Optional
	cookieDays		Number of days until cookie expires [1]				Optional
	width			Width of pop-under window [740]						Optional
	height			Height of pop-under window [330]					Optional
	windowOptions	Window chrome options								Optional
	
	USE:
	<script type="text/javascript" src="http://www.dispatch.com/live/static/javascript/popunder.js"></script>
	<script type="text/javascript">
		var popunder = new sitePopunder("http://www.google.com","8/15/2010"); 
		popunder.ini();
	</script>
	
*/

function sitePopunder(url, width, height, expire, cookieDays, windowOptions) {
	// Setup defaults
	this.url    = url;
	this.expire = expire;
	this.active = true;
	this.cookie = "dpcpopunder";
	
	if (cookieDays)   { this.cookieDays = cookieDays; } else { this.cookieDays = 1; }
	if (width)  { this.width = width; } else { this.width = 740; }	
	if (height) { this.height = height; } else { this.height = 330; }
	if (windowOptions) { this.windowOptions = windowOptions+',height='+this.height+',width='+this.width; 
		} else { this.windowOptions = 'scrollbars=no, resizable=no, toolbar=no, menubar=no, status=no, location=no, left=100, top=50, height='+this.height+',width='+this.width; }
	
	// Check expiration date, if used
	this.dateCheck = function() {
		if (this.expire && this.active) {
			this.dt  = new Date();
			this.dtx = new Date(this.expire);
			
			if (this.dt > this.dtx) { this.active = false; }
		}
	};
	
	// Cookie handling
	this.readCookie=function(e){var d=e+"=";var c=document.cookie.split(';');for(var b=0;b<c.length;b++){var a=c[b];while(a.charAt(0)==' ')a=a.substring(1,a.length);if(a.indexOf(d)==0)return a.substring(d.length,a.length)}return null};
	this.createCookie=function(e,d,c){if(c){var b=new Date();b.setTime(b.getTime()+(c*24*60*60*1000));var a="; expires="+b.toGMTString()}else var a="";document.cookie=e+"="+d+a+"; path=/"};
	
	// Execute and launch pop-up if active
	this.ini = function() {
		this.dateCheck();
		if (this.active && this.url) {
			if (!this.readCookie(this.cookie) || (this.cookieDays < 0)) { 
				if (this.cookieDays > 0) { this.createCookie(this.cookie,"y",this.cookieDays); }
				win2 = window.open(this.url,this.cookie,this.windowOptions);
				win2.blur(); window.focus();
			} 
		}
	};
	
} // END class
