(function($){
	$.fn.mxpTooltips = function( options ) {
		var settings = {
			tooltipClass: 'mxp-tooltip',
			effect: 'none',
			add: [10, 10],
			zIndex: 9999,
			container: ''
		};

		if( options ) {
			$(this).extend( settings, options );
		}

		settings.parts = [];
		settings.width = 0;
		settings.base = this;

		this.each(function(){
			$(this).attr( 'tooltipid', new String(Math.random()).replace(/(.*?\.)/, '') + '-tooltip' );
			$(this).bind( 'mouseover', function(event){
				$(this).stop(true,true);

				if( document.getElementById($(this).attr('tooltipid')+'-show') ) {
					$('#'+$(this).attr('tooltipid')+'-show').css({left: parseInt(event.pageX) + 10 + parseInt(settings.add[0]), top: parseInt(event.pageY) + 10 + parseInt(settings.add[1])});
				}
				else {
					var eDiv = document.createElement( 'div' );
					$(eDiv).attr( 'style', 'display: none;z-index:' + settings.zIndex + ';' );
					$(eDiv).attr( 'id', $(this).attr('tooltipid') + '-show' );
					if( settings.container != '' ) {
						settings.parts = new String(settings.container).split( ':' );
						if( document.getElementById(settings.parts[0]) ) {
							var node = document.getElementById(settings.parts[0]).cloneNode( true );
							$(node).children().each(function(){
								if( $(this).attr('id') == settings.parts[settings.parts.length-1] || $(this).hasClass(settings.parts[settings.parts.length-1]) ) {
									$(this).html( $(settings.base).attr('rel') );
								}
							});
							$(node).css({display: 'block'});
							eDiv.appendChild( node );
						}
					}
					else {
						$(eDiv).attr( 'class', settings.tooltipClass );
						if( $(this).attr('rel') != false ) {
							eDiv.innerHTML += $(this).attr('rel');
						}
					}
					$(eDiv).css({position: 'absolute', left: parseInt(event.pageX) + 10 + parseInt(settings.add[0]), top: parseInt(event.pageY) + 10 + parseInt(settings.add[1])});

					$('body').append( eDiv );
				}

				switch( settings.effect ) {
					case 'fade':
						$('#'+$(this).attr('tooltipid')+'-show').fadeIn();
						break;

					case 'flipUp':
						var width = parseInt( $('#'+$(this).attr('tooltipid')+'-show').width() );
						var height = parseInt( $('#'+$(this).attr('tooltipid')+'-show').height() );
						var offset = $(this).offset();
						//$('#'+$(this).attr('tooltipid')+'-show').css({left: offset.left - (width / 2), top: offset.top - (height * 5) })
						$('#'+$(this).attr('tooltipid')+'-show').css({left: offset.left - (width / 2) -  parseInt(settings.add[0]), top: offset.top - (height * 5) - parseInt(settings.add[0])})
						$('#'+$(this).attr('tooltipid')+'-show').css({'display': 'block'}).animate({
							'top': offset.top - ( height + 20 ) + parseInt(settings.add[1]),
							'opacity': 1
						});
						break;

					default:
						if( $('#'+$(this).attr('tooltipid')+'-show').css('display') != 'block' ) {
							$('#'+$(this).attr('tooltipid')+'-show').css({'display': 'block'});
						}
						break;
				}
			}).bind('mousemove', function(event){
				$(this).stop(true,true);
				switch( settings.effect ) {
					case 'fade':
						$('#'+$(this).attr('tooltipid')+'-show').css({position: 'absolute', left: parseInt(event.pageX) + 10 + parseInt(settings.add[0]), top: parseInt(event.pageY) + 10 + parseInt(settings.add[1])});
						//$('#'+$(this).attr('tooltipid')+'-show').fadeIn();
						break;

					case 'flipUp':
						break;

					default:
						break;
				}
			}).bind('mouseout', function(){
				$(this).stop(true,true);
				switch( settings.effect ) {
						case 'fade':
							$('#'+$(this).attr('tooltipid')+'-show').fadeOut();
							break;
						case 'flipUp':
							var height = parseInt( $('#'+$(this).attr('tooltipid')+'-show').height() );
							var offset = $(this).offset();
							$('#'+$(this).attr('tooltipid')+'-show').animate({
								'top': offset.top - ( height * 5 ),
								'opacity': 0
							});
							break;
						default:
							if( $('#'+$(this).attr('tooltipid')+'-show').css('display') != 'none' ) {
								$('#'+$(this).attr('tooltipid')+'-show').css({'display': 'none'});
							}
							break;
					}

			});
		});
	};
})(jQuery);
