
function linkalert() {

// check for DOM support
if (!document.getElementsByTagName) return false;

// settings
var autoalert 		= true;					// set to true to auto detect external links, if set to false links with "linkalert" class name will be caught
	// depreciated by module activation
var emailalert		= true;					// set to true to alert email links, if set to false links with "emailalert" class will be caught
var setclassname 	= false; 				// set to true to set "theclassname" class | Note: does'nt degrade gracefully
var linkclassname 	= "linkalert";			// the set class name of the link alert links
var emailclassname	= "emailalert";			// the set class name of the email alert links
var allow 			= new Array ();			// create an array of allowed domains
					

// get array of linkalert links
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
	url = links[i].getAttribute("href");
	if(url.indexOf("linkalert=")>=0) {
		var chunk = url.split("linkalert=");
		links[i].setAttribute( "class",linkclassname );
		links[i].setAttribute( "href",chunk[1] );
		links[i].onclick = function() {
			var alertdata='';//todo pass multiple objects
			popup.show( alertdata, this );
			return false;
		}	
	} 
}
};


function popup() {
	
	var popupDiv = null;
	var theBody;

	function createPopup(containerClass) {
		var el = document.createElement("div");
		el.id = "popupContainer";
		if (typeof containerClass == 'undefined') {
			containerClass = "lxPopupContainer";
		}
		el.className = containerClass;
		return el;
	}

	function createPopupShield() {
		var el = document.createElement("div");
		el.id = "popupShield";
		el.className = "lxPopupShield";
		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 = "popupTrim";
		el.className = "lxPopupTrim";
		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 createPopupByData(dataObj) {
		var trimEl = createTrim(dataObj.win);
		var popupEl = createPopup(dataObj.containerClass);
		if(dataObj && dataObj.data) {
			var html = [];
			//html.push(dataObj.data.getHeader());
			html.push(dataObj.data.getBody());
			html.push(dataObj.data.getFooter());
			popupEl.innerHTML = html.join("");
		}
		trimEl.appendChild(popupEl);
		return trimEl;		
	}
	
	function createPopupData(link) {
		var trimEl = createTrim();
		var popupEl = createPopup();
		
		var ajaxRequest = newrequest();
		ajaxRequest.open("GET", modpath+"settings.php", false);
		ajaxRequest.send(null);
		response = ajaxRequest.responseText;
		
		var html = [];
		html.push('<div id="popBody" class="LxPopupBody">');
		html.push('<p>'+response+'</p>');
		html.push('<div id="popupButtons" class="LxpopupButtons">');	
		html.push('<a href="#" onclick="popup.go(\''+link+'\')">Continue</a><br />');
		html.push('<a href="#" onclick="popup.hide()">Close</a>');
		html.push('</div>');
		html.push('</div>');
		popupEl.innerHTML = html.join("");

		trimEl.appendChild(popupEl);
		return trimEl;		
	}
	
	popup.go = function(link) {
		window.open(link);
		// @todo: track outlinks with ajax??
		popup.hide();
	}

	popup.show = function(obj,link) {
		
		theBody = document.getElementsByTagName("BODY")[0];
		if(!popupDiv) {
			var popupDiv = document.createElement("div");
			popupDiv.id = "popupDiv";
			popupDiv.className = "lxpopupDiv";
			popupDiv.style.visibility = "visible";
			theBody.appendChild(popupDiv);
		}
				
		var popupShield = createPopupShield();
		popupDiv.appendChild(popupShield);
		//var popupEl = createPopupByData(obj);
		var popupEl = createPopupData(link);
		popupDiv.appendChild(popupEl);
		window.setTimeout(popup.center, 0);
		setupEventHandler();
		return false;
	}
	
	
	popup.hide = function() {
		var el = getEl('popupDiv');
		if(el) {
			el.innerHTML = "";
		}
		releaseEventHandler();
		popupDiv = null;
		theBody.removeChild(el);
		return false;
	};
	
	popup.center = function() {
	var popupTrim = getEl("popupTrim");
	var popupShield = getEl("popupShield");
	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);


