//Tests whether 'link' begins with http://(www.)domain. Needn't pass the www., or the .com or .org (though these will narrow the match down).
function linkIsToDomain(link, domain) {
	var re = new RegExp("^http://((www.)|)"+domain, "i"); //i=case insensitive
	return re.test(link);
}

//Add the openInNewWindow function, and an icon to the onclick event of external links
function insertNewWindowLinks() {
// Check that the browser is DOM compliant and not home page
	if (document.getElementById && document.createElement && document.appendChild && typeof(homepage) == 'undefined') {
		// Find all links
		var links = document.getElementsByTagName('a');
		var objWarningText;
		var link;
		for (var i = 0; i < links.length; i++) {
			link = links[i].href;
			if (linkIsToDomain(link, "") && //tests that the link is absolute
				!linkIsToDomain(link, "afc") && 
				!linkIsToDomain(link, "abc") && 
				!linkIsToDomain(link, "ether") &&
				!linkIsToDomain(link, "creativecommons") && 
				!linkIsToDomain(link, "thepuredrop") &&
				!linkIsToDomain(link, "en.wikipedia")) {
				// Create an img element containing the new window warning text and insert it after the link text
				objWarningIcon = document.createElement("img");
				objWarningIcon.src = "../img/ext_link.png";
				links[i].title = links[i].title + " (new window)";
				links[i].appendChild(objWarningIcon);
				links[i].onclick = openInNewWindow;
			}
		}
		objWarningText = null;
	}
}

//Add the openInNewWindow function, and a wikipedia icon to the onclick event of links to wikipedia
function insertWikipediaLinks() {
// Check that the browser is DOM compliant and not home page
	if (document.getElementById && document.createElement && document.appendChild && typeof(homepage) == 'undefined') {
		// Find all links
		var links = document.getElementsByTagName('a');
		var objWarningText;
		var link;
		for (var i = 0; i < links.length; i++) {
			link = links[i].href;
			if (linkIsToDomain(link, "en.wikipedia")) {
				// Create an img element containing the new window warning text and insert it after the link text
				objWarningIcon = document.createElement("img");
				objWarningIcon.src = "../img/wiki_link.png";
				links[i].title = links[i].title + "Wikipedia (new window)";
				links[i].appendChild(objWarningIcon);
				links[i].onclick = openInNewWindow;
			}
		}
		objWarningText = null;
	}
}

//Looks through all the links to find links to MP3 files, and inserts the mp3 player button in front of each one.
function insertInlineMP3Players() {
	var page_links = document.getElementsByTagName('a');
	for (var i=0; i<page_links.length; i++){

		if (page_links[i].href.match(/\.mp3$/i)) {
			var span = document.createElement("span");
			var url = "musicplayer.swf?&song_url="+escape(page_links[i].href)+"&song_title="+escape(page_links[i].innerHTML)
			var width = 17
			var height = 17
			code_str = ""
			code_str += " <object type=\"application/x-shockwave-flash\"\n"
			code_str += "data=\""+url+"\" \n"
			code_str += "width=\""+width+"\" height=\""+height+"\">\n"
			code_str += "<param name=\"movie\" \n"
			code_str += "value=\""+url+"\" />\n"
			code_str += "<param name=\"wmode\" \n"
			code_str +=	"value=\"transparent\" />\n"
			code_str += "</object>\n"
			span.innerHTML = code_str
			page_links[i].parentNode.insertBefore(span, page_links[i].nextSibling)
			//page_links[i].appendChild(span);
		}
	}
}

listen('load', window, insertNewWindowLinks);
listen('load', window, insertWikipediaLinks);
listen('load', window, insertInlineMP3Players);
