﻿/*
 * 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.
 */

var Tooltip = Class.create();
Tooltip.prototype = {
	initialize: function(element, tool_tip) {
		var tipText = element + '-text';
		var tipVideo = element + 'mpl';
    		var options = Object.extend({
      			default_css: false,
      			margin: "0px",
			padding: "0px",
			backgroundColor: "#d6d6fc",
			min_distance_x: 10,
     			min_distance_y: 0,
     			delta_x: 0,
      			delta_y: -50,
      			zindex: 9999
    		}, arguments[2] || {});
    		this.element = $(element);
   		this.options = options;
      		this.tool_tip = $(tool_tip);
      		this.tool_tip_text = $(tipText);
      		this.tool_tip_vid = $(tipVideo);
    		this.eventMouseOver = this.showTooltip.bindAsEventListener(this);
    		this.registerEvents();
  	},
  	
  	registerEvents: function() {
    		Event.observe(this.element, "mouseover", this.eventMouseOver);
  	},

  	moveTooltip: function(event){
		Event.stop(event);
		
		//Determine scroll offset
		var scrOfX = 0, scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}

		//determine window size
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		
		
	  	// get Mouse position
	  	var dimensions = Element.getDimensions( this.tool_tip );
    		var mouse_x = Event.pointerX(event);
	  	var mouse_y = Event.pointerY(event);
	  	var element_width = dimensions.width;
	  	var element_height = dimensions.height;

	  	if ((element_height + mouse_y) >= myHeight + scrOfY) {
	  		mouse_y = (mouse_y - element_height) + this.options.min_distance_y;
	  		var bg = "url(/images/tips/arrowbottom.gif) no-repeat bottom left";
	  		var deltaY = this.options.delta_y * (-1);
	  	} else {
	  		mouse_y = mouse_y + this.options.min_distance_y;
	  		var bg = "url(/images/tips/arrowtop.gif) no-repeat top left";
	  		var deltaY = this.options.delta_y;
	  	}
	  	if ((element_width + mouse_x) >= myWidth + scrOfX) {
	  		mouse_x = (mouse_x - element_height) + this.options.min_distance_x;
	  	} else {
	  		mouse_x = mouse_x + this.options.min_distance_x;
	  	}

	  	// now set the right styles
	  	if(this.tool_tip.style.display != 'none'){
	  	} else {
	  		this.setStyles(mouse_x + this.options.delta_x, mouse_y + deltaY, element_height, bg);
	  	}
  	},
		
  	showTooltip: function(event) {
    		Event.stop(event);
    		this.moveTooltip(event);
	 	//new Element.show(this.tool_tip);
	 	new Effect.Appear(this.tool_tip, { duration: 0.5 });
  	},
  
	setStyles: function(x, y, h, bg){
    		// set the right styles to position the tool tip
		Element.setStyle(this.tool_tip, { 
			position:'absolute',
			top:y + "px",
			left:x + "px",
			background:bg,
			zindex:this.options.zindex
		});
  	},

	hideTooltip: function(event){
		new Element.hide(this.tool_tip);
	}
}