

	function fireSelection() {
		var userSelection, selectedText, rangeObject, parentText, endWord, startWord;
		if (window.getSelection) {   // Mozilla, Safari, Opera
			userSelection = window.getSelection().getRangeAt(0);
			parentText = getInnerText(userSelection.startContainer.parentNode);
			// Make sure we get "  's ", or "  'foo' " e.g.
			endWord = userSelection.endOffset-1;
			startWord = userSelection.startOffset;
			while(endWord < userSelection.endContainer.nodeValue.length) {
				var c = userSelection.endContainer.nodeValue[endWord];
				if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
					break;
				++endWord;
			}
			while (startWord > 0) {
				var c = userSelection.startContainer.nodeValue[startWord];
				if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
					break;
				--startWord;
			}
			userSelection.setEnd(userSelection.endContainer, endWord);
			userSelection.setStart(userSelection.startContainer, startWord);
		}
		else if (document.selection) { // IE
			userSelection = document.selection.createRange();
			userSelection.expand("word");  // Make sure we get "  's " e.g.
			parentText = userSelection.parentElement().innerText;
		}
		selectedText = userSelection;
		if (userSelection.text) {
			selectedText = userSelection.text;
		}
		if (parentText == null)
			parentText = selectedText;
		if (selectedText == null)
			selectedText = parentText;
		processSelection(parentText, selectedText);
	}

	function trim(str) {
        return str.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
	}

	function getInnerText(o)
	{
		var txt = "";
		var i;

		for(i = 0; i < o.childNodes.length; i++)
		{
			if(o.childNodes[i].nodeType == 1 /* ELEMENT_NODE */)
				txt += getInnerText(o.childNodes[i]);
			if(o.childNodes[i].nodeType == 3 /* TEXT_NODE */)
				txt += o.childNodes[i].nodeValue;
		}
		return(txt);
	}

	
	function processSelection(string, word) {
		var wi = 0, a, start, end;
		string = trim(string.toString().replace(/[\.;,\(\)\[\]\?\"\'!:]/g, " "));
		string = string.replace(/\s+/g," ");
		word = word.toString().replace(/[\.;,\(\)\[\]\?\"\'!:]/g, "");
		a = string.split(" ");
		word = trim(word);
		while(wi < a.length) {
			if (a[wi] == word) break;
			++wi;
		}
		// Now limit our neighborhood to at most 5 words
		start = -1;
		end = a.length;

		for (var i = 0; i < a.length; ++i) {
			if (start == -1 && i >= wi-2) {
				start = i;
			}
			if (i >= wi+2 || i == a.length - 1) {
				end = i;
				break;
			}
		}
		string = "";
		for (var i = start; i <= end; ++i) {
			string = string + a[i] + " ";

		}
		string = trim(string).replace(/ /g, "+");
		wi = wi - start;
		parent.location.href= rhr_root + "/lookup/concept?query=" + string + "&fw=" + wi;
	}
	

	if (document.addEventListener) {
		document.addEventListener("dblclick", fireSelection, true);
	} else if (document.all) {
		document.ondblclick = fireSelection;
	}

