/***********************************************\
| _Blank Replacement with memory v0.1           |
| Copyright: Brook Harding, all rights reserved |
\***********************************************/

addLoadEvent(hrefSearch);

// Searches through all links and adds new_w function to external ones
function hrefSearch() {
	var a=document.getElementsByTagName("a");

	var website = "mtoli.org";
	// Regular expression checks for [http(s)][optional subdomain][specified domain]
	var searchString = new RegExp("^https?://[a-z0-9\\-_\\.]*?"+website, "i");

	for (var i=0; i < a.length; i++) {
		var externalLink = a[i];		
		if (searchString.test(externalLink.href.toString()) === false) {
			externalLink.onclick = function() {	return new_w(this); }
		}

	}
}

// Creates DOM elements
function newElmnt(nLocation,nElement,nText,nType,nValue,nId) {
	var n = document.createElement(nElement);
	if (!nLocation) { nLocation = Body; }
	n.type = nType;
	if (nValue) { n.value = nValue; }
	if (nId) { n.id = nId; }	
	nChild = nLocation.appendChild(n);
	if (typeof nText == 'string' ? nText : '') { nChild.appendChild(document.createTextNode(nText)); }
	return n;
}

function createOverlay(alink) {
	Body = document.getElementsByTagName("body")[0];
	
	//Scrolls to top
	window.scroll(0,0);

	// Create overlay, add IE opacity, and hide body overflow
	var ovrlay = newElmnt('','form','','','','New_W_Overlay');
	ovrlay.style.filter = 'alpha(opacity=80)';
	Body.style.overflow = 'hidden';

	// Create form items and add functions
	var cntnr = newElmnt(ovrlay,'fieldset','','','','New_W_Box');
	newElmnt(cntnr,'legend','Would you like to open this link in a new window?');
	var yes = newElmnt(cntnr,'input','','button','Yes');
	yes.onclick = function() { deleteOverlay(alink,'_Blank'); }
	yes.focus();
	var no = newElmnt(cntnr,'input','','button','No');
	no.onclick = function() { deleteOverlay(alink,'_Self'); }
	rchk = newElmnt(cntnr,'input','','checkbox','remember');
	newElmnt(cntnr,'label','Apply preference to all external links.');
}

function deleteOverlay(alink,target) {
	Body.style.overflow = 'auto';
	Body.removeChild(document.getElementById("New_W_Overlay"));
	if (rchk.checked != false) { createCookie('new_w_memory', target, 1); }
	if (target == '_Blank') { window.open(alink.href); }
	else if (target == '_Self') { window.location = alink.href; }
}

// Prompts to open link in new window
function new_w(alink) {
	var Cookie = readCookie('new_w_memory');
	// Check for preference cookie and executes preference
	if (Cookie == '_Blank') {
		window.open(alink.href);
	} else if (Cookie == '_Self') {
		window.location = alink.href;
	// If no preference cookie is set create form
	} else {
		createOverlay(alink);
	}
	// Stops link href from being followed
	return false;
}

// Creates Cookies http://w3cschools.com
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

// Reads Cookies http://w3cschools.com
function readCookie(c_name) {
	var nameEQ = c_name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// Appends a load event http://simon.incutio.com
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

// Show delayed rollover menu - Copyright:Brook Harding
function showMenu(parent) {
	var submenu = parent.getElementsByTagName('ul')[0];
	var show;
	var hide;

	if (submenu != null) {
		show = setTimeout(function(){
			submenu.style.display = 'block';
		}, 250);
	}

	parent.onmouseout = function() {
		clearTimeout(show);
		hide = setTimeout(function(){
			submenu.style.display = 'none';
		}, 500);
	}

	parent.onmouseover = function() {
		clearTimeout(hide);
		showMenu(parent);
	}
}
