// $Id$
String.prototype.trim = function() {
	return this.replace( /^\s+|\s+$/, "" );
}
var utils = new Object();
utils.popup = function(url, name, features) {
	if (!name) name = "_blank";
	if (features) {
		var width = parseInt (features.replace(/.*width=/, ""));
		var height = parseInt (features.replace(/.*height=/, ""));
		var newWindow = window.open(url, name, features);
		if (width && height) try {newWindow.resizeTo (width, height)} catch (e) {};
		try {newWindow.moveTo(0,0)} catch (e) {};
	} else var newWindow = window.open(url, name);
	newWindow.focus();
	return false;
}
utils.updateOpener = function(url) {
	if (window.opener) {
		window.opener.location = url;
		window.opener.focus(); 
	} else {
		newWindow = window.open(url,'main');
		newWindow.focus();
	}
}
utils.removeClassName = function(elem, className) {
	elem.className = elem.className.replace(className, "").trim();
}
utils.addClassName = function(elem, className) {
	utils.removeClassName(elem, className);
	elem.className = (elem.className + " " + className).trim();
}
utils.toggle = function(id) {
	var element = document.getElementById(id);
	if (element.className == "display-none") element.className = "display-block";
	else element.className = "display-none";
}
utils.switchTab = function(tab) {
	var index = parseInt (tab.id.replace(/.*-/,""));
	var prefix = tab.id.replace(/-header-\d*$/,"");
	if (document.getElementById) {
		var i = 1;
		var tab_header = true;
		while (tab_header) {
			tab_header = document.getElementById(prefix + "-header-" + i);
			var tab = document.getElementById(prefix + "-" + i);
			if (tab_header) {
				if (i == index) {
					tab_header.className = "tab-current";
					tab.style.display = "block";
				} else {
					tab_header.className = "";
					tab.style.display = "none";
				}
			}
			i++;
		}
	}
}
utils.folder = function(object) {
	var depliant = object.parentNode;
	if (depliant) {
		if (depliant.className == "fold-off") {
			object.title = "Cliquez pour masquer le pavé";
			depliant.className = "fold-on";
		} else {
			object.title = "Cliquez pour en savoir plus";
			depliant.className = "fold-off";
		}
	}
}
utils.getCookie = function(name) {
	var cookies = document.cookie.split(";");
	var pattern = name + "="
	for (var i = 0; i < cookies.length; i++) {
		var index = cookies[i].indexOf(pattern);
		if (index >= 0) return unescape(cookies[i].substr(index + pattern.length));
	}
	return null;
}
utils.setCookie = function(name, value) {
	var arguments = this.setCookie.arguments;
	var cookie = name + "=" + escape(value);
	if (typeof arguments[2] == "object") cookie = cookie + "; expires=" + arguments[2].toGMTString();
	if (typeof arguments[3] == "string") cookie = cookie + "; path=" + arguments[3];
	if (typeof arguments[4] == "string") cookie = cookie + "; domain=" + arguments[4];
	if (arguments[5]) cookie = cookie + "; secure";
	document.cookie = cookie;
}
utils.navigator = {
	isOpera: function() {
		if (this.opera === undefined) this.opera = (navigator.userAgent.indexOf("Opera") >= 0) ? parseFloat(navigator.appVersion) : 0;
		return this.opera;
	},
	isIE: function() {
		if (this.IE === undefined) {
			this.IE = false;
			if (document.all && !this.isOpera()) this.IE = parseFloat(navigator.appVersion.split("MSIE ")[1]) || 0;
		}
		return this.IE;
	},
	isSafari: function() {
		if (this.safari === undefined) {
			this.safari = false;
			if (Math.max(navigator.appVersion.indexOf("WebKit"), navigator.appVersion.indexOf("Safari"), 0)) {
				this.safari = parseFloat(navigator.appVersion.split("Version/")[1]) || ((parseFloat(navigator.appVersion.substr(idx+7)) >= 419.3 ) ? 3 : 2 ) || 2;
			}
		}
		return this.safari;
	},
	isKhtml: function() {
		if (this.khtml === undefined) this.khtml = (navigator.appVersion.indexOf("Konqueror") >= 0 || this.isSafari()) ? parseFloat(navigator.appVersion) : 0;
		return this.khtml;
	},
	isMozilla: function() {
		if (this.mozilla === undefined) this.mozilla = (navigator.userAgent.indexOf("Gecko") >= 0 && !this.isKhtml()) ? parseFloat(navigator.appVersion) : 0;
		return this.mozilla;
	}
}
utils.getViewPort = function() {
	// get viewport size
	var width = 0, height = 0;
	if (utils.navigator.isMozilla()) {
		// mozilla
		// _window.innerHeight includes the height taken by the scroll bar
		// clientHeight is ideal but has DTD issues:
		// #4539: FF reverses the roles of body.clientHeight/Width and documentElement.clientHeight/Width based on the DTD!
		// check DTD to see whether body or documentElement returns the viewport dimensions using this algorithm:
		var minw, minh, maxw, maxh;
		var dbw = document.body.clientWidth;
		if (dbw > document.documentElement.clientWidth) {
			minw = document.documentElement.clientWidth;
			maxw = dbw;
		} else {
			maxw = document.documentElement.clientWidth;
			minw = dbw;
		}
		var dbh = document.body.clientHeight;
		if (dbh > document.documentElement.clientHeight) {
			minh = document.documentElement.clientHeight;
			maxh = dbh;
		} else {
			maxh = document.documentElement.clientHeight;
			minh = dbh;
		}
		width = (maxw > window.innerWidth) ? minw : maxw;
		height = (maxh > window.innerHeight) ? minh : maxh;
	} else if (!utils.navigator.isOpera() && window.innerWidth) {
		//in opera9, dojo.body().clientWidth should be used, instead
		//of window.innerWidth/document.documentElement.clientWidth
		//so we have to check whether it is opera
		width = window.innerWidth;
		height = window.innerHeight;
	} else if (utils.navigator.isIE() && document.documentElement && document.documentElement.clientHeight) {
		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
	} else if (document.body().clientWidth) {
		// IE5, Opera
		width = document.body().clientWidth;
		height = document.body().clientHeight;
	}
	return {width:width, height:height};
}