// JavaScript Document
var TabManager = Class.create({	
	/* function to call after the html code of tabs */
	init_tabs: function(tabs){
		this.visible_div=null;
		this.arr_tabs=new Array();
		this.visible_div=null;
		this.active_tab=null;
		this.debug=false;
	
		this.init_arr_tabs(tabs);
		this.init_tab_link();
		this.init_styles();
	},
	
	init_arr_tabs : function (tabs){
		this.arr_tabs = new Array(tabs.length);
		for(var i=0;i<tabs.length;i++)this.arr_tabs[i]=document.getElementById(tabs[i]);
	},
	
	/* init the onlick action on the tabs */
	init_tab_link : function(){
		var i;
		for(i=0;i<this.arr_tabs.length;i++){
			var lien = this.arr_tabs[i];
			Event.observe(lien,'click', this.show.bindAsEventListener(this));
		}
	},
	
	/* init the on over/out style and the active style */
	init_styles : function(){
		var i;
		for(i=0;i<this.arr_tabs.length;i++){
			var _elem = this.arr_tabs[i];
			this._log("init",_elem.id);
			
			//Event.observe(_elem,'mouseover', this.over_tab.bindAsEventListener(this));
			//Event.observe(_elem,'mouseout', this.out_tab.bindAsEventListener(this));
			
			//check if the tab has to be init as visible
			
			var isVisible = _elem.readAttribute("visible");
			if(isVisible=="visible"){
				this._show(_elem);
			}
		}
		
	},

	/* cross brwowser method to get the current class name of an element */
	getClassName : function(elem){
		if(navigator.appName== "Microsoft Internet Explorer")
			var cls = elem.getAttribute("className");
		else
			var cls = elem.getAttribute("class");
		return cls+"";
	},
	
	over_tab : function(event){
		var _elem = Event.findElement(event,"td");
		this._log("over",_elem.id);
		this.changeTabStyle(_elem,"_over","add");
	},
	
	out_tab : function(event){
		var _elem = Event.findElement(event,"td");
		this.changeTabStyle(_elem,"_over","remove");
	},
	
	/* public method called by event handler */
	show : function(event){
		var _elem = Event.findElement(event,"td");
		this._show(_elem);
	},
	
	/* 	tab -> td contenant les different element de la tab
		style : _over, _active , 
		mode : add, remove
	*/
	changeTabStyle : function(tab,style,mode){
		var arrChildren = tab.getElementsByTagName("div");
		var i;
		for(i=0;i<arrChildren.length;i++){
			var child = arrChildren[i];
			if(mode=="remove")var _class = child.className.replace(style,"");
			else var _class = ""+ child.className + style;
			this._log("change_tab_style",_class);
			this.changeStyle(child,_class);
		}
	},
	
	/* change the classname and the id of an element */
	changeStyle : function(elem,new_style){
		elem.className=new_style;
		elem.id=new_style;
	},
	
	_log : function(titre,text){
		if(this.debug){
			var logdiv = $("log");
			logdiv.style.display="block";
			logdiv.innerHTML="----------------------"+titre+"----------------------\n"+text+"\n"+logdiv.innerHTML+"\n";
		}
	},
	
	/* call the required method to show a tab and change the styles */
	
	_show : function(elem){
		var target_name = elem.id;
		target_name = target_name.substr(1,target_name.length);
		target = document.getElementById(target_name);
		if(target==this.visible_div)return;
		/*----------- OLD DIV/TAB -------------------------*/
		if(this.active_tab != null){
			//remove the active style from the previous active tab
			this.changeTabStyle(this.active_tab,"_active","remove");
			//hide the old div
			this.visible_div.style.display ="none";
		}
		/*------------NEW DIV/TAB ------------------------*/
		//show target
		target.style.display = "block";
		//replace the old active tab with the new one
		this.active_tab = elem;
		//change the style of the new active tab
		this.changeTabStyle(this.active_tab,"_over","remove");
		this.changeTabStyle(this.active_tab,"_active","add");
		
		//replace the old visible div with the new one
		this.visible_div = target;
	},
	
	/* get all element of a classname */
	getElementsByClassName : function(className){
		var arr = new Array();
		var elems = document.getElementsByTagName("*");
		for(var i = 0; i < elems.length; i++){
			var elem = elems[i];
			if(navigator.appName== "Microsoft Internet Explorer")
				var cls = elem.getAttribute("className");
			else
				var cls = elem.getAttribute("class");
			cls=cls+"";
			if(cls==className){
				arr[arr.length] = elem;
			}
		}
		return arr;
	}
});

