/*
 * Copyright (c) 2006 Jonathan Weiss <jw@innerewut.de>
 *
 * 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.
 */


/* tooltip-0.1.js - Small tooltip library on top of Prototype 
 * by Jonathan Weiss <jw@innerewut.de> distributed under the BSD license. 
 *
 * Unlike other libraries it does not declare its own tooltip 
 * div or window. It relies on an already existing div or element defined by you to display as 
 * the tooltip. This element will be placed (and shown) near the mouse pointer when a trigger-element is moused-over.
 * 
 *
 * Usage: 
 *   <script src="/javascripts/prototype.js" type="text/javascript"></script>
 *   <script src="/javascripts/tooltip.js" type="text/javascript"></script>
 *   <script type="text/javascript">
 *     var my_tooltip = new Tooltip('id_of_trigger_element', 'id_of_tooltip_to_show_element')
 *   </script>
 * 
 * Now whenever you trigger a mouseOver on the `trigger` element, the tooltip element will
 * be shown. On o mouseOut the tooltip disappears. 
 * 
 * Example:
 * 
 *   <script src="/javascripts/prototype.js" type="text/javascript"></script>
 *   <script src="/javascripts/scriptaculous.js" type="text/javascript"></script>
 *   <script src="/javascripts/tooltip.js" type="text/javascript"></script>
 *
 *   <div id='tooltip' style="display:none; margin: 5px; background-color: red;">
 *     Detail infos on product 1....<br />
 *   </div>
 *
 *   <div id='product_1'>
 *     This is product 1
 *   </div>
 *
 *   <script type="text/javascript">
 *     var my_tooltip = new Tooltip('product_1', 'tooltip')
 *   </script>
 *
 * You can use my_tooltip.destroy() to remove the event observers and thereby the tooltip.
 */

var Tooltip = Class.create();
Tooltip.prototype = {
  initialize: function(element, tool_tip) {
    var options = Object.extend({
      default_css: false,
      margin: "0px",
	  padding: "5px",
	  backgroundColor: "#d6d6fc",
	  delta_x: 30,
	  delta_y: 30,
      zindex: 1000
    }, arguments[1] || {});

    this.element      = $(element);
    this.tool_tip     = $(tool_tip);

    this.options      = options;

    // hide the tool-tip by default
    //this.tool_tip.hide();

    
		/*if(!$ENV_bNoTooltip)*/ this.eventMouseOver = this.showTooltip.bindAsEventListener(this);
    /*if(!$ENV_bNoTooltip)*/ this.eventMouseOut   = this.hideTooltip.bindAsEventListener(this);

    /*if(!$ENV_bNoTooltip)*/ this.registerEvents();
  },

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

  registerEvents: function() {
    Event.observe(this.element, "mouseover", this.eventMouseOver);
    Event.observe(this.element, "mouseout", this.eventMouseOut);
  },

  showTooltip: function(event){
	Event.stop(event);
	// get Mouse position
  var mouse_x = Event.pointerX(event);
	var mouse_y = Event.pointerY(event);
	
	
	// 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;
	
	// recog. the Scrolling-Offset
	mouse_x -= this.getScrollingOffsetX();
	mouse_y -= this.getScrollingOffsetY();
	//window.status= mouse_y;

		if ( (mouse_x) > ( this.getWindowWidth() / 2 ) ){ // RT: wenn MausposX ueber der Haelfte
	//ORIG: if ( (element_width + mouse_x) >= ( this.getWindowWidth() - this.options.delta_x) ){ // too big for X
		mouse_x = mouse_x - element_width;
		// apply delta to make sure that the mouse is not on the tool-tip
		mouse_x = mouse_x - this.options.delta_x;
	} else {
		mouse_x = mouse_x + this.options.delta_x;
	}
	if ( mouse_x<0 ) { // RT
		mouse_x=0;
	}
	mouse_x += this.getScrollingOffsetX();

	
	if(element_height<560) {
		element_height=560;
	}
	if ( (element_height/2 + mouse_y) >= ( this.getWindowHeight() - this.options.delta_y ) ){ // too big for Y
		mouse_y = this.getWindowHeight() - this.options.delta_y - element_height; // RT: von mir hinzugefuegt
		// ORIG: mouse_y = mouse_y - element_height;
	    // apply delta to make sure that the mouse is not on the tool-tip
		mouse_y = mouse_y - this.options.delta_y;
	} else {
		mouse_y = mouse_y - element_height/2 + this.options.delta_y;
	} 
	if ( mouse_y<40 ) { // RT
		mouse_y=40;
	}
	mouse_y += this.getScrollingOffsetY();
	
	
	// now set the right styles
	this.setStyles(mouse_x, mouse_y);
	
		
	// finally show the Tooltip
	//new Effect.Appear(this.tool_tip, {duration:0.5}); // wird von aussen gesteuert - im onload des Thumbnail-Bildes
	//new Element.show(this.tool_tip);
	//$ENV_bTooltipOn= true;
  },
  
  setStyles: function(x, y){
    // set the right styles to position the tool tip
	Element.setStyle(this.tool_tip, { position:'absolute',
	 								  top:y + "px",
	 								  left:x + "px",
									  zindex:this.options.zindex
	 								});
	
	// apply default theme if wanted
	if (this.options.default_css){
	  	Element.setStyle(this.tool_tip, { margin:this.options.margin,
		 								  padding:this.options.padding,
		                                  backgroundColor:this.options.backgroundColor,
										  zindex:this.options.zindex
		 								});	
	}	
  },

  hideTooltip: function(event){
		//new Effect.Fade(this.tool_tip, {duration:0.1});
		new Element.hide(this.tool_tip);
		//$ENV_bTooltipOn= false;
  },

	
	getScrollingOffsetX: function() {
		var $offsetX;
		if (self.pageYOffset) // all except Explorer
		{
			$offsetX = self.pageXOffset;
			//y = self.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
			// Explorer 6 Strict
		{
			$offsetX = document.documentElement.scrollLeft;
			//y = document.documentElement.scrollTop;
		}
		else if (document.body) // all other Explorers
		{
			$offsetX = document.body.scrollLeft;
			//y = document.body.scrollTop;
		}		
		
		return $offsetX;
	},

	getScrollingOffsetY: function() {
		var $offsetY;
		if (self.pageYOffset) // all except Explorer
		{
			$offsetY = self.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
			// Explorer 6 Strict
		{
			$offsetY = document.documentElement.scrollTop;
		}
		else if (document.body) // all other Explorers
		{
			$offsetY = document.body.scrollTop;
		}		
		
		return $offsetY;
	},

	
  getWindowHeight: function(){
    var innerHeight;
		if (self.innerHeight) // all except Explorer
		{
			innerHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			innerHeight = document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			innerHeight = document.body.clientHeight;
		}

    return innerHeight;	
  },
 
  getWindowWidth: function(){
    var innerWidth;
		if (self.innerHeight) // all except Explorer
		{
			innerWidth = self.innerWidth;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			innerWidth = document.documentElement.clientWidth;
		}
		else if (document.body) // other Explorers
		{
			innerWidth = document.body.clientWidth;
		}
		
    return innerWidth;	
  }

}
