var isConciergerieInitialised = false;
var conciergerie = {
    _langSubdir: "",
    _base16: "0A12B34C56D78E9F",
    _baseClassName: "concierge",

    encode: function (str) {
        var retour = "";
        for (var i = 0; i < str.length; i++) {
            var cc = str.charCodeAt(i);
            var ch = cc >> 4;
            var cl = cc - (ch * 16);
            retour += this._base16[ch] + this._base16[cl];
        }
        return this._baseClassName + retour;
    },

    decode: function (str) {
        var retour = "";
        for (var i = 0; i < str.length; i += 2) {
            var ch = this._base16.indexOf(str.charAt(i));
            var cl = this._base16.indexOf(str.charAt(i + 1));
            retour += String.fromCharCode((ch * 16) + cl);
        }
        return retour;
    },

    init: function () {
        if (!isConciergerieInitialised) {
            var tagsA = document.getElementsByTagName("span");
            for (var i = 0; i < tagsA.length; i++) {
                if (tagsA[i].className.substring(0, this._baseClassName.length) == this._baseClassName) {
                    var url = this.decode(tagsA[i].className.substring(9));

                    if (this._langSubdir != "" && url.match("^/")) {
                        url = "/" + this._langSubdir + url;
                    }

                    var nlink = document.createElement("a");
                    nlink.href = url;

                    if (tagsA[i].getAttribute("target") != null)
                        if (tagsA[i].getAttribute("target").toLowerCase() == "_blank") {
                            nlink.setAttribute("target", "_blank");
                            tagsA[i].removeAttribute("target")
                        }
                    if (tagsA[i].getAttribute("style") != null) {
                        nlink.setAttribute("style", tagsA[i].getAttribute("style"));
                        nlink.style.cssText = tagsA[i].style.cssText;
                        tagsA[i].removeAttribute("style");
                        tagsA[i].style.cssText = "";
                    }
                    for (var j = 0; j < tagsA[i].childNodes.length; j++) {
                        nlink.appendChild(tagsA[i].childNodes[j]);
                    }
                    tagsA[i].appendChild(nlink);
                }
            }
            isConciergerieInitialised = true;
        }
    },

    element: function (id) {
        var element = document.getElementById(id);
        if (element.className.substring(0, this._baseClassName.length) == this._baseClassName) {
            var url = this.decode(element.className.substring(9));

            if (this._langSubdir != "" && url.match("^/")) {
                url = "/" + this._langSubdir + url;
            }

            var nlink = document.createElement("a");
            nlink.href = url;

            if (element.getAttribute("target") != null)
                if (element.getAttribute("target").toLowerCase() == "_blank") {
                    nlink.setAttribute("target", "_blank");
                    element.removeAttribute("target")
                }
            if (element.getAttribute("style") != null) {
                nlink.setAttribute("style", element.getAttribute("style"));
                nlink.style.cssText = element.style.cssText;
                element.removeAttribute("style");
                element.style.cssText = "";
            }
            for (var j = 0; j < element.childNodes.length; j++) {
                nlink.appendChild(element.childNodes[j]);
            }
            element.appendChild(nlink);
        }
    }
}

$(document).ready( function () {conciergerie.init();} );
