$(document).ready(function () {
    var jouer = false;

    function closeMenu(){

        $('#menu').css({'animation-name':'animateup'});
        $('#menu').css({'animation-play-state':'play'});
        $('#menu-back').css({'visibility':'visible'});
        $('#sound').attr('src','assets/audio/play1.ogg');
        document.getElementById('audio').load();
        document.getElementById('audio').play();
        jouer=true;
        play();

    }
    function showMenu(){
        closeCredits();
        $('#sound').attr('src','assets/audio/intro.ogg');
        document.getElementById('audio').load();
        document.getElementById('audio').play();
        $('#sound').attr('src','assets/audio/intro.ogg');
        $('#menu').css({'animation-name':'animatedown'});
        $('#menu').css({'animation-play-state':'play'});
        $('#menu-back').css({'visibility':'hidden'});
    }
    function showCredits(){

        closeMenu();
        $('#sound').attr('src','assets/audio/credits.ogg');
        document.getElementById('audio').load();
        document.getElementById('audio').play();
        $('#credits').css({'display':'block'});
        $('#credits').css({'animation-name':'animateup'});
        $('#credits').css({'animation-play-state':'play'});
        $('#menu-back').css({'visibility':'visible'});
    }
    function closeCredits(){
        $('#credits').css({'animation-name':'animateup'});
        $('#credits').css({'animation-play-state':'play'});
        $('#credits').css({'display':'none'});
        $('#menu-back').css({'visibility':'hidden'});
    }

    $('#launch').on("click", closeMenu);
    $('#menu-back').on("click", showMenu);
    $('#to-credits').on("click", showCredits);

    function play() {

        if (jouer) {

            $('.back-game').css({'height': window.innerHeight, 'background-position-y': window.innerHeight - 615});
            $('.game').css({'height': window.innerHeight, 'background-position-y': window.innerHeight - 415});
            $('.back-game2').css({'height': window.innerHeight, 'background-position-y': window.innerHeight - 615});
            $('.game2').css({'height': window.innerHeight, 'background-position-y': window.innerHeight - 415});
            $('.back-game3').css({'height': window.innerHeight, 'background-position-y': window.innerHeight - 615});
            $('.game3').css({'height': window.innerHeight, 'background-position-y': window.innerHeight - 415});

            var ligne1 = $('.poteau_container.ligne1');
            var ligne2 = $('.poteau_container.ligne2');
            var ligne3 = $('.poteau_container.ligne3');
            var speed;
            var pos = 1;
            var body = $('body')[0].offsetLeft;
            var perso = $('#perso')[0].offsetLeft;
            var counter = 0;
            var lignesArr = $('.slide .poteau_container');
            var pillarElm = "";
            var slidesArr = $('.slideshow .slide');
            var typeOfPillar = "";
            var i = 1;

            var pot1 = "'background', 'url(assets/images/poteau1.png) no-repeat'";
            var pot2 = "'background', 'url(assets/images/poteau2.png) no-repeat'";
            var pot3 = "'background', 'url(assets/images/poteau3.png) no-repeat'";

            // OLD CODE + BG
            setInterval(function () {
                setTimeout(function () {
                    var random = Math.floor(Math.random() * 100);
                    if (random > 0 && random < 33) {
                        ligne1.append('<div style="left: ' + (ligne1.children('div').last()[0].offsetLeft + 775) + 'px; ' + pot1 + '; background-size: contain; ">');
                    } else if (random > 33 && random < 66) {
                        ligne1.append('<div style="left: ' + (ligne1.children('div').last()[0].offsetLeft + 775) + 'px; ' + pot2 + '; background-size: contain; ">');
                    } else if (random > 66 && random < 100) {
                        ligne1.append('<div style="left: ' + (ligne1.children('div').last()[0].offsetLeft + 775) + 'px; ' + pot3 + '; background-size: contain; ">');
                    }
                    if (random > 10 && random < 75) {
                        ligne1.append('<span class="usb1" style="left: ' + (ligne1.children('span').last()[0].offsetLeft + 1700 + 'px') + ' ">');
                    }
                    if (random > 30 && random < 100) {
                        ligne3.append('<span class="fire1" style="left: ' + (ligne1.children('span').last()[0].offsetLeft + 2000 + 'px') + ' ">');
                    }

                }, 200);
                setTimeout(function () {
                    var random = Math.floor(Math.random() * 100);
                    if (random > 0 && random < 33) {
                        ligne2.append('<div style="left: ' + (ligne2.children('div').last()[0].offsetLeft + 775) + 'px; ' + pot1 + '; background-size: contain; ">');
                    } else if (random > 33 && random < 66) {
                        ligne2.append('<div style="left: ' + (ligne2.children('div').last()[0].offsetLeft + 775) + 'px; ' + pot2 + '; background-size: contain; ">');
                    } else if (random > 66 && random < 100) {
                        ligne2.append('<div style="left: ' + (ligne2.children('div').last()[0].offsetLeft + 775) + 'px; ' + pot3 + '; background-size: contain; ">');
                    }
                    if (random > 75 && random < 100) {
                        ligne2.append('<span class="usb2" style="left: ' + (ligne1.children('span').last()[0].offsetLeft + 2000 + 'px') + ' ">');
                    }
                    if (random > 25 && random < 85) {
                        ligne3.append('<span class="fire2" style="left: ' + (ligne1.children('span').last()[0].offsetLeft + 1200 + 'px') + ' ">');
                    }
                }, 200);
                setTimeout(function () {
                    var random = Math.floor(Math.random() * 100);
                    if (random > 0 && random < 33) {
                        ligne3.append('<div style="left: ' + (ligne3.children('div').last()[0].offsetLeft + 790) + 'px; ' + pot1 + '; background-size: contain; ">');
                    } else if (random > 33 && random < 66) {
                        ligne3.append('<div style="left: ' + (ligne3.children('div').last()[0].offsetLeft + 790) + 'px; ' + pot2 + '; background-size: contain; ">');
                    } else if (random > 66 && random < 100) {
                        ligne3.append('<div style="left: ' + (ligne3.children('div').last()[0].offsetLeft + 790) + 'px; ' + pot3 + '; background-size: contain; ">');
                    }
                    if (random > 0 && random < 45) {
                        ligne3.append('<span class="usb3" style="left: ' + (ligne1.children('span').last()[0].offsetLeft + 2500 + 'px') + ' ">');
                    }
                    if (random > 0 && random < 70) {
                        ligne3.append('<span class="fire3" style="left: ' + (ligne1.children('span').last()[0].offsetLeft + 3000 + 'px') + ' ">');
                    }
                    i++;
                }, 200);
                // if($('.poteau_container > div').length == 400){
                //     for(var i =0; i<200; i++){
                //         console.log($('.poteau_container > div')[i]);
                //     }
                // }
                if (i == 1000) {
                    // alert('Vous avez survécu ! Bravo !');
                    window.location.reload();
                }

                body -= 70;
                switch (pos) {
                    case 1:
                        speed = 1;
                        break;
                    case 2:
                        speed = 1.5;
                        break;
                    case 3:
                        speed = 0.3;
                        break;
                }
                perso += 70 * speed;
                $('#perso').css('left', perso);
                $('.usb1').each(function () {
                    var leftpers = $('#perso').css('left').split('px');
                    var leftusb = $(this).css('left').split('px');
                    leftpers = parseInt(leftpers[0]);
                    leftusb = parseInt(leftusb[0]);
                    if (leftpers - 50 < leftusb && leftpers + 50 > leftusb && $('#perso').css('bottom') == $(this).css('bottom')) {
                        $(this).hide();
                        counter += 1;
                        $('p.score').text("Vous avez " + parseInt(counter / 3) + " usb");
                    }
                });
                $('.usb2').each(function () {
                    var leftpers = $('#perso').css('left').split('px');
                    var leftusb = $(this).css('left').split('px');
                    leftpers = parseInt(leftpers[0]);
                    leftusb = parseInt(leftusb[0]);
                    if (leftpers - 50 < leftusb && leftpers + 50 > leftusb && $('#perso').css('bottom') == $(this).css('bottom')) {
                        $(this).hide();
                        counter += 1;
                        $('p.score').text("Vous avez " + parseInt(counter / 3) + " usb");
                    }
                });
                $('.usb3').each(function () {
                    var leftpers = $('#perso').css('left').split('px');
                    var leftusb = $(this).css('left').split('px');
                    leftpers = parseInt(leftpers[0]);
                    leftusb = parseInt(leftusb[0]);
                    if (leftpers - 50 < leftusb && leftpers + 50 > leftusb && $('#perso').css('bottom') == $(this).css('bottom')) {
                        $(this).hide();
                        counter += 1;
                        $('p.score').text("Vous avez " + parseInt(counter / 3) + " usb");
                    }
                });
                $('.fire1').each(function () {
                    var leftpers = $('#perso').css('left').split('px');
                    var leftusb = $(this).css('left').split('px');
                    leftpers = parseInt(leftpers[0]);
                    leftusb = parseInt(leftusb[0]);
                    if (leftpers - 50 < leftusb && leftpers + 50 > leftusb && $('#perso').css('bottom') == "347px") {
                        $('#perso').hide();
                        // alert('Vous êtes mort, LE FEU ÇA BRULE LOL ! -_-');
                        window.location.reload();
                    }
                });
                $('.fire2').each(function () {
                    var leftpers = $('#perso').css('left').split('px');
                    var leftusb = $(this).css('left').split('px');
                    leftpers = parseInt(leftpers[0]);
                    leftusb = parseInt(leftusb[0]);
                    if (leftpers - 50 < leftusb && leftpers + 50 > leftusb && $('#perso').css('bottom') == "465px") {
                        $('#perso').hide();
                        // alert('Vous êtes mort, LE FEU ÇA BRULE LOL ! -_-');
                        window.location.reload();
                    }
                });
                $('.fire3').each(function () {
                    var leftpers = $('#perso').css('left').split('px');
                    var leftusb = $(this).css('left').split('px');
                    leftpers = parseInt(leftpers[0]);
                    leftusb = parseInt(leftusb[0]);
                    if (leftpers - 50 < leftusb && leftpers + 50 > leftusb && $('#perso').css('bottom') == "480px") {
                        $('#perso').hide();
                        // alert('Vous êtes mort, LE FEU ÇA BRULE LOL ! -_-');
                        window.location.reload();
                    }
                });
                $('.score').css({'left': body * -1});
                $('body').css('margin-left', body);
                var bodyLeft = $('body').css('margin-left').split('px');
                bodyLeft = bodyLeft[0] * -1;
                if (($('#perso')[0].offsetLeft + 140) < bodyLeft) {
                    // alert('Vous êtes mort');
                    window.location.reload();
                }
                console.log(window.innerWidth);
                if (($('#perso')[0].offsetLeft + 140) > bodyLeft + (window.innerWidth)) {
                    // alert('Vous êtes mort');
                    window.location.reload();
                }
            }, 100);

            $(document).on('keydown', function (e) {
                switch (e.which) {
                    case 38:
                        if (pos !== 3) {
                            if (pos == 1) {
                                $('#perso').addClass('ligne2');
                            }
                            if (pos == 2 && $('#perso').hasClass('ligne2')) {
                                $('#perso').removeClass('ligne2');
                                $('#perso').addClass('ligne3');
                            }
                            pos += 1;
                        }
                        break;
                    case 40:
                        if (pos !== 1) {
                            if (pos == 2 && $('#perso').hasClass('ligne2')) {
                                $('#perso').removeClass('ligne2');
                            }
                            if (pos == 3 && $('#perso').hasClass('ligne3')) {
                                $('#perso').removeClass('ligne3');
                                $('#perso').addClass('ligne2');
                            }
                            pos -= 1;
                        }
                        break;
                    case 32:
                        if (pos == 1) {
                            var left = $('#perso').css('left').split('px');
                            left = parseInt(left[0]) + speed;
                            $('#perso').css({'left': left});
                        }
                }
            });
        }
    }
});