// Effect.Tooltip
// Nick Stakenburg
// requires - prototype     : http://www.prototypejs.org
//          - scriptaculous : http://script.aculo.us

Effect.Tooltip = Class.create();
  Object.extend(Object.extend(Effect.Tooltip.prototype, Effect.Base.prototype), {
  initialize: function(element, content) {
    this.element = $(element);

    if(!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({
      content: content,
      title: false,
      className: 'tooltip',
      offset: {'x':0, 'y':30}
    }, arguments[2] || {});
    this.start(options);
  },
  setup: function() {
    // add observers
    this.element.observe('mousemove', this.showTip.bind(this));
    this.element.observe('mouseout', this.hideTip.bind(this));
  },
  buildTip: function() {
    // create a wrapper
    this.wrapper = document.createElement('div');
    this.wrapper.className = this.options.className;
	this.wrapper.style.zIndex = this.options.zIndex;
    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);
    }
    // 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);

    // add wrapper to the body
    document.body.appendChild(this.wrapper);
  },
  showTip: function(event){
   

  //	foreach(tooltipArray as tooltip) {
  //		tooltip.style.display = "none";
  //	}

    if (!this.wrapper) this.buildTip();
    this.positionTip(event);
    this.wrapper.show();
    
  	var tooltipArray = $$('.tip');
  	for(i = 0; i < tooltipArray.length; i++) {
		if(tooltipArray[i] != this.wrapper) {
	  		tooltipArray[i].style.display = "none";
	  	}
  	}    

  },
  hideTip: function(){
    this.wrapper.hide();
  },
  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': this.element.cumulativeOffset().left + this.element.getWidth() + this.wrapper.getWidth(),
    'y' : this.element.positionedOffset()[1] + 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.element.getWidth() + this.options.offset['x']); }
    //if(tip['y']>page['y']) { offsets['y'] = 0-(this.wrapper.getHeight() + this.options.offset['y']); }

    this.wrapper.setStyle({
      left: (this.element.cumulativeOffset().left ) + this.element.getWidth() + offsets['x'] + 'px',
      top: (this.element.cumulativeOffset().top - this.element.cumulativeScrollOffset().top)+ offsets['y'] + 'px'
    });

  },
  viewportSize : function(){
    if (self.innerHeight) return {'x': self.innerWidth, 'y': self.innerHeight};
    else if (document.documentElement && document.documentElement.clientHeight)
    return {'x': document.documentElement.clientWidth, 'y': document.documentElement.clientHeight};
    else if (document.body) return {'x': document.body.clientWidth, 'y': document.body.clientHeight};
  }
});