// Create the wordclick package.
if ( typeof(wordclick) == "undefined" )
	{	wordclick = 
			{	// Constructor for the WordClickButton object class.
				WordClickButton: function (gid, config, write2page)
					{	// Create the content.
						var content = 
							'<div id="wc_box_top" class="header" onclick="var win = window.open(\'/wc_desc.html\', \'wclick_help\', \'menubar=0,toolbar=0,height=500,width=600\'); win.focus(); return false;"></div>' +
							'<div id="wc_box_btm">' + 
								'<input name="wordclick" type="radio" value="on"' + ( wordclick.isEnabled() ? ' checked="true"' : '' ) + ' onclick="wordclick.enable(true); return true;" />On&nbsp;' +
								'<input name="wordclick" type="radio" value="off"' + ( !wordclick.isEnabled() ? ' checked="true"' : '' ) + ' onclick="wordclick.enable(false); return true;" />Off' +
							'</div>';

						// Output the content directly to the page.
						if ( typeof(write2page) != "undefined" && write2page == true )
							{	// Output the button to the page.
								document.write
									(	'<div id="' + gid + '" style="float: right;">' + 
											content +
										'</div>'
									);

								// Get the peer.
								peer = document.getElementById(gid);
							} // if ( typeof(write2page) != "undefined" && write2page == true )

						// Create an instance without writing it directly to the page.
						else
							{	// Create the peer.
								peer = document.createElement("div");

								// Initialize it.
								peer.id			= gid;
								peer.innerHTML	= content;
							}; // else

						return peer;
					}, // class WordClickButton (gid, config, write2page)
				
				// Constructor for the WordClickWindow object class.
				WordClickWindow: function (gid, parent, config)
					{	// Create the peer.
						var peer = document.createElement("div");
				
						// Initialize it.
						peer.id			= gid;
						peer.className	= "WordClickWindow";

						document.onclick = 
							function (event) 
								{	// Get the event.
									var target = window.event ? window.event.srcElement : event.target;

									if ( peer && peer.style.display == "none" )
										{	return target.onclick ? target.onclick() : true;
										}; // if ( peer && peer.style.display == "none" )

									// Get the node.
									var node = target;

									while ( node != null) 
										{		if ( node == peer )
													{	break;
													}; // if ( node == peer )

											node = node.offsetParent;
										}; // while ( node != null) 

									// Hide the peer.
									if ( node == null )
										{	peer.hide();
										}; // if ( node == null )

									// Handle issues with IE 6.
									// if ( window.external &&	typeof(window.XMLHttpRequest) == "undefined" )
									return target.onclick ? target.onclick() : true;
								};

						/**
						// Set up Javascript to detect clicks outside this element.
						document.body.onclick = function(event) 
							{	// Get the event.
								var node = window.event ? window.event.srcElement : event.target

								while ( node != null) 
									{	if ( node == peer )
											{	return false;
											}; 

										node = node.offsetParent;
									}; // while ( node != null) 

								peer.hide();
							}; // document.body.onclick = function(event) 

						// This method closes the window.
						peer.onclick = function(event) 
							{	if ( window.event )
									{	return true;
									}
								else 
									{	event.stopPropagation();
									}; 
							};
						**/

						peer.blur = function (event)
							{	
							}; // function blur ()

						// This method hides the gadget.
						peer.hide = function ()
							{	// Set our visibility.
								this.setVisibility(false);
							}; // function hide ()

						// This method handles all loaded word click content.
						peer.onWordClickContent = function (content)
							{	// Terminate singleton span tags.
								content = content.replace(/<SPAN class=sense_break[ \t]*\/?>/gi, "<span class=\"sense_break\"></span>");
							
								// Set our new content.
								this.innerHTML = 									
									'<div class="border-top"><div class="border-east"><div class="border-middle"></div></div></div>' + 
									'<div class="header">' + 
										'<div class="border-east">' + 
											'<div class="header-middle">' + 
												'<div class="icon-tray">' + 
													/** '<a href="#" onclick="document.getElementById(\'' + this.id + '\').hide(); return false;"><img src="/images/wordclick/back-button.jpg" alt="Back" /></a>' + **/
													'<a href="#" onclick="document.getElementById(\'' + this.id + '\').hide(); return false;"><img src="/images/wordclick/close-button.gif" alt="Close" /></a></div>' + 
												/** '<h1>Word Click</h2>' +	**/
											'</div>' + 
										'</div>' + 
									'</div>' + 
									'<div class="wordclick-content">' + 
										'<div class="border-east">' + 
											'<div id="' + this.id + '_content" class="wordclick-middle">' +
												'<div class="wordclick" onmouseover="this.style.cursor = wordclick.isEnabled() ? \'url(http://www.merriam-webster.com/wordclick.cur), help\' :\'default\';">' + 
													content + 
												'</div>' + 								 
											'</div>' +
										'</div>' + 
									'</div>' + 
									'<div class="border-bottom"><div id="' + this.id + '_direction" class="arrow-left"><div class="border-east"><div class="border-middle"></div></div></div></div>';

								// Get the content.
								content = document.getElementById(this.id + "_content");

								// Get all the links.
								var content_links = content ? content.getElementsByTagName("a") : null;

								// Modify all the links.
								if ( content_links != null )
									{	for ( var index = 0; index < content_links.length; index++ )
											{	// Get the link.
												var word_link = content_links.item(index);

												// No onclick has been set for this link; therefore, assume it is an entry lookup.
												if ( !word_link.onclick && word_link.getElementsByTagName("img").length == 0 )
													{	// Set the onclick.
														word_link.onclick = 
															function () 
																{	// Get the word.
																	var word = this.href.substring(1 + this.href.lastIndexOf("/"));

																	// Load the word click entry.
																	wordclick.loadWordClickEntry(word);

																	return false;
																};
													}; // if ( !word_link.onclick && word_link.getElementsByTagName("img").length == 0 )
											}; // for ( var index = 0; index < content_links.length; index++ )
									}; // if ( content_links != null )

								if ( this.style.display == "none" )
									{	// The scroll coordinates.
										var sx = 0;
										var sy = 0;

										// Get the scroll coordinates.
										// Firefox 2, Opera 9, Chrome, Safari 3 (Win), etc.
										if ( typeof(window.pageXOffset) != "undefined" )
											{	sx = window.pageXOffset;
												sy = window.pageYOffset;
											} // if ( typeof(window.pageXOffset) != "undefined" )

										// 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;
											}; // else if ( typeof(document.body.scrollLeft) != "undefined" )								

										// Set our new coordinates.
										//this.style.left	= Math.max(0, sx + wordclick.clickX - 450) + "px";
										//this.style.top	= Math.max(0, sy + wordclick.clickY - 445) + "px";
										this.style.left	= Math.max(0, sx + wordclick.clickX - 245) + "px";
										this.style.top	= Math.max(0, sy + wordclick.clickY - 455) + "px";

										// Set the corresponding class.
										document.getElementById(this.id + "_direction").className = 
											( (sy + wordclick.clickY) < 450 ) ? "arrow-none" : ((sx + wordclick.clickX - 245) < -12 ? "arrow-right" : "arrow-left");
									}; // if ( this.style.display == "none" )

								// Show the window.
								this.show();
							}; // function onWordClickContent (content)

						// This method sets the gadget's visibility.
						peer.setVisibility = function (visible)
							{	// Set our visibility.
								//this.style.display = ( visible == true ? "block" : "none" );

								// Get our parent.
								var parent = this.parentNode;

								// Remove and then readd the element.
								if ( parent )
									{	// Remove ourself.
										parent.removeChild(this);

										// Add ourself back in.
										parent.appendChild(this);
									}; // if ( parent )

								// Set our display.
								this.style.display = visible == true ? "block" : "none";
							}; // function setVisibility (visible)

						// This method shows the gadget.
						peer.show = function ()
							{	// Set our visibility.
								this.setVisibility(true);
							}; // function show ()

						// Add it to the parent.
						parent.appendChild(peer);

						// Initially hide the peer.
						peer.hide();

						return peer;
					}, // function WordClickWindow (parent, config)

				// Enumeration of enabled cookie values.
				WORDCLICK_DISABLED: "no",
				WORDCLICK_ENABLED:	"yes",

				// Name of the property for storing the enabled configuration value.
				PROPERTY_ENABLED: "wordclick.enabled",

				// The last target that was clicked.
				target: null,

				// This method enables the word click feature.
				enable: function (enabled)
					{	//if ( document.getElementById("wordclickDiv") )
						//	{	document.getElementById("wordclickDiv").style.cursor = value ? "url(http://www.merriam-webster.com/wordclick.cur), help" : "default";
						//	} 

						// Set the cookie value.
						this.setCookie(wordclick.PROPERTY_ENABLED, enabled == true ? wordclick.WORDCLICK_ENABLED : wordclick.WORDCLICK_DISABLED, 100000);

						// Set the word click CSS classes.
						this.replaceCSSClass
							(	document.getElementsByTagName("body").item(0), 
								enabled == true ? 
									"wordclick_disabled" : "wordclick", 
								enabled == true ?
									"wordclick" : "wordclick_disabled",
								true
							);						
					}, // function enable (enabled)

				// This method returns a cookie value.
				getCookie: function (name, def_value)
					{	// There are cookies defined.
						if ( document.cookie.length > 0 )
							{	// 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));
									}; // if ( document.cookie.length > 0 )
							}; // if ( document.cookie.length > 0 )

						// Return the default value.
						return def_value;
					}, // function getCookie (name, def_value)

				// This method returns the current selection.
				getSelection: function () 
					{	// The selected value.
						var str = "";
						
						// W3C compliant browsers.
						if ( top.document.getSelection ) 
							{	str = top.document.getSelection();
							} // if ( top.document.getSelection ) 

						// Internet Explorer.
						else if ( top.document.selection) 
							{	// Get the selection range.
								var range = top.document.selection.createRange();

								// Get the selection.
								str = range ? range.text : str;
							} // else if ( top.document.selection) 

						// Safari.
						else if ( window.getSelection )
							{	str = " " + window.getSelection();
							}; // else if ( window.getSelection )

						// Preprocess the selected value.
						if ( str ) 
							{	// Remove leading non-Word characters.
								var re = new RegExp ("^\\W*", "g");
								str = str.replace(re, '');

								// remove trailing non-Word characters.
								re = new RegExp ("\\W*$", "g");
								str = str.replace (re, "");

								// The above doesn't work on NS 4.05 to remove
								// trailing characters, so do it one by one.
								var lastChar	= str[str.length-1];
								re				= new RegExp ('\\W');

								while ( re.exec(lastChar) ) 
									{	str			= str.substr (0, str.length - 1);
										lastChar	= str[str.length-1];
								    }; // while ( re.exec(lastChar) ) 
							}; // if ( str ) 

						return str;
					}, // function getSelection ()

				// This method initializes the wordclick package.
				init: function ()
					{	// Use the addEventListener event model.
						if ( document.addEventListener )
							{	document.addEventListener("dblclick", function (event) { wordclick.onMouseDoubleClick(event); }, false);
								document.addEventListener("mouseup", function (event) { wordclick.onMouseUp(event); }, false);								
							} // if ( document.addEventListener )

						// Use the attachEvent event model.
						else if ( document.attachEvent )
							{	document.attachEvent("ondblclick", function (event) { wordclick.onMouseDoubleClick(event); });
								document.attachEvent("onmouseup", function (event) { wordclick.onMouseUp(event); });
							}; // else if ( document.attachEvent )

						// Determine whether word click is enabled.
						var enabled = wordclick.isEnabled();

						// Disable or enable word click.
						wordclick.replaceCSSClass
							(	document.getElementsByTagName("body").item(0),
								enabled == true ? 
									"wordclick_disabled" : "wordclick", 
								enabled == true ?
									"wordclick" : "wordclick_disabled",
								true
							); 

						/**
						// Disable/Enable word click on page load.
						window.onload = function () 
							{	// Determine whether word click is enabled.
								var enabled = wordclick.isEnabled();

								// Disable or enable word click.
								wordclick.replaceCSSClass
									(	document.getElementsByTagName("body").item(0),
										enabled == true ? 
											"wordclick_disabled" : "wordclick", 
										enabled == true ?
											"wordclick" : "wordclick_disabled",
										true
									); 
							}; 
							**/
					}, // function init ()

				// This method determines whether word click is enabled.
				isEnabled: function ()
					{	// Set the cookie value.
						var enabled = this.getCookie(wordclick.PROPERTY_ENABLED, wordclick.WORDCLICK_ENABLED) == wordclick.WORDCLICK_ENABLED;

						return enabled;
					}, // function enable (enabled)
	
				// This method loads a word click entry.
				loadWordClickEntry: function (word)
					{	// Get the word click loading frame.
						var wordclick_loader = document.getElementById("wordclick_loader");

						// Create the loading frame for the first time.
						if ( !wordclick_loader )
							{	// Create the loader.
								var wordclick_loader = document.createElement("div");

								// Initialize it.				
								wordclick_loader.style.border	= "0";
								wordclick_loader.style.display	= "block";
								wordclick_loader.style.height	= "0";
								wordclick_loader.style.margin	= "0";
								wordclick_loader.style.padding	= "0";
								wordclick_loader.style.width	= "0";

								// Set its content.
								wordclick_loader.innerHTML = '<iframe name="wordclick_loader" id="wordclick_loader"></iframe>';

								// Add it to the document.
								document.getElementsByTagName("body").item(0).appendChild(wordclick_loader);

								// Get the loader.
								wordclick_loader = document.getElementById("wordclick_loader");

								// Initialize it.				
								wordclick_loader.style.border	= "0";
								wordclick_loader.style.display	= "block";
								wordclick_loader.style.height	= "0";
								wordclick_loader.style.margin	= "0";
								wordclick_loader.style.padding	= "0";
								wordclick_loader.style.width	= "0";
							}; // if ( !wordclick_loader )

						// Load the wordclick content.
						wordclick_loader.src = "/wordclick/" + word;
					}, // function loadWordClickEntry (word)

				// This method handles all onMouseUp events.
				onMouseUp: function (event)
					{	return false;
					}, // function onMouseUp (event)

				// This method handles all ondblclick events.
				onMouseDoubleClick: function (event)
					{	// A word click has been pressed; therefore, process the word click.
						if ( wordclick.isEnabled() && wordclick.wordClickPressed(event) )
							{	// Get the word to look up.
								var word = this.getSelection();

								// Load the word click entry.
								this.loadWordClickEntry(word);

								// Get the element that triggered the event.
								var target = window.event ? window.event.srcElement : event.target

								// Set the target that was clicked.
								wordclick.target	= target;
								wordclick.clickX	= window.event ? window.event.clientX : event.clientX;
								wordclick.clickY	= 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 )
									{	wordclick.clickX -= window.pageXOffset;
										wordclick.clickY -= window.pageYOffset;
									}; // if ( ua.indexOf('safari/') != -1 )

								return true;
							}; // if ( wordclick.isEnabled() && wordclick.wordClickPressed(event) )

						return false;
					}, // function onMouseDoubleClick (event)

				// This method replaces a CSS class in the specified element and all its descendents.
				replaceCSSClass: function (node, src, dest, recursive)
					{	// This is an element node; therefore, replace its CSS class as needed.
						if ( node && node.nodeType == 1 && node.className )
							{	// Get the node's current class name.
								var class_name = " " + node.className;

								// Create the regular expression.
								var reg_exp = new RegExp("(^|[ \t\n]+)" + src + "($|[ \t\n]+)", "g");

								// Replace the class value.
								class_name	= class_name.replace(reg_exp, dest);
								class_name	= class_name.replace(/^\s+|\s+$/g, "");

								// Set the node's new class name.
								node.className = class_name;								
							}; // if ( node && node.nodeType == 1 && node.className )		
							
						// This is an element node and recursion has been defined.
						if ( node && node.nodeType == 1 && recursive == true )
							{	// Apply this to all the node's children.
								for ( var index = 0; index < node.childNodes.length; index++ )
									{	if ( node.childNodes[index].nodeType == 1 )
											{	this.replaceCSSClass(node.childNodes[index], src, dest, recursive);
											}; // if ( node.childNodes[index].nodeType == 1 )
									}; // for ( var index = 0; index < node.childNodes.length; index++ )
							}; // if ( node && node.nodeType == 1 && recursive == true )
					}, // function replaceCSSClass (node, src, dest, recursive)

				// This method sets a cookie value.
				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 = 
							name + "=" + escape(value) + 
							((expire_days == null) ? "" : ";expires=" + exdate.toGMTString()) 
							+ ";path=/;domain=" + domain;
					}, // function setCookie (c_name, value, expire_days)
				
				// This method sets the word click content on the parent page.
				setWordClickContent: function ()
					{	// Get the gadget id.
						var gid = lib.web.getURLParam("gid", "wordclickGadget");

						// Get the gadget.
						var gadget = window.top.document.getElementById(gid);

						// Get the gadget event handler.
						var onWordClickContent = lib.web.getURLParam("callback", "onWordClickContent");
					
						// The gadget exists and has the specified event handler.
						if ( gadget && gadget[onWordClickContent] )
							{	// Get the word click element.
								var wordclick = document.getElementById("wordclick");

								// Get the word click content.
								var content = wordclick ? wordclick.innerHTML : "";

								// Modify the form link values.
								content = content.replace(/var[ \t\n\r]+form[ \t\n\r]*=[ \t\n\r]*document.entry[ \t\n\r]*;/g, "var form = document.getElementById('wordclick_form');");
								content = content.replace(/[ \t\n]+class[ \t\n]*=[ \t\n]*\"lookup art\"/gi, " class=\"lookup art\" onclick=\"window.open('http://www.m-w.com' + this.href.replace(/^http:\\/\\/[^\\/]+/, ''), 'illustration', 'menubar=0,resizable=1,scrollbars=1,toolbar=0,height=600,width=600'); return false;\"");
								content = content.replace(/[ \t\n]+class[ \t\n]*=[ \t\n]*\"lookup table\"/gi, " class=\"lookup table\" onclick=\"window.open('http://www.m-w.com' + this.href.replace(/^http:\\/\\/[^\\/]+/, ''), 'table', 'menubar=0,resizable=1,scrollbars=1,toolbar=0,height=600,width=600'); return false;\"");

								// Invoke the gadget's event handler.
								gadget[onWordClickContent].call(gadget, content);
							}; // if ( gadget && gadget[onWordClickContent] )
					}, // function setWordClickContent ()

				// This method determines whether a word click element has been clicked on.
				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" )
									{	return true;
									}; // if ( target.className == "wordclick" )

								// Get the next node.
								target = target.parentNode;
							}; // while ( target && target.parentNode )

						// We couldn't find an ancestor DOM node which had the word click requirements defined.
						return false;
					} // function wordClickPressed (event)

			}; // package wordclick

		// Initialize the package.
		// wordclick.init();
	}; // if ( typeof(wordclick) == "undefined" )
