/**
 * jQuery tooltip plugin
 *
 * @name jquery-tooltip-0.1.js
 * @author Berea Cezar-Mihail
 * @version 0.1
 * @date July 01, 2008
 * @category jQuery plugin
 * @copyright (c) 2008 Berea Cezar-Mihail
 */

// Offering a Custom Alias suport - More info: http://docs.jquery.com/Plugins/Authoring#Custom_Alias
(function($) {

	$.fn.tooltip = function(data, options) {

		/**
		 * If there are no registered tooltip containers, let's create one.
		 */
		if ( !$.tooltips )
		{
			$("body").append('<div id="tooltip">' +
					'<div class="c clearfix">Default tooltip text</div>' +
			'</div>');
			$.tooltips = {def: $("#tooltip").hide()[0]};
		}

		options = $.extend({
			container: $.tooltips.def,
			offsety: 15,
			offsetx: -40,
			fadespeed: 100
		},options);



		function __move(e){
			var pscroll = $.getPageScroll();
			var scrollx = pscroll[0];
			var scrolly = pscroll[1];
			var h =  $(options.container).height();
			var w =  $(options.container).width();
			var x = e.clientX + scrollx + options.offsetx;
			var y = e.clientY + scrolly - h - options.offsety;

			var screen = $.getPageSize();
			var screenx = screen[2];
			var screeny = screen[3];

			if ( x + w > screenx ) x = screenx - w;
			if ( x < 0) x = 0;

			if ( y < 0) y = 0;
			//if ( y + h > screeny - scrolly) y = screeny - h;
//			console.log(x,y, screeny, scrolly);
			$(options.container).css({top: y+"px", left: x+"px"});
		}
		function __show(e){
			$('.c',options.container).html(data);
			$(options.container).fadeIn(options.fadespeed);
			__move(e);
			$(this).bind('mousemove', __move);
		}
		function __hide(e){
			$(options.container).fadeOut(options.fadespeed);
			//$('.c',options.container);
			$(this).unbind('mousemove');
		}



		return this.mouseover(__show).mouseout(__hide);
	};
})(jQuery); // Call and execute the function immediately passing the jQuery object