/**
 * /script/main.js
 * @version 1.1
 * @desc Main script file
 * @author Fándly Gergő Zoltán (gergo@systemtest.tk, systemtest.tk)
 * @copy 2018 Fándly Gergő Zoltán
 * License:
    Systemtest.tk website's.
    Copyright (C) 2018  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/>.
 **/

function disposeMessageOverlay(){
    $("#messageOverlay").fadeOut(function(){
        $("#messageOverlay").html("");
    });
}

function showMessage(html){
    $("#messageOverlay").html(html);
    $("#messageOverlay").fadeIn();
    setTimeout(function(){
        disposeMessageOverlay();
    }, 5000);
}

function loadMessage(){
    $.ajax({
        url: "./subs/msg.php",
        type: "GET",
        success: function(response){
            if(response!=""){
                showMessage(response);
            }
        }
    });
}

function goTo(site, pop=false){
    $("#content").slideUp(function(){
        $.getScript("./script/js.php?load="+site, function(){
            $.ajax({
                url: "./subs/loader.php",
                type: "GET",
                data: {"load": site},
                success: function(response){
                    $("#content").html(response);
                    document.title=$("#content").children("#title").text();
                    
                    if(!pop){
                        window.history.pushState({"site": site}, null, "/"+site);
                    }
                },
                complete: function(){
                    $("#content").slideDown(function(){
                        prepareSite();
                    });
                }
            });
        });
    });
}

function prepareSite(){
    //smooth scroll
    $('a[href^="#"]').on('click', function(event) {
        var target = $(this.getAttribute('href'));
        if( target.length ) {
            event.preventDefault();
            $('html, body').stop().animate({
                scrollTop: target.offset().top
            }, 500);
        }
    });
    
    //disable ajax forms submit
    $(".ajaxform").submit(function(e){
        e.preventDefault(); //prevent classic submit
    });
    
    //enable footable on certain tables
    $(".footable").footable();
}

function toggleDropdown(content){
    if($(content).css("display")=="none"){
        $(content).slideDown();
    }
    else{
        $(content).slideUp();
    }
}

jQuery(function($){
    window.addEventListener("popstate", function(e){
        if(e.state!=null){
            goTo(e.state["site"], true);
        }
        else{
            goTo("", true);
        }
    });
    prepareSite();
    loadMessage();
});

//quill loading functions
function loadQuill(object, delta){
    var tmp=$("<div></div>");
    (new Quill(tmp[0])).setContents(JSON.parse(delta));
    $(object).html(tmp.html());
}

//add leading zeros
function checkTime(t){
    if(t<10){
        t="0"+t;
    }
    return t;
}
