function Cloud(relParam, classParam, tagName) {
    this.relParam = relParam||'';
    this.classParam = classParam||'';
    this.tagName = tagName||'*';
}

Cloud.prototype.$ = function (oid) {
    if (typeof oid == "object") {
        return oid;
    }
    if (!oid) {
        return null;
    }
    oid = document.getElementById(oid);
    if (oid) {
        return oid;
    } else {
        return null;
    }
};

Cloud.prototype.findPosition = function (ob) {
    var nleft = 0;
    var ntop = 0;
    if (ob.offsetParent) {
            nleft = ob.offsetLeft
            ntop = ob.offsetTop
            while (ob = ob.offsetParent) {
                    nleft += ob.offsetLeft
                    ntop += ob.offsetTop
            }
    }
    return [nleft,ntop];
}

Cloud.prototype.onMouseOverFunction = function (ob) {
    var position = [], cloud = this.$(ob.idCloud), div;
    if (!cloud || cloud.style.display == 'block') {
        return false;
    }
    div = cloud.getElementsByTagName('div')[0];
    position = this.findPosition(ob);
    if (typeof ob.cloudText == 'undefined') {
        ob.cloudText = ob.getAttribute('title')||'';
        ob.removeAttribute('title');
    }
    div.innerHTML = ob.cloudText.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br>$2');
    cloud.style.display = 'block';
    if (cloud.clientWidth > 300) {
        cloud.style.width = '300px';
    }
    cloud.style.left = position[0] + 'px';
    cloud.style.top = (position[1] - 2 - cloud.clientHeight) + 'px';
    return false;
}

Cloud.prototype.onMouseOutFunction = function (ob) {
    var cloud = this.$(ob.idCloud);
    if (!cloud) {
        return false;
    }
    cloud.style.display = 'none';
    return false;
}

Cloud.prototype.inicjalize = function () {
    var link, rel, self = this, idCloud, t, cEl;
    if (self.relParam) {
        for (var i = 0, ln = document.links.length; i < ln; i++) {
            link = document.links[i];
            rel = link.getAttribute('rel')||0;
            if (rel && rel.match(new RegExp(self.relParam + '_'))) {
                idCloud = rel.replace(self.relParam + '_', '');
                if (!this.$(idCloud)) {
                    break;
                }
                link.idCloud = idCloud;

                link.onmouseover = function () {
                    var s = this;
                    t = setTimeout(function () {self.onMouseOverFunction(s);}, 100);
                }

                link.onmouseout = function() {
                    var s = this;
                    t = setTimeout(function () {self.onMouseOutFunction(s);}, 100);
                }
            }
        }
    }
    if (self.classParam) {
        cEl = self.$$(self.classParam, self.tagName);
        for (var i = 0, ln = cEl.length; i < ln; i++) {
            link = cEl[i];
            link.idCloud = self.classParam;

            link.onmouseover = function () {
                var s = this;
                t = setTimeout(function () {self.onMouseOverFunction(s);}, 100);
            }

            link.onmouseout = function() {
                var s = this;
                t = setTimeout(function () {self.onMouseOutFunction(s);}, 100);
            }
        }
    }
}

Cloud.prototype.$$ = function (className, tagName, ob) {
    var aEl, aREl = [];
    ob = this.$(ob);
    ob = (ob) ? ob : document;
    tagName = tagName||'*';
    aEl = ob.getElementsByTagName(tagName);
    for (var i = 0, ln = aEl.length; i < ln; i++) {
        if (aEl[i].className.match(new RegExp('(^|\\s)'+className+'(\\s|$)'))) {
            aREl[aREl.length] = aEl[i];
        }
    }
    return aREl
}

var cloud = new Cloud('cloud', 'cloudTitle', 'span');
cloud.inicjalize();
cloud = new Cloud(null, 'prodRealise', 'div');
cloud.inicjalize();

