/* mark word and lookup tranlsation */
var Width="200px" 
var ie5=document.all && !window.opera
var ns6=document.getElementById
document.onmouseup = evaluateSelection;

if (ie5||ns6) {
   if(typeof dix_font_size == "undefined") {    dix_font_size='10';  }
   document.write('<style type="text/css"> #dixmenu{ position: absolute; color: #000000; margin: 3px; z-index: 100; visibility: hidden; font: normal ' +  dix_font_size + 'px Arial,Helvetica,sans-serif;} #dixmenu a { color: #0000BB; text-decoration: none; } #dixmenu a:hover { color: #3333FF;} </style>')
   document.write('<div id="dixmenu" onMouseover="clearhidemenu();" onMouseout="dynamichide(event)"></div>')
}

function iecompattest() {   	 return (document.compatMode && document.compatMode.indexOf("CSS")!=-1)? document.documentElement : document.body }

function addScript(selection,coordX,coordY) {
  if(typeof dix_page_language == "undefined") {    dix_page_language='de';  }
  if(typeof dix_url == "undefined") { dixurl='http://dix.osola.com'; }
  var obj = new JSONscriptRequest(dix_url +'/dix.php?search=' + selection + '&X=' + coordX + '&Y=' + coordY + '&lang=' + dix_page_language);
  obj.buildScriptTag();
  obj.addScriptTag();
}

function containsDix(a){
  while(a.parentNode){
    if(a.id=='dix') return true;
    a = a.parentNode;
  }
}

String.prototype.stripTags = function () {return this.replace(/<([^>]+)>/g,'');}
String.prototype.trim = function()       { return this.replace(/^\s+|\s+$/g, ""); };
function evaluateSelection (ev) {
	if (!ev)    ev = window.event;
	var t = ev.target || ev.srcElement;
	if(containsDix(t)) return;
	if (window.getSelection) {       sel = window.getSelection();   } 
	else if (document.getSelection) {sel = document.getSelection(); } 
	else if (document.selection) {   sel = document.selection.createRange().text;   }
	if (sel != '') {// increase the probability that we find an answer: limit length, remove tags ...
		var s = sel+'';
		s = s.replace(/&nbsp;/g, ' ').stripTags();
		s = s.replace(/(der |die |das |el |la |los |las |the |to |a )/gi,' ');
		s = s.trim();
		if( s.length<30 )
		addScript(s,ev.clientX,ev.clientY);   
	}
}

function showmenu(data) {

   if (!document.all && !document.getElementById) {  return   }
   clearhidemenu()

   content = '<table style="border:2px solid #CCC;background-color:#EEEEEE;" >'
   content += '<tr><td colspan="2" style="text-align:center;">' + data.head + '</td></tr><tr><td colspan="2"><table style="background-color:#FFFFF0;" width="100%" >';
   if (data.trans.length<1) {      content += '<tr><td style="text-align:center;">' + data.error + '</td></tr></tr>'   } 
   else {  for (i=0;i< data.trans.length;i++) {     content += '<tr><td width="50%"><a href="' +data.dixurl+ '?search=' +data.trans[i].l1+ '">' +data.trans[i].l1+ '</a>&nbsp;</td><td>&nbsp;<a href="' +data.dixurl+ '?search=' +data.trans[i].l2+ '">' +data.trans[i].l2+ '</a></td></tr>'     }   }
   content += '</table></td></tr><tr><td>' + data.more + '</td><td style="text-align:right;">&copy; <a href="http://www.osola.com/" target="_blank">DIX</a></td></tr></table>';
  
   X = data.coordX-10;
   Y = data.coordY-10;

   mo = ie5? document.all.dixmenu : document.getElementById("dixmenu")
   mo.innerHTML=content
   // mo.style.width=(typeof optWidth!="undefined")? optWidth : Width
   mo.contentwidth=mo.offsetWidth
   mo.contentheight=mo.offsetHeight

   // X=ie5? event.clientX : e.clientX
   // Y=ie5? event.clientY : e.clientY

   //Find out how close the mouse is to the corner of the window
   var right=ie5? iecompattest().clientWidth-X : window.innerWidth-X
   var bottom=ie5? iecompattest().clientHeight-Y : window.innerHeight-Y
   //if the horizontal distance isn't enough to accomodate the width of the context menu
   if (right<mo.contentwidth) {
     //move the horizontal position of the menu to the left by it's width
     mo.style.left=ie5? iecompattest().scrollLeft+X-mo.contentwidth+"px" : window.pageXOffset+X-mo.contentwidth+"px"
   } else {
     //position the horizontal position of the menu where the mouse was clicked
     mo.style.left=ie5? iecompattest().scrollLeft+X+"px" : window.pageXOffset+X+"px"
   }

   //same concept with the vertical position
   if (bottom<mo.contentheight) {     mo.style.top=ie5? iecompattest().scrollTop+Y-mo.contentheight+"px" : window.pageYOffset+Y-mo.contentheight+"px"   } 
   else {     mo.style.top=ie5? iecompattest().scrollTop+Y+"px" : window.pageYOffset+Y+"px"   }

   mo.style.visibility="visible";
   return false
}

function contains_ns6(a, b) {
   //Determines if 1 element in contained in another- by Brainjar.com
   while (b.parentNode)   if ((b = b.parentNode) == a) {     return true;   }   return false;}
function hidemenu(){   if (window.mo) {     mo.style.visibility="hidden"   }}
function dynamichide(e){  if (ie5&&!mo.contains(e.toElement)) {    hidemenu()  } else {    if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget)) {       hidemenu()    }  }}
function delayhidemenu() {  delayhide=setTimeout("hidemenu()",500)}
function clearhidemenu() {  if (window.delayhide) {      clearTimeout(delayhide)  }}

// JSONscriptRequest -- a simple class for accessing Yahoo! Web Services
// using dynamically generated script tags and JSON
//
// Author: Jason Levitt
// Date: December 7th, 2005
//
// A SECURITY WARNING FROM DOUGLAS CROCKFORD:
// "The dynamic <script> tag hack suffers from a problem. It allows a page 
// to access data from any server in the web, which is really useful. 
// Unfortunately, the data is returned in the form of a script. That script 
// can deliver the data, but it runs with the same authority as scripts on 
// the base page, so it is able to steal cookies or misuse the authorization 
// of the user with the server. A rogue script can do destructive things to 
// the relationship between the user and the base server."
//
// So, be extremely cautious in your use of this script.

// Constructor -- pass a REST request URL to the constructor
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
JSONscriptRequest.prototype.buildScriptTag = function () {
    // Create the script tag
    this.scriptObj = document.createElement("script");
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}

