//
//          thanks to Peter-Paul Koch @ http://www.quirksmode.org/ for its great work
//
//          Ils ont dit : "Tu es devenu fou à cause de Celui que tu aimes."
//          J'ai dit : "La saveur de la vie n'est que pour les fous."
//
//                                                     Yäfi'ï, Raoudh al rayähin

/* gloabal variables */
var menu_id = null;
var menu_timeout = null;
var menu_timeout_length = 1000;

window.onload = initMenu;


function getAktion (e) {
	if (!e) var e = window.event;
	if (e.target) var tg = e.target;
	else if (e.srcElement) var tg = e.srcElement;
	while (tg.nodeType != 1) // Safari GRRRRRRRRRR
		tg = tg.parentNode;
	return tg;
}

function initMenu () {

	// re-initialize all submenu lists
	submenusInit();
	
	// adds an eventListener on first level menu links
	x = document.getElementById('menu').getElementsByTagName('ul')[0].childNodes;
	for (var i=0;i<x.length;i++) {
		if (x[i].nodeType == 1) {
			the_node = x[i].getElementsByTagName('a')[0];
			the_node.onmouseover = display_my_submenu;
			the_node.onmouseout  = hide_my_submenu;
		}
	}

	// adds an eventListener on submenu lists (not on submenu links! on th <ul> itself...)
	x = document.getElementById('menu').getElementsByTagName('ul')[0].childNodes;
	for (var i=0;i<x.length;i++) {
		if (x[i].nodeType == 1) {
			try {
				the_node = x[i].getElementsByTagName('ul')[0];
				the_node.onmouseover = display_me;
				the_node.onmouseout  = hide_me;
			} catch (ex) {}	
		}
	}

	// adds an id on each submenu list
	x = document.getElementById('menu').getElementsByTagName('ul')[0].getElementsByTagName('ul');
	for (var i=0;i<x.length;i++) x[i].id = i;
}
function display_my_submenu (e) {

	// returns the node that was tickeled
	tg = getAktion(e);
	
	// re-initialize all submenu lists
	submenusInit();
			
	try {
		// get the next sibling of the clicked link > the <ul> of the submenu
		x = tg.nextSibling;
		while (x.nodeType != 1)
			x = x.nextSibling;
		if (x.tagName == 'UL') {
			// shows the submenu
			x.style.display = "block";
			// declares that this submenu is the one currentely shown. 
			menu_set_id(x.id);
		}
	} catch (ex) {}
}

function display_me (e) {
	try {
		clearTimeout(menu_timeout);
	} catch (ex) {}
}

function hide_my_submenu (e) {

	// returns the node that was tickeled
	tg = getAktion(e);
			
	try {
		// get the next sibling of the clicked link > the <ul> of the submenu
		x = tg.nextSibling;
		while (x.nodeType != 1)
			x = x.nextSibling;
		if (x.tagName == 'UL') {
			// starts the timeout that will hide the submenu in a few seconds (menu_timeout_length)... 
			menu_timeout = setTimeout("menu_hide_item()", menu_timeout_length);
		}
	} catch (ex) {}
}

function hide_me (e) {
	menu_timeout = setTimeout("menu_hide_item()", menu_timeout_length);
}

function submenusInit() {
	x = document.getElementById('menu').getElementsByTagName('ul')[0].getElementsByTagName('ul');
	for (i=0; i<x.length; i++) {
		x[i].style.display = 'none';
		try {
			clearTimeout(menu_timeout);
		} catch (e) {}
	}	
}
function menu_hide_item() {
	try {
		var node = document.getElementById(menu_id);
		node.style.display = "none";
	} catch (e) {}
	try {
		clearTimeout(menu_timeout);
	} catch (e) {}
	try {
		menu_id = null;
	} catch (e) {}	
}
function menu_set_id(id) {
	menu_id = id;
}

function change_state(id) {
	if (document.getElementById(id).style.display=='block') document.getElementById(id).style.display = 'none';
	else document.getElementById(id).style.display = 'block';
}
