/*
 * Copyright (c) 2006 Jonathan Weiss <jw@innerewut.de>
 * (Heavily Modified by M. Long, 2009)
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
 
var Tooltip = Class.create();
Tooltip.prototype = {
	initialize: function(element, div_to_use, contentCallback, unsetCallback) {
		var options = Object.extend({
			min_distance_x: 15,
			min_distance_y: 15,
			delta_x: 0,
			delta_y: 0
		}, arguments[4] || {});

		this.element      = $(element);
		this.options      = options;
    		
		this.tool_tip = $(div_to_use);
		this.tool_tip.hide();

		this.contentCallback = contentCallback;
		this.unsetCallback = unsetCallback;
		
		this.eventMouseOver  = this.showTooltip.bindAsEventListener(this);
		this.eventMouseOut   = this.hideTooltip.bindAsEventListener(this);
		this.eventMouseMove  = this.moveTooltip.bindAsEventListener(this);

		Event.observe(this.element, "mouseover", this.eventMouseOver);
		Event.observe(this.element, "mouseout",  this.eventMouseOut);
		Event.observe(this.element, "mousemove", this.eventMouseMove);
	},

	destroy: function() {
		Event.stopObserving(this.element, "mouseover", this.eventMouseOver);
		Event.stopObserving(this.element, "mouseout",  this.eventMouseOut);
		Event.stopObserving(this.element, "mousemove", this.eventMouseMove);
	},

	moveTooltip: function(event){
		Event.stop(event);
		
		var mouse_x = Event.pointerX(event);
		var mouse_y = Event.pointerY(event);
	
		var dimensions = Element.getDimensions(this.tool_tip);
		var element_width = dimensions.width;
		var element_height = dimensions.height;
			
		if ( (element_width + mouse_x) >= (this.getWindowWidth()-this.options.min_distance_x) ) {
			mouse_x = mouse_x - element_width;
			mouse_x = mouse_x - this.options.min_distance_x;
		} else {
			mouse_x = mouse_x + this.options.min_distance_x;
		}
	
		if ( (element_height + mouse_y) >= (this.getWindowHeight()-this.options.min_distance_y) ) {
			mouse_y = mouse_y - element_height;
			mouse_y = mouse_y - this.options.min_distance_y;
		} else {
			mouse_y = mouse_y + this.options.min_distance_y;
		} 
	
		this.setStyles(mouse_x, mouse_y);
	},
			
	showTooltip: function(event) {
		Event.stop(event);
				
		this.contentCallback(this.tool_tip, this.options);
		this.moveTooltip(event);
		this.tool_tip.show();
	},
  
	setStyles: function(x, y) {
		Element.setStyle(this.tool_tip, { 
			position: 'absolute',
	 		top: y + this.options.delta_y + "px",
			left: x + this.options.delta_x + "px"
	 	});	
	},

	hideTooltip: function(event) {
		this.unsetCallback(this.tool_tip);
		this.tool_tip.hide();
	},

	getWindowHeight: function(){
		var innerHeight;
		if (navigator.appVersion.indexOf('MSIE')>0) {
			innerHeight = document.body.clientHeight;
		} else {
			innerHeight = window.innerHeight;
		}
		return innerHeight;	
	},
 
	getWindowWidth: function(){
		var innerWidth;
		if (navigator.appVersion.indexOf('MSIE')>0) {
			innerWidth = document.body.clientWidth;
		} else {
			innerWidth = window.innerWidth;
		}
		return innerWidth;	
	}

}

