/**
 * Sitebuilder tooltip implementation
 *
 * @copyright (c) 2004-2008, SWsoft
 */
var SbTooltip = Class.create(Tooltip);
Object.extend(
	SbTooltip.prototype,
	{
		/**
		 * Last position
		 *
		 * @var object
		 * @access private
		 */
		_lastPosition: null,

		/**
		 * Recalculates tooltip position
		 *
		 * @access public
		 * @return void
		 */
		recalculatePosition: function() {
			if (this._lastPosition) {
				this._moveTooltip(this._lastPosition);
			}
		},

		/**
		 * Overrides definition from Tooltip
		 *
		 * @access public
		 * @return void
		 */
		moveTooltip: function(event){
			Event.stop(event);
			this._lastPosition = Event.pointer(event);
			this._moveTooltip(this._lastPosition);
		},

		/**
		 * Moves tooltip
		 * Overrided method body from Tooltip.moveTooltip()
		 *
		 * @access private
		 * @param object
		 * @return void
		 */
		_moveTooltip: function(mousePosition){
			// get Mouse position
			var mouse_x = mousePosition.x;
			var mouse_y = mousePosition.y;

			// decide if wee need to switch sides for the tooltip
			var dimensions = Element.getDimensions( this.tool_tip );
			var element_width = dimensions.width;
			var element_height = dimensions.height;

			if ( // too big for X
				(element_width + mouse_x) >= (this.getWindowWidth() - this.options.min_distance_x)
			) {
				mouse_x = mouse_x - element_width;
				// apply min_distance to make sure that the mouse is not on the tool-tip
				mouse_x = mouse_x - this.options.min_distance_x;
			} else {
				mouse_x = mouse_x + this.options.min_distance_x;
			}

			if ( // too big for Y
				(element_height + mouse_y) >= (this.getWindowHeight() - this.options.min_distance_y)
			) {
				if ( // too small for Y
					(mouse_y - element_height) <= 0
				) {
					if ( // too small for Y
						(mouse_y - Math.round(element_height / 2)) <= 0
					) {
						mouse_y = mouse_y - Math.round(element_height / 4);
					} else {
						if ( // too big for Y
							(element_height + mouse_y - Math.round(element_height/2)) >= (this.getWindowHeight())
						) {
							mouse_y = mouse_y - Math.round(3 * element_height / 4);
						} else {
							mouse_y = mouse_y - Math.round(element_height / 2);
						}
					}
				} else {
					mouse_y = mouse_y - element_height;
					// apply min_distance to make sure that the mouse is not on the tool-tip
					mouse_y = mouse_y - this.options.min_distance_y;
				}
			} else {
				mouse_y = mouse_y + this.options.min_distance_y;
			}

			// now set the right styles
			this.setStyles(mouse_x, mouse_y);
		}
	}
);