/*
 * CMS DOM Link Alert
*/
function linkalert() {

if (!document.getElementsByTagName) return false;
	//var setclassname 	= false; 				// set to true to set "theclassname" class | Note: does'nt degrade gracefully
	var linkclassname 	= "";			// the set class name of the link alert links
	var emailclassname	= "";			// the set class name of the email alert links
	var links = document.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++) {
		url = links[i].getAttribute("href");
				if(url && url.indexOf("linkalert=")>=0) {
			var chunk = url.split("linkalert=");
			//links[i].setAttribute( "class",linkclassname );
			addClass( links[i], linkclassname );
			links[i].setAttribute( "href",chunk[1] );
			links[i].onclick = function() {
				var msg='<h3>You are leaving this site.</h3><p>The website you have selected is an external site not operated by <span class=\"mergetag settingtag\"><a class=\"\" href=\"/site/admin/cms_settings.php\"></a>Bank of Bozeman</span>. This link is provided for convenience and informational purposes only.</p>';
				popup.show( this, msg, false );
				return false;
			}	
		} 
						if( url && url.indexOf("mailto:") != -1 ) {
			addClass( links[i], emailclassname );
			links[i].onclick = function() {
				var msg = '<div class=\"emailalert-content\"><h3>Email is unsecure!</h3><p>You may contact [[bussiness_name]] via email for general, non-urgent communication with us. As regular email is not secure, we caution against using email for transmitting any sensitive personal information. As your identity cannot be confirmed, we cannot act on any instructions received via regular email.</p></div>';
				popup.show( this, msg, true );
				return false;
			}
		} 
			}
};


function addClass( element, value ) {
	if (!element.className) {
		element.className = value;
	} else {
		var newClassName = element.className;
		newClassName += " ";
		newClassName += value;
		element.className = newClassName;
	}
}

function popup() {
	var popupDiv = null;
	var theBody;
	function createPopup( containerClass ) {
		var el = document.createElement("div");
		if (typeof containerClass == 'undefined') {
			containerClass = "la-container";
		}
		el.className = containerClass;
		return el;
	}

	function createPopupShield() {
		var el = document.createElement("div");
		el.id = "la-shield";
		el.style.visibility = "visible";
		el.style.position = "absolute";
		el.style.top = 0;
		el.style.left = 0;
		el.style.zIndex = 1000;
		return el;
	}

	function createTrim( win ) {
		var el = document.createElement("div");
		el.id = "la-popup-trim";
		el.style.visibility = "hidden";
		el.style.position = "absolute";
		el.style.top = 0;
		el.style.left = 0;
		el.style.zIndex = 1001;
		if (win && win.w) {
			var width = win.w;
			if (!isNaN(width))
				el.style.width = width + 'px';
			else
				el.sytle.width = width;
		}
		return el;
	}
		
	function createPopupData( link, msg, email ) {
		var trimEl = createTrim();
		var popupEl = createPopup();
		var html = [];
		html.push('<div id="la-body">');
		html.push( msg );
		html.push('<ul id="la-buttons">');	
		html.push('<li><a href="#" onclick="popup.go(\''+link+'\', \'email\')">Continue</a></li>');
		html.push('<li><a href="#" onclick="popup.hide()">Close</a></li>');
		html.push('</ul>');
		html.push('</div>');
		popupEl.innerHTML = html.join("");
		trimEl.appendChild( popupEl );
		return trimEl;		
	}
	
	popup.go = function( link, email ) {
		if(email)
			location.href=link;
		else
			window.open(link);
		// @todo: track outlinks with ajax??
		popup.hide();
	}

	popup.show = function( link, msg, email ) {
		theBody = document.getElementsByTagName("BODY")[0];
		if(!popupDiv) {
			var popupDiv = document.createElement("div");
			popupDiv.id = "la-popup";
			popupDiv.style.visibility = "visible";
			theBody.appendChild(popupDiv);
		}	
		var popupShield = createPopupShield();
		popupDiv.appendChild(popupShield);
		var popupEl = createPopupData( link, msg, email );
		popupDiv.appendChild(popupEl);
		window.setTimeout(popup.center, 0);
		setupEventHandler();
		return false;
	}
	
	
	
	popup.hide = function() {
		var el = getEl('la-popup');
		if(el) {
			el.innerHTML = "";
		}
		releaseEventHandler();
		popupDiv = null;
		theBody.removeChild(el);
		return false;
	};
	
	popup.center = function() {
	var popupTrim = getEl("la-popup-trim");
	var popupShield = getEl("la-shield");
	if( popupTrim && popupShield ) {
		var winHeight = getViewportHeight();
		var winWidth = getViewAreaWidth();
		var scTop = parseInt(getScrollTop(),10);
		var scLeft = parseInt(getScrollLeft(),10);
		popupShield.style.height = winHeight + "px";
		popupShield.style.width = winWidth + "px";
		popupShield.style.top = scTop + "px";
		popupShield.style.left = scLeft + "px";
		popupShield.style.visibility = "visible";
		var width = popupTrim.offsetWidth;
		var height = popupTrim.offsetHeight;
		if(height < 30) {
			window.setInterval(popup.center, 10);
		} else {
			var topMargin = scTop + ((winHeight - height) / 2);
			topMargin = (topMargin > 0) ? topMargin : 0;
			var leftMargin = scLeft + ((winWidth - width) / 2);
			leftMargin = (leftMargin > 0) ? leftMargin : 0;
			popupTrim.style.top = topMargin + "px";
			popupTrim.style.left = leftMargin + "px";
			popupTrim.style.visibility = "visible";
		}
	}
}

}

popup();

function setupEventHandler() {
	this.onscrollOld = window.onscroll;
	window.onscroll = popup.center;
}

function releaseEventHandler() {
	window.onscroll = (this.onscrollOld) ? this.onscrollOld : null;
	Event.remove(window, "scroll", popup.center);
}

function getEl(id) {
	if(typeof id == "object")
		return id;
	else
		return document.getElementById(id);
}

function isMozilla() { return (window.addEventListener != null); }
function isIE() { return (window.attachEvent != null); }

var Event = {
	add: function(obj, type, func, capture) {
		capture = capture || false;
		if(isMozilla()) {
			obj.addEventListener(type, func, capture);
		} else if(isIE()) {
			obj.attachEvent("on" + type, func);
		} else {
			return false;
		}
	},
	
	remove: function(obj, type, func, capture) {
		capture = capture || false;
		if(isMozilla()) {
			obj.removeEventListener(type, func, capture);
		} else if(isIE()) {
			obj.detachEvent("on" + type, func);
		} else {
			return false;
		}
	},
	
	unify: function(e) {
		e = e? e: window.event;
		if(e.srcElement) {
			e.target = e.srcElement;
		}
		if(!e.preventDefault) {
			e.preventDefault = function() { this.returnValue = false;}
		}
		if(!e.stopPropagation) {
			e.stopPropagation = function () { if(window.event){window.event.cancelBubble = true;} }
		}
		return e;
	}
}

function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight;
	return window.undefined;
}

function getViewportWidth() {
	if (window.innerWidth!=window.undefined) return window.innerWidth;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;
	if (document.body) return document.body.clientWidth;
	return window.undefined;
}

function getViewAreaWidth() {
	var theBody = document.getElementsByTagName("BODY")[0];
	var fullWidth = getViewportWidth();
	return maskWidth = (fullWidth > theBody.scrollWidth) ? fullWidth : theBody.scrollWidth;
}

function getScrollTop() {
	if (self.pageYOffset) {
		return self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		return document.documentElement.scrollTop;
	} else if (document.body) {
		return document.body.scrollTop;
	}
}

function getScrollLeft() {
	if (self.pageXOffset) {
		return self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollLeft) {
		return document.documentElement.scrollLeft;
	} else if (document.body) {
		return document.body.scrollLeft;
	}
}

function newrequest(){
	var ajaxRequest;
	//Browser Support Code
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Your browser broke!");
				return false;
			}
		}
	}
	return ajaxRequest;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(linkalert);


