// This function outputs a My Dictionary link.
function outputMyDictionaryLink (word, fl, store_in_history)
	{	// Create the new My Dictionary link.
		MyDictionaryLink.create(word, fl, store_in_history);
	}; // function outputMyDictionaryLink (word)

// MyDictionaryLink static object class.
MyDictionaryLink =
	{	// The id of the last link which was created.
		last_id: 1,

		// This method creates a new My Dictionary link.
		create: function (word, fl, store_in_history)
			{	// The gadget id.
				var gid = "mydictionary_link_" + MyDictionaryLink.last_id++;

				// Construct the code.
				var code = 
					'<div id="' + gid + '" class="MyDictionaryLinkGadget"></div>';

				// Output the code.
				document.write(code);	

				// Get the peer.
				var peer = document.getElementById(gid);

				// This method adds slashes to a value.
				peer.addSlashes = function (value)
					{	// Normalize the word.
						value = value.replace(/['"\\]/g, "\\$1");
						value = value.replace(/\n/g, "\\n");

						return value;
					}; // peer.addSlashes = function (value)

				// This method adds a word to a user's My Dictionary list.
				peer.addWord = function (word, fl, token)
					{	// Load all the corresponding scripts.
						this.loadScripts
							(	new Array
									(	SITE_DOMAIN + 'mydict-add.php?hw=' + escape(word) + '&fl=' + escape(fl) + '&gid=' + escape(this.id) + '&event.onSuccess=onWordAdded&event.onFailure=onWordAddFailure&token=' + escape(token)
									)
							); 	

						return false;
					}; // function addWord (word, token)

				// This method checks a specified word.
				peer.checkWord = function (word, fl, store_in_history)
					{	// The scripts to be loaded.
						var script_urls = 
							new Array
									(	SITE_DOMAIN + 'mydict-contains.php?hw=' + escape(word) + '&fl=' + escape(fl) + '&gid=' + escape(this.id) + '&callback=onMyDictionaryContains'										
									);

						// Store the lookup in the user's search history.
						if ( typeof(store_in_history) == "undefined" || store_in_history == true )
							{	script_urls[script_urls.length] = SITE_DOMAIN + 'mydict-add-lookup.php?hw=' + escape(word) + '&fl=' + escape(fl);
							}; // if ( typeof(store_in_history) == "undefined" || store_in_history == true )

						// Load all the corresponding scripts.
						this.loadScripts(script_urls);
					}; // function checkWord (word, fl, store_in_history)

				// This method loads a series of scripts.
				peer.loadScripts = function (script_urls)
					{	// Load all the scripts.
						for ( var index = 0; index < script_urls.length; index++ )
							{	// Create new script tags.
								var script = document.createElement("script");								

								// Initialize it.
								script.src	= script_urls[index];

								// Add it.
								this.appendChild(script);
							}; // for ( var index = 0; index < script_urls.length; index++ )
					}; // function loadScripts (script_urls)

				// This method handles all contained in My Dictionary events.
				peer.onMyDictionaryContains = function (word, fl, found, props)
					{	// Get various properties.
						var logged_in	= typeof(props.logged_in) != "undefined" && props.logged_in;
						var token		= typeof(props.token) != "undefined" ? props.token : "";
					
						// The word is already in the user's My Dictionary list.
						if ( found == true )
							{	this.innerHTML = '<div id="' + this.id + '_button" class="word_found" onmouseover="document.getElementById(\'' + this.id + '_bubble\').style.display = \'block\';\" onmouseout="document.getElementById(\'' + this.id + '_bubble\').style.display = \'none\';"></div>';
							} // if ( found == true )

						// The word isn't already in the user's My Dictionary list.
						else
							{	this.innerHTML = '<div id="' + this.id + '_button" class="word_not_found" onmouseover="document.getElementById(\'' + this.id + '_bubble\').style.display = \'block\';\" onmouseout="document.getElementById(\'' + this.id + '_bubble\').style.display = \'none\';" onclick="this.app.addWord(this.word, this.fl, this.token); return false;"></div>';
							}; // else

						// Grab the button from the DOM.
						var button = document.getElementById(this.id + "_button");

						// Set its values.
						button.app		= this;
						button.word		= word;
						button.fl		= fl;
						button.token	= token;						

						// Get the bubble class.
						var bubble_class = logged_in ? ( found ? "found" : "not_found" ) : "signin";

						// Create the popup bubble.
						var bubble = document.createElement("div");

						// Initialize it.
						bubble.id			= this.id + "_bubble";
						bubble.className	= "bubble";
						bubble.innerHTML	= '<div class="view_' + bubble_class + '"></div>';

						// Add the bubble.
						this.appendChild(bubble);
					}; // function onMyDictionaryContains (word, fl, found, props)		

				// This method handles all My Dictionary word addition events.
				peer.onWordAdded = function (ret_code, word)
					{	// Add the added content.
						this.innerHTML = 
							this.innerHTML = '<div class="word_added" onmouseover="document.getElementById(\'' + this.id + '_bubble\').style.display = \'block\';\" onmouseout="document.getElementById(\'' + this.id + '_bubble\').style.display = \'none\';"></div>';

						// Get the bubble class.
						var bubble_class = "added";

						// Add the popup bubble.
						this.innerHTML += '<div id="' + this.id + '_bubble" class="bubble"><div class="view_' + bubble_class + '"></div></div>';
					}; // function onWordAdded (ret_code, word)

				// This method handles all My Dictionary word addition error events.
				peer.onWordAddFailure = function (ret_code, word)
					{	// Add the added content.
						this.innerHTML = '<div class="word_add_failure" onmouseover="document.getElementById(\'' + this.id + '_bubble\').style.display = \'block\';\" onmouseout="document.getElementById(\'' + this.id + '_bubble\').style.display = \'none\';"></div>';

						// Get the bubble class.
						var bubble_class = "add_failure";

						// Add the popup bubble.
						this.innerHTML += '<div id="' + this.id + '_bubble" class="bubble"><div class="view_' + bubble_class + '"></div></div>';
					}; // function onWordAddFailure (ret_code, word)

				// Check the initial word.
				peer.checkWord(word, fl, store_in_history);

				return peer;
			} // function create (word, fl, store_in_history)
			
	}; // static class MyDictionaryLink