var insymaUtil = {
    firstSib: function (obj) {
        var temp = obj.parentNode.firstChild;
        while (temp.nodeType != 1 && temp.nextSibling != null) {
            temp = temp.nextSibling
        }
        return (temp.nodeType == 1) ? temp : false
    },
    lastSib: function (obj) {
        var temp = obj.parentNode.lastChild;
        while (temp.nodeType != 1 && temp.previousSibling != null) {
            temp = temp.previousSibling
        }
        return (temp.nodeType == 1) ? temp : false
    },
    closestSib: function (obj, direction) {
        var temp;
        if (direction == -1 && obj.previousSibling != null) {
            temp = obj.previousSibling;
            while (temp.nodeType != 1 && temp.previousSibling != null) {
                temp = temp.previousSibling
            }
        } else if (direction == 1 && obj.nextSibling != null) {
            temp = obj.nextSibling;
            while (temp.nodeType != 1 && temp.nextSibling != null) {
                temp = temp.nextSibling
            }
        }
        return (temp.nodeType == 1) ? temp : false
    },
    elmByClass: function (tagName, cName) {
        var el = document.getElementsByTagName(tagName);
        var els = new Array();
        if (cName) {
            for (var i = 0; i < el.length; i++) {
                if (insymaUtil.cssjs("check", el[i], cName)) {
                    els.push(el[i])
                }
            }
        } else {
            for (var j = 0; j < el.length; j++) {
                els.push(el[j])
            }
        }
        return els
    },
    createElm: function (obj, attr, txt, append) {
        var temp = document.createElement(obj);
        if (attr) {
            insymaUtil.setAttr(temp, attr)
        }
        if (txt) {
            insymaUtil.setText(temp, txt);
            if (obj == "a") {
                insymaUtil.setAttr(temp, {
                    title: txt
                })
            }
        }
        if (append) {
            append.appendChild(temp)
        }
        return temp
    },
    getText: function (node) {
        if (!node.hasChildNodes()) {
            return false
        }
        var reg = /^\s+$/;
        var tempObj = node.firstChild;
        while (tempObj.nodeType != 3 && tempObj.nextSibling != null || reg.test(tempObj.nodeValue)) {
            tempObj = tempObj.nextSibling
        }
        return tempObj.nodeType == 3 ? tempObj.nodeValue : false
    },
    setText: function (obj, txt) {
        try {
            if (obj.firstChild == null) {
                obj.appendChild(document.createTextNode(txt))
            } else {
                if (obj.firstChild.nodeType == 3) {
                    obj.removeChild(obj.firstChild)
                }
                obj.appendChild(document.createTextNode(txt))
            }
        } catch (e) {}
    },
    setAttr: function (obj, attr) {
        for (var i in attr) {
            if (/class/i.test(i)) {
                insymaUtil.cssjs("add", obj, attr[i])
            } else {
                obj.setAttribute(i, attr[i])
            }
        }
    },
    cssjs: function (a, o, c1, c2) {
        switch (a) {
        case 'swap':
            o.className = !insymaUtil.cssjs('check', o, c1) ? o.className.replace(c2, c1) : o.className.replace(c1, c2);
            break;
        case 'add':
            if (!insymaUtil.cssjs('check', o, c1)) {
                o.className += o.className ? ' ' + c1 : c1
            }
            break;
        case 'remove':
            if (insymaUtil.cssjs('check', o, c1)) {
                var rep = o.className.match(' ' + c1) ? ' ' + c1 : c1;
                o.className = o.className.replace(rep, '')
            }
            break;
        case 'check':
            var found = false;
            var temparray = o.className.split(' ');
            for (var i = 0; i < temparray.length; i++) {
                if (temparray[i] == c1) {
                    found = true
                }
            }
            return found;
            break
        }
    },
    getQuerystring: function (query) {
        if (document.location.search.indexOf(query) > 0) {
            var querystring = document.location.search;
            var pairs = querystring.substring(1).split("&");
            for (var i = 0; i < pairs.length; i++) {
                var varName = pairs[i].split('=')[0];
                var varValue = pairs[i].split('=')[1];
                if (varName == query) {
                    if (typeof (varValue) != 'undefined') {
                        return varValue
                    } else {
                        return true
                    }
                }
            }
        } else {
            return false
        }
    },
    setQuerystring: function (query, qValue) {
        if (document.location.search.indexOf("?") > -1) {
            if (insymaUtil.getQuerystring(query)) {
                if (insymaUtil.getQuerystring(query) != true) {
                    return document.location.href.replace(query + "=" + insymaUtil.getQuerystring(query), query + "=" + qValue)
                } else {
                    return document.location.href.replace(query, query + "=" + qValue)
                }
            } else {
                if (document.location.href.indexOf("#") > -1) {
                    return document.location.href.split("#")[0] + "&" + query + "=" + qValue
                } else {
                    return document.location + "&" + query + "=" + qValue
                }
            }
        } else {
            if (document.location.href.indexOf("#") > -1) {
                return document.location.href.split("#")[0] + "?" + query + "=" + qValue
            } else {
                return document.location + "?" + query + "=" + qValue
            }
        }
    },
    getPageSize: function () {
        var xScroll, yScroll;
        if (window.innerHeight && window.scrollMaxY) {
            xScroll = window.innerWidth + window.scrollMaxX;
            yScroll = window.innerHeight + window.scrollMaxY
        } else if (document.body.scrollHeight > document.body.offsetHeight) {
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight
        } else {
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight
        }
        var windowWidth, windowHeight;
        if (self.innerHeight) {
            if (document.documentElement.clientWidth) {
                windowWidth = document.documentElement.clientWidth
            } else {
                windowWidth = self.innerWidth
            }
            windowHeight = self.innerHeight
        } else if (document.documentElement && document.documentElement.clientHeight) {
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight
        } else if (document.body) {
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight
        }
        if (yScroll < windowHeight) {
            pageHeight = windowHeight
        } else {
            pageHeight = yScroll
        }
        if (xScroll < windowWidth) {
            pageWidth = xScroll
        } else {
            pageWidth = windowWidth
        }
        arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
        return arrayPageSize
    },
    pause: function (ms) {
        var date = new Date();
        curDate = null;
        do {
            var curDate = new Date()
        } while (curDate - date < ms)
    },
    mailDecoder: function () {
        var atags = document.getElementsByTagName("a");
        var decoded = "";
        for (var i = 0; i < atags.length; i++) {
            if (atags[i].href.indexOf("L_") > -1) {
                var link = atags[i].href.substring(atags[i].href.indexOf("L_") + 2);
                link = link.replace(/__/g, ".").replace(/--/g, "@");
                decoded = insymaUtil.decodeMail(link);
                if (atags[i].innerHTML.indexOf("L_") > -1 && atags[i].innerHTML.indexOf("L_") < 2) {
                    atags[i].innerHTML = decoded
                }
                if (atags[i].title.indexOf("L_") > -1) {
                    atags[i].title = decoded
                }
                atags[i].href = 'mailto:' + decoded.replace(">naps/<","").replace("E3%naps/C3%","");
            }
        }
        
        var spantags = insymaUtil.elmByClass('span', 'encoding_mail');
        for (var i = 0; i < spantags.length; i++) {
            if (spantags[i].innerHTML.indexOf("L_") > -1) {
                var link = spantags[i].innerHTML.substring(spantags[i].innerHTML.indexOf("L_") + 2);
                link = link.replace(/__/g, ".").replace(/--/g, "@");
                decoded = insymaUtil.decodeMail(link);
                
                spantags[i].innerHTML = decoded;
            }
        }
    },
    decodeMail: function (adresse) {
        var result = "";
        var chr;
        for (var i = 0; i < adresse.length; i++) {
            chr = adresse.charAt(i);
            result = chr + result
        }
        return result
    },
    addEvent: function (elm, evType, fn, useCapture) {
        if (elm.addEventListener) {
            elm.addEventListener(evType, fn, useCapture);
            return true
        } else if (elm.attachEvent) {
            var r = elm.attachEvent('on' + evType, fn);
            return r
        } else {
            elm['on' + evType] = fn
        }
    }
};
insymaUtil.addEvent(window, "load", insymaUtil.mailDecoder, false);
