addEvent(window, "load", makeTitles);

var CURRENT_TITLE;
var browser = new Browser();



function makeTitles() {
	
	var blah = new Array("a", "area", "abbr", "acronym", "img", "input");

	for (var g=0; g < blah.length; g++) {


		var tags = document.getElementsByTagName(blah[g]);
	
	
		for (var i=0; i < tags.length; i++) {
	

		var tag = tags[i];

	  	if  (tag.title) {

	 	 tag.setAttribute("nicetitle",tag.title);
	 	 tag.removeAttribute("title");
	 	 addEvent(tag,"mouseover",showTitle);
	 	 addEvent(tag,"mouseout",hideTitle);
	 	 addEvent(tag,"focus",showTitle);
	 	 addEvent(tag,"blur",hideTitle);

        }
	}

}
}


function findPosition( oLink ) {
  if( oLink.offsetParent ) {
    for( var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent ) {

      posX += oLink.offsetLeft;
      posY += oLink.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oLink.x, oLink.y ];
  }
}


function showTitle() {
    if (CURRENT_TITLE) hideTitle(CURRENT_TITLE);

    nicetitle = this.getAttribute("nicetitle");
    var d = document.createElement("div");
    d.setAttribute("id","nicetitle");
    tnt = document.createTextNode(nicetitle);
    d.appendChild(tnt);



    if (nicetitle.length > 11) {
      w = 11;
    } else {
	 w = parseInt(nicetitle.length / 1.1);
    }

    d.style.width = w + 'em';

    mpos = findPosition(this);
    mx = mpos[0];
    my = mpos[1];

    eltern = this.parentNode.tagName.toLowerCase();
    
    ml = getMousePosition(this)[0];
    mt = getMousePosition(this)[1];

/* das mach ich besser, wenn ich mal zeit hab :) */

    if (eltern == "li") {
	    if (browser.isIE) {

		d.style.left = 160 + 'px'; //?????
		d.style.top = this.parentNode.offsetTop + 60 + 'px';

	    } else {
		d.style.position = "fixed";
		d.style.left = 13 + 'em';
		d.style.top = this.parentNode.offsetTop + 55 + 'px';
	    }
	    
    } else if (eltern == "td" || eltern == "p" || eltern == "span" || eltern == "a")  {
	    if (browser.isIE) {
	d.style.left = (mx+50) + 'px';
      	d.style.top = Math.abs(window.event.clientY - 70) + 'px';

	    } else if (d.firstChild.length <= 24) {

      	d.style.left = (mx+32) + 'px';
      	d.style.top = (my-28) + 'px';
	      } else {
		d.style.left = (mx+32) + 'px';
      	d.style.top = (my-55) + 'px';
		}
	    }
    
	else if (eltern == "map") {
		var coords = this.getAttribute("coords");
		var coord = coords.split(",");
		d.style.fontWeight = "bold";
		
		if (!this.offsetLeft && !browser.isIE) {
			var map = document.getElementById("map");
			d.style.left = parseInt(map.offsetLeft) + parseInt(coord[0]) + 50 + 'px';
			d.style.top = parseInt(map.offsetTop) + parseInt(coord[1]) - 20 + 'px';

		} else if (browser.isIE){
			
			d.style.left = (window.event.clientX) + 20 + 'px';
			d.style.top = (window.event.clientY) - 30 + 'px';
		} else {
		d.style.left = parseInt(this.offsetLeft) + parseInt(coord[0]) + 50 + 'px';
		d.style.top = parseInt(this.offsetTop) + parseInt(coord[1]) - 20 + 'px';

		}
    } else if (d.firstChild.length < 22)  {
	   
		d.style.left = (ml+35) + 'px';
		d.style.top = (mt-35) + 'px';
    } else {
    		d.style.left = (ml+35) + 'px';
		d.style.top = (mt-55) + 'px';
    }

    var breite = (window.innerWidth - 10);
    var breiteIE = (document.body.clientWidth - 10);
    if (window.innerWidth && (mx + w*15) > breite) {

	   d.style.direction = "rtl";
	   d.style.left = breite - w*15 + 'px';

    }
    if ( document.body.clientWidth && (mx+(w*15)) > breiteIE) {

	   d.style.left = breiteIE - 150 + 'px';


    }
 

    document.getElementsByTagName("body")[0].appendChild(d);

    fadeTarget = document.getElementById("nicetitle");

	fadeIn(1);

    CURRENT_TITLE = d;
}

function fadeIn(opacity) {
	if (fadeTarget && (opacity || filter)) {
		if (opacity <= 9) {
			
			if (fadeTarget.style.opacity!=null) {

				fadeTarget.style.opacity = (opacity/10);
			} else if (fadeTarget.style.filter!=null) {

				fadeTarget.style.filter = "alpha(opacity="+opacity+"0)";

			}
			opacity += 1;
			window.setTimeout("fadeIn("+opacity+")", 40);
		}
	}
}

function hideTitle(e) {
	

    if (CURRENT_TITLE) {

        document.getElementsByTagName("body")[0].removeChild(CURRENT_TITLE);
        CURRENT_TITLE = null;
    }
}

function addEvent( obj, type, fn ) /* die version ohne bug :) */
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function getMousePosition(event) {
  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;

  } else {
    x = event.offsetLeft;
    y = event.offsetTop;
  }
  return [x,y];
}

function Browser() {
/* blah, browser detect, but mouse-position stuff doesn't work any other way*/
  var ua, s, i;

  this.isIE = false;
  this.isNS = false;
  this.isOP = false;


  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;

    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;

    return;
  }

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;

    return;
  }
   s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOP = true;

    return;
  }
  
}
