// JavaScript Document
function newNode(NodeType)
{
	return document.createElement(NodeType);	
}

function newLabel(Text)
{
	return document.createTextNode(Text);	
}

//Fonction invoquée lorsqu'on active un onglet
function OnTabClick(event)
{
	this.Tab.Manager.ActivateTab(this.Tab);
}

//Fonction invoquée lorsqu'on survole un objet
function OnTabOver(event)
{
	this.Tab.Over();
}

//Fonction invoquée lorsqu'on quitte un objet
function OnTabOut(event)
{
	if(this.Tab.Manager.ActiveTab != this.Tab)
	{
		this.Tab.Out();
	}
}

//Arrière plan d'un onglet (images)
//Type = 'Color' ou 'Image'
function TabBG(Type, LBg, CBg, RBg, LBgOver, CBgOver, RBgOver)
{
	this.BgType = Type;	
	this.LBackGround = LBg;
	this.LBackGroundOver = LBgOver;
	this.CBackGround = CBg;
	this.CBackGroundOver = CBgOver;
	this.RBackGround = RBg;
	this.RBackGroundOver = RBgOver;
}

//Onglet
function Tab(Text, LinkedControlId, TabBackGround, Height, TextCssClass)
{	
	this.BackGround = TabBackGround;
	this.LinkedControl = document.getElementById(LinkedControlId);
	this.Text = Text;
	this.Left = null;
	this.Center = null;
	this.Right = null;
	
	if(arguments[3])
	{
		this.Height = Height;	
	}
	
	if(arguments[4])
	{
		this.TextCssClass = TextCssClass;	
	}
	
	this.SetManager = function(TabManager)
	{
		this.Manager = TabManager;	
	}
	
	this.CreateDomElem = function()
	{
		var _tab = newNode('table');
		_tab.border = 0;
		_tab.cellPadding = 0;
		_tab.cellSpacing = 0;
		
		var _tr = _tab.insertRow(0);
		_tr.onmouseover = OnTabOver;
		_tr.onmouseout = OnTabOut;
		_tr.onclick = OnTabClick;
		if(this.Height)
		{
			_tr.style.height = this.Height;
		}
		_tr.Tab = this;
		
		this.Left = _tr.insertCell(0);
		if(this.BackGround)
		{
			if(this.BackGround.BgType == 'Color')
			{
				this.Left.style.backgroundColor = this.BackGround.LBackGround;
			}
			else if(this.BackGround.BgType == 'Image')
			{
				this.Left.style.backgroundImage = 'url(' + this.BackGround.LBackGround + ')';
				this.Left.style.backgroundRepeat = 'no-repeat';
			}
		}
		
		this.Left.width = 10;
		
		this.Center = _tr.insertCell(1);
		if(this.BackGround)
		{
			if(this.BackGround.BgType == 'Color')
			{
				this.Center.style.backgroundColor = this.BackGround.CBackGround;
			}
			else if(this.BackGround.BgType == 'Image')
			{
				this.Center.style.backgroundImage = 'url(' + this.BackGround.CBackGround + ')';
				this.Center.style.backgroundRepeat = 'repeat-x';
			}
		}	

		var _span = newNode('span');
		_span.innerHTML = this.Text;
		if(this.TextCssClass)
		{
			if(_span.setAttribute)
			{
				_span.setAttribute('class', this.TextCssClass);	
				_span.setAttribute('className', this.TextCssClass);
			}
		}		
		
		this.Center.appendChild(_span);
		
		this.Right = _tr.insertCell(2);
		if(this.BackGround)
		{
			if(this.BackGround.BgType == 'Color')
			{
				this.Right.style.backgroundColor = this.BackGround.RBackGround;
			}
			else if(this.BackGround.BgType == 'Image')
			{
				this.Right.style.backgroundImage = 'url(' + this.BackGround.RBackGround + ')';
				this.Right.style.backgroundRepeat = 'no-repeat';
			}
		}
		
		this.Right.width = 10;
		
		return _tab;
	}
	
	this.Over = function()
	{
		if(this.BackGround && this.Left)
		{
			if(this.BackGround.BgType == 'Color')
			{
				this.Left.style.backgroundColor = this.BackGround.LBackGroundOver;
			}
			else if(this.BackGround.BgType == 'Image')
			{
				this.Left.style.backgroundImage = 'url(' + this.BackGround.LBackGroundOver + ')';
				this.Left.style.backgroundRepeat = 'no-repeat';
			}
		}
		
		if(this.BackGround && this.Center)
		{
			if(this.BackGround.BgType == 'Color')
			{
				this.Center.style.backgroundColor = this.BackGround.CBackGroundOver;
			}
			else if(this.BackGround.BgType == 'Image')
			{
				this.Center.style.backgroundImage = 'url(' + this.BackGround.CBackGroundOver + ')';
				this.Center.style.backgroundRepeat = 'repeat-x';
			}
		}
		
		if(this.BackGround && this.Right)
		{
			if(this.BackGround.BgType == 'Color')
			{
				this.Right.style.backgroundColor = this.BackGround.RBackGroundOver;
			}
			else if(this.BackGround.BgType == 'Image')
			{
				this.Right.style.backgroundImage = 'url(' + this.BackGround.RBackGroundOver + ')';
				this.Right.style.backgroundRepeat = 'no-repeat';
			}
		}
	}
	
	this.Activate = function()
	{
		this.Over();
		this.LinkedControl.style.display = 'block';
	}
	
	this.Out = function()
	{
		if(this.BackGround && this.Left)
		{
			if(this.BackGround.BgType == 'Color')
			{
				this.Left.style.backgroundColor = this.BackGround.LBackGround;
			}
			else if(this.BackGround.BgType == 'Image')
			{
				this.Left.style.backgroundImage = 'url(' + this.BackGround.LBackGround + ')';
				this.Left.style.backgroundRepeat = 'no-repeat';
			}
		}
		
		if(this.BackGround && this.Center)
		{
			if(this.BackGround.BgType == 'Color')
			{
				this.Center.style.backgroundColor = this.BackGround.CBackGround;
			}
			else if(this.BackGround.BgType == 'Image')
			{
				this.Center.style.backgroundImage = 'url(' + this.BackGround.CBackGround + ')';
				this.Center.style.backgroundRepeat = 'repeat-x';
			}
		}
		
		if(this.BackGround && this.Right)
		{
			if(this.BackGround.BgType == 'Color')
			{
				this.Right.style.backgroundColor = this.BackGround.RBackGround;
			}
			else if(this.BackGround.BgType == 'Image')
			{
				this.Right.style.backgroundImage = 'url(' + this.BackGround.RBackGround + ')';
				this.Right.style.backgroundRepeat = 'no-repeat';
			}
		}
	}
	
	this.Desactivate = function()
	{
		this.Out();
		this.LinkedControl.style.display = 'none';
	}
}

//Gestionnaire des onglets
function TabManager(TabContentPaneId)
{	
	this.TabContentPane = document.getElementById(TabContentPaneId);
	if(!this.TabContentPane)
	{
		throw "Pas de conteneur pour les onglets";	
	}
	
	var _tabs = new Array();
	this.ActiveTab = null;
	
	//Fonction pour afficher tous les onglets
	this.RenderTabs = function(){
		
		var _table = newNode('table');
		_table.border = 0;
		_table.cellPadding = 0;
		_table.cellSpacing = 5;
		
		var _tr = _table.insertRow(0);		
		
		for(i = 0; i < _tabs.length; i++)
		{			
			var _td = _tr.insertCell(i);
			
			//Pour tous les onglets ajoutés au manager
			_td.appendChild(_tabs[i].CreateDomElem());						
		}
		
		//Par défaut, on active le 1er tab
		this.ActivateTab(_tabs[0]);
		
		this.TabContentPane.appendChild(_table);
	}
	
	//Fonction pour activer un onglet
	this.ActivateTab = function(TabToActivate)
	{
		if(TabToActivate)
		{
			if(this.ActiveTab != TabToActivate)
			{
				TabToActivate.Activate();
				this.ActiveTab = TabToActivate;
				for(i = 0; i < _tabs.length; i++)
				{
					if(_tabs[i] != TabToActivate)
					{
						_tabs[i].Desactivate();	
					}
				}
			}
		}
	}
	
	//Fonction pour ajouter un onglet au manager
	this.AddTab = function(TabToAdd)
	{
		if(TabToAdd)
		{
			_tabs[_tabs.length] = TabToAdd;	
			TabToAdd.SetManager(this);
		}
	}
}
