var __Tooltip = DefineClass({

    isVisible: false,
    DefaultWidth: 250,
    DefaultDelay: 400,
    tmp: 27,

    init: function(width, delay) {
        var oThis = this;
        this.width = width ? width : this.DefaultWidth;
        this.delay = delay ? delay : this.DefaultDelay;
        this.Container = ce('div', document.body, { innerHTML: '<div class="header"></div><div class="content"></div><div class="footer"></div></div>', id: 'Tooltip' });
        this.Content = this.Container.firstChild.nextSibling;
        this.Footer = this.Content.nextSibling;
        this.Header = this.Content.previousSibling;
        this.SetSize();
    },

    SetSize: function() {
        this.Container.style.width = this.width + 'px';
    },

    SetPosition: function() {

        var t = Position.page(this.target);




        var target = getBounds1(this.target);

        target.top = t[1];
        target.left = t[0];

        var TooltipHeight = parseInt(this.Container.offsetHeight);
        var TooltipWidth = parseInt(this.Container.offsetWidth);
        var ScrollTop = parseInt(document.body.scrollTop);
        var BodyWidth = parseInt(document.body.offsetWidth);

        var page = Position.page(this.target);

        // низ или верх
        if (target.top < TooltipHeight) {
            var top = page[1] + $(this.target).getHeight();
            this.Footer.style.display = 'none';
            this.Header.style.display = 'block';
            //echo('низ');
            var n = 't';
            this.Content.className = "content BottomBorder";

        } else {
            var top = page[1] - TooltipHeight;
            //echo('верх');
            this.Footer.style.display = 'block';
            this.Header.style.display = 'none';
            var n = 'b';
            this.Content.className = "content TopBorder";
        }



        this.Container.style.top = top + 'px';



        var d = 0;

        if (target.left + TooltipWidth>Math.floor(BodyWidth/2)) {
            //echo('право');
            n = 'r' + n;
            this.Container.style.left = (target.left + target.width - TooltipWidth) + 'px';

        this.Footer.style.background = "url('/img/tooltip/"+n+".png') right";
        this.Header.style.background = "url('/img/tooltip/"+n+".png') right";



        } else {
            n = 'l' + n;
            //echo('лево');
            this.Container.style.left = target.left + d + 'px';

        this.Footer.style.background = "url('/img/tooltip/"+n+".png')";
        this.Header.style.background = "url('/img/tooltip/"+n+".png') ";


        }

    },

    Show: function(text, target, NewWidth, NewDelay, area) {
        if (NewDelay == null) NewDelay = this.DefaultDelay;
        if (area && Prototype.Browser.IE) this.area = area;
        this.target = target;
        if (target != this.target) {
            this.Hide(true);
            this.Content.innerHTML = '';
        }
        var oThis = this;
        if (this.HiddenTimer) clearTimeout(this.HiddenTimer);
        if (NewWidth && NewWidth!=this.width) {
            this.Container.style.width = NewWidth + 'px';
        }

        if (NewDelay) this.delay = NewDelay;

        this.Timer = setTimeout(function() {
            oThis.isVisible = true;
            oThis.Content.innerHTML = text;
            oThis.SetPosition();
            oThis.Container.style.visibility = 'visible';
            if (oThis.area) {
                oThis.selects = oThis.area.getElementsByTagName('select');
                for (var i=0;i!=oThis.selects.length;i++) {
                    oThis.selects[i].style.visibility = 'hidden';
                }
            }
        }, this.delay);
    },

    Hide: function(forse) {
        if (forse) {
            this.Container.style.visibility = 'hidden';
            this.isVisible = false;
            return;
        }
        var oThis = this;
        if (this.isVisible) {
            this.HiddenTimer = setTimeout(function() {
                oThis.Container.style.visibility = 'hidden';
                oThis.isVisible = false;

            if (oThis.area) {
                for (var i=0;i!=oThis.selects.length;i++) {
                    oThis.selects[i].style.visibility = 'visible';
                }
                delete oThis.selects;
                delete oThis.area;
            }



            }, 100)
        }
        if (this.Timer) clearTimeout(this.Timer);
    }

});