/**
 *  _mailto_speedbump.js
 *  Miniscule edit by Benaiah to Paul's _email_speedbump.js to make it work for mailto links instead
 *  If you need speedbumps for both third party links and mailtos, use _speedbump2.js instead.
 *  Attempting to use _speedbump.js and _mailto_speedbump.js will allow those speedbumps to pile up on each other.
 *
 *  @Author: Paul McLanahan <paul.mclanahan at digitalinsight dot com>
 *      with a lot of code from  Stuart Langridge's NiceTitles at http://kryogenix.org/
 *  @Usage: Simply add the following line to the <head>
<link href="_styles.css" rel="stylesheet" type="text/css"> of your HTML page and this script
 *      will seek out all links that point to a 3rd party domain and add a 3rd party Speed Bump (SB)
 *      to them.  All that's really required of you is that you add elements to the 'allowedDomains'
 *      array to tell the script which links to ignore.
 *  @Code: <script language="JavaScript" type="text/javascript" src="/path/to/script/_email_speedbump.js"></script>
 *  @CSS: The class for the SB <div> will be 'email_speedbump', so you may style accordingly. The following style elements
 *      are set by the JS, so you don't need to worry about them.
 *          d.style.position = 'absolute';
 *          d.style.width = '<mbWidth>px'; // where <mbWidth> is the variable below
 *          d.style.top = <link_position - SB height + mbYOffset>; // where <link_position is the calclated y position of the link relative to the page, which is added to mbYOffset> which is a variable below.
 *          d.style.left = <link_position + mbXOffset>; // where <link_position is the same as above, and mbXOffset> is a variable below.
 *      The rest of the CSS should go in _styles.css and in a declaration that looks like:
 *          div.email_speedbump{
 *              border:2px solid #685539;
 *              background:#fff;
 *              padding:5px;
 *              etc...
 *          }
 *      But using your own values for the actual styles of course.
 *      You may then style any elements you add to the 'mbContent' variable below using the following example:
 *          div.email_speedbump h2{
 *              color: #685539;
 *              font-size: 2em;
 *              letter-spacing: 3px;
 *              etc...
 *          }
 */

/////// start configuration ////////////

var debug = false; // set to true to show an alert on page load with all links that will have the SB added.
var mbXOffset = -100; // x offset. This is only used if mbCentered is false.  Negative number means move left, positive means right. If 0 the left side of the SB will line up with the leftmost part of the link.
var mbYOffset = -10; // y offset. Negative number means move up, positive means down. If 0 the bottom of the SB will align with the top of the link. This also sets how far the top of the SB is from the top of the window.
var mbWidth = 315; // width of the SB div
var mbCentered = true; // whether or not to center the SB on the page. If false, the SB will be aligned relative to the link.
var openInNewWindow = false;  // whether or not to open the 3rd party links (TPLs) in new windows. This should always be false unless specifically asked for by the FI!
var allowedDomains = [ // array containing strings which, if any one is found in a link, will cause the SB not to be added to said link.
	"javascript", // DO NOT REMOVE. This will find all links that use JavaScript in the href and ignore them.
	"YOUR FI's DOMAIN"  // add your FI's domain here.
];

// Edit the html in the 'mbContent' variable below to modify the contents of the SB div.
// Place the string ##SBLINK## inside an <a> tag (example below). ##SBLINK## will be replaced by the necessary 'href' and/or 'onclick' attributes.
var mbContent = '';
mbContent += '<div class="email_speedbumpIB"><!-- <img src="'+(document.location.href.indexOf('/site/')==-1?'':'../')+'images/logo.gif" width="309" height="50" alt="" border="0"> -->';
mbContent += '<h2>E-mail Disclosure</h2>';
mbContent += '<p>E-mail messages sent over the Internet\nare not secure. Please do not include any\naccount or personal information such as\nsocial security numbers in the message.</p>';
mbContent += '<div align="center" class="sblinks"><a ##SBLINK##>Continue</a>&nbsp;&nbsp;<a href="#" onclick="hideemail_speedbump();return false;">Decline</a></div></div>';

/////// end configuratin ////////////////
/////// do not edit below this line /////

addEvent(window, "load", makeemail_speedbumps);

var XHTMLNS = "http://www.w3.org/1999/xhtml";
var CURRENT_email_speedbump;

function makeemail_speedbumps(){
	if (!document.createElement || !document.getElementsByTagName) return;
	// add namespace methods to HTML DOM; this makes the script work in both
	// HTML and XML contexts.
	if(!document.createElementNS){
	    document.createElementNS = function(ns,elt){
	        return document.createElement(elt);
	    }
	}
	// do our best to get the links[] array.
	if( !document.links ){
	    document.links = document.getElementsByTagName("a");
	}
	if(document.links){
		if(debug)var out = "Links to which SBs will be attached:\n------------------------------------------\n";
		// test every link
		for(i=0;i<document.links.length;i++){
			var isMailto= false;
			var lnk = document.links[i];

			// A change here to look for mailto instead of allowed domains
			if(lnk.href){
				if(lnk.href.toLowerCase().indexOf("mailto:") != (-1)) {
					isMailto= true;
				}

				// if mailto link, add the SB stuff
				if(isMailto){
					if(debug)out += "-> " + lnk.href + "\n";
					lnk.setAttribute("email_speedbump",lnk.href);
					lnk.setAttribute("href","javascript:void(0);");
		            addEvent(lnk,"click",showemail_speedbump);
		            addEvent(lnk,"mouseover",fakeStatus);
		            addEvent(lnk,"mouseout",clearStatus);
				}
			}
		}
		if(debug)alert(out);
	}
}

function showemail_speedbump(e) {
	hideemail_speedbump();
	if(lnk = getLnkObj(e)){
	    lnkPos = findPosition(lnk);
	    lnkPosX = lnkPos[0];
	    lnkPosY = lnkPos[1];
		
		// create the DIV via the DOM
		var d = document.createElementNS(XHTMLNS,"div");
	    
		// add styles
		d.className = "email_speedbump";
		d.style.width = mbWidth + 'px';
		d.style.position = 'absolute';
		
		// calculate the left position. Either centered or relative to the link
	    if (mbCentered && document.body && document.body.offsetWidth) {
	    	d.style.left = ((document.body.offsetWidth - mbWidth)/2) + 'px';
		}
		else{
			d.style.left = (lnkPosX+mbXOffset) + 'px';
		    if (document.body && document.body.offsetWidth && ((lnkPosX+mbWidth) > document.body.offsetWidth)) {
		        d.style.left = (document.body.offsetWidth - mbWidth - 0) + "px";
		    }
		}
		
		// add the mbContent
		var reSBLink = /##SBLINK##/g;
		var sbHREF = lnk.getAttribute("email_speedbump");
		var sbATag = openInNewWindow ? 'href="#" onclick="window.open(\'' + sbHREF + '\');hideemail_speedbump();return false;"' : 'href="' + sbHREF + '" onclick="document.location.href=this.href;hideemail_speedbump();return false;"';
		d.innerHTML = mbContent.replace(reSBLink,sbATag);
		
		// append the DIV to the body of the document
	    document.getElementsByTagName("body")[0].appendChild(d);
		
		// calculate the top position of the div by getting its height after the mbContent is in
		if (lnkPosY <= 200) lnkPosY= 200;
		if(d.offsetHeight){
			// if we can get the height of the div, then use it
			var dTop = lnkPosY - d.offsetHeight + mbYOffset;
		}
		else{
			// otherwise, we'll guess that they'll be 250px high on average
			var dTop = lnkPosY - 250 + mbYOffset;
		}
		d.style.top = dTop + 'px';
				    
		// scroll the window such that the top of the SB is mbYOffset from the top of the window
		window.scrollTo(0,dTop + mbYOffset);
		
		CURRENT_email_speedbump = d;
	}
	return false;
}

function hideemail_speedbump() {
    if (!document.getElementsByTagName) return;
    if (CURRENT_email_speedbump) {
        document.getElementsByTagName("body")[0].removeChild(CURRENT_email_speedbump);
        CURRENT_email_speedbump = null;
    }
}

function fakeStatus(e){
	if(lnk = getLnkObj(e)){
	//	alert("Link to: "+lnk.getAttribute("email_speedbump"));
	    window.status = lnk.getAttribute("email_speedbump");
		return true;
	}
	else{
	    window.status = 'E-mail Disclaimer';
		return true;
	}
}

function clearStatus(e){
	window.status = '';
}

function getParent(el, pTagName) {
	if (el == null) return null;
	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())	// Gecko bug, supposed to be uppercase
		return el;
	else
		return getParent(el.parentNode, pTagName);
}

function getLnkObj(e){
	if(!document.getElementsByTagName) return false;
	if(window.event && window.event.srcElement) {
		lnk = window.event.srcElement
	}
	else if(e && e.target){
		lnk = e.target;
	}
	if(!lnk) return false;
	if(lnk.nodeName.toUpperCase() != "A") {
		// lnk is a textnode -- ascend parents until we hit a link
		lnk = getParent(lnk,"A");
	}
	if(!lnk) return false;	
	return lnk;
}

function findPosition( oLink ) {
	if( oLink.offsetParent ) {
		for( var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent ) {
			posX += oLink.offsetLeft;
			posY += oLink.offsetTop;
		}
		return [ posX, posY ];
	} else {
		return [ oLink.x, oLink.y ];
	}
}

// Add an eventListener to browsers that can do it somehow.
// Originally by the amazing Scott Andrew.
function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, true);
//		if(debug)alert("obj.addEventListener worked");
		return true;
	}
	else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
//		if(debug)alert("obj.attachEvent worked");
		return r;
	}
	else if (document.getElementById){
		// feeble attempt to get this to work in IE for Mac
//		if(debug)alert("obj.on"+evType+"=fn worked");
		eval("obj.on"+evType+"=fn");
	}
	else{
		if(debug)alert("addEvent won't work");
		return false;
	}
}

function getParent(el, pTagName) {
	if (el == null) return null;
	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())	// Gecko bug, supposed to be uppercase
		return el;
	else
		return getParent(el.parentNode, pTagName);
}