!function($){
    "use strict";

    $('vekrosta-form').each(function(idx, el){
        var $el = $(el);

        if($el.attr('id')==undefined){
            $el.attr('id', 'vrform_'+(idx+1)+'_'+new Date().getTime());
        }

        setTimeout(function(){
            oseFormHelper.insertForm({uid: $el.attr('data-uid'), tid: $el.attr('data-tid'), ver: '3.0'}, $el.attr('id'));
        }, 10);
    });/*

    document.querySelectorAll('vekrosta-form').forEach(function(el){
        if(el.getAttribute('id')==undefined){
            el.setAttribute('id', 'vrform_'+new Date().getTime());
        }
        setTimeout(function(){
            oseFormHelper.insertForm({uid: el.getAttribute('data-uid'), tid: el.getAttribute('data-tid'), ver: '3.0'}, el.getAttribute('id'));
        }, 10);
    });*/

    var secondsElapsed = 0;
    function showWaitBlocks(){
        var $blocks = $('[data-delay]');

        $blocks.each(function(idx, el){
            var sec = parseInt($(el).data('delay'));
            if(isNaN(sec) || sec<=secondsElapsed){
                $(el).removeData('delay').removeAttr('data-delay');
            }
        });

        secondsElapsed++;
        if($blocks.size()) {
            setTimeout(showWaitBlocks, 1000);
        }
    }
    showWaitBlocks();

    var $timerA = $('.time-counter.timer-a');
    $timerA.each(function(i, el){
        var
            $el = $(el),
            h = parseInt($el.find('[role="h"]').text()),
            m = parseInt($el.find('[role="m"]').text()),
            s = parseInt($el.find('[role="s"]').text())
        ;
        if(isNaN(h)) h = 0;
        if(isNaN(m)) m = 0;
        if(isNaN(s)) s = 0;

        $el.data('seconds-left', s+m*60+h*3600);
    });

    var $timerB = $('.time-counter.timer-b');
    $timerB.each(function(i, el){
        var
            $el = $(el),
            dd = parseInt($el.find('[role="dd"]').text()),
            mm = parseInt($el.find('[role="mm"]').text()),
            yy = parseInt($el.find('[role="yy"]').text()),
            h = parseInt($el.find('[role="h"]').text()),
            m = parseInt($el.find('[role="m"]').text()),
            s = parseInt($el.find('[role="s"]').text())
        ;

        if(isNaN(dd)) dd = 0;
        if(isNaN(mm)) mm = 0;
        if(isNaN(yy)) yy = 0;
        if(isNaN(h)) h = 0;
        if(isNaN(m)) m = 0;
        if(isNaN(s)) s = 0;
        var d = new Date(yy,mm-1,dd,h,m,s);
        $el.data('date-event', d);
    });

    $timerA.find('.timer-counter-note').remove();
    $timerB.find('.timer-counter-note').remove();

    var checkDate = new Date();
    function updateTimerCounters(){
        var
            newDate = new Date(),
            diff = Math.floor((newDate - checkDate) / 1000);

        $timerA.each(function(i, el){
            var
                $el = $(el),
                newVal = Math.max($el.data('seconds-left') - diff, 0),
                h = Math.floor(newVal / 3600),
                m = Math.floor( (newVal-h*3600) / 60 ),
                s = newVal - h*3600 - m*60;
            ;
            if(h<9){ h = '0' + h;}
            if(m<9){ m = '0' + m;}
            if(s<9){ s = '0' + s;}
            $el.find('[role="h"]').html(h);
            $el.find('[role="m"]').html(m);
            $el.find('[role="s"]').html(s);
        });

        $timerB.each(function(i, el){
            var
                $el = $(el), dateEvent = $el.data('date-event'),
                diff,
                dd,h,m,s, out = [];
            ;

            diff = Math.max(Math.floor((dateEvent - newDate) / 1000), 0);

            dd = Math.floor(diff / 86400);
            diff -= dd * 86400;
            h = Math.floor(diff / 3600);
            diff -= h*3600;
            m = Math.floor(diff / 60);
            s = diff - m*60;

            if(dd>0){ out.push(dd + ' дн.'); }
            if(h>0||out.length>0){ out.push(h + 'ч.'); }
            if(m>0||out.length>0){ out.push(m + 'м.'); }
            if(s>0||out.length>0){ out.push(s + 'c.'); }
            if(out.length>0){
                out = out.join(' ');
            }else{
                out = 'время пришло!';
            }
            $el.html(out);
        });

        setTimeout(updateTimerCounters, 500);
    }
    updateTimerCounters();
}(jQuery);
