function Tabs(divname) {
    if (!Tabs.Instances) Tabs.Instances = new Array();
    Tabs.Instances[divname] = this;

    this._divNode = document.getElementById(divname);
    if (this._divNode==null) {
	alert("l'élément " + divname + " n'existe pas 'new Tabs("+divname+")");
	return;
    }

    this._ulNode = document.createElement('ul');
    this._ulNode.className = "tabnav";
    this._divNode.appendChild(this._ulNode);

    this._tabs = new Array();
    this._tabActive = null;

    this._urlToReloadTab = "";
    this._paramsToReloadTab = "";
    this._alwaysReload = false;

    this._defaultTitre = "titre";
}
Tabs.prototype.setUrlToReloadTab = function(url) { this._urlToReloadTab = url; }
Tabs.prototype.setParamsToReloadTab = function(params) { this._paramsToReloadTab = params; }
Tabs.prototype.setAlwaysReload = function(bool) { this._alwaysReload = bool; }

/**
 * array(
 *   'index'=>array(
 *     'titre'=>titre de l'onglet en dur
 *     'imgActif'=>titre de l'onglet en tant qu'image (onglet actif)
 *     'imgInactif'=>titre de l'onglet en tant qu'image (onglet inactif)
 *     'color'=>couleur du titre en dur
 *     'background-color'=>couleur de fond de l'onglet
 *     'html'=>contenu de l'onglet
 *   );
 *   ...
 * );
 */
Tabs.prototype.create = function(tabs) {
    if (this._divNode==null) return;
    this._divNode.style.display = "block";

    for (var key in tabs) {
	var tab = new Tab(this, key, tabs[key]);
	this._tabs.push(tab);
	this._ulNode.appendChild(tab.liNode());
	this._divNode.appendChild(tab.divNode());

	tab._divNode.innerHTML.evalScripts();
    }

    this.setActivateIndex(0);
}

Tabs.prototype.remove = function() {
    if (this._divNode==null) return;

    this._tabActive = null;
    for (var i=0;i<this._tabs.length;i++) this._tabs[i].remove();
    this._tabs = new Array();
}

Tabs.prototype.updateTabs = function(tabs) {
    for (var key in tabs) {
	var tab = this.getTabByName(key);
	if (tab==null) continue;
	tab.update(tabs[key]);
    }
}

Tabs.prototype.updateTabHtml = function(res) {
    var tab = this.getTabByName(res['tabname']);
    if (tab==null) {
	alert("l'onglet "+res['tabname']+" n'existe pas");
	return;
    }

    tab.update(res);
}

Tabs.prototype.getTabActive = function() { return this._tabActive; }
Tabs.prototype.getTabnameActive = function() { 
    if (this._tabActive==null) return "";
    return this._tabActive.name(); 
}

Tabs.prototype.getTabByName = function(tabname) {
    for (var i=0;i<this._tabs.length;i++) {
	if (this._tabs[i].name()==tabname) return this._tabs[i];
    }
    return null;
}

Tabs.prototype.setActivateIndex = function(ieme) {
    if (ieme<0 || ieme>=this._tabs.length) return;
    this.setActivateTab(this._tabs[ieme]);
}

Tabs.prototype.setActivateTab = function(tab) {
    if (this._tabActive!=null) this._tabActive.activate(false);
    this._tabActive = null;

    if (this._alwaysReload) {
	if (tabs.reload(tab)) {
	    tab.activate(true);
	    this._tabActive = tab;
	}
    } else {
	tab.activate(true);
	this._tabActive = tab;
    }
}

Tabs.prototype.reload = function(tab) {
    var url = this._urlToReloadTab;
    var parameters = this._paramsToReloadTab+"&tabname="+tab._name;
    
    var res = Ajax.makeRequestSynchroneJson(url, parameters);
    if (res==null) return false;
    
    // Attention, il y a peut-être une redirection sur un autre onglet
    if (res['goto']==null) {
	this.updateTabs(res['tabs']);
	return true;

    } else {
	if (res['messg']!=null) alert(res['messg']);
	this.setActivateTabname(res['goto']);
	return false;
    }
}

Tabs.prototype.setActivateTabname = function(tabname) {
    var tab = this.getTabByName(tabname);
    if (tab==null) return;

    this.setActivateTab(tab);
}

function Tab(tabs, name, vars) {
    this._tabs = tabs;
    this._name = name; // nom de l'onglet
    this._selected = false; // true si cet onglet est l'onglet courant

    this._titre = tabs._defaultTitre;
    if (vars['titre']!=null) this._titre = vars['titre'];
    this._color = vars['color'];
    this._backgroundColor = vars['background-color'];

    this._img = false;
    this._imgActif = vars['imgActif'];
    this._imgInactif = vars['imgInactif'];
    if (this._imgActif!=null && this._imgInactif!=null) this._img = true;

    var html = "";
    if (vars['html']!=null) html = vars['html'];

    this._liNode = document.createElement('li');
    if (this._color!=null) this._liNode.style.color = this._color;
    if (this._backgroundColor!=null) this._liNode.style.backgroundColor = this._backgroundColor;

    this._aNode = document.createElement('a');
    this._aNode.href = "#tabsPage";
    if (!this._img) this._aNode.innerHTML = this._titre;
    else this._aNode.innerHTML = '<img src="'+this._imgInactif+'" />';

    var tab = this;
    this._aNode.onclick = function() {
	tabs.setActivateTab(tab);
	return false;
    }
    this._liNode.appendChild(this._aNode);

    this._divNode = document.createElement('div');
    this._divNode.style.display = "none";

    this._divNode.innerHTML = html;
}

Tab.prototype.name = function() { return this._name; }
Tab.prototype.liNode = function() { return this._liNode; }
Tab.prototype.divNode = function() { return this._divNode; }

Tab.prototype.activate = function(bool) {
    if (bool) {
	this._selected = true;
	this._liNode.className = "active";
	this._divNode.style.display = "block";
	if (this._img) this._aNode.innerHTML = '<img src="'+this._imgActif+'" />';

    } else {
	this._selected = false;
	this._liNode.className = "";
	this._divNode.style.display = "none";
	if (this._img) this._aNode.innerHTML = '<img src="'+this._imgInactif+'" />';
    }
}

Tab.prototype.remove = function() {
    this._liNode.parentNode.removeChild(this._liNode);
    this._divNode.parentNode.removeChild(this._divNode);    
}

Tab.prototype.update = function(vars) {
    if (vars['titre']!=null) {
	this._titre = vars['titre'];
	if (!this._img) this._aNode.innerHTML = vars['titre'];
    }

    if (vars['color']!=null) {
	this._color = vars['color'];
	this._liNode.style.color = this._color;
    }
    if (vars['background-color']!=null) {
	this._backgroundColor = vars['background-color'];
	this._liNode.style.backgroundColor = this._backgroundColor;
    }

    var imgActifChanged = false;
    if (vars['imgActif']!=null && this._imgActif!=vars['imgActif']) {
	this._imgActif = vars['imgActif'];
	imgActifChanged = true;
    }
    var imgInactifChanged = false;
    if (vars['imgInactif']!=null && this._imgInactif!=vars['imgInactif']) {
	this._imgInactif = vars['imgInactif'];
	imgInactifChanged = true;
    }
    if (this._img) {
	if (this._selected && imgActifChanged) this._aNode.innerHTML = '<img src="'+this._imgActif+'" />';
	else if (imgInactifChanged) this._aNode.innerHTML = '<img src="'+this._imgInactif+'" />';
    }

    if (vars['html']!=null) {
	this._divNode.innerHTML = vars['html'];
	vars['html'].evalScripts();
    }
}


