﻿$(document).ready(function()
{

	function checkmenu(menu)
	{
		var ul = $(menu);
		ul.each(function()
		{
			var width = $(this).width();
			var itemsWidth = 40;
			$(this).children('li').each(function() { itemsWidth += $(this).width() + 5; });
			if (itemsWidth > width)
			{
				while (itemsWidth > width && $(this).children('li:not(.item-extended):last').size() > 0)
				{
					var item = $(this).children('li:not(.item-extended):last');
					item.addClass('item-extended');
					itemsWidth -= item.width() - 5;
				}
				if ($(this).children('.item-extended').size() > 1)
				{
				    $(this).children('.jq-menu-more').show('');
				}
				if ($(this).children('.aktywne-menu.item-extended').size() > 0)
				{
				    scrollMenuRight($(this).children('.jq-menu-more'));
				}
			}
			$(this).addClass('jq-checked');
		});
	}

	function scrollMenuRight(btn)
	{
		var extendedItems = btn.siblings('.item-extended');
		var width = 40;
		var extItemsWidth = 0;
		extendedItems.each(function() { extItemsWidth += $(this).width() + 5; });
		while (extItemsWidth > width && btn.siblings('li:visible:first').size() > 0)
		{
			var item = btn.siblings('li:visible:first');
			item.hide();
			width += item.width() + 5;
		}

		extendedItems.show();
		if (extendedItems.size() > 0)
		{
			btn.siblings('.jq-menu-less').show();
		}
		btn.hide();
	}

	function scrollMenuLeft(btn)
	{
		btn.parent().children('li:not(.item-extended)').show();
		btn.hide().parent().children('li.item-extended').hide();
		if (btn.parent().children('.item-extended').size() > 1)
		{
			btn.parent().children('.jq-menu-more').show();
		}
	}

	checkmenu('div.menu-glowne ul.poziom2:visible:not(.jq-checked)');
	checkmenu('div.menu-glowne ul.poziom3:visible:not(.jq-checked)');

	$('div.menu-glowne ul.poziom1 > li').hover(function() { checkmenu('div.menu-glowne ul.poziom2:visible:not(.jq-checked)'); });
	$('div.menu-glowne ul.poziom2 > li').hover(function() { checkmenu('div.menu-glowne ul.poziom3:visible:not(.jq-checked)'); });

	$('div.menu-glowne ul.poziom2 li.jq-menu-more').hover(function() { scrollMenuRight($(this)); });
	$('div.menu-glowne ul.poziom3 li.jq-menu-more').hover(function() { scrollMenuRight($(this)); });

	$('div.menu-glowne ul.poziom2 .jq-menu-less').hover(function() { scrollMenuLeft($(this)); });
	$('div.menu-glowne ul.poziom3 .jq-menu-less').hover(function() { scrollMenuLeft($(this)); });

	// rozmiar tekstu
	$('.rozmiar a.small').click(function()
	{
		var elements = $('.jq-resizes *');
		elements.removeClass('font-x').removeClass('font-xx');
		return false;
	});

	$('.rozmiar a.medium').click(function()
	{
		var elements = $('.jq-resizes *');
		elements.addClass('font-x').removeClass('font-xx');
		return false;
	});

	$('.rozmiar a.big').click(function()
	{
		var elements = $('.jq-resizes *');
		elements.removeClass('font-x').addClass('font-xx');
		return false;
	});

	if ($('.jq-blok-formularz .komunikat').size() > 0)
	{
		$('.jq-show-when-message').show();
	}

	if ($('span.jq-captcha-error').size() > 0)
	{
		$('<span class="error">Niepoprawny kod</span>').insertAfter('.contact-block .df-captha');
		$('.contact-block .df-captha input').css({ borderColor: "#f00" });
	}

	// poprawka atrybutów nadawanych przez FCK
	$('.editable-content img').each(function()
	{

		var vspace = $(this).attr('vspace');
		if (vspace > 0)
		{
			$(this).attr('vspace', 0);

			$(this).css('margin-top', vspace + 'px');
			$(this).css('margin-bottom', vspace + 'px');
		}

		var hspace = $(this).attr('hspace');
		if (hspace > 0)
		{
			$(this).attr('hspace', 0);

			$(this).css('margin-left', hspace + 'px');
			$(this).css('margin-right', hspace + 'px');
		}
	});

	// faq, czli rozwijanie i zwijanie zawartości niżej
	$('.jq-faq').click(function()
	{
		if ($(this).next().is(':visible')) $(this).next().hide();
		else
		{
			$(this).next().show();
			$(this).next().children().show();
			$(this).next().children().children().show();
		}
		return false;
	});

	//informacje i galerie przewijane    
	function showPager(parent)
	{
		var count = $(parent).find('.jq-tab').size();
		var index = $(parent).find('.jq-tab:visible').prevAll().size() + 1;
		$(parent).find('.jq-pager').html("<strong>" + index + "</strong> / " + count);
	}

	function makePagedGallery(parent)
	{
		$(parent + ' .jq-tab:first-child').show();
		showPager(parent);

		$(parent + ' .next-jq').click(function()
		{
			var next = $(parent + ' .jq-tab:visible').next();
			$(parent + ' .jq-tab').hide();
			if (next.size() > 0)
				next.show();
			else
				$(parent + ' .jq-tab:first-child').show();
			showPager(parent);
			return false;
		});

		$(parent + ' .prev-jq').click(function()
		{
			var prev = $(parent + ' .jq-tab:visible').prev();
			$(parent + ' .jq-tab').hide();
			if (prev.size() > 0)
				prev.show();
			else
				$(parent + ' .jq-tab:last-child').show();
			showPager(parent);
			return false;
		});
	}

	makePagedGallery('#aktualnosci-tabs');
	makePagedGallery('#ogloszenia-tabs');
	makePagedGallery('#galeria1-tabs');
	makePagedGallery('#galeria2-tabs');
	makePagedGallery('#galeriavideo1-tabs');
	makePagedGallery('#galeriavideo2-tabs');
	makePagedGallery('#galeriauser1-tabs');
	makePagedGallery('#galeriauser2-tabs');

	// rotator
	var rotatingEnabled = true;
	var speed = 1000;
	var timeout = 6000;
	var timer = setTimeout(showNextInfo, timeout);

	$('.rotator img').hide();
	$('.rotator img:nth-child(2)').show();

	function showNextInfo()
	{
		if (rotatingEnabled)
		{
			var next = $('.rotator li.li-aktywny').next('li');
			$('.rotator .li-aktywny').removeClass('li-aktywny');
			if (next.size() > 0)
				next.addClass('li-aktywny');
			else
				$('.rotator li:first-child').addClass('li-aktywny');

			var i = $('.rotator .li-aktywny').prevAll().size() + 1;

			var image = $('.rotator img:nth-child(' + i + ')').next('img');
			$('.rotator img').fadeOut(speed);
			if (image.size() > 0)
				image.fadeIn(speed);
			else
				$('.rotator img:nth-child(2)').fadeIn(speed);

			setTimeout(showNextInfo, timeout);
		}
	}

	$('.rotator li').hover(function()
	{
		rotatingEnabled = false;

		if ($(this).hasClass('li-aktywny') == false)
		{
			$('.rotator .li-aktywny').removeClass('li-aktywny');

			var i = $(this).prevAll().size() + 2;
			$('.rotator img').fadeOut(speed);
			$('.rotator img:nth-child(' + i + ')').fadeIn(speed).addClass('li-aktywny');
		}
	});

	$('.rotator li').mouseout(function()
	{
		$(this).addClass('li-aktywny');
	});

	if ($('.jq-captcha-error').size() > 0)
	{
		$('.jq-show-when-message').show();
		$('.jq-show-when-message .df-main').show();
		var x = $('.jq-show-when-message').offset().top - 100;
		$('html,body').scrollTop(x);
	}
});
