/* tooltip-0.2.js - Small tooltip library on top of Prototype 
 * by Jonathan Weiss <jw@innerewut.de> distributed under the BSD license. 
 */

var Tooltip = Class.create();
Tooltip.prototype = {
  initialize: function(element, tool_tip) {
    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, active: false, zindex: 1000}, arguments[2] || {});
    this.element = $(element);
    this.options = options;
    
    if($(tool_tip)) {
      this.tool_tip = $(tool_tip);
    } else {
      this.tool_tip = $(document.createElement("div")); 
      document.body.appendChild(this.tool_tip);
      this.tool_tip.addClassName("tooltip");
      this.tool_tip.appendChild(document.createTextNode(tool_tip));
    }
    
    this.tool_tip.hide();

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

    this.registerEvents();
  },

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

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

  moveTooltip: function(event, yOffset){
	  Event.stop(event);
	  xOffset = Element.getWidth(this.element)/2 
	  thePos = this.element.cumulativeOffset();
	  isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
	  x = thePos[0]-80+xOffset;
	  y = thePos[1]-20-yOffset;
	  //if(isIE6){ y -= 320; }
	  this.setStyles(x,y);
  },
	
		
  showTooltip: function(event) {
	  if(this.options.active == false){
		  this.element.addClassName("activeItem");
		  hideToolTips();
		  var leftDiv = document.createElement('DIV');	/* Create arrow div */
		  leftDiv.className='tooltip_arrow';
		  leftDiv.id = 'tooltip_arrow';
		  this.tool_tip.appendChild(leftDiv);
		  Event.stop(event);
		  this.moveTooltip(event, Element.getHeight(this.tool_tip));
		  //new Element.show(this.tool_tip);
		  $(this.tool_tip).appear({ duration: 0.5, from:0, to:1 });

		  this.options.active = true;
	  }
  },
  
  setStyles: function(x, y){
	  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
	 								  });
	
	  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 Element.hide(this.tool_tip);
	  if(this.options.active == true){
		 // $(this.tool_tip).appear({duration: 0.6, from:1, to:0});
		  $(this.tool_tip).hide();
		  this.element.removeClassName("activeItem");

	  }
	  this.options.active = false;
  },

  fadeTooltip: function(event){
	  if(this.options.active == true){
		 $(this.tool_tip).appear({duration: 0.5, from:1, to:0, afterFinish:function(effect) {effect.element.hide();} });
		  this.element.removeClassName("activeItem");
		  //new Element.hide(this.tool_tip);
		  //
	  }
	  this.options.active = false;
  },

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