var activeMenu;
var clickedSlug;

/** Activate a slug for a given resource link
 *
 * For all potential slug containers on the page, will add a slug
 * to all of them that match resourceURL.  That slug will point to 
 * repositoryURL
 *
 * @param resourceURL the URL of the link to add slugs to
 * @param repositoryURL the URL that the slug will point to.  Represents
 *      the resource's address in the repository
 */
function activateSlug(resourceURL, repositoryURL) {
	var matchingSlugContainers = getEmptySlugContainers(resourceURL);
	
	/* Add a slug (empty a tag of appropriate class and link) to each match */
	for (var i = 0; i < matchingSlugContainers.length; i++) {
		var slugLink = document.createElement("a");
		slugLink.href = repositoryURL;
		slugLink.className = "nsdl_slug";
		matchingSlugContainers[i].appendChild(slugLink);
	}
}

/** Find any matching links that we can attach a slug to 
 *
 * @param(resourceURL) URL of the link to add slugs to 
 */
function getEmptySlugContainers(resourceURL) {
	var containers = new Array();
	var index = 0;
	var candidates = document.getElementsByTagName("span");
	
	for (var i = 0; i < candidates.length; i++) {
		/* Get all enclosed a tags */
		var links = candidates[i].getElementsByTagName("a");
		
		/* We want only those without slugs, so 1 anchor only */
		if (links.length == 1) {
			if (links[0].href == resourceURL) {
				containers[index++] = candidates[i];
			} 
		}
	}
	
	return containers;
}

function getAllEmptySlugContainers() {
	var containers = new Array();
	var index = 0;
	var candidates = document.getElementsByTagName("span");
	
	for (var i = 0; i < candidates.length; i++) {
		/* Get all enclosed a tags */
		var links = candidates[i].getElementsByTagName("a");
		
		/* We want only those without slugs, so 1 anchor only */
		if (links.length == 1 && candidates[i].className == 'nsdlSlugContainer') {
				containers[index++] = candidates[i];
		}
	}
	return containers;
}


function slugMenuClick(e) {
	var origElement;
	
	var rightEdge;
	var bottomEdge;
	
	var xCoord;
	var yCoord;
	
	if (!e) var e = window.event;

	//window.alert("got click");	
	/* Get the clicked slug */
	if (e.target) {
		clickedSlug = e.target;
	} else if (e.srcElement) { /* IE */
		clickedSlug = e.srcElement;
	} else {
		/* Can't do anything.. */
		return true;
	}
	
	origElement = clickedSlug;
	
	/* Make sure that we actually are dealing with our slug container.
	 * Some browsers return different objects upon clicking */
	if (clickedSlug.nodeType == 3) /* Safari bug */
		clickedSlug = clickedSlug.parentNode;
		
	if (clickedSlug.tagName == "A" || clickedSlug.tagName == "a") {
		clickedSlug = clickedSlug.parentNode;
	}
	
	
	/* Respond only to right clicks on empty slugs 
	 * (note: if using onmousedown, add e.button == 2 as a condition to each)
	 */
	var empty = isEmpty(clickedSlug)
	if (empty) {
		displayNewSlugMenu(e, clickedSlug);
		return false;
	} else if (!empty) {
		displayExistingSlugMenu(e, clickedSlug);
	}
		
	return true;
}

function isEmpty(slug) {
		return (slug.getElementsByTagName("a").length == 1);
}

function displayNewSlugMenu(e, slug) {
	activateMenu(e, "slug-menu-new");
}

function displayExistingSlugMenu(e, slug) {
	activateMenu(e, "slug-menu-existing");
}

function activateMenu(e, id) {
	var menu = document.getElementById(id);
	
	//window.alert("activating " + menu);
	positionInBody(menu);
	var top;
	var left;
	var rightEdge;
	var bottomEdge;

	if (e.pageX || e.pageY) {
		rightEdge = window.innerWidth-e.pageX;
		bottomEdge = window.innerHeight-e.pageY;
		
		if (rightEdge < menu.offsetWidth) {
			left = window.pageXOffset+e.pageX-menu.offsetWidth;
		} else {
			left = e.pageX;
		}
		
		if (bottomEdge < menu.offsetHeight) {
			top = window.pageYOffset+e.pageY-menu.offsetHeight;
		} else {
			top = e.pageY;
		}
	} else {
		rightEdge = document.body.clientWidth-e.clientX;
		bottomEdge = document.body.clientHeight-e.clientY;
		
		if (!menu.offsetHeight) { /* Konq bug */
			menu.offsetHeight = 0;
			menu.offsetWidth = 0;
		}
		
		if (rightEdge < menu.offsetWidth) {
			left = document.body.scrollLeft+e.clientX-menu.offsetWidth;
		} else {
			left = document.body.scrollLeft+e.clientX;
		}
		
		if (bottomEdge < menu.offsetHeight) {
			top = document.body.scrollTop+e.clientY-menu.offsetHeight;
		} else {
			top = document.body.scrollTop+e.clientY;
		}
	}

	/* Need to make URL into something like: 
	http://localhost/mediawiki/index.php/Special:ReferencedResourceManager?url=URLENCODED_URL&action=edit
	*/
	var url = escape(clickedSlug.getElementsByTagName("a")[0].href);
	
	menu.style.display= "inline;"
	menu.style.left = left + "px;";
	menu.style.top = top + "px;";
	
	document.onmousedown=deactivateMenu;
	activeMenu = menu;
	return false;
}

function deactivateMenu(e) {
	if (!e) var e = window.event;
	if (e.button != 2) {
		activeMenu.style.display="none;";
		document.onclick="";
	}
}

function highlight() {
}

function dehighlight() {
}

/** Place an object directly in the <body> element */
function positionInBody(obj) {
	var bodies = document.getElementsByTagName("body");
	if (bodies.length != 1) window.alert("Could not find the body element!!!");
	var body = bodies[0];
	if (obj.parentNode != body) {
		body.appendChild(obj);
	}
}