if(typeof addHandler == "undefined") 
    function addHandler(object, event, handler) {
        if (typeof object.addEventListener != 'undefined')
            object.addEventListener(event, handler, false);
        else if (typeof object.attachEvent != 'undefined')
            object.attachEvent('on' + event, handler);
        else {
            var handlersProp = '_handlerStack_' + event;
            var eventProp = 'on' + event;
            if (typeof object[handlersProp] == 'undefined') {
                object[handlersProp] = [];
                if (typeof object[eventProp] != 'undefined')
                object[handlersProp].push(object[eventProp]);
                object[eventProp] = function(e) {
                    var ret = true;
                    for (var i = 0; ret != false && i < object[handlersProp].length; i++)
                        ret = object[handlersProp][i](e);
                    return ret;
                }
            }
            object[handlersProp].push(handler);
        }
    }

if(typeof cancelEvent == "undefined")
    cancelEvent = function(e) {
        e = e ? e : window.event;
        if(e.preventDefault) e.preventDefault();
        e.cancelBubble = true;
        return false;
    };

if(typeof Function.prototype.bind == "undefined")
    Function.prototype.bind = function(object, args) {
        var method = this
        return function() {
            var a = (args == null) ? arguments : args
            return method.apply(object, a) 
        }
    }


if ('undefined' == typeof String.prototype.ltrim) {
  String.prototype.ltrim = function() {
    return this.replace(/^\s+/, '');
  }
}

if ('undefined' == typeof String.prototype.rtrim) {
  String.prototype.rtrim = function() {
    return this.replace(/\s+$/, '');
  }
}

if ('undefined' == typeof String.prototype.trim) {
  String.prototype.trim = function() {
    return this.replace(/^\s+/, '').replace(/\s+$/, '');
  }
}


setGlobalOnLoad(function() {
    function getTextValue(node) {
        var text = "";
        if(node.hasChildNodes())
            for(var ch = node.firstChild; ch; ch = ch.nextSibling) text += getTextValue(ch);
        else
            text = node.nodeValue || node.alt;
        return text;
    }

    addHandler(document.body, "click", function(e) {
        e = e ? e : window.event;
        var target = e.target ? e.target : e.srcElement;

        if(target.tagName != "A" && target.parentNode.tagName != "A") return;
        if(target.tagName != "A" && target.parentNode.tagName == "A") target = target.parentNode;
        if(target.href.indexOf("mailto:") == 0) return;
        if(target.href.indexOf("skype:") == 0) return;
        if(target.className == "nocheck") return;

        var text = getTextValue(target) || "- none -";

        var path = "";
        var p = target.parentNode;
        while(p && typeof p.tagName != "undefined") {
            var name = "";
            if(p.id) name += p.id+" ";
            if(p.className) name += p.className;
            if(name) path = ">" + name.trim() + path;
            p = p.parentNode;
        }

        var url = "http://gazeta.aif.ru/refchecker?srcUrl="+encodeURIComponent(window.location)+"&tgtUrl="+encodeURIComponent(target.href)+"&title="+encodeURIComponent(text)+"&path="+encodeURIComponent(path);
        window.location = url;
        return cancelEvent(e);
    });
});
