var tooltipspan = null;

function initTooltips(fontSize) {
  tooltipspan = document.createElement("span");
  tooltipspan.className = "tooltip";
  tooltipspan.texts = new Array();
  tooltipspan.fontSize = fontSize;
  tooltipspan.currentlyShowing = null;
  tooltipspan.isCurrentlyWaiting = false;
  try {
    var body = document.getElementsByTagName("body");
    if (body.length > 0) {
      body[0].appendChild(tooltipspan);
      tooltip_initTooltip(body[0], 0, "");
    }
  } catch (exception) {
    // alert(exception.message);
  }
}

function tooltip_onmouseoverWorkaround() {
  //alert(this.onmouseoverWorkaround);
  eval(this.onmouseoverWorkaround);
}

function tooltip_onmouseoutWorkaround() {
  //alert(this.onmouseoutWorkaround);
  eval(this.onmouseoutWorkaround);
}

function tooltip_initTooltip(node, tooltipCounter, debugPrefix) {
  if (typeof(node.getAttribute) != "undefined") {
    var title = node.getAttribute("title");
    if ((title != null) && (title != "")) {
      var attribute = node.getAttribute("onmouseover");
      if (attribute == null) {
        attribute = "";
      }
      if (attribute != "") {
        // im IE ist ein Javascript Attribut schon eingeschlossen
        attribute = stripAnonymousMethod(attribute);
      }
      attribute = "tooltip_delayShowTooltip(" + tooltipCounter + ");" + attribute;
      node.onmouseoverWorkaround = attribute;
      node.onmouseover = tooltip_onmouseoverWorkaround;
      /////
      attribute = node.getAttribute("onmouseout");
      if (attribute == null) {
        attribute = "";
      }
      if (attribute != "") {
        // im IE ist ein Javascript Attribut schon eingeschlossen mit
        attribute = stripAnonymousMethod(attribute);
      }
      attribute = "tooltip_hideTooltips();" + attribute;
      node.onmouseoutWorkaround = attribute;
      node.onmouseout = tooltip_onmouseoutWorkaround;
      /////
      while (title.indexOf("@NEWLINE@") > -1) {
        title = title.replace(/@NEWLINE@/, "\n");
      }
      while (title.indexOf("@CRETURN@") > -1) {
        title = title.replace(/@CRETURN@/, "\n");
      }
      tooltipspan.texts[tooltipCounter] = title;

      tooltipCounter++;
      node.removeAttribute("title");
    }
  }
  if (node.hasChildNodes()) {
    for (var i = 0; i < node.childNodes.length; i++) {
      tooltipCounter = tooltip_initTooltip(node.childNodes[i], tooltipCounter, debugPrefix + "&nbsp;&nbsp;");
    }
  }
  return tooltipCounter;
}

function stripAnonymousMethod(attribute) {
  var orig = attribute + "";
  attribute = orig.replace(/^.*function.*anonymous.*\(.*\).*\n.*\{/g, '');
  if (attribute != orig) {
    attribute = attribute.replace(/}$/g,'');
  }
  return attribute;
}

function tooltip_delayShowTooltip(nr) {
  if (tooltipspan.currentlyShowing == null) {
    tooltipspan.currentlyShowing = nr;
    if (!tooltipspan.isCurrentlyWaiting) {
      tooltipspan.isCurrentlyWaiting = true;
      setTimeout("tooltip_showTooltip();", 750);
    }
  }
}

/**
 * Man kann auch direkt einen Text uebergeben
 */
function tooltip_showTooltip(text) {
  tooltipspan.style.display = "none";
  while (tooltipspan.childNodes.length > 0) {
    tooltipspan.removeChild(tooltipspan.firstChild);
  }
  if (tooltipspan.currentlyShowing != null) {
    if (text == null && tooltipspan.currentlyShowing == null) {
      tooltipspan.isCurrentlyWaiting = false;
      return;
    }
    if (text == null) {
      text = tooltipspan.texts[tooltipspan.currentlyShowing];
    }
    // workaround => mozilla macht leere eintraege
    var tmpstringarray = text.split(/\n/);
    var stringarray = new Array();
    for (var i = 0; i < tmpstringarray.length; i++) {
      if (tmpstringarray[i] != "") {
        stringarray[stringarray.length] = tmpstringarray[i];
      }
    }
    for (var i = 0; i < stringarray.length; i++) {
      var tableNode = document.createElement("table");
      tableNode.setAttribute("border", "0");
      tableNode.setAttribute("cellspacing", "0");
      tableNode.setAttribute("cellpadding", "0");
      tableNode.style.cssText = "background-color:transparent";
      tooltipspan.appendChild(tableNode);
      var tbodyNode = document.createElement("tbody");
      tableNode.appendChild(tbodyNode);
      var trNode = document.createElement("tr");
      tbodyNode.appendChild(trNode);
      var textValue = stringarray[i];
      while (textValue.indexOf("@IMAGE@") > -1) {
        var startIndex = textValue.indexOf("@IMAGE@");
        var stopIndex = textValue.indexOf("@/IMAGE@");
        var aSubstring = textValue.substring(0, startIndex);
        if (aSubstring != "") {
          var titleNode = document.createTextNode(aSubstring);
          var tdNode = document.createElement("td");
          tdNode.setAttribute("valign", "top");
          tdNode.style.cssText = "background-color:transparent;vertical-align:top;white-space: nowrap;";
          trNode.appendChild(tdNode);
          tdNode.appendChild(titleNode);
        }
        aSubstring = textValue.substring(startIndex+"@IMAGE@".length, stopIndex);
        var imgNode = document.createElement("img");
        imgNode.setAttribute("src", "../" + aSubstring);
        imgNode.setAttribute("border", "0");
        var tdNode = document.createElement("td");
        tdNode.setAttribute("valign", "top");
        tdNode.style.cssText = "background-color:transparent;vertical-align:top;white-space: nowrap;";
        trNode.appendChild(tdNode);
        tdNode.appendChild(imgNode);
        textValue = textValue.substring(stopIndex+"@/IMAGE@".length);
      }
      if (textValue != "") {
        var titleNode = document.createTextNode(textValue);
        var tdNode = document.createElement("td");
        tdNode.setAttribute("valign", "top");
        tdNode.style.cssText = "background-color:transparent;vertical-align:top;white-space: nowrap;";
        trNode.appendChild(tdNode);
        tdNode.appendChild(titleNode);
      }
    }
    if (tooltipspan.mousex) {
      tooltipspan.style.display = "block";
      tooltip_updateMouseCoords(tooltipspan.mousex, tooltipspan.mousey);
    }
    tooltipspan.style.display = "block";
  }
  tooltipspan.isCurrentlyWaiting = false;
}

function tooltip_hideTooltips() {
  tooltipspan.style.display = "none";
  tooltipspan.currentlyShowing = null;
}

document.onmousemove = tooltip_updateMouseCoordsPerEvent;

function tooltip_updateMouseCoordsPerEvent(e) {
  if (tooltipspan == null) {
    return;
  }
  var x;
	var y;
	if (document.all) {
    x = window.event.clientX;
    y = window.event.clientY;
  } else if (e.pageX && e.pageY) {
    x = e.pageX - window.pageXOffset;
    y = e.pageY - window.pageYOffset;
  } else if (e.clientX && e.clientY) {
    x = e.clientX;
    y = e.clientY;
  }
	tooltipspan.mousex = x;
	tooltipspan.mousey = y;
  tooltip_updateMouseCoords(x, y);
}

function tooltip_updateMouseCoords(mousex, mousey) {
  if (tooltipspan == null) {
    return;
  }

  var possibleDisplayX = mousex + 16;
  var possibleDisplayY = mousey + 5;

  var width  = (document.all) ? document.body.clientWidth : window.innerWidth;
	var height = (document.all) ? document.body.clientHeight : window.innerHeight;

  // wenn es rechts oder unten herauslaufen wuerde dann einpassen
  if (possibleDisplayX + tooltipspan.offsetWidth > (width - 16)) {
    possibleDisplayX = mousex - 16 - tooltipspan.offsetWidth;
  }
  if (possibleDisplayY + tooltipspan.offsetHeight > (height - 16)) {
    possibleDisplayY = mousey - 16 - tooltipspan.offsetHeight;
  }
  
  // wenn es aber nun oben oder links herauslaufen wuerde dann einpassen nach oben bzw. links
  if (possibleDisplayX < 0) {
    possibleDisplayX = 0;
  }
  if (possibleDisplayY < 0) {
    possibleDisplayY = 0;
  }

  // Konvertierung zu Bildschirmkoordinaten
  if (document.all) {
    possibleDisplayX = possibleDisplayX + document.body.scrollLeft;
    possibleDisplayY = possibleDisplayY + document.body.scrollTop;
  } else {
    possibleDisplayX = possibleDisplayX + window.pageXOffset;
    possibleDisplayY = possibleDisplayY + window.pageYOffset;
  }

  tooltipspan.style.left = possibleDisplayX + "px";
  tooltipspan.style.top  = possibleDisplayY + "px";
}
