﻿var HelpBox = new Class({	
	options: {
		toggleselector: '.infotooltip',
		helpclass: 'helpbox',
		leftoffset: 300,
		contenttop: '',
		contentbottom: ''
	},

	initialize: function(options) {
	    this.setOptions(options);
	    
	    this.helpbox = new Element('div', { 
			                                'styles': { top: '-1000px' }
		                                  }
		                           ).addClass(options.helpclass).injectInside(document.body);
	    
	    this.updatefn = null;
	    this.inmotion = false;
	    
        this.fx = new Fx.Tween(this.helpbox, {
                                              onStart: function() {
                                                this.inmotion = true;
                                              }.bind(this),
                                              onComplete: function() { 
                                                                        if (this.updatefn == null) 
                                                                        {
                                                                            this.updatefn = function() {
                                                                                if (this.currenttoggler != null && this.inmotion == false) {
                                                                                    var togglepos = this.currenttoggler.getPosition();
                                                                                    var boxpos = this.helpbox.getPosition();
                                                                                    
                                                                                    if (togglepos.y != boxpos.y) {
                                                                                        this.fx.start('top', togglepos.y);
                                                                                    }
                                                                                }
                                                                            }.bind(this);
                                                                        
                                                                            this.updatefn.periodical(500);
                                                                        }
                                                                        this.inmotion = false;
			                                                         }.bind(this)
                                             });
	    
	    $$(options.toggleselector).each(function(el) { 
	        el.addEvent('click', function(e) {
	            var pos = el.getPosition();
	            var size = el.getSize();
	            
	            this.currenttoggler = el;
	            
	            this.helpbox.setStyle('left', pos.x + options.leftoffset);
	            this.helpbox.innerHTML = options.contenttop + el.get('rel') + options.contentbottom;
	            
	            this.fx.start('top', pos.y);
	        }.bind(this));
	    }.bind(this));
	}
});

HelpBox.implement(new Events, new Options);