/*
 Tooltip Object
*/

var Tooltip = Class.create();
Tooltip.prototype = {
    initialize: function(container, element, content_area) {
	this.tool = $(element);
	this.content = $(content_area);
	this.container = $(container);
	this.item;
	this.dims = this.container.getDimensions();
	this.activeArrow = '';
	this.offset = -30;
	this.midX = this.container.offsetWidth/2 + Position.cumulativeOffset(this.container)[0];
	this.midY = this.container.offsetHeight/2 + Position.cumulativeOffset(this.container)[1]+0;
	this.toolX;
	this.toolY;
    },
    
    show: function(elem, content) {
	this.item = elem;
	this.toolX = Position.cumulativeOffset(elem)[0];
	this.toolY = Position.cumulativeOffset(elem)[1];

	$$('img[class^=png bubArrow]').invoke('hide');
	$$('img[class^=png bubArrow]').each(function(item) {
		item.observe('mouseout', function(event) { tooltip.hide(event) });
	    });
	this.content.update(content);
	//this.tool.getElementsBySelector('a').invoke('hide');
	this.tool.show();

	if ((this.toolX < this.midX) && (this.toolY < this.midY)) {
	    this.activeArrow = 0;
	    this.offsetX = 68 + elem.getWidth();
	    this.offsetY = this.offset - (.25 * elem.getHeight())-20;
	}
	else if ((this.toolX >= this.midX) && (this.toolY < this.midY)) {
	    this.activeArrow = 1;
	    this.offsetX = -this.tool.offsetWidth - 72;
	    this.offsetY = this.offset - (.25 * elem.getHeight()) -20;
	}
	else if ((this.toolX < this.midX) && (this.toolY >= this.midY)) {
	    this.activeArrow = 2;
	    this.offsetX = 68 + elem.getWidth();
	    this.offsetY = this.offset - this.tool.offsetHeight + elem.getHeight()+55;	    
	}
	else {
	    this.activeArrow = 3;
	    this.offsetX = -this.tool.offsetWidth - 72;
	    this.offsetY = this.offset - this.tool.offsetHeight + elem.getHeight()+55 ;
	}
	$$('img[class^=png bubArrow]')[this.activeArrow].show();
	this.tool.setStyle({ top: (this.toolY + this.offsetY)+ 'px', left: (this.toolX + this.offsetX) + 'px' });
    },
    
    hide: function(e) {
	if ((Position.within($$('img[class^=png bubArrow]')[this.activeArrow], Event.pointerX(e), Event.pointerY(e)) == false) ||
	    (Position.within(this.item, Event.pointerX(e), Event.pointerY(e)) == false)) {
	    this.tool.hide();
	    this.tool.setStyle({ top: -1000 + 'px', left: -1000 + 'px' });
	    this.content.update('');
	}
    }
}
