// Originally from http://www.jayallen.org

// add an onload event
addEvent(window, "load", makeNiceTitles);


var XHTMLNS = "http://www.w3.org/1999/xhtml";
var CURRENT_NICE_TITLE;
var browser = new Browser();


// include title stylesheet
function includeTitleStyle()
{
	if (document.createElement && document.getElementsByTagName)
	{
		document.write('<link rel="stylesheet" href="/_title.css" type="text/css" />');
	}
} // end includeTitleStyle


function makeNiceTitles()
{
	// return if non capable browser
	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);
		}
	}

	// add nice titles to images
	if(!document.img)
	{
		document.img = document.getElementsByTagName("img");
	}
	for (var ti = 0; ti < document.img.length; ti++)
	{
		var pic = document.img[ti];
		if (pic.alt)
		{
			pic.setAttribute("nicetitle", pic.alt);
			pic.removeAttribute("alt");
			if (pic.title)
			{
				pic.setAttribute("fulltitle", pic.title);
				pic.removeAttribute("title");
			}
			addEvent(pic, "mouseover", showNiceTitle);
			addEvent(pic, "mouseout", hideNiceTitle);
		//	addEvent(pic, "focus", showNiceTitle);
		//	addEvent(pic, "blur", hideNiceTitle);
		}
	}


	// add nicetitle attribute for menu links thus preventing them from
	// having a nicetitle later (see second loop below)
	if ( document.getElementById("menus"))
	{
		var rlinks = document.getElementById("menus").getElementsByTagName("a");
		for (var ti = 0; ti < rlinks.length; ti++)
		{
			document.getElementById("menus").getElementsByTagName("a")[ti].setAttribute("nicetitle", true);
		}
	}


	// add nice titles to links
	if(!document.links)
	{
		document.links = document.getElementsByTagName("a");
	}
	for (var ti = 0; ti < document.links.length; ti++)
	{
		var lnk = document.links[ti];

		// only add nicetitles if link does not already have a nicetitle
		if (!lnk.getAttribute("nicetitle"))
		{
			// if link has a title use that
			if (lnk.title)
			{
				lnk.setAttribute("nicetitle", lnk.title);
				lnk.removeAttribute("title");
			}
			// otherwise use the link text
			else
			{
				lnkTitle = "";
				// stupid workaround for Mozilla displaying entire innerHTML
				// links around images

				// Mozilla
				if (lnk.text)
				{
					lnkTitle = lnk.text
				}
				// IE
				else if (lnk.innerText)
				{
					lnkTitle =  lnk.innerText;
				}
				lnk.setAttribute("nicetitle", lnkTitle);
			}

			// journal thumbnail camera image
			if (lnk.className == "thumb")
			{
				// camera pic
				cameraImg = document.createElementNS(XHTMLNS, "img");
				cameraImg.src = "_camera.gif";
				lnk.appendChild(cameraImg);
			}


			lnk.setAttribute("islink", true);
			addEvent(lnk,"mouseover", showNiceTitle);
			addEvent(lnk,"mouseout", hideNiceTitle);
			//	addEvent(lnk,"focus", showNiceTitle);
			//	addEvent(lnk,"blur", hideNiceTitle);
		}
	}

	// add nicetitles to all other elements in the content section
	var nodeSet = document.getElementById("content").getElementsByTagName("*");
	for (var ti = 0; ti < nodeSet.length; ti++)
	{
		var node = nodeSet[ti];

		// if node has a title use that. note that image and link
		// nicetitle loops above remove title attribute so those nodes
		// will not have a title at this point
		if (node.title)
		{
			node.setAttribute("nicetitle", node.title);
			node.removeAttribute("title");

			addEvent(node,"mouseover", showNiceTitle);
			addEvent(node,"mouseout", hideNiceTitle);
		}
	}


} // end makeNiceTitles


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];
	}
} // end findPosition


function showNiceTitle(e)
{
	if (CURRENT_NICE_TITLE)
	{
		hideNiceTitle(CURRENT_NICE_TITLE);
	}
	// return if incapable browser
	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,"A");
	}
	if (!lnk)
	{
		return;
	}
	nicetitle = lnk.getAttribute("nicetitle");

	// if there's no nicetitle, then nothing to make
	if (!nicetitle)
	{
		return;
	}
	
	fulltitle = lnk.getAttribute("fulltitle");

	// to test if a lnk is a link or an image
	islink = lnk.getAttribute("islink");

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

	// Display the link destination if it is a link
	if (islink)
	{
		lnkHref = lnk.href;

		// test for noscript link
		if (lnkHref.match("noscript"))
		{
			lnkHref = "JavaScript link";
		}
		tnd = document.createTextNode(lnkHref);
		pad = document.createElementNS(XHTMLNS, "p");
		pad.className = "destination";
//		pad.appendChild(tnd);
//		d.appendChild(pad);

//// journal thumbnail links
		// test for thumbnail image link
		if (lnk.className == "thumb")
		{
			photoFile = lnk.href.substring(35);
			imgNode = document.createElementNS(XHTMLNS, "img");
			imgBot = document.createElementNS(XHTMLNS, "p");
			imgBot.appendChild(document.createTextNode("Click for larger image"));

			imgNode.src = "thumb/" + photoFile;
			pad.className = "photo";
			pad.appendChild(imgNode);
			pad.appendChild(imgBot);
		}
		else
		{
			pad.className = "destination";
			pad.appendChild(tnd);
		}
		d.appendChild(pad);
//// /journal thumbnail links
	}

	// Display title if it has one
	if (fulltitle)
	{
		tnd = document.createTextNode(fulltitle);
		pad = document.createElementNS(XHTMLNS, "p");
		pad.className = "fulltitle";
		pad.appendChild(tnd);
		d.appendChild(pad);
	}

	// The maximum width that will be applied
	STD_WIDTH = 300;

	if (islink)
	{
		h = lnk.href.length;
	}
	else if (fulltitle)
	{
		h = fulltitle.length;
	}
	else if (nicetitle)
	{
		h = nicetitle.length;
	}
	if (nicetitle)
	{
		t = nicetitle.length;
	}

	// determine length based on font size
	h_pixels = h * 7.5;
	t_pixels = t * 8.7;

	// if h_pixels is less than STD_WIDTH and greater than t_pixels then use it
	if ((h_pixels < STD_WIDTH) && (h_pixels > t_pixels))
	{
		w = h_pixels;
	}
	// if t_pixels is less than STD_WIDTH and greater than h_pixels then use it
	else if ((t_pixels < STD_WIDTH) && (t_pixels > h_pixels))
	{
		w = t_pixels;
	}
	// otherwise use STD_WIDTH
	else
	{
		w = STD_WIDTH;
	}

	// reduce the width if the width is greater than the available document size
	if (w > document.body.offsetWidth)
	{
		w = document.body.offsetWidth - 25;
	}
	d.style.width = w + 'px';

	/*
	mx = lnk.offsetLeft;
	my = lnk.offsetTop;
	*/

	if (islink)
	{
		// static element based positioning
		mpos = findPosition(lnk);

		// position at the bottom right hand corner of the link
		mx = mpos[0] + lnk.offsetWidth;
		my = mpos[1] + lnk.offsetHeight;

	// position 5% from the top left corner
	//	d.style.left = (mx + (lnk.width/100 * 5)) + 'px';
	//	d.style.top = (my + (lnk.height/100 *5)) + 'px';
	}
	else
	{
		// dynamic mouse based positioning
		xy = getMousePosition(e);

		// position just to the right and below the mouse pointer
		mx = xy[0] + 10;
		my = xy[1] + 10;

	}
	// position the nice title
	d.style.left = mx + 'px';
	d.style.top = my + 'px';

	// handle cases where the text is larger than the available document window size
	// add 25 here because we subtract 25 from the width above
	if (document.body.offsetWidth && ((mx + w + 25) > document.body.offsetWidth))
	{
		d.style.left = (document.body.offsetWidth - 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);

	// test if the nicetitle is off the bottom of the page
	if ((document.documentElement.scrollTop && document.documentElement.clientHeight) && ((my + d.offsetHeight) >= (document.documentElement.scrollTop + document.documentElement.clientHeight)))
	{
		d.style.top = (document.documentElement.scrollTop + document.documentElement.clientHeight) - d.offsetHeight - (islink?lnk.offsetHeight:10) + "px";

		// replace the nicetitle with the new nicetitle
		document.getElementsByTagName("body")[0].replaceChild(d, d);
	}

	CURRENT_NICE_TITLE = d;
} // end showNiceTitle


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


// Add an eventListener to browsers that can do it somehow.
// Originally by 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;
	}
} // end addEvent


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);
	}
} // end getParent


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];
} // end getMousePosition


// 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;
	}
} // end Browser
