/************************************************************/
/* Transmogrifier script (C)2004 Patrick H. Lauke aka redux */
/* Inspiration and challenge provided by Marcello Cerruti   */
/************************************************************/

/* addEvent handler for IE and other browsers */
function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
 if (elm.addEventListener){
   elm.addEventListener(evType, fn, useCapture);
   return true;
 } else if (elm.attachEvent){
   var r = elm.attachEvent("on"+evType, fn);
   return r;
 }
} 

/* this is the main function...it wraps every span with the right class with the javascript link */
function transmogrify() {
	/* get all a elements - warning: this may be slow on big pages with lots of links! */
	elements = document.getElementsByTagName('a');
	/* loop through the array of elements */
	n_elements = elements.length;
	for (i = 0; i < n_elements; i++) {
		/* if the link is of class="menureplace" */
		if (elements[i].className=='menureplace') {
			/* change the className, otherwise it will just loop and loop over the same one again */
			elements[i].className='menureplaced';
			/* get the link's parent */
			var elementParent = elements[i].parentNode;
			/* get the link's next sibling (must be the submenu/UL) */
			var elementSubmenu = elementParent.getElementsByTagName('ul');
			/* create a new unlinked A element */
			var newLink = document.createElement("A");
			/* assign an href attribute to the new A element
			this assumes that javascript-enabled browsers also handle javascript: URLs
			wanted to use addEvent here as well, and have a # as href, but can't add a
			 return false...so the activation of the menu would jump back to start... */
			newLink.href="javascript:trigger('"+elementSubmenu[0].id+"')";
			/* create a new image */
			var newImage = document.createElement("IMG");
			/* assign a proper source and stuff to the new image */
			newImage.src = 'menureplace-collapsed.png';
			newImage.width = 9;
			newImage.height = 9;
			newImage.alt= 'expand menu';
			/* assign a new id to the new image element */
			newImage.id="switcher"+elementSubmenu[0].id;
			/* add the image as a child of the new link */
			newLink.appendChild(newImage);	
			/* insert the new link and image in front of the original link */
			elementParent.insertBefore(newLink,elements[i]);
			
			elementSubmenu[0].style.display='none';
		}
	}
}

function trigger(targetSubmenu) {
	var target = document.getElementById('switcher'+targetSubmenu);
	var targetSubmenu = document.getElementById(targetSubmenu);
	/* kludge: wanted to test src, but IE and Opera expand src to have full domain name in front */
	if (target.getAttribute('alt')=='expand menu') {
		target.setAttribute('src','menureplace-expanded.png');
		target.setAttribute('alt','contract menu');
		targetSubmenu.style.display='block';
	} else {
		target.setAttribute('src','menureplace-collapsed.png');
		target.setAttribute('alt','expand menu');
		targetSubmenu.style.display='none';
	}	
}

/* let's add the transmogrify function to the onload handler of the page */
addEvent (window,'load',transmogrify)