const COOKIE_PREFIX = "FLOSS_MANUALS_xinha_localisation_";
const ELEMENT_PREFIX = 'location_';
const CONTENTS_ID = 'fm-contents';
/* map languages to writing system directions. This is not possible
 for languages that do not stick to a single system, of which there
 are surprisingly many.
*/

var fm_directional_languages = {
    ar: 'RTL',  // arabic (many variants)
    dv: 'RTL',  // dhivehi, maldives islands
    fa: 'RTL',  // farsi
    //ha: 'RTL',  // hausa, west africa, particularly niger and nigeria
    he: 'RTL',  // hebrew
    ps: 'RTL',  // pashto
    ur: 'RTL',  // urdu, pakistan
    yi: 'RTL'   // yiddish, israel
};


function fm_set_cookie(key, value){
    var cookie;
    key = COOKIE_PREFIX + key;
    if (value == 'auto'){
        cookie = (key + "=; path=/; expires=Thu 1 Jan 1970 00:00:01 GMT");
    }
    else {
        cookie = (key + "=" + value +
                  "; path=/; expires=Fri 1 Jan 2038 10:49:15 GMT");
    }
    document.cookie = cookie;
    //alert(document.cookie);
}

//XXX duplicate from my_config.js -- should be shared.
function fm_parse_cookies(){
    var s = document.cookie;
    var re = new RegExp(COOKIE_PREFIX + "(\\w+)=(\\w+)", "g");
    var cookies = {};
    var c;
    while ((c = re.exec(s)) != null){
        cookies[c[1]] = c[2];
    }
    return cookies;
}

function fm_cookies_set_form(){
    var cookies = fm_parse_cookies();
    for (var k in cookies){
        var e = document.getElementById(ELEMENT_PREFIX + k);
        //alert(k + ' ' + ELEMENT_PREFIX + ' ' + cookies[k]);
        e.value = cookies[k];
    }
}

function fm_detect_browser_lang(){
    //mozilla has navigator.language; msie has .{user,system,browser}Language
    var attrs = ['language', 'userLanguage', 'systemLanguage', 'browserLanguage'];
    for (var i = 0; i < attrs.length; i++){
        var x = navigator[attrs[i]];
        if (x != undefined){
            var bits = /(\w{2,3})([-_]\w{2,3})?/.exec(x);
            //alert(bits);
            return bits[1];
        }
    }
}


/* Try to work out the correct page direction and editor interface
 language. If the right cookies have been set, this is simple --
 otheriwse it is done based on the FLOSS manuals URL conventions */

function fm_guess_page_dir(){
    var cookies = fm_parse_cookies();
    var dir = cookies.dir;
    var editor_lang = cookies.editor_lang;
    var lang = fm_guess_language() || editor_lang;

    if (dir == undefined)
        dir = fm_directional_languages[lang] || 'LTR';

    //try to set the editor language, default to
    // a) page language,
    // b) the browser language
    // In non-xinha pages this has no effect
    editor_lang = editor_lang || lang || fm_detect_browser_lang();
    if (editor_lang)
        _editor_lang = editor_lang;
    return dir;
}

function fm_guess_language(){
    // split the url into constituent bits:
    // 'http://xx.flossmanuals.net'
    // '/bin/zzz/Manual_xx/Chapter'
    // '?skin=xinha&lang=xx'
    var lang;
    var host = location.host;
    var path = location.pathname;
    var args = location.search;

    var langs = args.match(/[?&]lang=([a-z][a-z])(?!\w)/i);
    if (! langs){
        langs = host.match(/^([a-z][a-z])\./i);
    }
    if (! langs){
        //should match end of third out of 4 path segments
        langs = path.match(/^\/[^/]+\/[^/]+\/[^_-]+[_-]([a-z][a-z])/i);
    }
    return langs ? langs[1] : undefined;
}

function fm_set_dir(dir){
    if (! dir || dir == 'auto'){
        dir = fm_guess_page_dir();
    }
    document.getElementById(CONTENTS_ID).style.direction = dir;
    //document.getElementById(CONTENTS_ID).style.background = '#FFc';
}