addEvent(window, "load", makeNiceTitles);
/*
<script type="text/javascript" src="nicetitle.js"></script>
<br><br><br>
asdfasdfasd asdfa<a href="" title="test<br>test" rel="cal">dfafaS</a> sdaf fdas
<div id="titleOver" style="display: none; position: absolute;" onmouseout="hideTitle();" onmouseover="CURRENT_NICE_TITLE_STATUS=1;"></div>
*/
var XHTMLNS = "http://www.w3.org/1999/xhtml";
var CURRENT_NICE_TITLE;
var CURRENT_NICE_TITLE_STATUS;
var browser = new Browser();

function makeNiceTitles() {
    if (!document.createElement || !document.getElementsByTagName) return;
    // add namespace methods to HTML DOM; this makes the script work in both
    // HTML and XML contexts.
    if(!document.createElementNS)
    {
        document.createElementNS = function(ns,elt) {
            return document.createElement(elt);
        }
    }

    if( !document.links )
    {
        document.links = document.getElementsByTagName("a");
    }
    for (var ti=0;ti<document.links.length;ti++) {
        var lnk = document.links[ti];
        if (lnk.title && lnk.rel=="cal") {
            lnk.setAttribute("nicetitle",lnk.title);
            lnk.removeAttribute("title");
            addEvent(lnk,"mouseover",showTitle);
            addEvent(lnk,"mouseout",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 hideTitle(e){

	if(CURRENT_NICE_TITLE_STATUS==3) {
		var o = document.getElementById('titleOver');
		lnk = CURRENT_NICE_TITLE;
		if(o == null || lnk == null) return;
		o.innerHTML = lnk.getAttribute("nicetitle");
		o.style.display = 'none';
		CURRENT_NICE_TITLE = null;
	}
	else if(e!=3){
		CURRENT_NICE_TITLE_STATUS = 3;
		setTimeout('hideTitle(3)', 100);
	}	
}


function showTitle(e) {
	var o = document.getElementById('titleOver');
    if (window.event && window.event.srcElement) lnk = window.event.srcElement
    else if (e && e.target) lnk = e.target
	if(o == null || lnk == null) return;

	CURRENT_NICE_TITLE_STATUS = 1;
	o.innerHTML = lnk.getAttribute("nicetitle");
	
	mpos = findPosition(lnk);
    mx = mpos[0]-1;
    my = mpos[1]+2;
    
	o.style.top = my + parseInt(lnk.offsetHeight,10) +'px';
	o.style.left = mx + 'px';
	o.style.display = 'block';	
	
	CURRENT_NICE_TITLE = lnk;
}


// Add an eventListener to browsers that can do it somehow.
// Originally by the amazing Scott Andrew.
function addEvent(obj, evType, fn){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
  } else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
	return false;
  }
}

function getParent(el, pTagName) {
	if (el == null) return null;
	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())	// Gecko bug, supposed to be uppercase
		return el;
	else
		return getParent(el.parentNode, pTagName);
}

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;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
  return [x,y];
}

// Determine browser and version.

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.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

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


