// thanx to <http://philringnalda.com/blog.js> and <http://www.kryogenix.org/code/browser/nicetitle/>

addEvent (window, "load", makeUrlTitles);

var CURRENT_NICE_TITLE;
var browser = new Browser ();
var loop= 0;

// alert ("browser.isIE= "+browser.isIE+" | browser.version= "+browser.version)


// opera calls the addEvent "load" 25 times ???
function operaLoopsonLoad () {
	loop++;
	alert ("loop= "+loop)
}

function makeUrlTitles () {
	if (!document.links || !document.createElement) return;
	for (var ti= 0;ti<document.links.length;ti++) {
		var lnk = document.links[ti];
		//alert ("links["+ti+"]= "+lnk.tagName.toLowerCase ())
		if (lnk.title) {
			lnk.setAttribute ("urltitle",lnk.title);
			lnk.removeAttribute ("title");
			addEvent (lnk,"mouseover",showUrlTitle);
			addEvent (lnk,"mouseout",hideUrlTitle);
//			addEvent (lnk,"focus",showUrlTitle);
//			addEvent (lnk,"blur",hideUrlTitle);
//			if (lnk.alt) lnk.removeAttribute ("alt");
//			alert ("link.alt= "+lnk.alt)
			lnk.alt=""
		}
	}

/*
	for (var ix= 0;ix<document.images.length;ix++) {
		var lnk = document.images[ix];
		if (lnk.alt) {
			alert ("link.alt= "+lnk.alt)
			lnk.removeAttribute ("alt");				
		}
	}
*/
	return;
}


function showUrlTitle (e) {
	hideUrlTitle (e);
	if (!document.getElementsByTagName) return;
	if (window.event && window.event.srcElement) {
		lnk = window.event.srcElement
	} else if (e && e.target) {
		lnk = e.target
	}
	if (!lnk) return;
	
	if (lnk.nodeType == 3 || (lnk.tagName.toLowerCase () != "a" && lnk.tagName.toLowerCase () != "area") ) {
		// lnk is a textnode -- ascend parents until we hit a link
		lnk = getParent (lnk, "A", "AREA");

	}
	//alert ("urltitle-link= "+lnk.tagName.toLowerCase());
	
	urltitle_text = lnk.getAttribute ("urltitle");
	urltitle_hit = urltitle_text.indexOf("urltitle_hit");
	if (urltitle_hit>-1) {
	}
	if (!lnk) return;
	
	var d = document.createElement ("div");
	d.className = "urltitle";

/*
if ((lnk.href == self.location) || (lnk.href == self.location+"#")) {
		d.innerHTML = '<p class= "titletext">' + urltitle_text +'</p>';
	} else { 
		tmpurl= lnk.href;
		if (tmpurl.length>60) {tmpurl=tmpurl.slice(0, 60)+"...";}
		d.innerHTML = '<p class= "titletext">' + urltitle_text + '</p><p class= "destination">'+tmpurl+'</p>';
	}
*/
	d.innerHTML = '<p class= "titletext">' + urltitle_text +'</p>';


	xy = getMousePosition (e);
	mx = xy[0]; my = xy[1];
	
//	d.style.left = (mx+16) + 'px';
    if (window.innerWidth) {
        d.style.left = (window.innerWidth-740)/2 + 24 +"px";
    } else if (document.body.scrollWidth) {
        d.style.left = (document.body.scrollWidth-740)/2 + 24 +"px";
    }

	d.style.top = (my+16) + 'px';
  	if (document.body.scrollHeight && ((my+160) > document.body.scrollHeight)) {
		d.style.top = (document.body.scrollHeight - 240 - 36) + "px";
    }
    
	document.getElementsByTagName ("body")[0].appendChild (d);
	
	CURRENT_NICE_TITLE = d;
}

function hideUrlTitle (e) {
	if (!document.getElementsByTagName) return;
	if (CURRENT_NICE_TITLE) {
		document.getElementsByTagName ("body")[0].removeChild (CURRENT_NICE_TITLE);
		CURRENT_NICE_TITLE = null;
	}
}

// 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, true);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent ("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}

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

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;
		// x= event.x+document.body.scrollLeft-10;
		// y= event.y+document.body.scrollTop+5;
	}
	if (browser.isNS) {
		x= event.pageX //-10;
		y= event.pageY //+5;
		// x = event.clientX + window.scrollX;
		// y = event.clientY + window.scrollY;
	}
	//alert (x+"|"+y)
	
	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;
	
	// tom: treat opera as nsie for correct mouse positioning
	s = "Opera";
	if ( (i = ua.indexOf (s)) >= 0) {
		this.isNS = true;
		this.version = parseFloat (ua.substr (i + s.length));
		return;
	}
	
	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;
	}
}
