$(document).ready(function(){
    $(".panel").hide();
    $(".panel:first-child").show();

    $(".questionblock .button").click(function(){

        var questionBlock = $(this).parent();
        var self = this;

        questionBlock.hide();
        var last = questionBlock.data('last');
        

        if (last != "1") {
            questionBlock.next().show();
        } else {
            //$(".panel").hide();
            //$(self).parents(".panel").next(".panel").show();
            $(".prelander-progress.hide").removeClass("hide");

            var si = setInterval(function() {
            var pending = $(".prelander-progress .hide").first();

                if (!pending.length) {
                    $(".panel").hide();
                    clearInterval(si);
                    $(".prelander-progress").hide();
                    $(self).parents(".panel").next(".panel").show();
                    // se sono su mobile mostro le robe di whatsapp
                    if($("#wabutton").length > 0)
                    {
                    initWhatsApp();
                    loadAllComment();
                   }
              } else {
                  pending.removeClass('hide');
              }
           }, 1500);
        }
    });


    $(".participate, #page1 .pic-wrap img").click(function(){
        $(".hero-line-sm").hide();
        $(".questionfield").show();
        $("#input1 .position-info, #input2 .position-info").show();
        $('html, body').animate({scrollTop : 0}, 400);
    });

    $("._socialcount").click(function(e){
        e.preventDefault();
        var socialcounthold = $("._socialcounthold").html()*1;
        socialcounthold++;
        $("._socialcounthold").html(socialcounthold);
    });

    if($("#modal-lang").length) {
        $('#modal-lang').modal({
            escapeClose: false,
            clickClose: false,
            showClose: false
        });
    }

    $(".warning-opener").click(function(){
        var warningAlert = $(this).parents(".header-warning");
        if (warningAlert.hasClass("opened")) {
            warningAlert.removeClass("opened");
        } else {
            warningAlert.addClass("opened");
        }
    });

$.fn.onEnterKey = function(callback) {
        $(this).keypress(function(event) {
            var code = event.keyCode ? event.keyCode : event.which;
            if (code == 13) {
                callback();
                return false;
            }
        });
    }

$('.like').on('click',function() {
            var _this = $(this);
            if(_this.hasClass('active-like')) {
                _this.removeClass('active-like');
                $('.you').removeClass('active');
            } else {
                _this.addClass('active-like');
                $('.you').addClass('active');
            }
        });
        
        $('.fblike').on('click', function() {
            var _this = $(this);
            var x = _this.next().next().html() == '' ? 0 : parseInt(_this.next().next().html());
            if(_this.hasClass('selected')) {
                _this.removeClass('selected').html('Mi piace');
                _this.next().next().html(--x);
                if(x == 0) _this.next().next().addClass('likehide');
            } else {
                _this.addClass('selected').html('Non mi piace più');
                _this.next().next().html(++x);
                if(_this.next().next().hasClass('likehide')) _this.next().next().removeClass('likehide');
            }
        });
        $("#addCom").onEnterKey(function() {
            var value = $("#addCom").val();
            $("#addCom").val("");
            $("#myComment").html(value);
            $(".addComment").hide();
            loadCom('#myCom', 250);
        });
   
});

function goToForm(param)
{
    $("#caricamento").css("display","none");    
    $(".panel").hide();
    $(param).parents(".panel").next(".panel").show();
}

 function updateTimer() 
 {
                timer--;
                $("#gutschein").text(timer);
                
                if (timer < 9) {
                    stopFunction();
                }
}

      
            function stopFunction() {
                window.clearInterval(myTimer);
            }

function loadCom(a,b) {
        setTimeout(function() {
            $(a).fadeIn(250);
        }, b);
    }
    
function loadAllComment(){
    var intervallo = 2500;
    for(cn = 1; cn <= window.hiddenNum; cn++)
    {
        loadCom("#com"+cn,intervallo)
        intervallo+= (1000 + Math.floor(Math.random() * 4000))
    }
        
    }

    function initWhatsApp()
    {
         var zeit = new Date();
            
            var sec = zeit.getSeconds();
            
            if (sec < 6 ){
                sec = 9
            }
            
            if (sec > 5 & sec < 13 ){
                sec = 8
            }
            
             if (sec > 11 & sec < 19 ){
                sec = 7
            }
            
             if (sec > 17 & sec < 25 ){
                sec = 6
            }
            
             if (sec > 23 & sec < 31 ){
                sec = 5
            }
            
             if (sec > 29 & sec < 37 ){
                sec = 4
            }
            
            if (sec > 35 & sec < 43 ){
                sec = 3
            }
            
            if (sec > 41 & sec < 49 ){
                sec = 2
            }
            
            if (sec > 47 & sec < 55 ){
                sec = 1
            }
            
            if (sec > 54){
                sec=0
            };
            
            
            var add = sec;
            
            var zeit2 = new Date();
            
            var minute = zeit2.getMinutes();
            
            var timer1 = (60-minute)*60;
            
            if (timer1 > 1800) {
               var resttimer = (30-minute)*10
            } else {
               var resttimer = (60-minute)*10
            };
            
            timer = resttimer + add + 67;

            myTimer = window.setInterval(updateTimer, 6000);
    }