// Copyright 2005 by Lexiteria unless otherwise specified below. 
//Developed by Elliott Stoddard and Andrew Shaffer.
//for more information please contact sales@lexiteria.com

	var EDSword;
	var targetY=null;
	var targetX=null;
	if (window.Event) {document.captureEvents(Event.CLICK);}
	window.onload = function() {document.getElementById("lookup").onclick = display;}

	function display(e) {
  		var str;
		if (!e) {							//if IE
			targetX = event.clientX;
			targetY = event.clientY;
		}else{								//if Mozilla
			targetX = e.pageX;
			targetY = e.pageY;
		}

  		if (window.getSelection) {
			str = window.getSelection()+"";
		} else if (document.selection && document.selection.createRange){
    			var range = document.selection.createRange();
    			range.expand("word");
    			range.select();
    			str = range.text;
  		} else {
    			alert("Sorry, this is not possible with your browser.");
  		}

		if (str.length > 0){
    			str=str.toLowerCase();
    			str=trimAll(str);
			var strArray = str.split(' ');
			if (strArray.length>1) {
				str = strArray[0];
				str=trimAll(str);
			}
			callToServer(str);
    			EDSword = str;
  		}
	}

	// Thank you very much <http://neo.dzygn.com/downloads/importNode.txt>

	if(!document.importNode) {
  		document._importNode = function(oNode, bImportChildren) {
    		var oNew;

		if(oNode.nodeType == 1){
			oNew = document.createElement(oNode.nodeName);
      			for(var i = 0; i < oNode.attributes.length; i++){
				//if(oNode.attributes[i].name!="disabled"){oNew.setAttribute(oNode.attributes[i].name, oNode.attributes[i].value);}
				if (oNode.attributes[i].value.length!=0 && oNode.attributes[i].name.toLowerCase()!="disabled" && oNode.attributes[i].value.toLowerCase()!="null" && oNode.attributes[i].value.toLowerCase()!="false" && oNode.attributes[i].value.toLowerCase()!="0") {
					oNew.setAttribute(oNode.attributes[i].name, oNode.attributes[i].value);
//alert(oNode.attributes[i].name + " " + oNode.attributes[i].value);
				}
      			}
			oNew.style.cssText = oNode.style.cssText

    		} else if(oNode.nodeType == 3){
      			oNew = document.createTextNode(oNode.nodeValue);
    		}

		if(bImportChildren && oNode.hasChildNodes()){
      			for(var oChild = oNode.firstChild; oChild != null; oChild = oChild.nextSibling){
				oNew.appendChild(document._importNode(oChild, true));
      			}
    		}
    		return oNew;
		}
	}

	// Thank you very much <http://developer.apple.com/internet/webcontent/iframe.html>

var IFrameObj; // our IFrame object
function callToServer() {
	if (!document.createElement) {return true};
	var IFrameDoc;

	var URL = "gawords.html"

	if (!IFrameObj && document.createElement) {
		// create the IFrame and assign a reference to the
		// object to our global variable IFrameObj.
		// this will only happen the first time
		// callToServer() is called
		try {
			var tempIFrame=document.createElement('iframe');
			tempIFrame.setAttribute('id','defFrame');
			tempIFrame.style.border='0px';
			tempIFrame.style.width='0px';
			tempIFrame.style.height='0px';
			IFrameObj = document.body.appendChild(tempIFrame);

			if (document.frames) {
				// this is for IE5 Mac, because it will only
				// allow access to the document object
				// of the IFrame if we access it through
				// the document.frames array
				IFrameObj = document.frames['defFrame'];
			}
		} catch(exception) {
			// This is for IE5 PC, which does not allow dynamic creation
			// and manipulation of an iframe object. Instead, we'll fake
			// it up by creating our own objects.
			iframeHTML='<iframe id="defFrame" style="';
			iframeHTML+='border:0px;';
			iframeHTML+='width:0px;';
			iframeHTML+='height:0px;';
			iframeHTML+='"><\/iframe>';
			document.body.innerHTML+=iframeHTML;
			IFrameObj = new Object();
			IFrameObj.document = new Object();
			IFrameObj.document.location = new Object();
			IFrameObj.document.location.iframe = document.getElementById('defFrame');
			IFrameObj.document.location.replace = function(location) {
				this.iframe.src = location;
			}
		}
	}

	if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
		// we have to give NS6 a fraction of a second
		// to recognize the new IFrame
		setTimeout('callToServer()',10);
		return false;
	}

	if (IFrameObj.contentDocument) {
		// For NS6
		IFrameDoc = IFrameObj.contentDocument;
	} else if (IFrameObj.contentWindow) {
		// For IE5.5 and IE6
		IFrameDoc = IFrameObj.contentWindow.document;
	} else if (IFrameObj.document) {
		// For IE5
		IFrameDoc = IFrameObj.document;
	} else {
		return true;
	}

	IFrameDoc.location.replace(URL);
	return false;
}

	// handleResponse is passed two parameters when called from the onload
	// event of the pages loaded in the hidden IFRAME:
	//	st: a string indicating which state is being loaded
	//	doc: the document object of the page loaded in the IFRAME

	function handleResponse(doc) {
		// get a reference to the DIV containing the data for this word
		var dataEl = doc.getElementById(EDSword);

		var responseMessage = document.getElementById('definitionDiv');
		responseMessage.innerHTML = '<img id="defBar" src="../images/lexiteria_lookup.png" onclick="hideDiv(\'definitionDiv\');"/>';

		if (dataEl!=null) {
			var newDiv = document.getElementById('definitionDiv');
			if (navigator.userAgent.indexOf('Gecko') != -1) {
				newDiv.appendChild(dataEl);
				showDiv(document.getElementById('definitionDiv'));
			} else {
				var dataNode = document._importNode(dataEl, true);
				newDiv.appendChild(dataNode);
				//newDiv.style.display = 'inline';
				showDiv(document.getElementById('definitionDiv'));
    			}
  		} else {
			hideDiv('definitionDiv');
		}
		//newText = document.createTextNode(dataText);
		//newDiv.appendChild(newText);
	}

	function trimAll(sString) {
		while (sString.substring(0,1) == ' '){
			sString = sString.substring(1, sString.length);
		}
		while (sString.substring(0,1) == ','){
			sString = sString.substring(1, sString.length);
		}
		while (sString.substring(0,1) == '.'){
			sString = sString.substring(1, sString.length);
		}
		while (sString.substring(0,1) == '-'){
			sString = sString.substring(1, sString.length);
		}
		while (sString.substring(sString.length-1, sString.length) == ' '){
			sString = sString.substring(0,sString.length-1);
		}
		while (sString.substring(sString.length-1, sString.length) == '.'){
			sString = sString.substring(0,sString.length-1);
		}
		while (sString.substring(sString.length-1, sString.length) == ','){
			sString = sString.substring(0,sString.length-1);
		}
			while (sString.substring(sString.length-1, sString.length) == '-'){
			sString = sString.substring(0,sString.length-1);
		}
  
		return sString;
	}

	function showDiv(domId) {
		var theWidth = 0;
		var theHeight = 0;
		var yOffset = 0;

		// Try to get the yoffset...
		if (window.pageYOffset) {
			  yOffset = window.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) 	{
			yOffset = document.documentElement.scrollTop;
		} else if (document.body){
			  yOffset = document.body.scrollTop;
		}

		// Try to get the "inner width" and "inner height"
		if (window.innerWidth)	{
			theWidth = window.innerWidth;
			theHeight = window.innerHeight;
		} else if (document.documentElement && document.documentElement.clientWidth) 	{
			theWidth = document.documentElement.clientWidth;
			theHeight = document.documentElement.clientHeight;
		} else if (document.body) {
			theWidth = document.body.clientWidth;
			theHeight = document.body.clientHeight;
		}

		var left = ((theWidth/4) -200) + "px";
		var top = (targetY - 105) + "px";

		domId.style.left = left;
		domId.style.top = top;

		domId.style.display = 'block';
		domId.style.position = 'absolute';
	}

	function hideDiv(domId) {
		domItem = document.getElementById(domId);
		domItem.style.display = 'none';
	}

	function playMP3(httpRef) {
		if (navigator.appName == "Microsoft Internet Explorer") {
    			var newObject = document.createElement("embed");
			newObject.setAttribute("id", "wordMP3");
    			newObject.setAttribute("type", "audio/mp3");
    			newObject.setAttribute("src", httpRef);
    			newObject.style.width="400px";
			newObject.style.height="42px";
		}else{
			var newObject = document.createElement('object');
			newObject.setAttribute("id", "wordMP3");
			newObject.setAttribute("src", httpRef);
			newObject.setAttribute("autoplay", true);
			newObject.setAttribute("controller", true);
			//newObject.setAttribute("type", "application/x-mplayer2");
			newObject.setAttribute("type", "audio/mp3");
			newObject.setAttribute("classid", "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B");
			newObject.setAttribute("codebase", "http://www.apple.com/qtactivex/qtplugin.cab");
			newObject.style.width="400px";
			newObject.style.height="42px";

			var newEmbed = document.createElement('embed');
			newEmbed.setAttribute("id", "wordMP3e");
			newEmbed.setAttribute("src", httpRef);
			newEmbed.setAttribute("autostart", true);
			newEmbed.setAttribute("controller", true);
			newEmbed.setAttribute("loop", false);
			newEmbed.style.width="400px";
			newEmbed.style.height="42px";	
			newObject.appendChild(newEmbed);
		}
		var responseMessage = document.getElementById('definitionDiv');

		var pList = responseMessage.childNodes;
			for(i=0; i<pList.length; i++){
				if (pList.item(i).getAttribute("id")=="wordMP3") {
					responseMessage.removeChild(pList.item(i));
				}
			}

		responseMessage.appendChild(newObject);
	}
