/* Create a new window */
function openInNewWindow(e) {
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
		return true;
	}
	else {
	    var newWindow = window.open(this.getAttribute('href'), '_blank');
		if (newWindow) {
			if (newWindow.focus) {
				newWindow.focus();
			}
			return false;
		}
		return true;
	}
}

//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, "youthmusic") &&
				!linkIsToDomain(link, "creative-partnerships") &&
				!linkIsToDomain(link, "artscouncil") &&
				!linkIsToDomain(link, "pedestrian") &&
				!linkIsToDomain(link, "forum.pedestrian") &&
				!linkIsToDomain(link, "downloads.pedestrian") &&
				!linkIsToDomain(link, "files.pedestrian") &&
				!linkIsToDomain(link, "dev.pedestrian") &&
				!linkIsToDomain(link, "tutoritool") &&
				!linkIsToDomain(link, "forum.tutoritool") &&
				!linkIsToDomain(link, "dev.tutoritool")) {
				// Create an img element containing the new window warning text and insert it after the link text
//				objWarningIcon = document.createElement("img");
//				objWarningIcon.src = "/media/pedestrian/image/ext_link.png";
				links[i].title = links[i].title + " (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)||
			page_links[i].href.match(/\.m3u$/i)
		) {
			var span = document.createElement("span");
			var url = "/media/pedestrian/flash/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 class=\"mp3player\" 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);
		}
	}
}
