/**
 * TZ-Player
 * @package TZ
 * @subpackage TzPlayer
 */

;(function($) {

	$.fn.tzPlayer = function(config)
	{
		var $this = this;
		var $selector = $(this).selector;
		var $defaults = {
			time: 2000
		};
		this.config = $.extend($defaults, config||{});

		return this.each(function() {

			if (this.nodeName.toUpperCase() != 'UL')
			{
				throw new Error('Selektor nie prowadzi do listy UL!');
			}
			var scope = this;
			scope.working = false;

			$(this).children('li').each(function() {
				
				if ($(this).is(':first-child'))
				{
					$(this).css({zIndex: 1});
					scope.currentVisible = "#" + $(this).attr('id');
				}
				else
				{
					$(this).css({zIndex: 0, 'display': 'none'});
				}

				$('a', this).bind('click', function() {
					if (scope.working)
					{
						return false;
					}
					scope.working = true;
					var href = $(this).attr('href');
					var nextLayer = $(href);
					nextLayer.css({zIndex: 1});
					$(scope.currentVisible, scope).css({zIndex: 0});
					
					nextLayer.fadeIn($this.config.time, function() {
						$(scope.currentVisible, scope).hide();
						scope.currentVisible = href;
						scope.working = false;
					});

					return false;
				});
			});
		});
	}

})(jQuery);

