var ToolTip = Class.create({
    initialize: function( element, options){
            this.element = $(element);
            if (!this.element){
                /* nie ma elemntu, wiec gudbaj
                 */
                throw 'Nie znaleziono elementu '+element;
            }
            this.options = options ? options : new Hash();
            this.help_text = this.options.get('help_text')? this.options.get('help_text'): this.element.readAttribute('help');
            this.lock = false;
            this.install();
        },
    install: function(){
            this.container = $('tooltip_container');
            if (!this.container){
            /*
             * brak kontenera. albercik, wychodzimy!
             */
                 throw 'Nie znaleziono elementu '+element;
            }
            this.container.absolutize();
            this.element.observe('mouseover', 
                    this.show.bindAsEventListener(this));
            this.element.observe('mouseout', this.hide.bindAsEventListener(this));

			this.element.observe('click', this.onclick.bindAsEventListener(this));
			this.stop = this.stop.bind(this);
			this._hide = this._hide.bind(this);
        },
	onclick: function(){
		this.stop();
		this._hide();
	},
	stop: function(){
		this.element.stopObserving('mouseover');
		this.element.stopObserving('mouseout');
	},
    show: function(){
            /*
             * pokazujemy kontener z zawartoscia pomocy, przesuwamy kontener nad element
             *
             */
            if (this.lock){
                clearTimeout(this.lock);
            }
            this.container.down('.tipcontent').update(this.help_text);
            this.container.absolutize();
            this.container.addClassName('tt_normal');
            this.container.removeClassName('up');
            this.container.show();
            var el_offset = this.element.viewportOffset();
            var vp_offset = document.viewport.getScrollOffsets();
            var cn_offset = this.container.viewportOffset();
            var max_left = document.width;
            var max_height = document.height;
            var cn_dimm = this.container.getDimensions();
            var el_dimm = this.element.getDimensions();
            var cn_left = el_offset.left + (el_dimm.width/2)+ 10;
            /*
             * sprawdzamy, czy koniec kontenera nie wystanie za bardzo
             * jak wystaje, to przesuwamy troche
             */
            if (cn_left + cn_dimm.width > max_left - 10){
                cn_left = el_offset.left + el_dimm.width - cn_dimm.width - 10;
                this.container.removeClassName('tt_normal');
                this.container.addClassName('tt_mirror');
            }

            /* top - kontener 5 px nad elementem
             *
             */
            var cn_top = vp_offset.top + el_offset.top - cn_dimm.height;
            /* przypadek, kiedy kontener nie mieści się z góry
             * obniżamy go poniżej elementu, dodajemy 10 px marginesu
             * dodajemy klasę .up 
             */
            if (cn_top< 10){
                cn_top = el_offset.top + el_dimm.height + 10;
                container.addClassName('up');
            }
            this.container.style.left = cn_left+'px';
            this.container.style.top = cn_top+'px';
            this.container.current_tooltip = this;
        
        },
		_hide: function(){
			this.container.hide();
		},

        hide: function(){
            this.lock = setTimeout(this._hide, (this.options.get('hide_timeout') || 3) * 1000);
            }
        }
       );


/*
 * instalujemy tooltipsy
 */

Event.observe(window, 'load',
    function(){ 
        $$('[help!=\'\']').each( function(el){
        var t = new ToolTip(el);
		el.tooltip =t ;
        });
    } );




