text: var startMaps = [{
      "id": "cae92b24-c2c1-88e9-b467-97b566afaed8",
      "creator": {
          "id": "7fdf2c09-af51-35f6-a6b3-9a63e75fb24b",
          "name": "Lvl 1",
          "date": "12.09.2019"
      },
      "final": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      "map": [
          [132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143],
          [120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131],
          [108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119],
          [96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107],
          [84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95],
          [72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83],
          [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71],
          [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
          [36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47],
          [24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35],
          [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
          [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
      ],
      "time": {}
  }, {
      "id": "76daa1f8-1f27-dbbd-e955-b7b580899efb",
      "creator": {
          "id": "9fd4a43c-c59e-cc2f-26dc-4c503fdb381d",
          "name": "Lvl 2",
          "date": "12.09.2019"
      },
      "final": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 3, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 2, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      "map": [
          [272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288],
          [255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271],
          [238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254],
          [221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237],
          [204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220],
          [187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203],
          [170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186],
          [153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169],
          [136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152],
          [119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135],
          [102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118],
          [85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101],
          [68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84],
          [51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67],
          [34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50],
          [17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33],
          [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
      ],
      "time": {}
  }, {
      "id": "8148b7ba-eb72-2741-42f8-615c7a46d617",
      "creator": {
          "id": "3f866852-54a3-2ef6-4ffa-a87a77b77b21",
          "name": "Lvl 3",
          "date": "12.09.2019"
      },
      "final": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 2, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      "map": [
          [462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483],
          [440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461],
          [418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439],
          [396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417],
          [374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395],
          [352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373],
          [330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351],
          [308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329],
          [286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307],
          [264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285],
          [242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263],
          [220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241],
          [198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219],
          [176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197],
          [154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175],
          [132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153],
          [110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131],
          [88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109],
          [66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87],
          [44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65],
          [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43],
          [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
      ],
      "time": {}
  }, {
      "id": "ae98f001-f120-65a0-a0a0-6c08dc7e9b8f",
      "creator": {
          "id": "48cecb63-d953-0bc8-2b3d-64df49c55ce2",
          "name": "Lvl 4",
          "date": "13.09.2019"
      },
      "final": [0, 0, 0, 0, 0, 0, 0, 0, 0,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998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023],
          [960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991],
          [928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959],
          [896, 897, 898, 899, 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927],
          [864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895],
          [832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844, 845, 846, 847, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859, 860, 861, 862, 863],
          [800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 810, 811, 812, 813, 814, 815, 816, 817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831],
          [768, 769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794, 795, 796, 797, 798, 799],
          [736, 737, 738, 739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767],
          [704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735],
          [672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 703],
          [640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671],
          [608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639],
          [576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607],
          [544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575],
          [512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543],
          [480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511],
          [448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479],
          [416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447],
          [384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415],
          [352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383],
          [320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351],
          [288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319],
          [256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287],
          [224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255],
          [192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223],
          [160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191],
          [128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159],
          [96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127],
          [64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95],
          [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63],
          [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
      ],
      "time": {}
  }];
  var countClick = 0,
      timer,
      time = {},
      hours = 0,
      minutes = 0,
      seconds = 0,
      moveCounter = 0,
      renderFinish = false,
      startSelected = false,
      pathSelected = false,
      finishConverting = false,
      ownerSaved = false,
      hover = false,
      vrView = false,
      finishSet = false,
      finishSelected = false,
      pathArray = [],
      availablePathsArray = [],
      activePathArray = [],
      labyrinthArray = [],
      disableIfSelected = [],
      validPathOptions = [],
      validGridOptions = [],
      validPaths = [],
      countSelectedBlocks = 0,
      pathCountClick = 0,
      finalArray = [],
      newArr = [],
      mapArray = [],
      lastClicked,
      applyLabyrinth,
      newCreatedMap,
      newCreatedMapIndex,
      maps = [],
      loadedMapObj = {},
      loadedMapIndex = '',
      loadMap = JSON.parse(localStorage.getItem('maps')) ? JSON.parse(localStorage.getItem('maps')) : startMaps;
  
  /* === Usefull functions === */
  Date.prototype.getDateString = function () {
      var dd = String(this.getDate()).padStart(2, '0'),
          mm = String(this.getMonth() + 1).padStart(2, '0'),
          yyyy = String(this.getFullYear()).padStart(2, '0');
  
      return dd + '.' + mm + '.' + yyyy;
  };
  
  function getRandomId() {
      var random = function () {
          return (((1+Math.random()) * 0x10000)|0).toString(16).substring(1);
      };
      return (random()+random()+"-"+random()+"-"+random()+"-"+random()+"-"+random()+random()+random());
  }
  function getSeconds(timeObject) {
      if (typeof timeObject === 'undefined') {
         return 0;
      }
      return ((timeObject.hours * 60) * 60) + (timeObject.minutes * 60) + timeObject.seconds
  }
  function getCloseButton(targetModal) {
      var fragment = document.createDocumentFragment(),
          closeBtnTemplate = document.createElement('div'),
          firstLine = document.createElement('div'),
          secondLine = document.createElement('div');
  
      firstLine.classList.add('first-line');
      secondLine.classList.add('second-line');
      closeBtnTemplate.classList.add('close-btn');
      closeBtnTemplate.appendChild(firstLine);
      closeBtnTemplate.appendChild(secondLine);
      fragment.appendChild(closeBtnTemplate);
  
      closeBtnTemplate.addEventListener('click', function () {
          targetModal.classList.add('hidden');
          targetModal.innerHTML = '';
          document.querySelector('.editor').classList.remove('blury');
      });
  
      return fragment;
  }
  function modalFunction(modalElements) {
      for (element in modalElements) {
          modalElements[element].addEventListener('click', function (e) {
              var el = e.target,
                  target = document.getElementById(el.dataset.target);
              if (target) {
                  if (target.id === 'loadedMaps' && loadMap.length > 0) {
                      loadMapsPreview(loadMap);
                  }
                  target.classList.remove('hidden');
                  target.prepend(getCloseButton(target));
                  document.querySelector('.editor').classList.add('blury');
              }
          });
      }
  }
  
  /* === Timer === */
  function countTime() {
      var timerValue = document.getElementById('timer');
      seconds++;
      if (seconds >= 60) {
          seconds = 0;
          minutes++;
          if (minutes >= 60) {
              minutes = 0;
              hours++;
          }
      }
      var fullTime = String(hours).padStart(2, '0') + ":" + String(minutes).padStart(2, '0') + ":" + String(seconds).padStart(2, '0');
      time = {
          seconds: seconds,
          minutes: minutes,
          hours: hours,
          stringTime: fullTime,
          owner: {}
      };
      timerValue.setAttribute('value', fullTime);
      count();
  }
  
  function count() {
      timer = setTimeout(countTime, 1000);
  }
  /* === Show valid options after selecting path === */
  function showValidOptions(pathIndex) {
      var activePaths = availablePathsArray;
      
      countSelectedBlocks = countSelectedBlocks + 1;
  
      Array.from(validPathOptions[pathIndex]).forEach(function (option) {
          var rest = activePaths.indexOf(parseFloat(option));
          // validPathOption looks like [clicked element, right, left, bottom, top]
          var pathOption;
          if (vrView) {
              pathOption = document.getElementsByClassName('grid-path')[option];
          } else {
              pathOption = document.getElementsByClassName('path')[option];
          }
          lastClicked = pathIndex;
  
          if (rest > -1) {
              activePaths.splice(rest, 1);
          }
  
          // mark path to use, but not already disabled blocks
          if (activePathArray[option] && !pathOption.classList.contains('disabled')) {
              labyrinthArray[pathIndex].selected = true;
              activePathArray[pathIndex] = false;
  
              pathOption.style.pointerEvents = 'auto';
              pathOption.classList.add('to-use');
              if (vrView && !pathOption.classList.contains('selected') && !pathOption.classList.contains('start-selected') && !pathOption.classList.contains('finish') && !pathOption.classList.contains('start-selected')) {
                  pathOption.setAttribute('color', '#ec902e');
                  pathOption.classList.add('clickable');
              }
          }
      });
      /* === Disable block - create wall === */
      Object.keys(disableIfSelected[pathIndex]).map(function (position) {
          var disOption = disableIfSelected[pathIndex][position],
              pathElement;
              if (vrView) {
                  pathElement = document.getElementsByClassName('grid-path')[disOption.disable];
              } else {
                  pathElement = document.getElementsByClassName('path')[disOption.disable];
              }
          var countDisabled = 0;
          Array.from(disOption.options).forEach(function (opt) {
              if (labyrinthArray[opt].selected) {
                  countDisabled = countDisabled + 1;
                  if (disOption.options.length === countDisabled) {
                      countDisabled = 0;
                      pathElement.classList.remove('to-use');
                      pathElement.classList.add('disabled');
                      if (vrView) {
                          pathElement.setAttribute('color', '#333333');
                      }
                      pathElement.style.pointerEvents = 'none';
                      labyrinthArray[disOption.disable].active = false;
                      document.getElementsByClassName('js-clock');
                      document.querySelector(".js-clock");
                      document.getElementsByTagName('h1');
                  }
              }
          });
      });
      /* === Show selected paths / adding class to selected === */
      Array.from(pathArray).forEach(function (item) {
          var thisItem;
          if (vrView) {
              thisItem = document.getElementsByClassName('grid-path')[item];
          } else {
              thisItem = document.getElementsByClassName('path')[item];
          }
          Array.from(activePaths).forEach(function (ap) {
              if (item === ap) {
                  thisItem.style.pointerEvents = 'none';
                  thisItem.classList.remove('to-use');
              }
          });
          if (!activePathArray[item]) {
              thisItem.classList.remove('to-use');
          }
      });
  }
  /* === Selecting start function === */
  function selectStart(element, index) {
      var clickable = document.querySelectorAll('.clickable');
      Array.from(clickable).forEach(function (clickableElement) {
          clickableElement.classList.remove('clickable');
      });
  
      if (renderFinish) {
          countClick = 1;
          labyrinthArray[index].option = 2;
          element.classList.add('start-selected');
          if (vrView) {
              element.setAttribute('color', '#fd2929');
          }
          startSelected = true;
          showValidOptions(index);
      }
  }
  /* === Selecting path function === */
  function selectPath(element, index, wasClick) {
      if (startSelected && renderFinish && labyrinthArray.length !== 0) {
          if (element.classList.contains('selected') && pathCountClick === 0 && !finishSet && wasClick) {
              pathCountClick = 1;
              finishSet = true;
          } else if (pathCountClick === 1 && element.classList.contains('finish') && wasClick && finishSelected) {
              element.classList.remove('finish');
              element.classList.add('selected');
              if (vrView) {
                  element.setAttribute('color', '#1ace65');
              }
              labyrinthArray[index].option = 1;
              pathCountClick = 0;
              finishSet = false;
              finishSelected = false;
          } else if (!element.classList.contains('finish')) {
              element.classList.add('selected');
              if (vrView) {
                  element.setAttribute('color', '#1ace65');
              }
              labyrinthArray[index].option = 1;
          }
          if (finishSet && wasClick && !finishSelected) {
              element.classList.add('finish');
              if (vrView) {
                  element.setAttribute('color', 'blue');
              }
              element.classList.remove('selected');
              labyrinthArray[index].option = 3;
              finishSelected = true;
          }
          showValidOptions(index);
      }
  }
  /* === Labyrinth creator function - based on click counting (recognize which is start, witch is path) === */
  function selectLabyrinthPath(event, wasClick) {
      var element = event.target,
          pathId = parseFloat(element.dataset.pathIndex);
  
      switch (countClick) {
          case 0:
              selectStart(element, pathId);
              break;
          case 1:
              selectPath(element, pathId, wasClick);
              break;
          default:
              return false;
      }
  }
  /* === Setting rest nonechoosed options to disabled (Walls) === */
  function setRestToDisabled() {
      Object.keys(labyrinthArray).map(function (block) {
          var pathElement;
          if (vrView) {
              pathElement = document.getElementsByClassName('grid-path')[block];
          } else {
              pathElement = document.getElementsByClassName('path')[block];
          }
          if (!labyrinthArray[block].selected) {
              labyrinthArray[block].active = false;
              pathElement.classList.remove('to-use');
              pathElement.classList.remove('clickable');
              pathElement.classList.add('disabled');
              pathElement.style.pointerEvents = 'none';
          }
      });
  }
  /* Update labyrinth array and set choosen path option to 1 (selected green path):
      labyrinthArray = [
          0: {active: false, selected: false, pathId: 0, option: 0}
      ]
  */
  function convertBlocks() {
      var path;
      if (vrView) {
          path = document.getElementsByClassName('grid-path');
      } else {
          path = document.getElementsByClassName('path');
      }
      path[lastClicked].classList.remove('to-use');
      Object.keys(labyrinthArray).map(function (path) {
          if (labyrinthArray[path].active && labyrinthArray[path].selected && labyrinthArray[path].option === 0) {
              labyrinthArray[path].option = 1;
          }
      });
      finishConverting = true;
  }
  /* Create final array to save - will be used to generate 3D blocks:
      finalArray = [0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 1, 2, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
      0 - define wall
      1 - define path
      2 - define start
      3 - define finish
  */
  function createArrayToSave() {
      Object.keys(labyrinthArray).map(function (path) {
          finalArray.push(parseFloat(labyrinthArray[path].option));
      });
  }
  /* === Render 2D view (finalArray and map array) to 3D blocks === */
  function generate3DBlocks(map, final) {
      var scene = document.getElementById('labyrinth-scene');
      var container = document.createDocumentFragment();
  
      document.getElementById('render-vr').setAttribute('visible', false);
  
      mapArray = typeof map !== 'undefined' ? map : mapArray;
      finalArray = typeof final !== 'undefined' ? final : finalArray;
  
      Array.from(mapArray).forEach(function (n, x) {
          Array.from(mapArray).forEach(function (a, z) {
              var blockHtml = document.createElement('a-box');
              blockHtml.setAttribute('rotation', "0 0 0");
              blockHtml.setAttribute('color', "#0e7ef6");
              blockHtml.setAttribute('position', `${x} 0.5 ${z}`);
              /* Render finish block (blue) */
              if (finalArray[mapArray[x][z]] === 3) {
                  blockHtml.setAttribute('color', "#0e7ef6");
                  blockHtml.setAttribute('data-name', "finish");
                  blockHtml.setAttribute('option', "3");
                  blockHtml.setAttribute('height', "0.1");
                  blockHtml.setAttribute('width', "0.90");
                  blockHtml.setAttribute('depth', "0.90");
                  blockHtml.setAttribute('position', `${x} 0.1 ${z}`);
              }
              /* Render start block (red) */
              if (finalArray[mapArray[x][z]] === 2) {
                  blockHtml.setAttribute('color', "#ff2c2c");
                  blockHtml.setAttribute('data-name', "start");
                  blockHtml.setAttribute('option', "2");
                  blockHtml.setAttribute('height', "0.1");
                  blockHtml.setAttribute('width', "0.90");
                  blockHtml.setAttribute('depth', "0.90");
                  blockHtml.setAttribute('position', `${x} 0.1 ${z}`);
              }
              /* Render path block (green) */
              if (finalArray[mapArray[x][z]] === 1) {
                  blockHtml.setAttribute('color', "#1ace65");
                  blockHtml.setAttribute('data-name', "path");
                  blockHtml.setAttribute('option', "1");
                  blockHtml.setAttribute('height', "0.1");
                  blockHtml.setAttribute('width', "0.90");
                  blockHtml.setAttribute('depth', "0.90");
                  blockHtml.setAttribute('position', `${x} 0.1 ${z}`);
              }
              /* Render wall block (0) */
              if (finalArray[mapArray[x][z]] === 0) {
                  blockHtml.setAttribute('color', "#333333");
                  blockHtml.setAttribute('data-name', "wall");
                  blockHtml.setAttribute('option', "0");
                  blockHtml.setAttribute('height', "5");
              }
              container.appendChild(blockHtml);
          });
      });
      document.getElementById('timer').setAttribute('visible', true);
      scene.appendChild(container);
      document.querySelector('[cursor]').setAttribute('cursor', `fuseTimeout: 1000; fuse: true;`);
  }
  /* === Setting player position === */
  function setPlayerPosition() {
      var start = document.querySelector('a-box[option="2"]');
      var player = document.getElementById('player');
  
      if (!AFRAME.components['get-start-position']) {
          AFRAME.registerComponent('get-start-position', {
              init: function () {
                  var position = this.el.object3D.position;
                  player.setAttribute('position', {
                      x: position.x,
                      y: "0.5",
                      z: position.z
                  });
                  player.setAttribute('rotation', "0 180 0");
                  document.querySelector('a-scene').enterVR();
              }
          });
      }
      
      start.setAttribute('get-start-position', '');
  }
  /* === Logic for saving best time / hiscore and his owner === */
  function saveBestTimeOwner(map, index) {
      var ownerPanel = document.getElementById('hiscore-owner'),
          saveButton = document.getElementById('save-owner'),
          ownerName = document.getElementById('owner-name');
  
      document.querySelector('a-scene').exitVR();
      var randomId = getRandomId();
  
      if (map && typeof index === 'number' || map && typeof index === 'string') {
          if (loadMap[index].id === map) {
              if (getSeconds(loadMap[index].time) > getSeconds(time) || getSeconds(loadMap[index].time) === 0 || Number.isNaN(getSeconds(loadMap[index].time))) {
                  ownerPanel.classList.remove('hidden');
                  saveButton.addEventListener('click', function () {
                      if (newCreatedMap && newCreatedMapIndex !== '') {
                          map = newCreatedMap.id;
                          index = newCreatedMapIndex;
                      }
                      if (loadedMapObj && loadedMapIndex) {
                          map = loadedMapObj;
                          index = loadedMapIndex;
                      }
                      if (ownerName.value !== '') {
                          ownerSaved = true;
  
                          loadMap[index].time = time;
                          loadMap[index].time.owner.id = randomId;
                          loadMap[index].time.owner.name = ownerName.value;
                          loadMap[index].time.owner.date = new Date().getDateString();
                          /* Save new hiscore to localstorage */
                          localStorage.setItem('maps', JSON.stringify(loadMap));
                          resetStats();
                      }
                  });
              } else {
                  resetStats();
              }
          }
      }
  }
  /* === Reset all stats function === */
  function resetStats() {
      var loadedMaps = document.getElementById('loadedMaps');
      var scene = document.querySelector('a-scene');
  
      document.querySelector('.preview').classList.add('hidden');
      document.querySelector('.editor').classList.remove('hidden');
      document.querySelector('[cursor]').setAttribute('visible', true);
      document.getElementById('loadedMaps').classList.remove('hidden');
      document.querySelector('.panel-settings').classList.add('hidden');
      document.getElementById('grid-settings').value = '';
      document.getElementById('owner-name').value = '';
      document.getElementById('grid-settings').removeAttribute('disabled');
      document.getElementById('settings-place').classList.remove('hidden');
      document.getElementById('hiscore-owner').classList.add('hidden');
      document.getElementById('timer').setAttribute('value', '00:00:00');
      document.querySelector('.creator-content').classList.add('collapsed');
      document.getElementById('creator-place').classList.add('hidden');
      renderFinish = false;
      countClick = 0;
      countClick = 0;
      time = {};
      hours = 0;
      minutes = 0;
      seconds = 0;
      moveCounter = 0;
      pathCountClick= 0;
      renderFinish = false;
      startSelected = false;
      pathSelected = false;
      finishConverting = false;
      hover = false;
      ownerSaved = false;
      vrView = false;
      finishSet = false;
      finishSelected = false;
      pathArray = [];
      availablePathsArray = [];
      activePathArray = [];
      labyrinthArray = [];
      disableIfSelected = [];
      validPathOptions = [];
      validGridOptions = [];
      validPaths = [];
      countSelectedBlocks = 0;
      finalArray = [];
      newArr = [];
      mapArray = [];
      maps = [];
      loadedMapIndex = '';
      loadedMapObj = {};
      newCreatedMap = undefined;
      newCreatedMapIndex = '';
      Array.from(scene.children).forEach(function (el) {
          if (typeof el !== 'undefined') {
              if (el.tagName !== 'CANVAS' && !el.classList.contains('a-loader-title') && !el.classList.contains('a-enter-vr') && !el.classList.contains('a-orientation-modal') && el.tagName !== "A-ENTITY") {
                  scene.removeChild(el);
              }
          }
      });
      document.getElementById('render-grid').innerHTML = '';
      Array.from(loadedMaps.children).forEach(function (el) {
          if (typeof el !== 'undefined') {
              if (!el.classList.contains('close-btn')) {
                  loadedMaps.removeChild(el);
              }
          }
      });
      loadMap = JSON.parse(localStorage.getItem('maps')) ? JSON.parse(localStorage.getItem('maps')) : [];
      document.getElementById('creator-name').value = '';
      document.getElementById('render-grid').value = '';
      document.querySelector('.maps-counter').innerHTML = `( ${loadMap.length} )`;
      document.getElementById('loadedMaps').classList.add('hidden');
  }
  /* === Player control - moving function === */
  function movePlayer(mapId, mapIndex) {
      var player = document.getElementById('player');
      var pathBoxes = document.querySelectorAll('a-box[option="1"], a-box[option="3"]');
  
      if (!AFRAME.components['movement']) {
          AFRAME.registerComponent('movement', {
              init: function () {
                  var position = this.el.object3D.position;
                  
                  this.el.addEventListener('click', function (evt) {
                      /* Count will start after teleporting to first block */
                      if (moveCounter === 0) {
                          moveCounter = 1;
                          count();
                      }
                      /* Triggering finish block */
                      if (evt.target.dataset.name === 'finish') {
                          clearTimeout(timer);
                          if (newCreatedMapIndex) {
                              saveBestTimeOwner(newCreatedMap.id, newCreatedMapIndex);
                          }
                          if (loadedMapIndex !== '') {
                              saveBestTimeOwner(loadedMapObj.id, loadedMapIndex);
                          }
                      }
                      /* Set new color on path block for a 20sec (something like footsteps) */
                      if (evt.target.dataset.name === 'path') {
                          evt.target.setAttribute('color', "#e68815");
                      }
                      setTimeout(function () {
                          evt.target.setAttribute('color', "#1ace65");
                      }, 20000);
                      player.setAttribute('position', {
                          x: position.x,
                          y: "0.5",
                          z: position.z
                      });
                  });
              }
          });
      }
      
      Array.from(pathBoxes).forEach(function (e, i) {
          pathBoxes[i].setAttribute('movement', '');
      });
  }
  
  function renderLabyrinth(loadedLab, indexMap) {
      if (loadedLab && indexMap) {
          loadedLab = loadedMapObj;
          indexMap = loadedMapIndex;
          finishConverting = true;
          renderFinish = true;
          startSelected = true;
      }
      if (renderFinish && startSelected && finishConverting) {
          var preview3D = document.querySelector('.preview'),
              editor = document.querySelector('.editor');
  
          /* New or loaded maps */
          if (typeof loadedLab !== 'undefined' && indexMap) {
              generate3DBlocks(loadedLab.map, loadedLab.final);
              setPlayerPosition();
              movePlayer(loadedLab.id, indexMap);
          } else {
              createArrayToSave();
              /* loadMap is an Array, that will be saved in localstorage, with all data like:
                  - person that created map
                  - finalArray to render correct places (wall, start, path, finish)
                  - mapArray (3x3 example): [
                      [0, 1, 2, 3, 4],
                      [5, 6, 7, 8, 9],
                      [10, 11, 12, 13, 14],
                      [15, 16, 17, 18, 19],
                      [20, 21, 22, 23, 24]
                  ]
                  - best time (owner, time, date)
              */
              loadMap.push({
                  id: getRandomId(),
                  creator: {
                      id: getRandomId(),
                      name: document.getElementById('creator-name').value,
                      date: new Date().getDateString()
                  },
                  final: finalArray.reverse(),
                  map: mapArray.reverse(),
                  time: time
              });
  
              newCreatedMap = loadMap[loadMap.length - 1];
              newCreatedMapIndex = loadMap.length - 1;
  
              localStorage.setItem('maps', JSON.stringify(loadMap));
  
              generate3DBlocks(loadMap[Object.keys(loadMap).length - 1].map, loadMap[Object.keys(loadMap).length - 1].final);
              setPlayerPosition();
              movePlayer(loadMap[loadMap.length - 1].id, loadMap.length - 1);
          }
  
          preview3D.classList.remove('hidden');
          editor.classList.add('hidden');
          document.getElementById('loadedMaps').classList.add('hidden');
          document.getElementById('creator-place').innerHTML = '';
      }
  }
  /* === Load saved maps function === */
  function loadMapsPreview(m) {
      var container = document.createDocumentFragment(),
          loadedMaps = document.getElementById('loadedMaps');
          
      if (!loadedMaps.classList.contains('hidden')) {
          loadedMaps.classList.add('hidden');
      }
      m.forEach(function (mapObject, mapIndex) {
          var final = mapObject.final,
              mapBox = document.createElement('div'),
              creatorDiv = document.createElement('p'),
              placeWidth = Math.sqrt(final.length) * (25 + 2) + 20,
              creatorName = typeof mapObject.creator !== 'undefined' ? mapObject.creator.name : 'NotDefined',
              validName = typeof mapObject.time !== 'undefined' && typeof mapObject.time.owner !== 'undefined' && typeof mapObject.time.owner.name !== 'undefined' ? mapObject.time.owner.name : 'Not played yet',
              validTime = typeof mapObject.time !== 'undefined' ? mapObject.time.stringTime : '00:00:00',
              validDate = typeof mapObject.time !== 'undefined' && typeof mapObject.time.owner !== 'undefined' && typeof mapObject.time.owner.date !== 'undefined' ? mapObject.time.owner.date : '-';
  
          mapBox.classList.add('map-preview');
          mapBox.setAttribute('style', `min-width: ${placeWidth}px; width: ${placeWidth}px`);
          mapBox.dataset.index = mapIndex;
  
          for (var i = 0; i < final.length; i++) {
              var mapElement = document.createElement('div');
  
              mapElement.dataset.pathIndex = i;
              switch (final[i]) {
                  case 0:
                      mapElement.classList.add('path', 'disabled');
                      break;
                  case 1:
                      mapElement.classList.add('path', 'selected');
                      break;
                  case 2:
                      mapElement.classList.add('path', 'start-selected');
                      break;
                  case 3:
                      mapElement.classList.add('path', 'finish');
                      break;
              }
              mapBox.prepend(mapElement);
          }
          
          creatorDiv.classList.add('creator-name');
          creatorDiv.innerHTML  += '<span>Created by: <br></span>' + creatorName;
          mapBox.innerHTML += `
              <div class="hiscore-box">
                  <p class="hi-score-owner"><span>Name:</span> <br> ${validName}</p>
                  <p class="hi-score"><span>Best time:</span> <br> ${validTime}</p>
                  <p class="hi-score-date"><span>Date:</span> <br> ${validDate}</p>
              </div>
          `;
          mapBox.prepend(creatorDiv);
          container.appendChild(mapBox);
      });
      document.getElementById('loadedMaps').appendChild(container);
      Array.from(document.querySelectorAll('.map-preview')).forEach(function (clickedMap) {
          clickedMap.addEventListener('click', function (e) {
              if (e.target.tagName !== 'P') {
                  document.querySelector('.editor').classList.remove('blury');
                  loadedMapIndex = e.target.dataset.index;
                  loadedMapObj = loadMap[loadedMapIndex];
                  renderLabyrinth(true, true);
                  document.querySelector('a-scene').setAttribute('visible', true);
              }
          });
      });
  }
  
  window.onload = function () {
      var loaded = document.getElementById('loadedMaps'),
          preview3D = document.querySelector('.preview'),
          gridSelect = document.getElementById('grid-settings'),
          lastChange = 0;
  
      preview3D.classList.add('hidden');
      document.querySelector('a-scene').setAttribute('visible', false);
      loaded.classList.add('hidden');
      applyLabyrinth = document.getElementById('apply');
      document.getElementById('timer').setAttribute('visible', false);
      document.querySelector('.maps-counter').innerHTML = `( ${loadMap.length} )`;
      document.querySelector('a-entity').setAttribute('test-new', '');
  
      function render2DPlane(value) {
          var gridSettingsNumber;
          if (vrView) {
              gridSettingsNumber = parseFloat(value);
          } else {
              gridSettingsNumber = parseFloat(value);
          }
          var roundWalls = gridSettingsNumber + 2,
              sumOfPaths = roundWalls * roundWalls,
              vrInfo = document.getElementById('vr-info');
          var place;
              if (vrView) {
                  place = document.getElementById('render-grid');
              } else {
                  place = document.getElementById('creator-place');
              }
          var placeWidth = roundWalls * (25 + 2),
              fragment = document.createDocumentFragment();
  
          document.getElementById('timer').setAttribute('visible', false);
  
          if (gridSettingsNumber > 0) {
              if (lastChange !== gridSettingsNumber) {
                  place.innerHTML = '';
              }
  
              lastChange = parseFloat(gridSettingsNumber);
  
              for (var p = 0; p < sumOfPaths; p++) {
                  var path;
                  if (vrView && value) {
                      path = document.createElement('a-plane');
                      path.classList.add('grid-path');
                      path.setAttribute('data-path-index', p);
                  } else {
                      path = document.createElement('div');
                      path.classList.add('path');
                      path.dataset.pathIndex = p;
                  }
                  pathArray.push(p);
  
                  fragment.appendChild(path);
              }
              place.appendChild(fragment);
              document.querySelector('.panel-settings').classList.remove('hidden');
  
              place.style.width = placeWidth + 'px';
              renderFinish = true;
          }
  
          for (var p = 0; p < pathArray.length; p = p+roundWalls) {
              newArr.push(pathArray[p]);
          }
          if (renderFinish) {
              var prev = 0,
                  outer = Array.from(pathArray).forEach(function (item) {
                  var pathItem;
                  if (vrView) {
                      pathItem = place.querySelectorAll('.grid-path')[item];
                  } else {
                      pathItem = place.querySelectorAll('.path')[item];
                  }
                  var pathIndex = parseFloat(pathItem.dataset.pathIndex),
                      isOutside = pathItem.dataset.pathIndex <= roundWalls
                      || pathItem.dataset.pathIndex - prev === roundWalls
                      || pathItem.dataset.pathIndex > sumOfPaths - roundWalls
                      && pathItem.dataset.pathIndex < sumOfPaths + 1,
                      rightSide = pathItem.dataset.pathIndex - prev === roundWalls - 1;
  
                  if (isOutside) {
                      prev = pathItem.dataset.pathIndex;
                  }
                  if (isOutside || rightSide) {
                      pathItem.classList.add('disabled');
                  }
                  if (pathItem.className.indexOf('disabled') < 0) {
                      var right = pathIndex + 1,
                          left = pathIndex - 1,
                          bottom = pathIndex + parseFloat(roundWalls),
                          top = pathIndex - parseFloat(roundWalls),
                          bottomRight = pathIndex + parseFloat(roundWalls) + 1,
                          bottomLeft = pathIndex + parseFloat(roundWalls) - 1,
                          topRight = pathIndex - parseFloat(roundWalls) + 1,
                          topLeft = pathIndex - parseFloat(roundWalls) - 1;
  
                      /* Generate next choosable option */
                      validPathOptions[pathIndex] = [pathIndex, right, left, bottom, top];
                      /* Generate possible walls array for every block index */
                      disableIfSelected[pathIndex] = {
                          TopLeft: {
                              options: [pathIndex, right, topRight],
                              disable: top
                          },
                          TopRight: {
                              options: [pathIndex, left, topLeft],
                              disable: top
                          },
                          BottomLeft: {
                              options: [pathIndex, left, bottomLeft],
                              disable: bottom
                          },
                          BottomRight: {
                              options: [pathIndex, right, bottomRight],
                              disable: bottom
                          },
                          LeftTop: {
                              options: [pathIndex, top, topLeft],
                              disable: left
                          },
                          LeftBottom: {
                              options: [pathIndex, bottom, bottomLeft],
                              disable: left
                          },
                          RightTop: {
                              options: [pathIndex, top, topRight],
                              disable: right
                          },
                          RightBottom: {
                              options: [pathIndex, bottom, bottomRight],
                              disable: right
                          }
                      };
                      availablePathsArray.push(pathIndex);
                      activePathArray[pathIndex] = true;
                      /* Create standard option in labyrinthArray for choosable blocks */
                      labyrinthArray.push({
                          active: true,
                          selected: false,
                          pathId: pathIndex,
                          option: 0
                      });
                  } else {
                      activePathArray[pathIndex] = false;
                      /* Create standard option in labyrinthArray for not choosable blocks */
                      labyrinthArray.push({
                          active: false,
                          selected: false,
                          pathId: pathIndex,
                          option: 0
                      });
                  }
                  if (vrView) {
                      if (!AFRAME.components['selectblock']) {
                          AFRAME.registerComponent('selectblock', {
                              init: function () {
                                  var index = this.el.dataset.pathIndex,
                                      el = this.el;
  
                                  this.el.addEventListener('click', function (evt) {
                                      if (labyrinthArray[index].active && el.classList.contains('clickable')) {
                                          selectLabyrinthPath(evt, true);
                                      }
                                  });
                              }
                          });
                      }
                  } else {
                      if (hover) {
                          pathItem.addEventListener('mouseover', function (event) {
                              if (labyrinthArray[pathIndex].active && startSelected) {
                                  selectLabyrinthPath(event, false);
                              }
                          });
                      }
                      pathItem.addEventListener('click', function (event) {
                          if (event.type === 'click') {
                              if (labyrinthArray[pathIndex].active) {
                                  selectLabyrinthPath(event, true);
                              }
                          }
                      });
                  }
              });
              document.getElementById('grid-settings').setAttribute('disabled', true);
  
              mapArray = [...Array(roundWalls).keys()].reduce((prev, curr) => {
                  return [...prev,  pathArray.slice(roundWalls * curr, roundWalls * curr + roundWalls)];
              }, []);
  
              if (vrView && value) {
                  mapArray.forEach(function (e, x) {
                      mapArray.forEach(function (a, y) {
                          var planes = document.querySelectorAll('.grid-path'),
                              index = mapArray[x][y];
                              planes[index].setAttribute('scale', '0 0 0');
                              planes[index].setAttribute('animation', 'easing: easeOutElastic; from: 0 0 -' + index + '; to: 0.7 0.7 1; property: scale; dur: 3500; elasticity: 600; delay: ' + index + '0');
                              planes[index].setAttribute('rotate', '0 0 0');
                              planes[index].setAttribute('color', '#af71db');
                              if (labyrinthArray[index].active && countClick === 0) {
                                  planes[index].classList.add('clickable');
                              }
                              if (planes[index].classList.contains('disabled')) {
                                  planes[index].setAttribute('color', '#333333');
                              }
                              planes[index].setAttribute('position', `${x} ${y} -1`);
                      });
                  });
                  var gridPath = document.querySelectorAll('.grid-path');
                  Array.from(gridPath).forEach(function (e, i) {
                      gridPath[i].setAttribute('selectblock', '');
                  });
                  vrInfo.setAttribute('value','Create your own labyrinth');
                  vrInfo.setAttribute('animation','property: position; from: 0 2.7 -1.4; to: 0 -20 -1.4; easing: easeInQuart; delay: 3000; dur: 1500');
              }
              document.getElementById('render-grid').setAttribute('animation', `easing: easeOutElastic; from: -${Math.round(value / 2)} 0 -30; to: -${Math.round(value / 2)} 0 -25; property: position; dur: 1500;`);
              document.querySelector('[cursor]').setAttribute('cursor', `fuseTimeout: 300; fuse: true;`);
              document.getElementById('settings-place').classList.add('hidden');
              document.getElementById('creator-place').classList.remove('hidden');
          }
          if (vrView) {
              var gridValue = document.querySelectorAll('.get-grid-value');
              if (!AFRAME.components['rendergrid']) {
                  AFRAME.registerComponent('rendergrid', {
                      init: function () {
                          var selectedElement = this.el.object3D,
                              value = this.el.dataset.value;
                          this.el.addEventListener('click', function (evt) {
                              render2DPlane(value);
                              Array.from(gridValue).forEach(function (ele) {
                                  ele.setAttribute('visible', false);
                                  ele.setAttribute('position', '0 -20 5');
                              });
                          });
                      }
                  });
              }
              if (!AFRAME.components['startgame']) {
                  AFRAME.registerComponent('startgame', {
                      init: function () {
                          this.el.addEventListener('click', function () {
                              if (finishSet) {
                                  setRestToDisabled();
                                  convertBlocks();
                                  document.getElementById('error-info-vr').setAttribute('visible', false);
                                  document.getElementById('render-vr').setAttribute('visible', false);
                                  renderLabyrinth();
                              } else {
                                  document.getElementById('error-info-vr').setAttribute('visible', true);
                              }
                          });
                      }
                  });
              }
              document.getElementById('start-game').setAttribute('startgame', '');
  
              if (!AFRAME.components['togglecursor']) {
                  AFRAME.registerComponent('togglecursor', {
                      init: function () {
                          var cursor = document.querySelector('[cursor]'),
                              visible = true;
                          this.el.addEventListener('click', function (e) {
                              if (visible) {
                                  cursor.setAttribute('visible', false);
                                  visible = false;
                              } else {
                                  cursor.setAttribute('visible', true);
                                  visible = true;
                              }
                          });
                      }
                  });
              }
              document.getElementById('toggle-cursor').setAttribute('togglecursor', '');
              
              Array.from(gridValue).forEach(function (e, i) {
                  gridValue[i].setAttribute('rendergrid', '');
              });
              preview3D.classList.remove('hidden');
          } else {
              /* open vr creator */
              document.getElementById('vr-creator').addEventListener('click', function () {
                  var gridValue = document.querySelectorAll('.get-grid-value');
                  vrView = true;
                  document.getElementById('render-vr').setAttribute('visible', true);
                  document.querySelector('a-scene').setAttribute('visible', true);
                  document.getElementById('player').setAttribute('position', "0 2 0");
                  Array.from(gridValue).forEach(function (ele) {
                      ele.setAttribute('visible', true);
                  });
                  gridValue[0].setAttribute('position', "-0.3 2.3 -1.14");
                  gridValue[1].setAttribute('position', "0 2.3 -1.14");
                  gridValue[2].setAttribute('position', "0.3 2.3 -1.14");
                  vrInfo.setAttribute('value','Chose your labyrinth grid');
                  vrInfo.setAttribute('position','0 2.7 -1.4');
                  vrInfo.removeAttribute('animation');
                  document.getElementById('render-grid').removeAttribute('animation');
                  document.getElementById('render-grid').removeAttribute('position', '0 0 -35');
                  render2DPlane();
                  document.querySelector('a-scene').enterVR();
              });
          }
      }
  
      render2DPlane();
  
      Array.from(gridSelect.children).forEach(function (value, i) {
          gridSelect.children[i].addEventListener('click', function (event) {
              var target = event.target;
              var targetValue = target.dataset.gridValue;
              applyLabyrinth.classList.remove('hidden');
              render2DPlane(targetValue);
          });
      });
  
      document.querySelector('.creator-2d').addEventListener('click', function () {
          var content = document.querySelector('.creator-content');
          if (content.classList.contains('collapsed')) {
              content.classList.remove('collapsed');
          } else {
              content.classList.add('collapsed');
          }
      });
  
      applyLabyrinth.addEventListener('click', function () {
          if (!vrView) {
              var errorMsg = document.getElementsByClassName('apply-error')[0],
                  message = '';
              document.getElementById('render-vr').setAttribute('visible', false);
              
              if ((labyrinthArray.length / 3) - 5 <= countSelectedBlocks || vrView) {
                  errorMsg.style = '';
                  setRestToDisabled();
                  convertBlocks();
                  if (finishSet) {
                      errorMsg.removeAttribute('style');
                      document.querySelector('a-scene').setAttribute('visible', true);
                      renderLabyrinth();
                  } else {
                      message = 'Please select FINISH point';
                      errorMsg.style.display = 'block';
                  }
              } else if (!finishConverting) {
                  message = 'Please select more blocks. <br> <b>Blocks to select:</b> ' + Math.round((labyrinthArray.length / 3) - countSelectedBlocks) + '<br> <b>Selected Blocks: </b>' + countSelectedBlocks;
                  errorMsg.style.display = 'block';
              }
              errorMsg.innerHTML = message;
          }
      });
  
      document.querySelector('a-scene').addEventListener('exit-vr', function () {
          setTimeout(function () {
              if (document.getElementById('hiscore-owner').classList.contains('hidden')) {
                  resetStats();
              }
          }, 500);
          if (document.querySelector('.close-btn')) {
              document.querySelector('.close-btn').remove();
          }
      });
      document.querySelector('a-scene').addEventListener('enter-vr', function () {
          document.getElementById('loadedMaps').innerHTML = '';
      });
  
      document.querySelector('.checkbox').addEventListener('click', function (el) {
          if (!hover) {
              el.target.classList.add('checked');
              hover = true;
          } else {
              el.target.classList.remove('checked');
              hover = false;
          }
      });
  
      modalFunction([document.getElementById('help'), document.getElementById('another-maps')]);
  };