addEvent(window, "load", makeNiceTitles);

var XHTMLNS = "http://www.w3.org/1999/xhtml";
var CURRENT_NICE_TITLE;
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.setAttribute("nicetitle",lnk.title);
            lnk.removeAttribute("title");
            addEvent(lnk,"mouseover",showNiceTitle);
            addEvent(lnk,"mouseout",hideNiceTitle);
            addEvent(lnk,"focus",showNiceTitle);
            addEvent(lnk,"blur",hideNiceTitle);
        }
    }
    var instags = document.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);
            addEvent(instag,"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 sL(s){
	var len = s.replace(/[^\x00-\xff]/g,"aa").length/2 + 1;
	return len;
} 

function showNiceTitle(e) {
    if (CURRENT_NICE_TITLE) hideNiceTitle(CURRENT_NICE_TITLE);
    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.nodeName.toUpperCase() != 'A') {
        lnk = getParent(lnk,"A");
    }
    if (!lnk) return;
    nicetitle = lnk.getAttribute("nicetitle");
    
    var d = document.createElementNS(XHTMLNS,"div");
    d.className = "nicetitle";
    
    STD_MAX_WIDTH = 300;
	if(typeof(qad_width) == 'string' && qad_width.constructor==String && qad_width.length>0){
		STD_MAX_WIDTH = parseInt(qad_width) - 25;
	}
	
	STD_MIN_WIDTH = 100;
	if(STD_MAX_WIDTH < 100){
		STD_MIN_WIDTH = STD_MAX_WIDTH;
	}
	
	var maxChars = STD_MAX_WIDTH/15;
    
    var segs = nicetitle.split("<CR/>",5);
    var maxLen = 0;
    var rows = 0;
    for(var i=0; i<segs.length; i++){
    	if("" == segs[i]) continue;
    	var strLen = sL(segs[i]);
    	if(strLen > maxLen) maxLen = strLen;
    	
    	var row = (strLen/maxChars + 1);
    	row = row > 5 ? 5:row;    	
    	rows = rows + row;
    	
    	var seg = segs[i];
    	if(strLen/maxChars > 5){
    		seg = segs[i].substring(0,5*maxChars) + " ...";
    	} 
    	tnt = document.createTextNode(seg);
    	pat = document.createElementNS(XHTMLNS,"p");
    	pat.className = "titletext";
    	pat.appendChild(tnt);
    	if(i==0){
    	 	pat.style.fontSize = "14px";
    	 	pat.style.color = "#006600";
    	 	pat.style.fontWeight = "bold";
    	}else if(i==1){
    		pat.style.fontSize = "12px";
    		pat.style.color = "#663300";
    	}else if(i>=2){
    		pat.style.fontSize = "12px";
    		pat.style.color = "#006600";
    	}
    	d.appendChild(pat);
    }
	
	w = (maxLen*15 > STD_MAX_WIDTH)?STD_MAX_WIDTH:(maxLen*15);
	w = (w < STD_MIN_WIDTH)?STD_MIN_WIDTH:w;
	d.style.width = w + 'px';    

    /*
    mx = lnk.offsetLeft;
    my = lnk.offsetTop;
    */
    mpos = findPosition(lnk);
    //mpos = findPosition(d);
    mx = mpos[0];
    //my = mpos[1];
    xy = getMousePosition(e);
    //mx = xy[0]; 
    my = xy[1];
    
    if( mx > xy[0] || mx + (lnk.offsetRight-lnk.offsetLeft) < xy[0]){
    	mx = xy[0];
    	//alert("mx["+mx+"],xy0["+xy[0]+"]");
    }
    
    //d.style.left = (mx+15) + 'px';
    d.style.left = (mx+5) + 'px';
    //d.style.top = (my+35) + 'px';
    var ch = rows*15 + 20;
    if(typeof(qad_height) == 'string' && qad_height.constructor==String && qad_height.length>0 &&
    	(my + ch) > parseInt(qad_height)){
    	d.style.top = (my+20-ch) + 'px';
    	if(my+20-ch < 0){
    		d.style.display = "none";
    	}
	}else{
		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";
    }
    
    document.getElementsByTagName("body")[0].appendChild(d);
    
    CURRENT_NICE_TITLE = d;
}

function hideNiceTitle(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, 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;
  }
}

