var tab;
var Tab;
Tab = new Class({
	target: 'tab',
	content_in_tab: null,
	current_tab: null,
	filter: null,

	initialize: function(target_area){

		var _this = this;
		if(target_area == undefined){
			_this.target = $(_this.target);
		}
		else{
			_this.target = $(target_area);
		}
		
		//	Get list of element
		var elements = _this.target.getElements('*[rel=tab]');
		_this.content_in_tab = new Object();
		elements.each(function(element) {
			//	Get current element and set tab with(out) content
			var element_id = element.get('id')

			if(element.hasClass('selected')){
				_this.current_tab = element_id;
				_this.content_in_tab[element_id] = true;
			}
			else
				_this.content_in_tab[element_id] = false;
			
			//	Change event on click
			element.addEvent('click', function(e) {
				e.stop();
				//	Start the process
				_this.switch_tab(element);
			});
		});
		
		_this.filter = new Object();
	},
	
	
	/**
	 *	Switch to selected tab
	 *
	 *
	 */
	switch_tab: function(target){
		var _this = this;
		
		var target_id = target.get('id');
		//	If there is allready a content just show tab
		if(_this.content_in_tab[target_id] == true){
/* console.log('switch contenu deja charge'); */
			//	Hide current element
			$(_this.current_tab).removeClass('selected');
			$('tab_'+_this.current_tab+'_content').removeClass('tab_visible');
			$('tab_'+_this.current_tab+'_content').addClass('tab_hidden');

			//	Show target element
			target.addClass('selected');
			$('tab_'+target_id+'_content').addClass('tab_visible');
			$('tab_'+target_id+'_content').removeClass('tab_hidden');
			_this.current_tab = target_id;
		}
		
		//	Else load content
		else{
/* console.log('switch nouveau contenu'); */
			//	Get link href
			target.set('href', target.getElement('a').get('href'));
			//	Get current size
			var current_size = $('tab_'+_this.current_tab+'_content').getParent('div').getSize();

			//	Update target size
			$('tab_'+target_id+'_content').setStyles({
				'width': current_size.x+'px',
				'height': current_size.y+'px'
			});

			//	Hide current element
			$(_this.current_tab).removeClass('selected');
			$('tab_'+_this.current_tab+'_content').removeClass('tab_visible');
			$('tab_'+_this.current_tab+'_content').addClass('tab_hidden');

			//	Show target element
			target.addClass('selected');
			var target_id = target.get('id');
			$('tab_'+target_id+'_content').addClass('tab_visible');
			$('tab_'+target_id+'_content').removeClass('tab_hidden');
			_this.current_tab = target_id;

			_this.filter['tab_'+target_id+'_content'].element_click(target);
			
			_this.content_in_tab[target_id] = true;
		}
		target.addClass('tab_visible')
	},
	
	addRef: function(obj_name, ref_obj){
		var _this = this;
		_this.filter[obj_name] = ref_obj;
	},

	end: function(){
	
	}
});
