

Effect.Tooltip = Class.create();
Object.extend(Object.extend(Effect.Tooltip.prototype, Effect.Base.prototype), {
  initialize: function(element, content) {
    this.element = $(element);
    this.isVisible = false;
    if(!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({
      content: content,
      title: false,
      className: 'tooltip',
      offset: {'x':16, 'y':16}
    }, arguments[2] || {});
    this.start(options);
  },
  setup: function() {
    // create a wrapper
    this.wrapper = document.createElement('div');
    this.wrapper.className = this.options.className;
    Element.setStyle(this.wrapper, {
      position: 'absolute',
      display: 'none'
    });
    // add the title
    if(this.options.title) {
      var title = document.createElement('div');
      title.className = 'title';
      Element.update(title, this.options.title);
      this.wrapper.appendChild(title);
    }
    
    
     this.steam = document.createElement('div');
    this.steam.className = 'steam';
    this.wrapper.appendChild(this.steam);
    
    var left = document.createElement('div');
    left.className = 'left';
    this.wrapper.appendChild(left);
    
    
    
    // create the actual tooltip
    this.tip = document.createElement('div');
    this.tip.className = 'content';
    Element.update(this.tip, this.options.content);
    this.wrapper.appendChild(this.tip);
    
    var right = document.createElement('div');
    right.className = 'right';
    this.wrapper.appendChild(right);

    // add wrapper to the body
    document.body.appendChild(this.wrapper);

    // add observers
    this.element.observe('mousemove', this.showTipDelayed.bind(this));
    this.element.observe('mouseout', this.hideTip.bind(this));
  },
  showTipDelayed: function(event) {
  	this.delay = 0.5;
  	this.positionTip(event);
  	if(this.timer) clearTimeout(this.timer);
    this.timer = setTimeout(this.showTip.bind(this), this.delay * 1000);
  
  },
  showTip: function(){
  
	
  	if(!this.isVisible){
  	

  	
    this.wrapper.appear({ duration: 0.3,
    afterFinish: function() {
    	
		this.isVisible = true;	
			
		
			}.bind(this)
    
     });
  	}
  	
  	
    
    
  },
  hideTip: function(){
  if(this.timer) clearTimeout(this.timer);
  if(this.isVisible){
    this.wrapper.fade({ duration: 0.3,
    afterFinish: function() {
    this.isVisible = false;
				this.wrapper.hide();
			}.bind(this)
    
     });
     }

  },
  positionTip: function(event){
    var offsets = {'x': this.options.offset['x'],'y': this.options.offset['y']};
    var mouse = {'x': Event.pointerX(event), 'y': Event.pointerY(event)};
    var page = {'x':this.viewportSize()['x'], 'y':this.viewportSize()['y']};
    var tip = {'x': mouse['x'] + this.options.offset['x'] + this.wrapper.getWidth(),
    'y' : mouse['y'] + this.options.offset['y'] + this.wrapper.getHeight()};

    // inverse x or y to keep tooltip within viewport
    if(tip['x']>page['x']) { offsets['x'] = 0-(this.wrapper.getWidth() + this.options.offset['x']); }
    if(tip['y']>page['y']) { offsets['y'] = 0-(this.wrapper.getHeight() + this.options.offset['y']); }

    this.wrapper.setStyle({
      left: mouse['x'] + offsets['x'] + 'px',
      top: mouse['y'] + offsets['y'] + 'px'
    });
  },
  viewportSize : function(){
    var x = self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
    var y = self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
    return {'x': x, 'y': y};
  }
});
