function emptyFalseFunction(args) { return false; }
function emptyTrueFunction(args) { return true; }

function DI(args) {
	this.fiName = args["fiName"];
	this.custom = args["custom"] || {};
	
	// developer-customizable parameters
	this.disclaimer = args["disclaimer"] || {};
	this.disclaimer["defaultTarget"] = this.disclaimer["defaultTarget"] || "_blank";
	this.disclaimer["cssID"] = this.disclaimer["cssID"] || "ex_dis";
	this.disclaimer["messages"] = this.disclaimer["messages"] || {
		default_message: '<h2 style="text-align:center">Third Party Site Disclaimer</h2><p>You are leaving '+this.fiName+'\'s website. Links that may be accessed via this site are for the convenience of informational purposes only. Any products and services accessed through this link are not provided or guaranteed by '+this.fiName+'. The site you are about to visit may have a privacy policy that is different than '+this.fiName+'\'s. Please review their privacy policy. '+this.fiName+' does not endorse the content contained in these sites, nor the organizations publishing those sites, and hereby disclaims any responsibility for such content.</p>',
		warning: '<h2 style="text-align:center; color: red;">WARNING!</h2><p>You are leaving '+this.fiName+'\'s website. Links that may be accessed via this site are for the convenience of informational purposes only. Any products and services accessed through this link are not provided or guaranteed by '+this.fiName+'. The site you are about to visit may have a privacy policy that is different than '+this.fiName+'\'s. Please review their privacy policy. '+this.fiName+' does not endorse the content contained in these sites, nor the organizations publishing those sites, and hereby disclaims any responsibility for such content.</p>'
	};
	
	this.newWindow = args["newWindow"] || {};
	this.newWindow["width"] = this.newWindow["width"] || 800;
	this.newWindow["height"] = this.newWindow["height"] || 600;
	
	// static parameters
	this.disclaimer["visible"] = false;
	this.disclaimer["cssSelector"] = "#"+this.disclaimer["cssID"];
}

DI.prototype.onload = function(args) {
	var diObj = this;
	
	// Prepare the disclaimer popup, adding it if necessary
	var disclaimerCSSID = diObj.disclaimer["cssSelector"];
	
	if( ! $(disclaimerCSSID).length ) {
		this.addDisclaimerHTML();
	}
	
	var matchDisclaimers = /disclaimer(\w+)/;
	
	// Add the disclaimer popup callbacks based on class names
	$("a.disclaimer").each( function() {
		var searchMatch = matchDisclaimers.exec(this.className);
		var messageType = searchMatch ? searchMatch[1] : "default_message";
		var linkTarget = $(this).attr("target") || diObj.disclaimer["defaultTarget"];
		$(this).click( function() { diObj.showDisclaimer( { message: messageType, url: this.href, target: linkTarget } ); return false; } );
	} );
	
	// Add the new window popups based on URL parameters
	$("a").not(".disclaimer").
		filter( function(args) {
			return /\?.*diNewWindow(=[^&]*)?/.test(this.href);
		} ).
		click( function(args) {
			var width = diObj.newWindow["width"];
			var height = diObj.newWindow["height"];
			
			var parameters = /\?.*diNewWindow(=[^&]*)?/.exec(this.href);
			var parameterSearch = /(\d+)x(\d+)/.exec(parameters[1]);
			if(parameterSearch) {
				// TODO: parameterize these hard-coded values
				width = parameterSearch[1];
				height = parameterSearch[2];
			}
			diObj.openInNewWindow( { url: this.href, width: width, height: height } );
			return false;
		} );
	
	// Prepare the disclaimer popup div links
	$(disclaimerCSSID + " .continue").click( function() {  diObj.hideDisclaimer(); return true; } );
	$(disclaimerCSSID + " .decline").click( function() { diObj.hideDisclaimer(); return false; } );
}

DI.prototype.addDisclaimerHTML = function(args) {
	var diObj = this;
	
	var disclaimerHTML = [];
	disclaimerHTML.push('<div id="'+this.disclaimer["cssID"]+'">');
	disclaimerHTML.push('	<div class="message"> </div>');
	disclaimerHTML.push('	<div class="progressLinks">');
	disclaimerHTML.push('		<a href="#" class="continue">Continue</a> <a href="#" class="decline">Decline</a>');
	disclaimerHTML.push('	</div>');
	disclaimerHTML.push('</div>');
	
	$('body').append(disclaimerHTML.join(''));
}

DI.prototype.showDisclaimer = function(args) {
	var diObj = this;
	var parentArgs = args;
	var disclaimerCSSID = diObj.disclaimer["cssSelector"];
	
	if(diObj.disclaimer["visible"]) {
		diObj.hideDisclaimer();
	}
	
	
	var message = args["message"].toLowerCase();
	if( typeof(this.disclaimer.messages[message]) == 'undefined' ) {
		message = "default_message";
	}
	var url = args["url"];
	var target = args["target"] || "_blank";
	// Setting a size for the window overrides the target
	var sizedWindow = (args["width"] || args["height"]) ? true : false;
	if(sizedWindow) {
		target = "";
	}
	
	var content = new Array();
	var index = 0;
	$(disclaimerCSSID + " .continue").attr("href", url);
	$(disclaimerCSSID + " .continue").attr("target", target);
	if(sizedWindow) {
		$(disclaimerCSSID + " .continue").click(function(args) { diObj.openInNewWindow(parentArgs); return false; });
	} else {
		$(disclaimerCSSID + " .continue").click(emptyTrueFunction);
	}
	$(disclaimerCSSID + " .message").html(this.disclaimer.messages[message]);
	
	if( ! diObj.showDisclaimerCallback( { disclaimerDiv: $(disclaimerCSSID) } ) ) {
		diObj.disclaimer["visible"] = true;
		$(disclaimerCSSID).show();
	}
	scrollTo(0,0);
	
	return false;
}

// showDisclaimerCallback: Replace this method to customize how the disclaimer appears, e.g. by adding a transition effect
// Parameters: disclaimerDiv - the DOM object of the disclaimer, normally a jQuery-wrapped <div> 
// Return true to override the default behavior
DI.prototype.showDisclaimerCallback = function(args) {
	return false;
}

DI.prototype.hideDisclaimer = function(args) {
	var diObj = this;
	var disclaimerCSSID = diObj.disclaimer["cssSelector"];
	
	if( ! diObj.hideDisclaimerCallback( { disclaimerDiv: $(disclaimerCSSID) } ) ) {
		diObj.disclaimer["visible"] = false;
		$(disclaimerCSSID).hide();
	}
}

// hideDisclaimerCallback: Replace this method to customize how the disclaimer disappears, e.g. by adding a transition effect
// Parameters: disclaimerDiv - the DOM object of the disclaimer, normally a jQuery-wrapped <div> 
// Return true to override the default behavior
DI.prototype.hideDisclaimerCallback = function(args) {
	return false;
}

DI.prototype.openInNewWindow = function(args) {
	var diObj = this;
	
	var url = args["url"];
	var width = args["width"] || this.newWindow["width"];
	var height = args["height"] || this.newWindow["height"];
	
	var settings = 'toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,width='+width+',height='+height /* TODO: Add support for positioning +',top='+pHeight+',left='+pWidth */;
	window.open(url,'externalpopup',settings);
	
	return false;
}

