﻿/*
Dictionary Tool Tips - Create tool tips from a dictionary list.
Copyright (c) 2008 Thomas Peri, http://www.tumuski.com/

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The Software shall be used for Good, not Evil.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/**
 * Dictionary Tool Tips - Not yet documented.
 * version 2008-05-14
 */
function dictionaryToolTips(triggerClass, triggerOnClass)
{
	function yankTip(id)
	{
		var anchor, parent, dt, dd, dl, div;
		
		anchor = document.getElementById(id);
		dt = anchor.parentNode;
		parent = dt.parentNode;
		
		dt.removeChild(anchor); // the anchor isn't needed anymore, and can cause css wackiness
		
		dd = dt;
		while ((dd = dd.nextSibling).nodeName != 'DD')
			{ /* next */ }
		
		parent.removeChild(dt);
		parent.removeChild(dd);
		
		dl = document.createElement('dl');
		dl.appendChild(dt);
		dl.appendChild(dd);
		
		return dl;
	}
	
	function replaceTrigger(link)
	{
		function over()
		{
			CSSClass.add(div, triggerOnClass);
		}
		
		function out()
		{
			CSSClass.remove(div, triggerOnClass);
		}

		var div, child;
		
		div = document.createElement('div');
		while (link.firstChild)
		{
			child = link.removeChild(link.firstChild);
			div.appendChild(child);
		}
		CSSClass.set(div, CSSClass.get(link));
		div.onmouseover = over;
		div.onmouseout = out;
		link.parentNode.replaceChild(div, link);
		
		return div;
	}
	
	function main()
	{
		var link, id, tip, div;
		var links = document.body.getElementsByTagName('a');
		for (var i = 0; i < links.length; i++)
		{
			link = links[i];
			if (CSSClass.has(link, triggerClass))
			{
				id = link.href.split('#')[1];
				tip = yankTip(id);
				div = replaceTrigger(link);
				div.appendChild(tip);
			}
		}
	}
	
	main();

}
