/*
 * 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.
 */

/* tooltip-0.2.js - Small tooltip library on top of Prototype 
 * by Jonathan Weiss <jw@innerewut.de> distributed under the BSD license. 
 *
 * CHANGED BY ROB CLAYBURN <rob@pollen-8.co.uk>
 * to work with mootools
 * 
 */

var Tooltip = new Class({
  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,
      zindex: 1000
    }, arguments[2] || {});

    this.element      = $(element);
    this.options      = options;
    
    // use the supplied tooltip element or create our own div
    if($(tool_tip)) {
      this.tool_tip = $(tool_tip);
    } else {
      this.tool_tip = new Element('div', {'class':'tooltip'}).appendText(tool_tip);
      document.body.appendChild(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();
  },

  destroy: function() {
    this.element.removeEvent( "mouseover", this.eventMouseOver);
    this.element.removeEvent( "mouseout", this.eventMouseOut);
    this.element.removeEvent( "mousemove", this.eventMouseMove);
  },

  registerEvents: function() {
    $(this.element).addEvent( "mouseover", this.eventMouseOver);
    $(this.element).addEvent( "mouseout", this.eventMouseOut);
    $(this.element).addEvent( "mousemove", this.eventMouseMove);
  },

  moveTooltip: function(event){
	  var e = new Event(event);
	  e.stop();
	  // get Mouse position
	var mouse_x  = e.client.x;
	var mouse_y = e.client.y;
	
	  // decide if wee need to switch sides for the tooltip
	 var dimensions = this.tool_tip.getSize();
	 var element_width = this.tool_tip.getStyle('width').toInt();
	 var element_height = this.tool_tip.getStyle('height').toInt();
	
	  if ( (element_width + mouse_x) >= ( window.getWidth() - 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) >= ( window.getHeight() - 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.setTipStyles(mouse_x, mouse_y);
  },
		
  setTipStyles: function(x, y){
    // set the right styles to position the tool tip
	 this.tool_tip.setStyles( { 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){
	  	  this.tool_tip.setStyles({ margin:this.options.margin,
			padding:this.options.padding,
		    backgroundColor:this.options.backgroundColor,
			zindex:this.options.zindex
		});	
	  }	
  },

  hideTooltip: function(event){
	 $(this.tool_tip).setStyle('display', 'none');
  },
  
  showTooltip: function(event){
  	$(this.tool_tip).setStyle('display', 'block');
  }

});

window.addEvent("domready",function() {
      $$(".notempty").each(function(node){
	     new Tooltip(node,'Required field');
       });
	   
      $$(".alphanumeric").each(function(node){
	     new Tooltip(node,'Alphanumeric data only');
       });
	   
      $$(".isemail").each(function(node){
	     new Tooltip(node,'Please supply a valid email address');
       });
	   
	   	$$('.inputbox').each(function(el){
		el.addEvent( 'focus', function(event){
			var e = new Event(event);
			var tr = e.target;
			tr.addClass('highlightRow');
		});

		el.addEvent( 'blur', function(event){
			var e = new Event(event);
			var tr = e.target;
			tr.removeClass('highlightRow');
		});
	});
	
	$$('.elementErrorHighlight').each(function(g){
	g.addEvent( 'focus', function(e){
		g.removeClass('elementErrorHighlight');
	}, this);
	});
}); 