
	// Define the Merriam-Webster wordclick package.
	mw				= typeof(mw) != "undefined" ? mw : {};
	mw.wordclick	= {};

	// Enumeration of enabled cookie values.
	mw.wordclick.WORDCLICK_DISABLED = "no";
	mw.wordclick.WORDCLICK_ENABLED	= "yes";

	// Name of the property for storing the enabled configuration value.
	mw.wordclick.PROPERTY_ENABLED = "wordclick.enabled";

	// This method enables/disables the wordclick feature.
	mw.wordclick.enable = function (enabled)
		{	// Set the cookie value.
			this.setCookie(mw.wordclick.PROPERTY_ENABLED, enabled == true ? mw.wordclick.WORDCLICK_ENABLED : mw.wordclick.WORDCLICK_DISABLED, 100000);
		}; 

	// This method returns a cookie value.
	mw.wordclick.getCookie = function (name, def_value)
		{	// There are cookies defined.
			if ( document.cookie.length > 0 )
				{	// Add the prefix.
					name = "mw-wordclick-" + name;
				
					// Get the starting position of the cookie.
					var start = document.cookie.indexOf(name + "=");

					// Found the cookie.
					if ( start != -1 )
						{	// Get the starting position of the cookie value.
							start = start + name.length + 1; 

							// Get the end position of the cookie value.
							var end	= document.cookie.indexOf(";", start);
							end		= end != -1 ? end : document.cookie.length;

							// Return the cookie value.
							return unescape(document.cookie.substring(start, end));
						}; 
				}; 

			// Return the default value.
			return def_value;
		}; 

	// This method gets the scroll coordinates.
	mw.wordclick.getScrollCoords = function ()
		{	// Get the scroll coordinates.
			// Firefox 2, Opera 9, Chrome, Safari 3 (Win), etc.
			if ( typeof(window.pageXOffset) != "undefined" )
				{	sx = window.pageXOffset;
					sy = window.pageYOffset;
				} 

			// IE6, IE8, etc.
			else if ( typeof(document.documentElement.scrollLeft) != "undefined" )
				{	sx = document.documentElement.scrollLeft;
					sy = document.documentElement.scrollTop;
				} // else if ( typeof(document.documentElement.scrollLeft) != "undefined" )

			// ????
			else if ( typeof(document.body.scrollLeft) != "undefined" )
				{	sx = document.body.scrollLeft;
					sy = document.body.scrollTop;
				}; 

			return { x: sx, y: sy };
		}; 

	// This method returns the current text selection.
	mw.wordclick.getSelection = function () 
		{	// The selected value.
			var str = "";

			try
				{	// W3C compliant browsers.
					if ( document.getSelection ) 
						{	str = document.getSelection();
						}

					// Internet Explorer.
					else if ( document.selection) 
						{	// Get the selection range.
							var range = document.selection.createRange();

							// Get the selection.
							str = range ? range.text : str;
						} 

					// Safari.
					else if ( window.getSelection )
						{	str = " " + window.getSelection();
						};
				} // try

			catch ( exception )
				{	
				}; 

			// Preprocess the selected value.
			if ( str ) 
				{	str = str + "";
					str = str.replace(/^\s+|\s+$/, "").replace(/\s+/, " ");
				}; 

			return str;
		}; 

	// This method returns the wordclick URL for a given word.
	mw.wordclick.getWordClickURL = function (word)
		{	// Get the reference base URL.
			var base_url = "";
			
			// No base referenece URL has been defined; therefore, define one now.
			if ( typeof(this.ref_url) == "undefined" )
				{	base_url = this.ref_url = "%word%";
				} 

			// Use the defined base URL.
			else
				{	base_url = this.ref_url;
				};

			return base_url.replace(/%word%/g, encodeURIComponent(word));
		};

	// This method determines whether wordclick is enabled.
	mw.wordclick.isEnabled = function ()
		{	var enabled = this.getCookie(mw.wordclick.PROPERTY_ENABLED, mw.wordclick.WORDCLICK_ENABLED) == mw.wordclick.WORDCLICK_ENABLED;

			return enabled;
		};

	// This method handles all mouse double click events.
	mw.wordclick.onMouseDoubleClick = function (event)
		{	// The target that was clicked.
			var target = false;

			// A word click has been pressed; therefore, process the word click.
			if ( this.isEnabled() && (target = this.wordClickPressed(event)) != false  )
				{	// Get the word to look up.
					var word = this.getSelection();					

					// The wordclick div is configured to load in the same window.
					if ( target.className == "wordclick-no-win" )
						{	document.location = encodeURIComponent(word);
							return false;
						};

					// Get the element that triggered the event.
					target = window.event ? window.event.srcElement : event.target;

					// Set the target that was clicked.
					this.target	= target;
					this.click_x	= window.event ? window.event.clientX : event.clientX;
					this.click_y	= window.event ? window.event.clientY : event.clientY;

					// Get the user agent.
					var ua = navigator.userAgent.toLowerCase();

					// The user is using a version of Safari before 3.0.
					if ( ua.indexOf('safari/4') != -1 )
						{	this.click_x -= window.pageXOffset;
							this.click_y -= window.pageYOffset;
						};

					// Load the word click entry.
					this.showWordClick(word, this.click_x, this.click_y);

					return true;
				};

			return false;
		};

	// This method handles all mouse up event.
	mw.wordclick.onMouseUp = function (event)
		{	return false;
		}; 

	// This method sets a cookie value.
	mw.wordclick.setCookie = function (name, value, expire_days)
		{	// Compute the expiration date.
			var exdate = new Date();
			exdate.setDate(exdate.getDate() + expire_days);

			// Get the domain.
			var domain	= document.location.href;
			domain		= domain.replace(/http:\/\/([^$|\/]+)[^$]*/, "$1");
			domain		= domain.replace(/(.*?)\.([^\.]+\.[^$]+)/, ".$2");

			// Set the cookie value.
			document.cookie = 
				"mw-wordclick-" + name + "=" + escape(value) + 
				((expire_days == null) ? "" : ";expires=" + exdate.toGMTString()) 
				+ ";path=/;domain=" + domain;
		}; 

	// This method sets the reference base URL.
	mw.wordclick.setReferenceBaseURL = function (url)
		{	this.ref_url = url;
		}; 

	// This method shows the wordclick window.
	mw.wordclick.showWordClick = function (word, click_x, click_y)
		{	// document.location = this.getWordClickURL(word);
			window.open(this.getWordClickURL(word), "_blank");

		}; 

	// This method determines whether a wordclick region has been clicked.
	mw.wordclick.wordClickPressed = function (event)
		{	// Get the element that triggered the event.
			var target = window.event ? window.event.srcElement : event.target;

			// Search up the DOM tree for an element with the required CSS class name.
			while ( target && target.parentNode )
				{	// We found a node that meets the criteria; therefore, return success here.
					if ( target.className == "wordclick" || target.className == "wordclick-no-win" )
						{	return target;
						};

					// Get the next node.
					target = target.parentNode;
				};

			// We couldn't find an ancestor DOM node which had the word click requirements defined.
			return false;
		};

	// Use the addEventListener event model.
	if ( document.addEventListener )
		{	document.addEventListener("dblclick", function (event) { mw.wordclick.onMouseDoubleClick(event); }, false);
			document.addEventListener("mouseup", function (event) { mw.wordclick.onMouseUp(event); }, false);								
		}

	// Use the attachEvent event model.
	else if ( document.attachEvent )
		{	document.attachEvent("ondblclick", function (event) { mw.wordclick.onMouseDoubleClick(event); });
			document.attachEvent("onmouseup", function (event) { mw.wordclick.onMouseUp(event); });
		}; 