
// copyright - begin
//
//   Copyright (C) 2005 Espace Courbe inc.
//   You are free to modify and redistribute this code as long
//   as the following message is included:
//
//   Last modified:
//   $Date: 2006/10/05 16:49:02 $
//   $Revision: 1.1 $
//
//   Portions Copyright (C) 2006 Espace Courbe inc.
//   Home Page: http://www.espacecourbe.com/
//
//   Author: Christian Fecteau -- christian.fecteau@espacecourbe.com
//   Creation:  2005/12/06 17:05:02
//
// copyright - end

function ECI_onglets(
	instanceName, // nom de l'instance de ECI_onglets pour des back references
	mainId, // id de l'élément qui contient tout le HTML pour les onglets
	liensId, // id de l'élément qui contient les liens pour les onglets
	defaultTab, // onglet ouvert par défaut (0 = aucun ouvert)
	classeLienDevant, // attribut "class" de l'élément PARENT IMMÉDIAT d'un lien dont l'onglet est montré
	classeLienDerriere, // attribut "class" de l'élément PARENT IMMÉDIAT d'un lien dont l'onglet est caché
	classeOngletDevant, // attribut "class" d'un onglet montré
	classeOngletDerriere // // attribut "class" d'un onglet caché
	// NOTER QUE LES NOMS DE CLASSE DÉJÀ ATTRIBUÉS DANS LE HTML NE SONT PAS ÉCRASÉS PAR LE SCRIPT. 
	// Voir la fonction setClassName
)
{

	if (this.browsers.ie5mac) return;

	this.instanceName = instanceName;
	this.mainId = mainId;
	this.liensId = liensId;
	this.defaultTab = defaultTab;
	this.classeLienDevant = classeLienDevant;
	this.classeLienDerriere = classeLienDerriere;
	this.classeOngletDevant = classeOngletDevant;
	this.classeOngletDerriere = classeOngletDerriere;

	window.ECI_onglets_instanceName = instanceName;
	this.ongletsIds = [];
	this.getAnchorPattern = new RegExp("^.+#(.+)$");

	this.init();
}

ECI_onglets.prototype.browsers = {
	"safari" : /safari/i.test(navigator.userAgent),
	"ie"     : (window.ActiveXObject ? true : false),
	"opera"	 : (window.opera ? true : false),
	"ie5mac" : (!window.showModelessDialog && window.ActiveXObject && document.getElementById)
}

ECI_onglets.prototype.init = function()
{
	if (!document.getElementById || !document.getElementById(this.mainId)) return;
	var a = document.getElementById(this.liensId).getElementsByTagName('a');
	for (var i = 0; i < a.length; i++)
	{
		this.ongletsIds[i] = a[i].href.replace(this.getAnchorPattern,'$1');
		this.setClassName(
			document.getElementById(this.ongletsIds[i]),
			this.classeOngletDerriere,
			this.classeOngletDevant
		);
		this.setClassName(
			a[i].parentNode,
			this.classeLienDerriere,
			this.classeLienDevant
		);
		a[i].onclick = function()
		{
			var instance = eval(window.ECI_onglets_instanceName);
			instance.showTab(this.href.replace(instance.getAnchorPattern,'$1'));
			this.blur();
			return false;
		}
	}
	if (this.defaultTab)
	{
		this.setClassName(
			document.getElementById(this.ongletsIds[this.defaultTab-1]),
			this.classeOngletDevant,
			this.classeOngletDerriere
		);
		this.setClassName(
			a[this.defaultTab-1].parentNode,
			this.classeLienDevant,
			this.classeLienDerriere
		);
	}
}

ECI_onglets.prototype.setClassName = function(el, newValue, oldValue)
{
	if (typeof el.className != 'string') { el.className == ''; }
	var patternOld = new RegExp(oldValue,"g");
	var patternNew = new RegExp(newValue,"g");
	var str = el.className;
	str = str.replace(patternOld,'');
	str = str.replace(patternNew,'');
	str += (' ' + newValue);
	el.className = str;
}

ECI_onglets.prototype.showTab = function(ongletId)
{
	var a = document.getElementById(this.liensId).getElementsByTagName('a');
	for (var i = 0; i < a.length; i++)
	{
		if (this.ongletsIds[i] == ongletId)
		{
			this.setClassName(
				document.getElementById(this.ongletsIds[i]),
				this.classeOngletDevant,
				this.classeOngletDerriere
			);
			this.setClassName(
				a[i].parentNode,
				this.classeLienDevant,
				this.classeLienDerriere
			);
		}
		else
		{
			this.setClassName(
				document.getElementById(this.ongletsIds[i]),
				this.classeOngletDerriere,
				this.classeOngletDevant
			);
			this.setClassName(
				a[i].parentNode,
				this.classeLienDerriere,
				this.classeLienDevant
			);
		}
	}
}



