/***********************************************\
| _Blank Replacement with memory v0.1           |
| Copyright: Brook Harding, all rights reserved |
\***********************************************/
nw_website = "drgreenbean.com";

// Searches through all links and adds new_w function to external ones
function hrefSearch() {
	var a=document.getElementsByTagName("a");
	// Anything beginning with this regex is a local link
	var nw_regex = new RegExp("^(https?://[a-z0-9\\-_\\.]*?"+nw_website+"|(#|/|\\.\\./|\\./)|javascript:)", "i");	

	for (var i=0; i < a.length; i++) {
		var href = a[i].href.toString();
		// If does not contain a local link but does contain a colon
		if ((nw_regex.test(href) === false) && (href.match("(:)") != null)) {
			a[i].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','','','','modal_overlay');

	Body.style.overflow = 'hidden';

	// Create form items and add functions
	var cntnr = newElmnt(ovrlay,'div','','','','modal_box');
	newElmnt(cntnr,'h1','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("modal_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();
		}
	}
}

addLoadEvent(hrefSearch);