/**
 * infoToolTip plugin for jQuery
 * @copyright 2009 info.nl
 * @author Arno Wolkers
 * @version 1.0
 * 
 * Show the label text of a form input element inside the element itself
 *
 *
 * ----------------------------------------
 * HOW TO USE
 * ----------------------------------------
 * $('div').infoToolTip();
 * 
 *
 * ----------------------------------------
 * OPTIONS
 * ----------------------------------------
 * See at the bottom of this page for public settings
 * Use the following to set the default value for all the sliders at once:
 * $.fn.infoToolTip.defaults.className = 'foo';
 *  
 * 
 */
(function($){
	$.fn.infoToolTip = function(options) {
		var settings = $.extend({}, $.fn.infoToolTip.defaults, options);
		
		return this.each(function(i, elem) {
			if( settings.htmlSource === '' )
				elem.content = settings.htmlStart + '' + $(elem).find('.tooltip-content').html()  + '' + settings.htmlEnd; //set the html of the tooltip
			else
				elem.content = settings.htmlStart + '' + $(settings.htmlSource).html()  + '' + settings.htmlEnd; //set the html of the tooltip
			elem.show = false;
			
			$(elem).bind('mouseover', function(e) {
				$(this).addClass(settings.classNameHover); // add toggler rollover class
				if (!settings.showOnClick) { // if showOnClick is not set then show tooltip on mouse over
					toggleToolTip(elem, e);
				}
			}).bind('mouseout', function(e) {
				$(this).removeClass(settings.classNameHover); // remove toggler rollover class
				if (!settings.showOnClick) { // if showOnClick is not set then hide tooltip on mouse out
					hideToolTip(elem);
				}
			}).bind('click', function(e) {
				if (settings.showOnClick) { // if showOnClick is set then toggle tooltip on click
					toggleToolTip(elem, e);
					return false; // in case activated by link
				}
			});
		});
		
		
		function setPosition(elem, e) {
			var offset = {};

			if (!settings.followMouse || settings.showOnClick) {
				// static position
				offset = $(elem).offset();
				offset.left += $(elem).outerWidth();
			} else {
				// follow mouse
				offset.left = e.pageX + (settings.offsetX == 0 ? 15 : 0); // the 15px prevents the tooltip from flickering when an offset of 0 is set
				offset.top  = e.pageY;	
			}
			// add the give offset values
			offset.left += settings.offsetX;	
			offset.top  += settings.offsetY;	

			// check if bottom of tooltip is outside browser window and reset position
			var tooltip_bottom = offset.top + $(elem.ref).outerHeight() - $(window).scrollTop() + settings.marginToWindow;	
			if (tooltip_bottom > $(window).height()) {
				offset.top += ($(window).height() - tooltip_bottom);
			}
			
			// check if bottom of tooltip is outside browser window and reset position
			var tooltip_right = offset.left + $(elem.ref).outerWidth() - $(window).scrollLeft() + settings.marginToWindow;	
			if (tooltip_right > $(window).width()) {
				if (!settings.followMouse || settings.showOnClick) {
					offset.left = $(elem).offset().left - $(elem.ref).outerWidth() - settings.offsetX;
				} else {
					offset.left = e.pageX - $(elem.ref).outerWidth() - (settings.offsetX == 0 ? 10 : settings.offsetX);
				}
			}
			
			// set the actual position of the tooltip
			$(elem.ref).css({
				'top' : offset.top,
				'left': offset.left
			});
		}
		

		/**
		 * Toggles the toolTip
		 * 
		 * @param {object} elem The tooltip element
		 * @param {object} e The event object for getting mouse position
		 */
		function toggleToolTip(elem, e) {
			if (!elem.show) {
				elem.show = true;
				elem.ref = $(elem.content).prependTo('body'); // add tooltip to the body and store a reference to it
				
				setPosition(elem, e); // set the position of the tooltip
				
				// if a close btn is set add functionality
				if (settings.classNameCloser) {
					$(elem.ref).find('.'+settings.classNameCloser).bind('click', function(){
						hideToolTip(elem);
						return false;
					})	
				}
				
				// if follow mouse is set then add follow functionality
				if (settings.followMouse && !settings.showOnClick) {
					$(elem).bind('mousemove', function(e){
						setPosition(elem, e);
					});
				}
				
				// reposition tooltip on window resize
				if (settings.showOnClick) {
					$(window).bind('resize.infoToolTip', function(e){
  						setPosition(elem, e);
					});	
				}
				
				// prefents tooltip to stay visible in IE when moves out over tooltip box
				if (!settings.followMouse && !settings.showOnClick) {
					$(elem.ref).bind('mouseover', function(e){
						hideToolTip(elem);
					});
				}
				
			} else {
				hideToolTip(elem);
			}
		};
		
		function hideToolTip(elem) {
			elem.show = false;
			$(elem.ref).remove(); // remove the tooltip
			$(elem).unbind('mousemove'); // unbind mousemove event
			$(window).unbind('resize.infoToolTip'); // unbind repositioning on window resize
		};
	};
	
	// plugin defaults 
	$.fn.infoToolTip.defaults = {
		htmlSource : '',
		htmlStart		: '<div class="infoToolTip"><div>',
		htmlEnd			: '</div></div>',
		classNameHover	: 'itt-hover',		// className of hover state of the tooltip toggler
		classNameCloser	: false,		// if you want to add a close button to the tooltip define it's className
		showOnClick		: false,		// show the tooltip on click instead of on mouseOver
		followMouse		: false,		// should the tooltip follow the mouse
		offsetX			: 0,			// offset left from tooltip toggler or mouse
		offsetY			: 0,			// offset top from tooltip toggler or mouse
		marginToWindow	: 5				// how many px should the tooltip stay away from browser window edge
	};

})(jQuery);