/*
 * Copyright (c) 2006 Jonathan Weiss <jw@innerewut.de> v0.2 - http://blog.innerewut.de/pages/tooltip - Modified to our needs
 */
var Tooltip = Class.create();
Tooltip.prototype = {
	initialize: function(element, tool_tip, options) {
    	this.options = Object.extend({
    	  	default_css: "tooltip",
	    	min_distance_x: 10,
		    min_distance_y: 10,
    		delta_x: 0,
      		delta_y: 0,
	      	zindex: 1000
    	}, options || {});
    	
    	setTimeout((function () {
    		this._init(element, tool_tip);
    	}).bind(this), 1000);
	},
	
	_init: function(element, tool_tip) {

	    this.element = $(element);
	    this.tool_tip = $(tool_tip);
	    
		if (this.element && this.tool_tip) {
	    // hide the tool-tip by default
		    this.tool_tip.hide();
		
		    this.eventMouseOver = this.showTooltip.bindAsEventListener(this);
		    this.eventMouseOut   = this.hideTooltip.bindAsEventListener(this);
		    this.eventMouseMove  = this.moveTooltip.bindAsEventListener(this);
		
		    this.registerEvents();
		}
	},
	test: function(){
		console.debug;
	},

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

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

  	moveTooltip: 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;
	
		var pageSize = AppUtils.getPageSize();
		
	  	if ( (element_width + mouse_x) >= ( pageSize[2] - this.options.min_distance_x) ){ // too big for 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;
	  	}
	
		
		var remPageSize = Position.page(Event.element(event));
		
		// windowHeight - remainingSizeTop - 10
		var remainingSizeBottom = pageSize[3] - remPageSize[1] - 20;
				
	  	// if ( (element_height + mouse_y) >= ( pageSize[3] - this.options.min_distance_y) ){ // too big for Y
	  	if ( (element_height) >= ( (remainingSizeBottom) - this.options.min_distance_y) ){ // too big for Y
		 	 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);
  	},
	
 	showTooltip: function(event) {
    	Event.stop(event);
    	this.moveTooltip(event);
	  	Element.show(this.tool_tip);
  	},
  
  	setStyles: function(x, y){
    	// set the right styles to position the tool tip
	  	Element.setStyle(this.tool_tip, { position:'absolute', top: y + this.options.delta_y + "px", left: x + this.options.delta_x + "px", zindex: this.options.zindex });
  	},

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

  	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;	
  	}
}
