(function ($) {
	$.fn.fadeTransition = function(options) {

		var options = $.extend({pauseTime: 6500, transitionTime: 1000}, options);
		var transitionObject;

		Trans = function(obj) {
			var timer = null;
			var current = 0;
			var els = $("> *", obj).css("display", "none").css("left", "0").css("top", "0").css("position", "absolute");
			$(obj).css("position", "relative");
			$(els[current]).css("display", "block");

			function transition(next) {
				$(els[current]).fadeOut(options.transitionTime);
				$(els[next]).fadeIn(options.transitionTime);
				current = next;
				cue();
			};

			function cue() {
				if ($("> *", obj).length < 2) return false;
				if (timer) clearTimeout(timer);
				timer = setTimeout(function() { transition((current + 1) % els.length | 0)} , options.pauseTime);
			};
			
			this.showItem = function(item) {
				if (timer) clearTimeout(timer);
				transition(item);
			};
			
			this.stopItem = function(item) {
				if (timer) clearTimeout(timer);
			};
			
			this.playItem = function(item) {
				cue();
			};

			cue();
		}

		this.showItem = function(item) {
			transitionObject.showItem(item);
		};
		
		this.stopItem = function(item) {
			transitionObject.stopItem(item);
		};
		
		this.playItem = function(item) {
			transitionObject.playItem(item);
		};

		return this.each(function() {
			transitionObject = new Trans(this);
		});
	}

})(jQuery);

var page = {
	tr: null,
	init: function() {
		page.tr = $("#banner").fadeTransition({pauseTime: 6500, transitionTime: 1000});
		$("div.banner_nawigacja").each(function() {
			$(this).children().each( function(idx) {
				if ($(this).is("a"))
					$(this).click(function() { page.tr.showItem(idx); })
			});
		});
		
		$("#banner").each(function() {
			$(this).children().each( function(idx) {
				if ($(this).is("div"))
					$(this).mouseover(function() { page.tr.stopItem(idx); })
					$(this).mouseout(function() { page.tr.playItem(idx); })
			});
		});
	},

	show: function(idx) {
		if (page.tr.timer) clearTimeout(page.tr.timer);
		page.tr.showItem(idx);
	}
};

$(document).ready(page.init); 