/**
 * Function toggleHash toggles a specified location.hash value.
 *
 * @param {string} identifier ID to toggle.
 *
 * @return {void}
 */
function toggleHash(identifier) {
    if (window.location.hash == "#" + identifier) window.location.hash = "";
    else window.location.hash = "#" + identifier;
}

document.addEventListener("DOMContentLoaded", function() {

    let translations = {
        "en" : {
            "More"                      : "More",
            "MoreAtMuseumDigital"       : "museum-digital",
            "ObjectAtMuseumDigital"     : "Object entry at museum-digital",
            "InstitutionAtMuseumDigital": "Institution at museum-digital",
            "CollectionAtMuseumDigital" : "Collection at museum-digital",
            "ExhibitionAtMuseumDigital" : "Exhibition at museum-digital",
            "EventAtMuseumDigital"      : "Event at museum-digital",
            "Collection"                : "Collection",
            "Collections"               : "Collections",
            "object_material_technique" : "Material / Technique",
            "object_dimensions"         : "Dimensions",
            "Metadata"                  : "Metadata",
            "LastUpdated"               : "Last Updated",
            "Licence"                   : "Licence",
            "Tags"                      : "Tags",
            "People"                    : "People",
            "Places"                    : "Places",
            "Times"                     : "Times",
            "Search"                    : "Search",
            "SearchingFor"              : "Searching for",
            "Hits"                      : "Hits:",
            "ResultsInPages"            : "Results in texts",
            "ResultsInObjects"          : "Results in objects",
            "... who"                   : "... who",
            "... where"                 : "... where",
            "... when"                  : "... when",
            "eventType1"                : "Created",
        },
        "de" : {
            "More"                      : "Mehr",
            "MoreAtMuseumDigital"       : "museum-digital",
            "ObjectAtMuseumDigital"     : "Objekt bei museum-digital",
            "InstitutionAtMuseumDigital": "Institution bei museum-digital",
            "CollectionAtMuseumDigital" : "Sammlung bei museum-digital",
            "ExhibitionAtMuseumDigital" : "Ausstellung bei museum-digital",
            "EventAtMuseumDigital"      : "Veranstaltung bei museum-digital",
            "Collection"                : "Sammlung",
            "Collection"                : "Sammlungen",
            "object_material_technique" : "Material / Technik",
            "object_dimensions"         : "Ausmaße",
            "Metadata"                  : "Metadaten",
            "LastUpdated"               : "Zuletzt geupdatet",
            "Licence"                   : "Lizenz",
            "Tags"                      : "Schlagworte",
            "People"                    : "Personen",
            "Places"                    : "Orte",
            "Times"                     : "Zeiten",
            "Search"                    : "Suche",
            "SearchingFor"              : "Suche nach",
            "Hits"                      : "Treffer:",
            "ResultsInPages"            : "Treffer in Texten",
            "ResultsInObjects"          : "Treffer in Objekten",
            "... who"                   : "... wer",
            "... where"                 : "... wo",
            "... when"                  : "... wann",
            "eventType1"                : "Hergestellt",
        }
    };

    var debugging = false;

    /**
     * Function queryPage queries a web page and runs the specified function over the output.
     *
     * @param string   url   URL to query.
     * @param function func  Callback function to run on the request after loading.
     * @param boolean  debug Enable / disable debug mode.
     *
     * @return boolean
     */
    function queryPage (url, func, debug = false) {

        let request     = new XMLHttpRequest();
        request.open('GET', url);
        request.setRequestHeader("Cache-Control", "no-cache");
        request.responseType = 'htm';
        request.send();
        request.onload = function() {

            func(request, debug);
        };

    }

    /**
     * Returns a requested translation from an array in the currently used language.
     *
     * @param mixed[] list      Translation variable.
     * @param string  specifier Specifies which translation to get.
     *
     * @return string
     */
    function getTranslation (list, specifier) {

        let preferedLang = document.getElementsByTagName("html")[0].getAttribute("lang");

        if (list[preferedLang] !== undefined && list[preferedLang][specifier] !== null) return list[preferedLang][specifier];
        return list["en"][specifier];

    }

    /**
     * Function to get all GET variables passed by user
     * Based on gion_13's answer at https://stackoverflow.com/questions/12049620/how-to-get-get-variables-value-in-javascript
     *
     * @return string[]
     */
    function getGETvars () {

        var output = {};
        if(document.location.toString().indexOf('?') !== -1) {
            var query = document.location
                           .toString()
                           // get the query string
                           .replace(/^.*?\?/, '')
                           // and remove any existing hash string (thanks, @vrijdenker)
                           .replace(/#.*$/, '')
                           .split('&');

            for(var i = 0, l = query.length; i < l; i++) {
                var aux = decodeURIComponent(query[i]).split('=');
                output[aux[0]] = aux[1];
            }
        }
        return (output);

    }

    /**
     * Open the URL given in the href attribute of the element with the given ID.
     *
     * @param {string} identifier ID of the element with whose href to replace location.href
     *
     * @return {void}
     */
    function replaceWindowById(identifier) {

        var link = document.getElementById(identifier);

        if (link != null) {
            window.location.href = link.href;
        }
    }

    /**
     * Set placeholder for all input element of type="search".
     */
    (function() {
        let toTranslate = document.getElementsByTagName("input");
        for (let i = 0, max = toTranslate.length; i < max; i++) {
            console.log(toTranslate[i].type);
            if (toTranslate[i].type !== "search") continue;
            toTranslate[i].placeholder = getTranslation(translations, "Search");
        }
    })();

    (function() {
        let toTranslate = document.getElementsByClassName("toTranslate");
        for (let i = 0, max = toTranslate.length; i < max; i++) {
            if (debugging === true) console.log(toTranslate[i].getAttribute("data-content"));
            toTranslate[i].textContent = getTranslation(translations, toTranslate[i].getAttribute("data-content"));
        }
    })();

});


