// tabs w oparciu o http://stilbuero.de/tabs/
// autor: Patryk yarpo Jar
// data: 10 lutego 2010 r.
// wymaga jQuery [jquery.com]

// uzycie: $.tabs(id, [interval])
// wymaga zdefiniowania klasy 'anchor' w 

$.tabs = function(containerId, interval) {
			
	var sId       = '#' + containerId,
		iIndex    = 0,
		iInterval = ('number' === typeof interval) ? interval : -1,
		aDivs     = [];

	// zmienia aktualnie wyswietlana zakladke
	function change(that) {

		var re = /([_\-\w]+$)/i,
			param = re.exec(that.href)[1],
			target = $('#' + param);

		if (target.size() == 0) {
			param = '#' + that;
			target = $(param);
		}

		if (target.size() > 0) {
			$(sId + '>div:visible').hide();
			target.show();
			$(sId + '>ul>li>a').removeClass('active');
			$(sId + '>ul>li>a[href="'+ param +'"]').addClass('active');
		}
	}

	// uruchamia czasowy przelacznik zakladek
	function switcher() {

		change(aDivs[iIndex]);
		iIndex = (iIndex+1)%aDivs.length;
		window.setTimeout(arguments.callee, iInterval);
	}

	// inicjalizacja wtyczki
	(function init() {

		$(sId + '>div'). // zlap wszystkie divy bezposrednie dzieci id
			each(function() {
				if ('undefined' !== typeof this.id) {
					aDivs.push(this.id); // pobierz id do tablicy
				}
			}).
			not(':eq(' + iIndex + ')'). // wylacz ze zbioru wyswietlany
			hide(); // schowaj pozostale

		$(sId + '>ul>li>a').click(function() {
			change(this);
			$(this).addClass('active');
			return false;
		});

		if (-1 != iInterval) {
			switcher();
		}
		else if ('undefined' !== typeof aDivs[0]) {
			change(aDivs[0]);
		}
	})();
};
