/**
 * The following code is origionally from the Neoseeker JavaScript file forums/js/neoformfuncs.js.
 * lord monkey ported it to the wiki's so the wiki's could have their own spoiler tag.
 */

/**
 * Shows or hides the spoiler contents, depending on the current state.
 *
 * @param input_button     The "Show Spoiler" button.
 * @author                 Artificer
 */
function flipshow(input_button) {
	 var show_spoiler       = false,                             // Keeps track of whether we show or hide the spoiler
		  spoiler_root       = find_spoiler_root(input_button),   // The root <div> element of the dynamic spoiler.
		  child_nodes        = spoiler_root.childNodes,           // The child nodes of that <div> element
		  child_node_count   = child_nodes.length;                // The number of child nodes in that <div> element

	 // Change the spoiler button text.
	 if (input_button.value === "Show Spoiler") {
		  input_button.value = "Hide Spoiler";
		  show_spoiler = true;
	 } else {
		  input_button.value = "Show Spoiler";
	 }

	 // Show or hide the actual spoiler contents
	 for (var i = 0; i < child_node_count; ++i) {
		  if (child_nodes[i].className === "spoiler") {
				if (show_spoiler) {
					 child_nodes[i].style.display = "block";
				} else {
					 child_nodes[i].style.display = "none";
				}
				break;
		  }
	 }
}

/**
 * Finds the "root" <div> element of the HTML that displays a spoiler using scripts.
 * Utilized to account for inconsistent DOM models when the spoiler is wrapped in other
 * tags, such as [size=1][/size].
 *
 * @param input_button     The "Show Spoiler" button
 * @author                 Artificer
 */
function find_spoiler_root(input_button) {
	 var parent_node;      // A parent node, cached while enumerating the node hierarchy.
	 parent_node = input_button.parentNode;

	 while (parent_node && parent_node.className !== "spoiler_header") {
		  parent_node = parent_node.parentNode;
	 }

	 return parent_node;
}


function findparent(el,elType,parentNode) {
	if (document.forms.length == 1) {
		return document.forms[0];
	}
	if (el.parentNode && el.parentNode.tagName !='undefined') {

		if (el.parentNode.tagName.toLowerCase() != elType) {
			return  findparent(el.parentNode,elType);
		}
	}
	return el.parentNode;;
}

