

/**
 * Main page onload function
 */
function doOnload() {
	// Initial and run sIFR
	if ( typeof sIFR == "function" ) {
		sIFR.replaceElement( 
			"#page #header .titlearea h1", 
			named( { sFlashSrc: "/op/premierevents/championshipseries/worlds/info/06-07/minisite/common/swf/tiki-surf.swf", sWmode: "transparent", sColor: "#000000", sFlashVars: "textalign=center&offsetTop=0" } ) );
		sIFR();
	}
	
	// Initialize the menu
	menuOnLoad( "menu" );
}

// [TJK_SlideMENU] v1.6 Copyright 2006, Thierry Koblentz - TJKDesign.com. all rights reserved. */
// Report bugs or errors to thierry@tjkdesign.com
// Modified by Pokémon USA, Inc.
/**
 * The menu's onload function
 */
function menuOnLoad(menu) {
	// we check first if the browser can handle our script, if it does not then we stop right there
	if (!document.getElementById || !document.getElementsByTagName) {
		return;
	}
	
	// the parameter we pass through the function is assigned to the variable 
	var menuElement = document.getElementById(menu);
	if ( !menuElement ) {
		return;
	}

	var skipId = "skip_1";
	
	// an array that contains all our DTs
	var ddArray = menuElement.getElementsByTagName("dd");
	// an array that contains all our DDs
	var dtArray = menuElement.getElementsByTagName("dt"); 
	// we go through the whole array to do what we have to do
	for (var i = 0; i < dtArray.length; i++) {
		var curDT = dtArray[ i ];
	
		// we create a new DT
		var newDT = document.createElement("dt");
		newDT.className = new String( curDT.className );

		var newAnchor;
		var curDTChild = getFirstChildElement( curDT );
		if ( curDTChild != null && curDTChild.nodeName.toUpperCase() == "A" ) {
			// Use the existing anchor element, if found
			newAnchor = getFirstChildElement( curDT ).cloneNode( true );
		}
		else {
			// we create a A element
			newAnchor = document.createElement( "a" );
			// we wrap the text in the DT into the new anchor	
			newAnchor.appendChild( curDT.firstChild.cloneNode( true ) );
		}
		// we plug the anchor into the new DT
		newDT.appendChild(newAnchor);	
		// we set a href attribute
		newAnchor.href = "#";
		// we set an ID to use it as a named anchor	(to be used with the skip links)
		//	newAnchor.id=skipId;
		//	newAnchor.name=skipId;
		// we set a title	
		newAnchor.title = "Show/hide the sub-menu";
		// we use redundant mechanisms to call the function that will do the job
		newAnchor.onclick = function (event) {
			return menuDoSlide(this, event, menuElement);
		};
		newAnchor.onkeypress = function (event) {
			return menuDoSlide(this, event);
		};
		// we replace the old one with the one we have just created
		dtArray[i].parentNode.replaceChild(newDT, dtArray[i]);
		// we plug a skip link in all DTs but the last one. this skip link contains some static text + a BR element + the text found in the next DT 
		if (i < dtArray.length - 1) {
			// we start by creating an A element
			var newSkipLink = document.createElement("a");
			// now we set some static text
			var startString = "Skip to:";
			// we include a BR element to create a line break after the above static text
			var newBR = document.createElement("br");
			// we fetch the text within the next DT
			var nextSibling = dtArray[i + 1].firstChild;
			var endString;
			if ( nextSibling.nodeName.toUpperCase() == "#TEXT" ) {
				endString = nextSibling.data;
			}
			else if ( nextSibling.nodeName.toUpperCase() == "A" ) {
				endString = nextSibling.title;
			}
			else if ( nextSibling.nodeName.toUpperCase() == "IMG" ) {
				endString = nextSibling.alt;
			}
			// we start to put everything together, first the static text
			newSkipLink.appendChild(document.createTextNode(startString));
			// now the BR element			
			newSkipLink.appendChild(newBR);
			// and now the text inside the next DT			
			newSkipLink.appendChild(document.createTextNode(endString));			
			// we apply a class name to the "skip link" (used to hide/reveal with active/focus)
			newSkipLink.className = "skipper";
			// we set a href attribute so it is possible to "jump" to the next "skip link"		
			newSkipLink.href = "#";
			newSkipLink.id = skipId;	
			// we cannot use the "href" above to navigate to the named anchors because that would feed the history object. We could use replace(), but using focus() is a better approach.
			if (i == dtArray.length - 2) {
				newSkipLink.onclick = function () {
					this.parentNode.parentNode.getElementsByTagName("dd")[dtArray.length - 1].getElementsByTagName("a")[0].focus();
				};
			} else {
				newSkipLink.onclick = function () {
					menuSkipToLink(this.id + "1");
				};
			}
			// 	we call menuRemoveSkipLink() onblur and onfocus to "help" MS IE do the job
			newSkipLink.onblur = function () {
				menuRemoveSkipLink(this);
			};
			newSkipLink.onfocus = function () {
				menuRemoveSkipLink(this);
			};
			dtArray[i].appendChild(newSkipLink);
		}
		// we set the class for all the DTs and the DDS in there
		if ( dtArray[i].className ) {
			dtArray[i].className = dtArray[i].className + " dtopen";
		}
		else {
			dtArray[i].className = "dtopen";
		}
		if ( ddArray[i].className ) {
			ddArray[i].className = ddArray[i].className + " hidedd";
		}
		else {
			ddArray[i].className = "hidedd";
		}
		// we call "menuRevealOnFocus()" on all nested links
		var zAnchors = ddArray[i].getElementsByTagName("a");
		for (var j = 0; j < zAnchors.length; j++) {
			ddArray[i].getElementsByTagName("a")[j].onfocus = function () {
				menuRevealOnFocus(this, menuElement);
			}
		}
		skipId = skipId + "1";
	}
	
	// we create one more anchor to help keyboard users jump back at the very top of the menu
	var newSkipBackLink = document.createElement("a");
	newSkipBackLink.appendChild(document.createTextNode("Skip To:"));
	// now the BR element			
	newSkipBackLink.appendChild(newBR);
	// and now the text inside the next DT			
	newSkipBackLink.appendChild(document.createTextNode("Top of the Menu"));			
	// we apply a class name to the "skip link" (used to hide/reveal with active/focus)
	newSkipBackLink.className = "skipper";
	// we set a href attribute so it is possible to "jump" to the very first anchor in the Menu
	newSkipBackLink.href = "#";
	newSkipBackLink.onclick = function () {
		document.getElementById("skip_1").focus();
	};
	// we call menuRemoveSkipLink() onblur and onfocus to "help" MS IE do the job
	newSkipBackLink.onblur = function () {
		menuRemoveSkipLink(this);
	};
	newSkipBackLink.onfocus = function () {
		menuRemoveSkipLink(this);
	};
	// we plug this new anchor in the DL	
	ddArray[ddArray.length - 1].appendChild(newSkipBackLink);
}

/**
 * Get the first child element of this node that is not a text node
 */
function getFirstChildElement( n ) {
	if ( n == null ) {
		return null;
	}

	var childElement = null;

	for ( i = 0; i < n.childNodes.length; i++ ) {
		if ( n.childNodes[ i ].nodeName.toUpperCase() != "#TEXT" ) {
			childElement = n.childNodes[ i ];
			break;
		}
	}

	return childElement;
}

/**
 * Skip to the link passed into the function
 */
function menuSkipToLink(z_thisSKIP) {
	document.getElementById(z_thisSKIP).focus();
	return false;
}

/**
 * Reset the menu to original state.
 */
function resetMenu(menuElement) {
	if ( !menuElement ) {
		return;
	}
	
	ddArray = menuElement.getElementsByTagName("dd");
	dtArray = menuElement.getElementsByTagName("dt"); 
	
	// we go through the whole array and reset it to original values.
	for (var i = 0; i < dtArray.length; i++) {
		dtArray[i].className = "dtopen";
		ddArray[i].className = "hidedd";
	}
}

/**
 * Show the submenu
 */
function menuDoSlide(zItem, objEvent, menuElement) {
// to take care of "onkeypress": see http://juicystudio.com/article/ecmascriptmenu.php
	var iKeyCode;
    // Check if from a keyboard - non IE, but irrelevant as tab doesn't trigger the keypress event in IE
	if (objEvent && objEvent.type == "keypress") {
		if (objEvent.keyCode) {
			iKeyCode = objEvent.keyCode;
		} else {
			if (objEvent.which) {
				iKeyCode = objEvent.which;
			}
		}
        // If it is not the enter key or space key, pass control back to the browser
		if (iKeyCode != 13 && iKeyCode != 32) {
			return true;
		}
	}
	
	var dtopen, hidedd
	
	dtopen = (zItem.parentNode.className.indexOf( "dtopen" ) >= 0 ) ? true : false;
	
	if (zItem.parentNode.nextSibling.nodeType == 1) {
		hidedd = (zItem.parentNode.nextSibling.className == "hidedd") ? true : false;
	} else {
		hidedd = (zItem.parentNode.nextSibling.nextSibling.className == "hidedd") ? true : false;
	}
	
	// Don't close the menu if it's already open
	if ( zItem.parentNode.className.indexOf( "here" ) >= 0 || zItem.parentNode.className.indexOf( "dtclose" ) >= 0 ) {
		zItem.parentNode.blur();
		return false;
	}
	
	resetMenu(menuElement);

// we do the stuff - swapping classes for the DTs and the DDs
	zItem.parentNode.className = dtopen ? "dtclose here" : "dtopen";
	zItem.parentNode.blur();
// we swap the class of the DDs differently depending on the browser
	if (zItem.parentNode.nextSibling.nodeType == 1) {
// for MSIE
		zItem.parentNode.nextSibling.className = hidedd ? "showdd" : "hidedd";
	} else {
		zItem.parentNode.nextSibling.nextSibling.className = hidedd ? "showdd" : "hidedd";
	}
	
// to make sure the browser does not load the href value
	return false;
}

/**
 * Reveals the menu on focus
 */
function menuRevealOnFocus(theItem, menuElement) {
	resetMenu(menuElement);
	
// we swap DD class to reveal them when the user hits a nested (hidden) link
	theItem.parentNode.parentNode.parentNode.className = "showdd";
// we swap the related DT class (the previous sibling)
	if (theItem.parentNode.parentNode.parentNode.previousSibling.nodeType == 1) {
// for MSIE	
		theItem.parentNode.parentNode.parentNode.previousSibling.className = "dtclose";
	} else {
		theItem.parentNode.parentNode.parentNode.previousSibling.previousSibling.className = "dtclose";
	}
}

/**
 * Removes the skip link from the passed in object
 */
function menuRemoveSkipLink(zObject) {
// this is to avoid an IE bug with the skip links when tabbing backward/forward
	zObject.className = "skipper";
}

