/*
Function: __functionName__
	__functionDescription__

Parameters:
	__parameterName__	- __parameterDescription__

Dependencies:
	<__functionName__>;

Returns:
	NA

Change Log:
	2006.09.07	LM	- Initial version
	2006.10.23	JEM	- STD_WIDTH now checks to see if overriding value is specified.
	2006.11.16	ALP	-	Modified <findPosition()> to calculate position correctly when the word is the first on a new line in IE. NOTE: This assumes some line heights and spacing
	2006.11.21	ALP	-	Added "+15" to width tests beginning on line 203 to avoid tooltip adding horizontal scrollbar.

Bugs:
	None known. This script was copied from Active Living, al-al201.

To do:
	
*/



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.anchors )
    {
*/
        eArray = document.getElementsByTagName("dfn");
//    }
    for (var ti=0;ti<eArray.length;ti++) {
        var lnk = eArray[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);
        }
    }

//	alert ("1");
    var instags = document.getElementsByTagName("dfn");
    if (instags) {
    for (var ti=0;ti<instags.length;ti++) {
//		alert ("B: " + ti);
        var instag = instags[ti];
//		alert (instag.dateTime + ": is instag.dateTime");
//        if (instag.dateTime) {
//			alert ("fire");
//            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);
//			alert ("hold your fire");
 //       }
    }
    }
}

function findPosition( oLink ) {
// AmandaP/2006.11.16
//NOTE: this depends on the niceTitleLineHeight and niceTitleTopPadding variables in projectVars.js
	if( oLink.offsetParent ) {
		var posX = 0;
		var posY = 0;
		// Check to see if browser is IE and link is the first word on a line
		//alert(oLink.offsetTop + ' ' + oLink.parentNode.offsetTop + ' ' + oLink.offsetParent.offsetTop);
		if (browser.isIE  // if the browser is IE
			&& (oLink.offsetLeft == oLink.parentNode.offsetLeft) // if the text is on the left
			// the text is on line 3 or greater
			&& ((((oLink.offsetTop - oLink.parentNode.offsetTop) > niceTitleTopPadding)) 
					// or the text is on the first or second line
					|| (((oLink.offsetTop - oLink.parentNode.offsetTop) == niceTitleTopPadding)) 
						// and the text is on the left of the page
						&& (oLink.offsetLeft == 0))) {
		// IE calculates the top position as if the text were on the line above, so we need to add some Y
			posY += niceTitleLineHeight;
		}
		for( posX; oLink.offsetParent; oLink = oLink.offsetParent ) {
      posX += oLink.offsetLeft;
      posY += oLink.offsetTop;
    }
		
	/*if( oLink.offsetParent ) {
    for( var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent ) {
      posX += oLink.offsetLeft;
      posY += oLink.offsetTop;
		}*/
// End AmandaP/2006.11.16
// JM/2005.05.09
	if (document.getElementById("content")) {
		posY -= document.getElementById("content").scrollTop;
	}
// end JM/2005.05.09
    return [ posX, posY ];
  } else {
    return [ oLink.x, oLink.y ];
  }
}

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.nodeType == 3) {
        // lnk is a textnode -- ascend parents until we hit a link
        lnk = getParent(lnk,"DFN");
    }
    if (!lnk) return;
    nicetitle = lnk.getAttribute("nicetitle");
//	alert ("lnk "+ lnk);
//    alert ("nicetitle " + nicetitle);
	
    var d = document.createElementNS(XHTMLNS,"div");
	if (lnk.className == "popuptext") {
		d.className = "popuptext";
	} else {
		d.className = "nicetitle";
	}
    tnt = document.createTextNode(nicetitle);
    pat = document.createElementNS(XHTMLNS,"p");
    pat.className = "titletext";
    pat.appendChild(tnt);
    d.appendChild(pat);
/*    if (lnk.href) {
        tnd = document.createTextNode(lnk.href);
        pad = document.createElementNS(XHTMLNS,"p");
        pad.className = "destination";
        pad.appendChild(tnd);
        d.appendChild(pad);
    }
*/    
    STD_WIDTH = (window.STD_WIDTH) ? window.STD_WIDTH : 250; // Added defaulting JasonM/2006.10.23
    if (lnk.href) {
        h = lnk.href.length;
    } else { h = nicetitle.length; }
    if (nicetitle.length) {
      t = nicetitle.length;
    }

    h_pixels = h*6; t_pixels = t*10;
    
    if (h_pixels > STD_WIDTH) {
        w = h_pixels;
    } else if ((STD_WIDTH>t_pixels) && (t_pixels>h_pixels)) {
        w = t_pixels;
    } else if ((STD_WIDTH>t_pixels) && (h_pixels>t_pixels)) {
        w = h_pixels;
    } else {
        w = STD_WIDTH;
    }
	w = STD_WIDTH;
	h = STD_WIDTH;
//	alert ("W: " + w);       
//    d.style.width = w + 'px';    
	d.style.width = STD_WIDTH + 'px';

    /*
    mx = lnk.offsetLeft;
    my = lnk.offsetTop;
    */
    mpos = findPosition(lnk);

	mx = mpos[0];
    my = mpos[1];

    //xy = getMousePosition(e);
    //mx = xy[0]; my = xy[1];
//		alert ("h is " + h);
    d.style.left = (mx+15) + 'px';
    d.style.top = (my+35) + 'px';
//	alert ("W: " + w + ", mx " + mx + ", window.innerWidth " + window.innerWidth + ", window.innerHeight: " + window.innerHeight);
    if (window.innerWidth && ((mx+w+15) > window.innerWidth)) {
//       alert ("if 1 fired");
//		d.style.left = (window.innerWidth - h - 25) + "px";
		d.style.left = (window.innerWidth - h - 25) + "px";
    }
    if (document.body.scrollWidth && ((mx+w+15) > document.body.scrollWidth)) {
//       alert ("if 2 fired");
        d.style.left = (document.body.scrollWidth - h - 25) + "px";
    }
/*    if (window.innerHeight && ((mx+w) > window.innerHeight)) {
        alert ("if 3 fired");
        d.style.left = (window.innerHeight - h - 25) + "px";
    }
    if (document.body.scrollHeight && ((mx+w) > document.body.scrollHeight)) {
        alert ("if 4 fired");
        d.style.left = (document.body.scrollHeight - h - 25) + "px";
    }
*/    

    document.getElementsByTagName("body")[0].appendChild(d);
// JM/2005.05.09
			if (self.innerHeight) { // all except Explorer
				y = self.innerHeight;
			}
			else if (document.documentElement && document.documentElement.clientHeight) {
				// Explorer 6 Strict Mode
				y = document.documentElement.clientHeight;
			}
			else if (document.body) { // other Explorers
				y = document.body.clientHeight;
			}
			if (y <= (d.clientHeight + my + document.getElementById('content').offsetTop - 35)) { // 35 as default offset
				d.style.top = (my - d.clientHeight + 5) + 'px'; // 5 for line height
//				d.style.left = (mx+60) + 'px';
			}

// end JM/2005.05.09

    
    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, true);
    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;
  }
}

