var Width="200px" 
var ie5=document.all && !window.opera
var ns6=document.getElementById

document.onmouseup = Auswertung;

if (ie5||ns6) {
    document.write('<style type="text/css"> #myjmkmenu{ position: absolute; background-color: #' + myjmk_color_bg + '; border:1px solid black; margin: 3px; z-index: 100; visibility: hidden;} #myjmkmenu td { font: normal ' +  myjmk_text_size + 'px Arial; color: #' + myjmk_color_text + ';} #myjmkmenu a { color: #203f9c; text-decoration: none; font-family: arial; font-size: ' + myjmk_text_size + 'px; font-weight: bold; } #myjmkmenu a:hover { color: #ffbf00;} #myjmkmenu a:visited{ color: #203f9c; } </style>')
    document.write('<div id="myjmkmenu" 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 myjmk_lang == "undefined") {
    myjmk_lang='';
  }
  var obj = new JSONscriptRequest('http://rpc.myjmk.com/xmlhttp/rsp.php?tsearch=' + selection + '&X=' + coordX + '&Y=' + coordY + '&lang=' + myjmk_lang);
  obj.buildScriptTag();
  obj.addScriptTag();
}

function Auswertung (Ereignis) {
  if (!Ereignis)
    Ereignis = window.event;

  if (window.getSelection) {
       sel = window.getSelection();
   } else if (document.getSelection) {
       sel = document.getSelection();
   } else if (document.selection) {
       sel = document.selection.createRange().text;
   }
   if (sel != '' ) {
     addScript(sel,Ereignis.clientX,Ereignis.clientY);
   }

}

function showmenu(data) {

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

   clearhidemenu()

   content = '<table>'
   content += '<tr><td colspan="3" style="text-align:left;">' + data.service + '</td></tr>';
   content += '<tr><td></td><td height="10"></td><td></td></tr>';
   if (data.translations.length<1) {
      content += '<tr>'
      content += '<td colspan="3">' + data.error + '</td></tr>';
      content += '</tr>'
   } else {
     for (i=0;i< data.translations.length;i++) {
       content += '<tr>'
       content += '<td>' + data.translations[i].german + '</td><td width="10"></td><td>' + data.translations[i].spanish + '</td>';
       content += '</tr>'
     }
   }
   content += '<tr><td></td><td height="10"></td><td></td></tr>';
   content += '<tr><td colspan="3" style="text-align:center;">' + data.more + '</td></tr>';
   content += '</table>'
  
   X = data.coordX;
   Y = data.coordY;

   mo = ie5? document.all.myjmkmenu : document.getElementById("myjmkmenu")
   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);
}

