function Taber() {
	this.showFirst 		= true;	//	TODO
	this.containers		= new Array();
	this.manyContainers = false;
};

Taber.prototype = {
	init: function() {
		// 	wyszukaj wszystkie 'magazyny' zakładek :)
		var _self = this;
		$('.taber_container').each(function() {
			//	ukryj wszystkie zakładki  w magazynie (również w podmagazynach)
			$(this).find('.taber_box').hide();
			var containerID = this.id;
			_self.containers.push(containerID);
			//alert('found .taber_container ID : '+containerID);
			
			if ( $('#'+containerID+' .taber_menu').length == 1 ) {
				//	jesli jest tylko jedno menu... (nie ma żadnych podmagazynów)
				var menu = $('#'+containerID+' .taber_menu');
			} else {
				// 	jesli jest wiecej niż jedno menu (istnieją podmagazyny zakładek)
				//	wyszukaj menu powiązane z tym magazynem
				_self.manyContainers = true;
				var menu = $('#'+containerID+' .taber_menu[rel="'+containerID+'"]');
			}
			if ( !menu ) 
				return false;
			$(menu).each( function() {
				var ustawionoAktywna = false;
				$(this).find('li a').each(function() {
					var tabID = $(this).attr('href').slice(1);
					if ( !ustawionoAktywna && $(this).hasClass('active') ) {
						$('#tab_'+tabID).fadeIn();
						if ( typeof footer == 'function' ) 
							footer();
					}
					//alert('menu : '+tabID);
					$(this).click(function() {
						//alert('Klik na '+this.id+'\n containerID : '+containerID+' tabID : '+tabID+'\n$(\'#tab_'+tabID+').length:'+$('#tab_'+tabID).length );
						_self.show(this);
						return false;
					});
					$(this).focus(function() { $(this).blur(); });
				});
			});
		});
		
		//	coś pokazać na początku ?
		if ( getUParam('taber') && getUParam('tab') ) {
			var _taber = getUParamCase('taber');
			var _tab = getUParamCase('tab');
			//alert(_tab);
			//alert($('#'+_taber).length);
			var link = $('#'+_taber+' .taber_menu li a[href="#'+_tab+'"]');
			//alert('#'+_taber+' .taber_menu li a[href="#'+_tab+'"]');
			//alert(link);
			_self.show(link,_taber,_tab);
		} else if ( this.showFirst ) {
			//	jesli nie ma nic do pokazania (z parametrow w URL) i jest wlaczone pokazywanie pierwszej zakladki, to pokazujemy :) 
			//	TODO (na razie obejscie z klasami .active w kodzie )  - wziasc je pod uwage przy showFirst
		};
	},
	//	obj 	- obiekt $()   do linku w menu 
	//	container ID - opcjonalnie
	//	tabID - opcjonalnie
	show: function(obj, containerID, tabID) {
		//alert('SHOW()\n\n coninerID : '+containerID+'\btabID : '+tabID);
		if ( !obj || $(obj).hasClass('active') )
			return false;
		var link = obj;
		//	poszukiwanie containera
		if ( !containerID ) {
			while ( obj && !$(obj).hasClass('taber_container') ) {
				obj = $(obj).parent();
			}
			containerID = $(obj).attr('id');
		}
		
		//alert('containerID w show() :'+containerID);
		//containerID = '_taber_TEST';
		$('#'+containerID+' .taber_menu').each(function() {
			if ( !$(this).attr('rel') || 
				( $(this).attr('rel')  && $(this).attr('rel') == containerID  ) ) {
					$(this).find('li a').removeClass('active');
			}
		});
		
		if ( !tabID ) {
			tabID = $(link).attr('href').slice(1);
		}
		$('#'+containerID+' .taber_box').not('#'+containerID+' .taber_container .taber_box').hide();
		$('#tab_'+tabID).fadeIn();
		$(link).addClass('active');
		
		//	jesli jest jakis nadrzeny container / magazyn... otworzyć zakładkę, która zawiera powyższą....
		containerIDold = containerID;
		containerID = false;
		tabID = false;
		
		if ( this.manyContainers ) {
			obj = $(link);
			while ( obj && $(obj).length > 0) {
				if ( $(obj).hasClass('taber_box') ) {
					tabID = $(obj).attr('id').slice(4);
				}
				if ( $(obj).hasClass('taber_container') && $(obj).attr('id') != containerIDold ) {
					containerID = $(obj).attr('id');
				}
				if ( tabID && containerID ) {
					//alert('proba wywolania show... containerID : '+containerID+'\ntabID : '+tabID);
					var pLink = $('#'+containerID+' .taber_menu li a[href="#'+tabID+'"]');
					//alert('link znaleziono : '+ pLink.attr('href'));
					this.show( pLink, containerID, tabID )
					//alert(obj.attr('class'));
					//return false;
					//alert(obj);
					tabID = false;
					containerID = false;
				}
				//obj = $(obj).parent('.taber_box') || $(obj).parent('.taber_container');
				obj = $(obj).parent();
			}
		}
		
		if ( typeof footer == 'function' ) 
			footer();
		return false;
	}
};

if ( typeof taber == 'undefined' ) {
	var taber = new Taber();
}

$(function() {	
	taber.init();
});