// vim: set fileencoding=utf-8 expandtab shiftwidth=4 tabstop=4 softtabstop=4:

function rotate(el, dir) {
    if ($(el).attr('lock') == "1") {
        return;
    }
    $(el).attr('lock', 1);
    clearTimeout($(el).attr('timeout_id'));
    var tout = $(el).attr('slideshow_speed');
    var current = $($(el).children().filter(':not(.hidden)')[0]);
    var wybrany = parseInt($($(el).nextAll('div.przelacznik_elementow')[0]).find('.nr_elementu_wybranego').html());
    var next;
    if (dir>0) {
        next = current.next();
        wybrany++;
    } else {
        next = current.prev();
        wybrany--;
    }
    if (next.size() == 0) {
        var first;
        if (dir>0) {
            first = ':first';
            wybrany = 1;
        } else {
            first = ':last';
            wybrany = parseInt($($(el).nextAll('div.przelacznik_elementow')[0]).find('.ilosc_elementow').html());
        }
        next = $(el).children().filter(first);
    }
    if (!$.browser.msie) {
        current.animate({ 'opacity': 0.0 }, "slow");
    }
    current.queue(function() {
        current.addClass('hidden');
        $($(el).nextAll('div.przelacznik_elementow')[0]).find('.nr_elementu_wybranego').html(wybrany);
        if (!$.browser.msie) {
            next.css({ opacity: 0.0 });
            next.removeClass('hidden');
            next.animate({ 'opacity': 1.0 }, 'slow');
        } else {
            next.removeClass('hidden');
        }
        next.queue(function() {
            $(el).attr('timeout_id', setTimeout(function() { rotate(el, 1); }, tout));
            $(el).attr('lock', 0);
            $(this).dequeue();
        } );
        $(this).dequeue();
    });
}

$(function() {
    $('.slideshow').each(function(i) {
        if ($(this).children().size() < 2) {
            return;
        }
        $($(this).nextAll('div.przelacznik_elementow')[0]).find('.ilosc_elementow').html($(this).children().size());
        $($(this).nextAll('div.przelacznik_elementow')[0]).find('.nr_wybranego_elementu').html(1);
        var timeout = $(this).attr('slideshow_speed');
        var e = this;
        $(this).attr('timeout_id', setTimeout(function(e0) { return function() { rotate(e0, 1); } } (e), timeout));
    });
});

function rotate2(el, n) {
    if ($(el).attr('lock') == "1") {
        return;
    }
    $(el).attr('lock', 1);
    clearTimeout($(el).attr('timeout_id'));
    var tout = $(el).attr('slideshow_speed');
    var current = $($(el).children().filter(':not(.przelacznik_elementow2)').filter(':not(.hidden)')[0]);
    var next = $($(el).children()[n]);
    if (!$.browser.msie) {
        current.animate({ 'opacity': 0.0 }, "slow");
    }
    current.queue(function() {
        current.addClass('hidden');
    //    $($(el).nextAll('div.przelacznik_elementow')[0]).find('.nr_elementu_wybranego').html(wybrany);
        $($(el).children().filter('.przelacznik_elementow2')[0]).children().removeClass('wybrany');
        $($($(el).children().filter('.przelacznik_elementow2')[0]).children()[n]).addClass('wybrany');
        if (!$.browser.msie) {
            next.css({ opacity: 0.0 });
            next.removeClass('hidden');
            next.animate({ 'opacity': 1.0 }, 'slow');
        } else {
            next.removeClass('hidden');
        }
        next.queue(function() {
            $(el).attr('timeout_id', setTimeout(function() { rotate2(el, (n+1) % $(el).children().filter(':not(.przelacznik_elementow2)').size()); }, tout));
            $(el).attr('lock', 0);
            $(this).dequeue();
        } );
        $(this).dequeue();
    });
}

$(function() {
    $('.slideshow2').each(function(i) {
        if ($(this).children().filter(':not(.przelacznik_elementow2)').size() < 2) {
            $(this).children().filter('.przelacznik_elementow2').hide();
            return;
        }
        var timeout = $(this).attr('slideshow_speed');
        var e = this;
        $(this).attr('timeout_id', setTimeout(function(e0) { return function() { rotate2(e0, 1); } } (e), timeout));
    });
});

