////////////////////////////////////////////////////
// XSHOCK_TARGETCHOOSER  |  BY MATTHIAS SCHUETZ   //
// HTTP://WWW.XSHOCK.DE  |  HTTP://BLOG.XSHOCK.DE //
////////////////////////////////////////////////////

// TOOLTIP CONFIGURATION
var tipname =			"targetchooser"	; // Name of the anchors that activate the TargetChooser
var identifier =		"name"		; // Method of getting the anchor name ("all" activates all links). Possible values: "name", "rel" or "all"
var hidedelay =			250		; // Delay of how long the tip will be shown after the mouse has moved away
var sourcelinkclick =		true		; // Condition if the source link is clickable. Possible values: true or false
var linknameblank =		"New window"	; // Displayed name of the "Open in new window" link
var linknameself =		"This window"	; // Displayed name of the "Open in same window" link
var linkspace =			10		; // Space in pixels between the two links (the space is doubled)
// END OF CONFIGURATION

var tooltip = document.getElementsByTagName("body")[0].appendChild(document.createElement("div"));
tooltip.className = "targetchooser";
tooltip.style.visibility = "hidden";

function setPosition(destination, position) {
	if (destination.offsetParent) {
		return destination[position] + this.setPosition(destination.offsetParent, position);
	} else {
		return destination[position];
	}
}

function hidetip(tooltip, delay) {
	tiphider = setTimeout (
		function() {
			tooltip.onmouseover = function() { clearTimeout(tiphider); }
			tooltip.style.visibility = "hidden";
		},
	delay)
}

function showtip(sourcelink, tooltip) {
	var destination = sourcelink.getAttribute("href");
	var sourceleft = this.setPosition(sourcelink, "offsetLeft");
	var sourcetop = this.setPosition(sourcelink, "offsetTop") + sourcelink.offsetHeight;

	tooltip.innerHTML =	"<a style=\"padding-right:" + linkspace + "px;\" target=\"_blank\" href=\""+destination+"\">"+ linknameblank + "</a>" +
				"<a style=\"padding-left:" + linkspace + "px;\" onclick=\"hidetip(tooltip, 0);\" target=\"_self\" href=\""+destination+"\">" + linknameself + "</a>";

	with (tooltip.style) {
		top = sourcetop + "px";
		left = sourceleft + "px";
		visibility = "visible";
	}
}

if (identifier == "name") {
	var docElements = document.getElementsByName(tipname);
} else {
	var docElements = document.getElementsByTagName('a');
}

for (var i = 0; i < docElements.length; i++) {
	var sourcelink = docElements[i];
	
	if (identifier == "name") {
		var linkattribute = sourcelink.getAttribute("name");
	} else if (identifier == "rel") {
		var linkattribute = sourcelink.getAttribute("rel");
	} else if (identifier == "all") {
		var linkattribute = tipname;
	}

	if (linkattribute && linkattribute == tipname) {
		with (sourcelink) {
			title = "";
			onmouseover = onmousemove = function() { try { clearTimeout(tiphider); } catch(e){}; showtip(this, tooltip); }
			onmouseout = function() { hidetip(tooltip, hidedelay); }
		}
		if (!sourcelinkclick) sourcelink.onclick = function() { return false; }
		
		tooltip.onmouseover = function() { clearTimeout(tiphider); }
		tooltip.onmouseout = function() { hidetip(tooltip, hidedelay); }
	}
}