// Autohyphen library for DOM-endabled browsers.
// Version 2003-12-02.
// (C) Dmitry Koteroff, 2003.
// http://dklab.ru
function Autohyphen() { this.Autohyphen() }
Autohyphen.prototype = {
        tshy:         "&shy;",
        shy:          null,
        x:            '[éüú]',
        g:            '[àå¸èîóûýþÿaeiouy]',
        s:            '(?:sh|ch|qu|[áâãäæçêëìíïðñòôõö÷øùbcdfghjklmnpqrstvwxz])',
        l:            '[üúéàå¸èîóûýþÿáâãäæçéêëìíïðñòôõö÷øùúüaeiouybcdfghjklmnpqrstvwxz]',
        rules:        null,
        filterName:   "hyphenize",
        argCache:     {},
        Autohyphen: function() {
                if (Autohyphen.prototype.shy == null) with (Autohyphen.prototype) {
                        var node = document.createElement("span");
                        node.innerHTML = tshy;
                        shy = node.childNodes[0].nodeValue;
                        rules = [
                                [s+g, g+l],
                                [g+s, s+g],
                                [s+g, s+g],
                                [g+s, s+s+g],
                                [g+s+s, s+g],
                                [g+s+s, s+s+g],
                                [x, l+l],
                                null
                        ];
                }
        },
        hyphenizeTextNode: function (node, forceApply) {
                if (node.nodeType != 3) return false;
                var parent = node.parentNode;
                var apply = forceApply || this.getApplyMethod(parent);
                if (apply) {
                        node.nodeValue = this.hyphenizeText(node.nodeValue);
                        if (apply != true) parent.style.cssText = apply;
                        return true;
                }
                return false;
        },
        hyphenizeTree: function (node, forceApply) {
                var children = node.childNodes;
                var apply = forceApply || this.getApplyMethod(node);
                for (var i=0, len=children.length; i<len; i++) {
                        var child = children[i];
                        if (child.nodeType == 3) {
                                if (apply) {
                                        child.nodeValue = this.hyphenizeText(child.nodeValue);
                                        if (apply != true) node.style.cssText = apply;
                                }
                        } else {
                                this.hyphenizeTree(child, apply);
                        }
                }
        },
        dehyphenizeTree: function (node) {
                var children = node.childNodes;
                for (var i=0, len=children.length; i<len; i++) {
                        var child = children[i];
                        if (child.nodeType == 3) {
                                child.nodeValue = this.dehyphenizeText(child.nodeValue);
                        } else {
                                this.dehyphenizeTree(child);
                        }
                }
        },
        hyphenizeText: function (text) {
                for (var i=this.rules.length-1; i>=0; i--) {
                        var ru = this.rules[i]; if (!ru) continue;
                        var re = new RegExp('('+ru[0]+')(?='+ru[1]+')', 'gi')
                        text = text.replace(re, '$1'+this.shy);
                }
                return text;
        },
        dehyphenizeText: function (text) {
                re = new RegExp(this.shy, 'g');
                return text.replace(re, "");
        },
        getApplyMethod: function(elt) {
                var filter = (elt.currentStyle || elt.style || "").filter;
                if (!filter) return false;
                if (!filter.indexOf || filter.indexOf(this.filterName)<0) return false;
                if (new RegExp(this.filterName+"\s*(\\((.*)\\))?").exec(filter) != null) {
                        var st = RegExp.$2;
                        if (st) return st.replace(/^(["'])(.*)\1$/, "$2");
                        return true;
                }
                return false;
        }
};
function DynamicEnumerator(node, callback) { this.DynamicEnumerator(node, callback) }
DynamicEnumerator.prototype = {
        node: null,
        root: null,
        DynamicEnumerator: function(node, callback) {
                this.callback = callback;
                this.root = node;
                this.node = node;
        },
        process: function() { with (this) {
        	    var passed = 0;
                while (1) {
                        var n = node;
                        while (1) {
                                var nProcessed = (n.processed||0);
                                if (n && n.childNodes.length > nProcessed) {
                                        this.callback(n.childNodes[nProcessed]);
                                        passed++;
                                        n.processed = nProcessed + 1;
                                        node = n.childNodes[nProcessed];
                                        break;
                                } else if (!n || n == root) {
                                        return passed;
                                } else {
                                        n = n.parentNode;
                                }
                        }
                }
        }}
};
document.hyphenized = 0;
function Autohyphen_onStart() {
        document.hyphenized++;
        if (document.hyphenized > 1) return;
        var autohyphen = new Autohyphen();
        var enumerator = new DynamicEnumerator(document.body, function(node) {
                autohyphen.hyphenizeTextNode(node);
        });
        enumerator.process();
        setInterval(function() { enumerator.process() }, 100);
        var name = "ClipboardCleanerTempPad";
        if (!window.frames[name]) {
                var s = document.createElement("span");
                s.style.position="absolute";
                s.style.visibility="hidden";
                document.body.insertBefore(s, document.body.firstChild);
                s.innerHTML = "<iframe name="+name+" width=0 height=0></iframe>";
        }
        var pad = window.frames[name].document;

        var insideOncutcopy = false;
        var f_oncutcopy = function (method) {
                if (insideOncutcopy) return true;
                insideOncutcopy = true;
                document.execCommand(method);
                event.returnValue = false;
                insideOncutcopy = false;
                pad.innerHTML = "";
                var r = pad.body.createTextRange();
                r.execCommand("Paste");
                autohyphen.dehyphenizeTree(pad.body);
                var r = pad.body.createTextRange();
                r.execCommand("Copy");
        }

        document.body.attachEvent("oncopy", function() { f_oncutcopy('Copy') });
        document.body.attachEvent("oncut", function() { f_oncutcopy('Cut') });
}

var win_ie_ver = parseFloat(navigator.appVersion.split("MSIE")[1]);
if (win_ie_ver >= 5.5) {
if (document.attachEvent) {
        document.attachEvent("onactivate", Autohyphen_onStart);
        document.attachEvent("onreadystatechange", Autohyphen_onStart);
}}
