var TooltipClick = Class.create();
TooltipClick.prototype = {
  initialize: function(element, tool_tip,pid,typeId,num,detail,corpus) {
    var options = Object.extend({
      default_css: false,
	  margin: "0px",
	  padding: "5px",
	  backgroundColor: "#d6d6fc",
	  min_distance_x: -5,
      min_distance_y: -5,
      delta_x: 0,
      delta_y: 0,
      zindex: 1000
    }, arguments[2] || {});

	this.nameelement = tool_tip;
    this.element      = $(element);
    this.detail = detail;
    this.options      = options;
    this.getWindowWidth();
    this.getWindowHeight();
    this.pid = pid;
    this.typeId = typeId;
    this.num = num;
    this.corpus = corpus;
    // use the supplied tooltip element or create our own div
	
    
	
        this.tool_tip = $('tooltip_contenedor_'+tool_tip);
        if( this.tool_tip == null)
        {
            this.tool_tip = $(document.createElement("div")); 	  
	    this.tool_tip.id = 'tooltip_contenedor_'+tool_tip;
	    this.tool_tip.style.display = 'none';
            this.tool_tip.style.zIndex = '999';
            $(tool_tip).style.display = '';           
            this.tool_tip.appendChild($(tool_tip));           
            document.body.appendChild(this.tool_tip);
            
            this.tool_tip.hide();
            
            if($('SearchAction') != null)
                Event.observe($('SearchAction'),'mouseover',function(event){
			new Element.hide($('tooltip_contenedor_'+tool_tip));});
        }
	  
   
    

    this.eventClick = this.showTooltip.bindAsEventListener(this);
   

    this.registerEvents();
  },

  destroy: function() {
    Event.stopObserving(this.element, "click", this.eventClick);
    
  },

  registerEvents: function() {
    Event.observe(this.element, "click", this.eventClick);
   
  },

  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;
          
	  if ( (element_width + mouse_x) >= ( this.getWindowWidth() - 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;
	  }
	
	  if ( (element_height + mouse_y) >= ( this.getWindowHeight() - this.options.min_distance_y) ){ // too big for Y
		  mouse_y = mouse_y - element_height;
                  if(mouse_y < 0) mouse_y = 0; 
	    // 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);

    aux_element = this.element;
    aux_tooltip = this.tool_tip;
    aux_pid = this.pid;
    aux_typeId = this.typeId;
    aux_num = this.num;
    aux_detail = this.detail;
    if(aux_detail == null)aux_detail = "";
    
    
   
    
    var mouse_x = Event.pointerX(event);
    var mouse_y = Event.pointerY(event);
    var scrollTop = document.documentElement.scrollTop;
    var ancho = this.getWindowWidth()
    var min_distance_x = this.options.min_distance_x;
    var alto = this.getWindowHeight();
    alto = alto + scrollTop;
    min_distance_y = this.options.min_distance_y;
    tool = $(this.nameelement);
    var corpus_aux = this.corpus;
     new Ajax.Request('ShowBioentityTooltip.action', {
                method:'get', 
                parameters:'pid=' + aux_pid + '&typeId=' + aux_typeId + '&numDocs='+aux_num+'&detail='+aux_detail+'&corpus='+corpus_aux, 
                asynchronous:true, 
                evalScripts:true, 
                onSuccess: function(theRequest) { 
                                    
                   tool.update(theRequest.responseText);
               
             
               
                      var dimensions = Element.getDimensions( aux_tooltip );
                      var element_width = dimensions.width;
                      var element_height = dimensions.height;

                      if ( (element_width + mouse_x) >= ( ancho - 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 - min_distance_x;
                      } else {
                              mouse_x = mouse_x + min_distance_x;
                      }
                           
                      if ( (element_height + mouse_y) >= ( alto  - min_distance_y) ){ // too big for Y
                              mouse_y = mouse_y - element_height;
                              if(mouse_y < 0) mouse_y = 0; 
                        // apply min_distance to make sure that the mouse is not on the tool-tip
                              mouse_y = mouse_y - min_distance_y;
                      } else {
                              mouse_y = mouse_y + min_distance_y;
                      }
               
                 
                    
                    Element.setStyle(aux_tooltip, { position:'absolute',
                                            top:mouse_y  + "px",
	 				    left:mouse_x  + "px",
					    zindex:1000                                            
	 				   });
                    
                    Element.show(aux_tooltip);
                    
                }
            });    
  },
  
  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
	 				   });
	
	  // 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){
	
	 if(event.explicitOriginalTarget.id == this.nameelement)
	  new Element.hide(this.tool_tip);
	 
  },

  getWindowHeight: function(w){
   
    var height;
    w = w ? w : window;
    height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
    return height;  
    
  },
 
  getWindowWidth: function(w){
   
    var width;
    w = w ? w : window;
    width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
    return width;
  }

}
