function ddmenu_init() { //{{{

	for (i=0; i<document.getElementsByTagName('div').length; i++) {
		var menubar = document.getElementsByTagName('div')[i];
		if ( menubar.className == 'ddmenu' ) {
			ddmenu_tweakRecurse(menubar,0);
		}
	} //}}}
	
} //}}}

function ddmenu_replaceClassRecurse(element,findstr,replacestr) { //{{{
	
	if ( element.className ) {
		var newClass = element.className.replace(findstr,replacestr);
		if ( element.className != newClass ) {
			element.className = newClass;
		}
	}
	for (var i=0; i<element.childNodes.length; i++) {
		ddmenu_replaceClassRecurse(element.childNodes[i],findstr,replacestr);
	} //}}}
		
} //}}}

function ddmenu_getSiblingPosition(element) { //{{{
	var thisTagName = element.tagName;
	var prevSibs = 0;
	var nextSibs = 0;
	
	// start with this tag, and go sibling by sibling, counting the ones that are the same tagName
	var sibling = element;
	while ( sibling = sibling.previousSibling ) {
		if ( sibling.tagName == thisTagName ) {
			prevSibs++;
		}
	} //}}}
	
	// same thing, but do the siblings after this one
	var sibling = element;
	while ( sibling = sibling.nextSibling ) {
		if ( sibling.tagName == thisTagName ) {
			nextSibs++;
		}
	} //}}}
	
	// return an array with the number of siblings before and after this one
	return( new Array(prevSibs,nextSibs) );
	
} //}}}

function ddmenu_tweakRecurse(element,depth) { //{{{
	tagClass = "";
	
	// Figure out the tag class from the depth and tagName, and set event handlers
	if ( element.tagName == 'DIV' && depth == 0 ) {
		tagClass = "ddmenu-menubar";
	}
	
	if ( element.tagName == 'DIV' && depth == 1 ) {
		tagClass = "ddmenu-menubaritem";
		element.onmouseover = ddmenu_handleMouseOver;
		element.onmouseout = ddmenu_handleMouseOut;
	}

	if ( element.tagName == 'A' && depth == 2 ) {
		tagClass = "ddmenu-menubaritem";
	}
	
	if ( element.tagName == 'UL' && depth == 2 ) {
		tagClass = "ddmenu-menu";
	}

	if ( element.tagName == 'LI' && depth == 3 ) {
		tagClass = "ddmenu-menuitem";
		element.onmouseover = ddmenu_handleMouseOver;
		element.onmouseout = ddmenu_handleMouseOut;
	}

	if ( element.tagName == 'A' && depth == 4 ) {
		tagClass = "ddmenu-menuitem";
	}
	
	
	if ( tagClass != "" ) {

		// Set an id for this tag
		element.id = "ddmenu-" + ddmenu_counter++;
		
		// Set the classes for this tag based on the tagClass
		element.className += " " + tagClass;
		element.className += " " + tagClass + "-menuoveroff";
		element.className += " " + tagClass + "-itemoveroff";
		
		if ( ddmenu_getSiblingPosition(element)[0] == 0 ) {
			element.className += " " + tagClass + "-firstchild";
		}
		
		if ( ddmenu_getSiblingPosition(element)[1] == 0 ) {
			element.className += " " + tagClass + "-lastchild";
		}
	}
	
	// Loop through the kids and do it again for each one
	for (var i=0; i<element.childNodes.length; i++) {
		ddmenu_tweakRecurse(element.childNodes[i],depth+1);
	} //}}}
	
} //}}}

function ddmenu_handleMouseOver() { //{{{
	if ( this.className.indexOf('ddmenu-menubaritem') != -1 ) {
		// trash the timers
		window.clearTimeout(ddmenu_timers[0]);
		window.clearTimeout(ddmenu_timers[1]);
		
		// create .1 second timer for appear
		ddmenu_timers[1] = window.setTimeout('ddmenu_toggleMenu(document.getElementById("'+this.id+'"),"on")',100);
	}
	if ( this.className.indexOf('ddmenu-menuitem') != -1 ) {
		// Turn off fancy menus in Safari. See Ticket #1229 for details.
		if (!Prototype.Browser.WebKit) ddmenu_replaceClassRecurse(this,/-itemoveroff\b/,'-itemover');
	}
} //}}}

function ddmenu_handleMouseOut() { //{{{
	if ( this.className.indexOf('ddmenu-menubaritem') != -1 ) {
		// trash the timers
		window.clearTimeout(ddmenu_timers[0]);
		window.clearTimeout(ddmenu_timers[1]);
		
		// create a one second timer for disappear
		ddmenu_timers[0] = window.setTimeout('ddmenu_toggleMenu(document.getElementById("'+this.id+'"),"off")',1000);
	}
	if ( this.className.indexOf('ddmenu-menuitem') != -1 ) {
		// Turn off fancy menus in Safari. See Ticket #1229 for details.
		if (!Prototype.Browser.WebKit) ddmenu_replaceClassRecurse(this,/-itemover\b/,'-itemoveroff');
	}
} //}}}

function ddmenu_toggleMenu(element,mode) { //{{{
	var divs = document.getElementsByTagName('div');
	for (var i=0;i<divs.length;i++) { //{{{
		
		if ( divs[i].className.indexOf('ddmenu-menubaritem') != -1 ) {
			
			if ( divs[i] === element && mode == 'on' ) {
				ddmenu_replaceClassRecurse(divs[i],/-menuoveroff\b/,'-menuover');
			} else {
				ddmenu_replaceClassRecurse(divs[i],/-menuover\b/,'-menuoveroff');
			}
			
		}
	} //}}}
} //}}}


var ddmenu_timers = new Array();
var ddmenu_counter = 0;
FastInit.addOnLoad(ddmenu_init);

