addEvent(window, "load", makeNiceTitles);

var XHTMLNS = "http://www.w3.org/1999/xhtml";
var CURRENT_NICE_TITLE = null;
var LAST_ELEMENT_NICE_TITLE = null;
var CURRENT_NICE_TITLE_EVENT = null;
var CURRENT_NICE_TITLE_TIMER = null;
var HIDING_NICE_TITLE = false;
var browser = new Browser();

function makeNiceTitles() {
    if (!document.createElement || !document.getElementsByTagName || !document.getElementById) 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);
        }
    }

	var content = document.getElementById('content');
	var sidebar = document.getElementById('sidebar');
	if(!content) return;

    makeNiceTitlesLinks(content.getElementsByTagName("a"));
    if(sidebar)
    	makeNiceTitlesLinks(sidebar.getElementsByTagName("a"));

    var instags = content.getElementsByTagName("ins");
    if (instags) {
    for (var ti=0;ti<instags.length;ti++) {
        var instag = instags[ti];
        if (instag.dateTime) {
            var strDate = instag.dateTime;
            var dtIns = new Date(strDate.substring(0,4),parseInt(strDate.substring(4,6)-1),strDate.substring(6,8),strDate.substring(9,11),strDate.substring(11,13),strDate.substring(13,15));
            instag.setAttribute("nicetitle","Added on "+dtIns.toString());
            addEvent(instag,"mouseover",showNiceTitle);
            addEvent(instag,"mouseout",hideNiceTitle);
            //addEvent(instag,"focus",showNiceTitle); //bug in findPosition
            //addEvent(instag,"blur",hideNiceTitle);
        }
    }
    }
}

function makeNiceTitlesImg(list)
{
	var retVal = false;
    for (var ti=0;ti<list.length;ti++) {
        var obj = list[ti];
        if (obj.title) {
        	retVal = true;
            obj.setAttribute("nicetitle",obj.title);
            obj.removeAttribute("title");
            addEvent(obj,"mouseover",showNiceTitle);
            addEvent(obj,"mouseout",hideNiceTitle);
            addEvent(obj,"mousemove",showNiceTitle);
        }
        if(browser.isIE) obj.removeAttribute("alt");
    }
    return retVal;
}

function makeNiceTitlesLinks(list)
{
    for (var ti=0;ti<list.length;ti++) {
        var lnk = list[ti];
        var haveImg = makeNiceTitlesImg(lnk.getElementsByTagName("img"));
        if (lnk.title) {
            lnk.setAttribute("nicetitle",lnk.title);
            lnk.removeAttribute("title");
            addEvent(lnk,"mouseover",showNiceTitle);
            addEvent(lnk,"mouseout",hideNiceTitle);
            addEvent(lnk,"mousemove",showNiceTitle);
            //addEvent(lnk,"focus",showNiceTitle); //bug in findPosition.
            //addEvent(lnk,"blur",hideNiceTitle);
        }
    }
}

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 getNiceTarget(e) {
	if (!e) return null;
    if (window.event && window.event.srcElement)
        return window.event.srcElement;
    if (e && e.target)
        return e.target;
    return null;
}

function showNiceTitle(e) {
	var lnk = getNiceTarget(e);
    if (!lnk) return;

    if (CURRENT_NICE_TITLE) {
		if(LAST_ELEMENT_NICE_TITLE == lnk)
    	{
    		stopHideNiceTitle();
    		return;
		}
	}

	CURRENT_NICE_TITLE_EVENT = browser.isIE? window.event : e;
	CURRENT_NICE_TITLE_EVENT.xy = getMousePosition(e);
	CURRENT_NICE_TITLE_EVENT.lnk = lnk;
	if (CURRENT_NICE_TITLE_TIMER != null) window.clearTimeout(CURRENT_NICE_TITLE_TIMER);
	CURRENT_NICE_TITLE_TIMER = window.setTimeout('showNiceTitle2()',200);
}

function showNiceTitle2() {
	if (!document.getElementsByTagName) return;

	var e = CURRENT_NICE_TITLE_EVENT;
	var lnk = e.lnk;
	LAST_ELEMENT_NICE_TITLE = lnk;
    if (!lnk || !lnk.getAttribute) return;

    nicetitle = lnk.getAttribute("nicetitle");
    while (!nicetitle || nicetitle=='') {
        // lnk is not actually the link -- ascend parents until we hit a link
        lnk = lnk.parentNode;
        if (!lnk || !lnk.getAttribute) return;
    	nicetitle = lnk.getAttribute("nicetitle");
    }
    
    if (CURRENT_NICE_TITLE) forceHideNiceTitle();

    var d = document.createElementNS(XHTMLNS,"div");
    d.className = "nicetitle";
    tnt = document.createTextNode(nicetitle);
    pat = document.createElementNS(XHTMLNS,"p");
    pat.className = "titletext";
    pat.appendChild(tnt);
    d.appendChild(pat);
    
    var alnk = getParent(lnk, "a");
    if (alnk && alnk.href) {
    	lnkd = document.createElementNS(XHTMLNS,"a");
    	lnkd.href = alnk.href;
    	lnkd.onclick = function(e){
    		if (!e) var e = window.event;
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
    	}
        tnd = document.createTextNode(alnk.href);
        lnkd.appendChild(tnd)
        pad = document.createElementNS(XHTMLNS,"p");
        pad.className = "destination";
        pad.appendChild(lnkd);
        d.appendChild(pad);
        d.style.cursor = alnk.style.cursor;
        d.onclick = function(){ forceHideNiceTitle(); alnk.onclick(); };
    }

    addEvent(d,"mouseover",stopHideNiceTitle);
	addEvent(d,"mouseout",hideNiceTitle);

	d.style.visibility = 'hidden';
    document.getElementsByTagName("body")[0].appendChild(d);
    CURRENT_NICE_TITLE = d;


    var w = d.width? d.width : d.offsetWidth;

	/*
    mx = lnk.offsetLeft;
    my = lnk.offsetTop+lnk.offsetHeight;
    */
    
    /*
    mpos = findPosition(lnk);
    mx = mpos[0];
    my = mpos[1];
    */
    
    mx = e.xy[0]-10; my = e.xy[1];

	mpos = findPosition(lnk);
    var h = lnk.height? lnk.height : lnk.offsetHeight;
	var offsetBottom = h + mpos[1];
	if(my+15 > offsetBottom)
	  my = offsetBottom - 14;

    d.style.left = (mx) + 'px';
    d.style.top = (my+15) + 'px';
    if (window.innerWidth && ((mx+w) > window.innerWidth)) {
        d.style.left = (window.innerWidth - w - 25) + "px";
    }
    if (document.body.scrollWidth && ((mx+w) > document.body.scrollWidth)) {
        d.style.left = (document.body.scrollWidth - w - 25) + "px";
    }

	d.style.display = 'none';
	d.style.visibility = 'visible';
	$(d).fadeIn("fast");
}

function stopHideNiceTitle(e) {
	HIDING_NICE_TITLE = false;
}

function forceHideNiceTitle() {
	HIDING_NICE_TITLE = true;
	hideNiceTitle2();
}

function hideNiceTitle(e) {
	CURRENT_NICE_TITLE_EVENT = null;
	if (CURRENT_NICE_TITLE_TIMER != null) window.clearTimeout(CURRENT_NICE_TITLE_TIMER);
	CURRENT_NICE_TITLE_TIMER = null;
    if (!CURRENT_NICE_TITLE || !document.getElementsByTagName) return;
    HIDING_NICE_TITLE = true;
    window.setTimeout("hideNiceTitle2();", 200);
}

function delElement(ele) {
    $(ele).fadeOut("fast", function(){
    	document.getElementsByTagName("body")[0].removeChild(ele);
	});
}

function hideNiceTitle2() {
	if (HIDING_NICE_TITLE && CURRENT_NICE_TITLE) {
        HIDING_NICE_TITLE = false;
        delElement(CURRENT_NICE_TITLE);
        CURRENT_NICE_TITLE = null;
    }
}

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);
}

// 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 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 if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  } else {
	x = (window.Event) ? event.pageX : event.clientX;
	y = (window.Event) ? event.pageY : event.clientY;
  }
  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;
  }
}

