﻿/* Add pop-up definitions to defined terms */
/* Marked up in content with <dfn><a href="#somehinghere?">Defined term</a></dfn> */
/* Appearing in page glossary as definition list with id derived from term */
function toggleTitle(title)
{
	var showReg = new RegExp("Show definition");
	var hideReg = new RegExp("Hide definition");
	if(showReg.test(title)) {
		return title.replace(showReg, "Hide definition");
	} else if(hideReg.test(title)) {
		return title.replace(hideReg, "Show definition");
	}
}
/* Plugin to determine horiz position of an element  */
$.fn.offsetLeft = function() {
 var e = this.get(0);
 if(!e.offsetParent) return e.offsetLeft;
 return e.offsetLeft + $(e.offsetParent).offsetLeft();
 }

$(document).ready(function(){

	 if($.browser.safari || $.browser.opera){return;} // CSS positioning of pop-up buggy in safari, opera
	
//	$("dfn>a").each(function(){							// Use this line instead of next for <dfn> instead of <em class="glossary">
	$("em.glossary>a").addClass("term").each(function(){

		var thisText = $(this).text();
		
		$(this).html(thisText.replace(/[ ]/g,"&nbsp;"));	// Replace spaces in inline term with non-breaking spaces
		
		if($(this).title()){
		var thisText = $(this).title();					// If available use title instead of text to allow links to variations a of term
		}
		else {
		var thisText = $(this).text();
		}
		
		var thisTextClean = thisText.replace(/[^a-z_0-9\-]/gi,"_").toLowerCase();
		
		if(!$("#"+ thisTextClean +"_page").is("dl")){return;}	// Only applies inline definition to terms that have one!
		
		$(this)		
		.title(thisText + " (Show definition)")			// Set title on each link
//		.id("term_" + thisTextClean)					// Set id on each link // Not sure if this is needed?
		.href("#" + thisTextClean + "_definition")		// Set href on each link (maybe this would be set already in the content?)
		.click(function(){								// onclick on linked term
			$("#opendefinition")
			.id("");									// Remove id on previously 'open' dfn
			
			if($(this).offsetLeft() < $("#content").width()/2)
			{
				$(this).parent().removeClass("right");
			} else {
				$(this).parent().addClass("right");
			}
			
			$(this).title(toggleTitle($(this).title()));// Toggle title
			$(this).id("opendefinition")				// Set id on 'open' dfn
			.next().toggle()							// Show/hide generated span
			.parent().toggleClass("show")				// Switch class on dfn
			;
			$(this).parents(".show").find("strong>a").get(0).focus();	// Set focus
			return false; 								// Do nothing onclick
		})
		.after("<span><strong><a href='#'></a></strong> <a href='#opendefinition'>Hide&nbsp;definition</a></span>")	// Add definition elements
		.next().hide()									// Hide added <span>
		.find("strong")
			.prepend("&mdash;").append("&nbsp;")			// Hack to get white space between unstyled elements in IE
		.end()									
		.find("a:first")								// first generated <a>
			.id(thisTextClean + "_definition")			// Set id
			.href("#" + thisTextClean + "_definition")	// Set href
			.append("Definition of " + thisText)		// Set defintion title in first generated <a>
			.click(function(){return false;})			// Do nothing onclick
		.end()
		.find("a:last")									// Second generated <a>
			.click(function(){							// Add onclick
//				var term = $(this).parents("dfn").find("a:first");	// Use this line instead of next for <em class="glossary"> instead of <dfn>
				var term = $(this).parents("em.glossary").find("a:first");	// Toggle title show/hide
				$(term).title(toggleTitle($(term).title()));
				$(this).parent().toggle()				// Show/hide generated span
				.parent().toggleClass("show")			// Switch class on dfn
				.find("a").get(0).focus();				// focus term link
				return false;							// Do nothing onclick
			})
			.before(" " + $("#"+ thisTextClean +"_page p").text() + " ")	// Set inline text to page defintion // Needs option when definition is not in page
			
		.end();
	})
});