var isDOM = (document.getElementById ? true : false);
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers && window.innerWidth ? true : false);
var debug = true;
var timer;

function getElement(id) {
	if (isDOM) return document.getElementById(id);
	if (isIE4) return document.all[id];
	if (isNS4) return document.layers[id];
}

function getCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i,j) == arg) {
			return getCookieVal (j);
		}
		i = document.cookie.indexOf(" ",i)+1;
		if (i == 0) {
			break;
		}
	}
	return null;
}

function setCookie(name,value) {
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie =	name + "=" + escape(value) +
						((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
						((path == null) ? "" : ("; path=" + path)) +
						((domain == null) ? "" : ("; domain=" + domain)) +
						((secure == true) ? "; secure" : "");
}

function deleteCookie (name) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = getCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

function init() {
	try {

		if (document.getElementsByTagName) {
			//Liens externes.
			var anchors = document.getElementsByTagName("a");
			for (var i=0; i<anchors.length; i++) {
				var anchor = anchors[i];
				if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
					anchor.target = "_blank";
				}
			}
			//Menu principal, sous-menus et boutons de droite.
			var m, div, ul, li, a, base, url, srv, params;
			if (m = document.location.href.match(/^((http:\/\/[^\/]+)[^\?]+)(\?(.+))?$/)) {
				url = m[1];
				base = url.replace(/[^\/]+\.php$/,'');
				srv = m[2]+'/';
				params = m[3];
			}
			ul = document.getElementsByTagName('ul');
			for (var i = 0; i < ul.length; i++) {
				if (m = ul[i].id.match(/^(navigation|boutons)$/)) {
					for (var j = 0; j < ul[i].childNodes.length; j++) {
						if (ul[i].childNodes[j].nodeName.match(/^li$/i)) {
							li = ul[i].childNodes[j];
							for (var k = 0; k < li.childNodes.length; k++) {
								if (li.childNodes[k].nodeName.match(/^a$/i)) {
									a = li.childNodes[k];
									a.onmouseover = menuOver;
									a.onmouseout = menuOut;
									if (a.href.replace(/(\?(.+))$/,'') == url || //La page correspond
										(base == a.href.replace(/[^\/]+\.php(\?(.+))?$/,'') && base != srv && m[1] != 'boutons' && !m[2]) || //La section correspond
										(a.href == url.replace(/-(\w+)\.php/,'.php') && m[2]) //Le sous-menu correspond
									) {
										if (!a.href.match(/\?apercu=(0|1)/)) {
											a.style.backgroundColor = '#bf7411';
											if (m[1] == 'boutons' || m[2]) {
												a.style.color = '#ffffff';
											}
										}
									}
								}
							}
						}
					}
				}
			}
			div = document.getElementById('hidden').getElementsByTagName('div');
			for (var i = 0; i < div.length; i++) {
				if (m = div[i].id.match(/^(items_(\w+))$/)) {
					for (var j = 0; j < div[i].childNodes.length; j++) {
						if (div[i].childNodes[j].nodeName.match(/^a$/i)) {
							a = div[i].childNodes[j];
							a.onmouseover = menuOver;
							a.onmouseout = menuOut;
							if (a.href.replace(/(\?(.+))$/,'') == url || //La page correspond
								(base == a.href.replace(/[^\/]+\.php(\?(.+))?$/,'') && base != srv && m[1] != 'boutons' && !m[2]) || //La section correspond
								(a.href == url.replace(/-(\w+)\.php/,'.php') && m[2]) //Le sous-menu correspond
							) {
								if (!a.href.match(/\?apercu=(0|1)/)) {
									a.style.backgroundColor = '#bf7411';
									if (m[1] == 'boutons' || m[2]) {
										a.style.color = '#ffffff';
									}
								}
							}
						}
					}
				}
			}
		}
	} catch (e) {
		if (debug) alert('function init() : '+e.message);
	}
}

function menuOver() {
	var m;
	if (m = this.id.match(/^items_(\w+)(\d+)$/)) {
		clearTimeout(timer);
	} else if (m = this.id.match(/^(continue|services)$/)) {
		sousMenuAffiche(m[1]);
	}
}
function menuOut() {
	var m;
	if (m = this.id.match(/^(items_)?(continue|services)(\d*)$/)) {
		timer = setTimeout("sousMenuCache('"+m[2]+"')",300);
	}
}

function sousMenuInit() {
	var div, m;
	clearTimeout(timer);
	if (document.getElementsByTagName) {
		var divs = document.getElementsByTagName("div");
		for (var i=0; i<divs.length; i++) {
			var div = divs[i];
			if (m = div.id.match(/^(sousmenu_(\w+))$/)) {
				getElement(m[1]).style.display = 'none';
			}
		}
	}
}

function sousMenuAffiche(id) {
	sousMenuInit();
	try {
		if (getElement('items_'+id).getElementsByTagName('a').length > 0) {
			getElement('sousmenu_'+id).style.display = 'block';
		}
	} catch (e) {
		if (debug) alert('function sousMenuAffiche() : '+e.message);
	}
}
function sousMenuCache(id) {
	try {
		getElement('sousmenu_'+id).style.display = 'none';
	} catch (e) {
		if (debug) alert('function sousMenuCache() : '+e.message);
	}
}


window.onload = init;