/*
 * Universal functions in javascript.
 * Author: Gaspar Sinai 2009-03-20, Tokyo.
 * Copyright: 2009-03-20
 * Version: 0.1
 */

imghjs = new Image();
imghjs.src = "images/linkhjs.png";

imghhs = new Image();
imghhs.src = "images/linkhhs.png";

imgjjs = new Image();
imgjjs.src = "images/linkjjs.png";

imghju = new Image();
imghju.src = "images/linkhju.png";

imghhu = new Image();
imghhu.src = "images/linkhhu.png";

imgjju = new Image();
imgjju.src = "images/linkjju.png";


util_lastJa = "block";
util_lastHu = "none";

/**
 * This is called from flex ExternalInterface.call("util_getLastJa")
 */
function util_getLastJaHu () {
    return util_lastJa + "," + util_lastHu;
}

/**
 * Toggle all hiddenClass objects to make the visible.
 * Change the imageID image accordingly.
 */
function util_hide (japd, hund) {
    util_lastJa = japd;
    util_lastHu = hund;
    var app = getFlexApp ("flexJaHuApp0");
    if (app != null) {
        if (app.setJaHu != undefined) app.setJaHu (japd, hund);
    }
    var body = document.getElementsByTagName("body");
    var linkhj = document.getElementById("linkhj");
    if (linkhj != null) {
        if (japd === "none" || hund == "none") {
            linkhj.src = imghju.src;
        } else {
            linkhj.src = imghjs.src;
        }
    }
    var linkhh = document.getElementById("linkhh");
    if (linkhh != null) {
        if (japd == "none" && hund != "none") {
            linkhh.src = imghhs.src;
        } else {
            linkhh.src = imghhu.src;
        }
    }
    var linkjj = document.getElementById("linkjj");
    if (linkjj != null) {
        if (japd != "none" && hund == "none") {
            linkjj.src = imgjjs.src;
        } else {
            linkjj.src = imgjju.src;
        }
    }
    util_traverse_hide (body[0], "lang-ja", japd, "lang-hu", hund);
}

function util_traverse_hide (node, jap, japd, hun, hund) {
    if (node.className == jap) node.style.display = japd;
    if (node.className == hun) node.style.display = hund;
    if (node.childNodes == null) return;
    for(var i=0; node.childNodes.length; i++) {
        if (node.childNodes[i] == null) break;
        util_traverse_hide (node.childNodes[i], jap, japd, hun, hund);
    }
}

var util_old_handler = null;

function util_disable_select () {
    if (util_old_handler != null) util_old_handler();
    var body = document.getElementsByTagName("body");
    var timer = setTimeout (
        function  () { 
            var userLang = (navigator.language) ? 
                navigator.language : navigator.userLanguage;
            var j = "none";
            var h = "block";
            if (userLang != null) {
                if (userLang.indexOf ("en") == 0) {
                    j = "none";
                    h = "block";
                } else if (userLang.indexOf ("hu") == 0) {
                    j = "none";
                    h = "block";
                } else if (userLang.indexOf ("ja") == 0) {
                    j = "block";
                    h = "none";
                }
            }
            util_hide (j, h);
        }, 500);
}

function getFlexApp (appName) {
    if (navigator.appName.indexOf ("Microsoft") !=-1) {
        return window[appName];
    } else {
        return document[appName];
    }
}

util_old_handler = window.onload;
window.onload = util_disable_select;

