$(function() {

    var
        tmp = null,
        processed = false,
        animateDocument = (function() {

            var
                target = '',
                start = 0,
                step = 0,
                steps = 30,
                stepLeft = 0, 
                anim = false,
                interval = setInterval(function() {

                    if (!anim) return;

                    start += step;
                    stepLeft--;

                    $(window).scrollTop(start);

                    if (stepLeft <= 0) {
                        anim = false;
                        document.location.hash = target;
                        target = '';
                    }

                }, 50);

            return function(newTarget) {

                var
                    pos = $(newTarget).offset(),
                    wTop = $(window).scrollTop(),
                    diff = wTop - pos.top;

                start = wTop;
                if (Math.abs(diff) < 200) steps = 15; else
                if (Math.abs(diff) < 1000) steps = 30; else
                steps = 50;
                step = -diff / steps;
                stepLeft = ~~(Math.abs(diff) / Math.abs(step));
                target = newTarget;
                anim = true;
            }

        }());

    $('#bohaterowieArt section.player').each(function() {

        $(this).find('ul a').click(function() {
            var name = $(this).attr('data-target');
            if ($(this).parent().hasClass('active')) return false;
            $('#bohaterowieArt iframe').attr('src', $(this).attr('href'));
            $('#bohaterowieArt .active').removeClass('active');
            $(this).parent().addClass('active');
            $('#bohaterowieArt [data-name="' + name + '"]').addClass('active');
            return false;
        });

        $(this).find('ul li.active').removeClass('active').find('a').click();

    });

    $('#expand .trigger').mouseenter(function() {

        if ($('#expand').hasClass('animating')) return;
        $('#expand .trigger').addClass('expanded');
        $('#expand').animate({ left: 0 }, { 
            easing: 'swing',
            duration: 500
        });

    });
    
    $('#expand').mouseleave(function() {

        $('#expand .trigger').removeClass('expanded');
        $('#expand').addClass('animating').animate({ left: -650 }, { 
            duration: 500,
            easing: 'swing',
            complete: function() {
                setTimeout(function() {
                    $('#expand').removeClass('animating');
                }, 100);
            }
        });

    });

    $('#form div[data-target]').each(function() {

        var 
            $me = $(this),
            $li = $me.parents('li').eq(0),
            $radio = $li.find('input[type="radio"]');

        $radio.click(function() {

            if ($(this).attr('data-show')) {
                if (processed) {
                    $me.slideDown('fast');
                } else {
                    $me.show();
                }
            } else {
                if (processed) {
                    $me.slideUp('fast');
                } else {
                    $me.hide();
                }
            }

            processed && $me.find('textarea').focus();

        });

    });

    $('#form input[type="radio"],#form input[type="checkbox"]').removeAttr('checked');
    $('#form input[type="text"],textarea').val('');
    $('#form input[name="back"]').val(document.location.href);

    if (window.userData) {

        if (tmp = userData['imie']) $('input[name="name"]').val(tmp);
        if (tmp = userData['wiek']) $('input[name="age"]').val(tmp);
        if (tmp = userData['szkola']) $('input[name="school"]').val(tmp);
        if (tmp = userData['telefon']) $('input[name="phone"]').val(tmp);
        if (tmp = userData['mail']) $('input[name="mail"]').val(tmp);
        if (tmp = userData['osiagniecia_sportowe']) $('textarea[name="ac-sport"]').val(tmp);
        if (tmp = userData['osiagniecia_naukowe']) $('textarea[name="ac-science"]').val(tmp);
        if (tmp = userData['osiagniecia_artystyczne']) $('textarea[name="ac-arts"]').val(tmp);
        if (tmp = userData['osiagniecia_inne']) $('textarea[name="ac-other"]').val(tmp);
        if (tmp = userData['wolontariat_tak_nie']) $('input[name="exp-question-1"][value="' + tmp + '"]').click();
        if (tmp = userData['wolontariat_opis']) $('textarea[name="exp-question-1-val"]').val(tmp);
        if (tmp = userData['festiwal_tak_nie']) $('input[name="exp-question-2"][value="' + tmp + '"]').click();
        if (tmp = userData['festiwal_opis']) $('textarea[name="exp-question-2-val"]').val(tmp);
        if (tmp = userData['motywacja_zamierzenia']) $('textarea[name="mot-question-1"]').val(tmp);
        if (tmp = userData['motywacja_pomysl']) $('textarea[name="mot-question-2"]').val(tmp);
        if (tmp = userData['regulamin']) if (tmp == '1') $('input[name="tos"]').attr('checked');
        if (tmp = userData['przetwarzanie_danych']) if (tmp == '1') $('input[name="perf"]').attr('checked');

    }

    $('#form .tos a').click(function() {
        open($(this).attr('href'));
        return false;
    });

    processed = true;

    $(window).load(function() {

        if (!$('#pageFoot li.ieFix').length) return;
        $('#pageFoot ul').each(function() {

            var maxH = 0;
            $(this).find('li.ieFix').each(function() {
                maxH = Math.max(maxH, $(this).height());
            });
            $(this).find('li.ieFix').each(function() {
                $(this).css({
                    paddingTop: ~~((maxH - $(this).height()) / 2)
                });
            });
          
        });

    });

    // $('#expand a').click(function() {
    //     $('#expand').mouseleave();
    //     animateDocument($(this).attr('href'));
    //     return false;
    // });

});


