/*
 * You can use my_tooltip.destroy() to remove the event observers and thereby the tooltip.
 */
   
function getPositionLeft(This){
	var el = This;var pT = 0;
	while(el){pT+=el.offsetLeft;el=el.offsetParent;}
	return pT
}

var Tooltip = Class.create();
Tooltip.prototype = {
  initialize: function(element, tool_tip) {
    var options = Object.extend({
      margin: "0px",
  	  padding: "5px",
  	  delta_x: 5,
  	  delta_y: 5,
  	  width:185,
  	  height:125,
      backgroundColor: '#999', // Default background color
			borderColor: '#666', // Default border color
			gapX: -10, // sposta di
			gapY: -22, // sposta di
			delay: 350, // Default delay before tooltip appears in ms
			mouseFollow: true, // Tooltips follows the mouse moving
			opacity: .75, // Default tooltips opacity
			appearDuration: .25, // Default appear duration in sec
			hideDuration: .15, // Default disappear duration in sec
      zindex: 9000
    }, arguments[1] || {});

    this.element      = $(element);
    this.n_tip     = tool_tip.replace("tooltop","");
    this.tool_tip     = $(tool_tip);
    this.rec_tip = [];
    this.options      = options;

    // hide the tool-tip by default
    this.tool_tip.hide();
    
//this.tool_tip.style.left = (getPositionLeft($("colonnaRegione"))-153)+"px";
    this.eventMouseOver = this.showTooltip.bindAsEventListener(this);
    this.eventMouseOut   = this.hideTooltip.bindAsEventListener(this);

    Event.observe(this.element, "mouseover", this.eventMouseOver);
    Event.observe(this.element, "mouseout", this.eventMouseOut);
  },
  showTooltip: function(event){

    $$("div.tooltrap-wrp").each(function (e,i){
      e.hide();
    });
  
  	//Event.stop(event);
  	// get Mouse position

		this.updateEvent = this.update.bindAsEventListener(this);
  	
		if(this.options.mouseFollow)
			Event.observe(this.element, "mousemove", this.updateEvent);
			
		Tooltip.prototype.initialized = true;
    $('hl'+this.n_tip).setOpacity(0.5);
		$('hl'+this.n_tip).show();
		if(!this.rec_tip[this.n_tip]){
		  this.rec_tip[this.n_tip]=true;
      new Insertion.Top($('tooltop'+this.n_tip).down(),'<img src="http://primocontatto.ospitalitalia.it/static/img/regioni/label_ico_'+this.n_tip+'.gif" alt="fai click" width="125" height="125"/>');
    }
		this.timeout = window.setTimeout(this.appear.bind(this), this.options.delay);
      //this.appear();		
  	// finally show the Tooltip
  	//
  	//new Element.show(this.tool_tip);
  },
	appear: function() {
    new Effect.Appear(this.tool_tip, {duration:0.2});
    if (navigator.appVersion.indexOf('MSIE')>0) {
      //this.tool_tip.style.background = "transparent url(/img/ombra_tooltrap.gif) no-repeat scroll 0 0";
    }else{
      this.tool_tip.style.background = "transparent url(/img/ombra_tooltrap.png) no-repeat scroll 0 0";
    }
    //this.appearingFX = new Effect.Appear(this.tool_tip, {duration: this.options.appearDuration, to: this.options.opacity });

  },

  hideTooltip: function(event){
	//new Effect.Fade(this.tool_tip);
	  this._clearTimeout(this.timeout);
  	if(this.options.mouseFollow){
  		Event.stopObserving(this.element, "mousemove", this.updateEvent);
  	}
    
  	$(this.tool_tip).hide();
		$('hl'+this.n_tip).hide();
		
		Tooltip.prototype.initialized = false;
  	//new Element.hide(this.tool_tip);
  },
	update: function(event){
		this.xCord = Event.pointerX(event);
		this.yCord = Event.pointerY(event);
  
    if(this.xCord > (Element.getWidth(document.body) - this.options.width)+this.options.gapX){ 
      this.xCord = this.xCord - $(this.tool_tip).getWidth();
      this.xCord =(this.xCord + this.options.gapX);
    }else{
      this.xCord =(this.xCord - this.options.gapX);
    }
    
    this.tool_tip.style.marginLeft = "-140px";
	
  	// decide if wee need to switch sides for the tooltip
  	var dimensions = Element.getDimensions( this.tool_tip );
  	var element_height = dimensions.height;
  	var ELdimensions = Element.getDimensions( this.element );
  	var ELelement_height = ELdimensions.height;
  	
  	
	var eY = Position.page($(this.element))[1];
  //$(this.tool_tip).innerHTML='windows '+(this.getWindowHeight())+' - pos img in win'+(getPositionTop($(this.element))+eY)+' - mouspos'+this.yCord+' - top + img'+(this.yCord-(getPositionTop($(this.element))-eY));
  	
  	if (((this.getWindowHeight()-element_height)-this.options.gapY) < (this.yCord-(getPositionTop($(this.element))-eY)) ){ // too big for Y
    //if(limit < this.getWindowHeight()){ 
      //this.yCord = this.yCord - $(this.tool_tip).getHeight();
      this.yCord =((this.yCord - element_height)-this.options.gapY);
    }else{
      this.yCord =(this.yCord + this.options.gapY);
    }
    this.tool_tip.style.top = (this.yCord) + "px";

    
  },
  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;	
  },
	_clearTimeout: function(timer) {
		clearTimeout(timer);
		clearInterval(timer);
		return null;
	}

}