﻿$(document).ready(function() {

    //menu górne
    $('.jq-menu1').hover(
			function() { $('ul', this).css('display', 'block'); },
			function() {
			    $('ul', this).css('display', 'none');
			});

    //menu glowne, belka 2
    $('li.jq-menu').hover(function() {
        $(this).parent().children('li').children('ul').hide();
        $(this).children('ul').show();
    });

    $('li.jq-menu').mouseleave(function() {
        if ($(this).parent().children('.active').length != 0) {
            $(this).parent().children('li').children('ul').hide();
        }
        $(this).parent().children('.active').children('ul').show();
    });

    //przewijanie informacje
    $('span.jq-next-page').click(function() {
        var curIndex = $(this).parent().parent().parent().children('.jq-tabs').children('.jq-display:first').prevAll().length + 1;
        var maxIndex = $(this).parent().parent().parent().children('.jq-tabs').children('.jq-tab').length;
        (curIndex + 3 > maxIndex) ? curIndex = 1 : curIndex = curIndex + 3;
        $(this).parent().parent().parent().children('.jq-tabs').children().removeClass('jq-display');
        for (i = curIndex; i < curIndex + 3; i++)
            $(this).parent().parent().parent().children('.jq-tabs').children(':nth-child(' + i + ')').addClass('jq-display');
        $(this).parent().children('.pager').html(Math.ceil(curIndex / 3) + '/' + Math.ceil(maxIndex / 3));
        return false;
    });

    $('span.jq-prev-page').click(function() {
        var curIndex = $(this).parent().parent().parent().children('.jq-tabs').children('.jq-display:first').prevAll().length + 1;
        var maxIndex = $(this).parent().parent().parent().children('.jq-tabs').children('.jq-tab').length;
        if (curIndex - 3 < 1) {
            if (maxIndex % 3 == 0) curIndex = maxIndex - 2;
            else curIndex = maxIndex - (maxIndex % 3 - 1);
        }
        else {
            curIndex = curIndex - 3;
        }
        $(this).parent().parent().parent().children('.jq-tabs').children().removeClass('jq-display');
        for (i = curIndex; i < curIndex + 3; i++)
            $(this).parent().parent().parent().children('.jq-tabs').children(':nth-child(' + i + ')').addClass('jq-display');
        $(this).parent().children('.pager').html(Math.ceil(curIndex / 3) + '/' + Math.ceil(maxIndex / 3));
        return false;
    });

    // przewijanie galerie i blogi
    $('span.jq-next-gal').click(function() {
        var curIndex = $(this).parent().parent().parent().children('.jq-gal').children('.jq-gal').children('.jq-display:first').prevAll().length + 1;
        var maxIndex = $(this).parent().parent().parent().children('.jq-gal').children('.jq-gal').children('.jq-tab').length;
        (curIndex + 4 > maxIndex) ? curIndex = 1 : curIndex = curIndex + 4;
        $(this).parent().parent().parent().children('.jq-gal').children('.jq-gal').children().removeClass('jq-display');
        for (i = curIndex; i < curIndex + 4; i++)
            $(this).parent().parent().parent().children('.jq-gal').children('.jq-gal').children(':nth-child(' + i + ')').addClass('jq-display');
        $(this).parent().children('.pager').html(Math.ceil(curIndex / 4) + '/' + Math.ceil(maxIndex / 4));
        return false;
    });

    $('span.jq-prev-gal').click(function() {
        var curIndex = $(this).parent().parent().parent().children('.jq-gal').children('.jq-gal').children('.jq-display:first').prevAll().length + 1;
        var maxIndex = $(this).parent().parent().parent().children('.jq-gal').children('.jq-gal').children('.jq-tab').length;
        if (curIndex - 4 < 1) {
            if (maxIndex % 4 == 0) curIndex = maxIndex - 3;
            else curIndex = maxIndex - (maxIndex % 4 - 1);
        }
        else {
            curIndex = curIndex - 4;
        }
        $(this).parent().parent().parent().children('.jq-gal').children('.jq-gal').children().removeClass('jq-display');
        for (i = curIndex; i < curIndex + 4; i++)
            $(this).parent().parent().parent().children('.jq-gal').children('.jq-gal').children(':nth-child(' + i + ')').addClass('jq-display');
        $(this).parent().children('.pager').html(Math.ceil(curIndex / 4) + '/' + Math.ceil(maxIndex / 4));
        return false;
    });

    // menu ztabowane
    $('.nawigacja .jq-tabs').click(function() {
        $(this).parent().children('.jq-tabs').removeClass('wcisniety');
        $(this).addClass('wcisniety');

        var curIndex = $(this).prevAll().length + 1;
        $(this).parent().parent().children('ul').children().removeClass('jq-display');
        $(this).parent().parent().children('ul').children(':nth-child(' + curIndex + ')').addClass('jq-display');

        return false;
    });

    // 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().children().show();
        }
        return false;
    });

    // logowanie
    $('li.logowanie a').click(function() {
        if ($('.login-pop').is(':visible')) {
            $('.login-pop').hide('fast');
            $(this).html('<span>Zaloguj się</span>');
        }
        else {
            $('.login-pop').show('fast');
            $(this).html('<span style="background-position: 50px -27px">Zamknij</span>');
        }
        return false;
    });

    // 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" });
    }

    //zamiana vspace i hspace edytowanych w fck na margin
    $('.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');
        }
    });
});

$(document).ready(function ()
{
	// inicjalizacja pierwszych zakładek
	$('.jq-menu-tab').each(function () {
		$(this).children('ul').children('li:first').addClass('jq-display');
		$(this).children('.nawigacja').children('a:first').addClass('wcisniety');
	});
	$('.jq-faq-tab').each(function() {
		$(this).children('ul').children('li:first').addClass('jq-display');
		$(this).children('.nawigacja').children('a:first').addClass('wcisniety');
	});
	
	// inicjalizacja pierwszego elementu menu na pierwszej stronie
	$('.master-page-containter .menu-gora ul.poziom1 > li.first').addClass('active');
});
