/**
 * /script/script.js
 * @version 1.6
 * @desc Main script file
 * @author Fándly Gergő Zoltán (gergo@systemtest.tk, systemtest.tk)
 * @copy 2017 Fándly Gergő Zoltán
 * License:
    sQuiz for creating small jQuery based quizs in an implementable way
    Copyright (C) 2017  Fándly Gergő Zoltán

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 **/

//load language
var lang;
function loadLang(){
    $.each(lang, function(key, val) {
        var repl=$("title").html().replace(new RegExp("{{"+key+"}}", "g"), val);
        $("title").html(repl);
        repl=$("body").html().replace(new RegExp("{{"+key+"}}", "g"), val);
        $("body").html(repl);
        window.cookieconsent_options={
            message: lang.cookie_message,
            dismiss: lang.cookie_dismiss,
        };
    });
}


//switch pages
function goTo(site, pop=false){
    Site=site;
    //slide up
    $("#content").slideUp(function(){
        //load part
        $.ajax({
            url: "./"+site,
            type: "GET",
            success: function(response){
                $("#content").html(response);
                if(!pop){
                    window.history.pushState({"site": site}, null, "./");
                }
            },
            complete: function(){
                loadLang();
                $("#content").slideDown();
            }
        });
    });
}


//toggle element visibility
function toggleElement(sel){
    sel=(sel instanceof $)?sel:$(sel);
    if(sel.css("display")=="none"){
        sel.slideDown();
    }
    else{
        sel.slideUp();
    }
}


//load
$(document).ready(function(){
    $.getJSON("lang.json", function(json){
        lang=json;
        loadLang();
    });
    
    window.addEventListener("popstate", function(e){
        if(e.state!=null){
            goTo(e.state["site"], true);
        }
        else{
            goTo("indexc.html", true);
        }
    });
});
