
/**
 * --------------------------------------------------------------------
 * jQuery-Plugin "aTitle"
 * Version: 1.0, 04.17.2009
 * by Jesse Vista, jvista@upwebdesign.com
 *                      http://www.upwebdesign.com/
 *
 * Copyright (c) 2009 Jesse Vista
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 *
 * Example
 * -------
 *
 *	$(document).aTitle({
 * 		startdom: 'body',
 * 		classname: 'note',
 * 		transition: 'slow',
 * 		left_adjustment: 10,
 * 		top_adjustment: 20,
 * 		opacity: .8,
 * 		defautstyle: true
 *	});
 * --------------------------------------------------------------------
 */

(function($) {

    jQuery.fn.aTitle = function(settings) {
        // Settings
    	settings = jQuery.extend({
			startdom: 'body',
    	    classname: 'message',
    		transition: 'slow',
			left_adjustment: 0,
			top_adjustment: 0,
			opacity: .9,
			defautstyle: true
    	}, settings);
		
		$(settings.startdom + ' span').each( function() {
			var atitle = $(this).attr('atitle');
			var atitle_header = $(this).html();
			var dependentLeft = 0;
			var dependentTop = 0;
			
			if(atitle != "" && typeof(atitle) != 'undefined') {
				
				parentPositionFix($(this));
				
				$(this).hover(
					function() {
						//var pos_start = $('body').position();
						var atitle_pos = $(this).position();
						var atitle_width = $(this).width();
						var atitle_height = $(this).height();
						var box_top = atitle_pos.top;
						var box_left = atitle_width + atitle_pos.left;
						
						var atitle_div = document.createElement('div');
						$(atitle_div).addClass(settings.classname);
						$(atitle_div).hide();
						
						//var atitle_h1 = document.createElement('h1');
						//$(atitle_h1).html(atitle_header);
						//$(atitle_h1).css({margin:0,padding:0});
						
						var atitle_p = document.createElement('p');
						$(atitle_p).html(atitle);
						
						// Put it all together
						$(atitle_div).append(atitle_p)
						
						// Add the helper box to the body
						$('body').append(atitle_div);
						
						// Fade in the message
						$(atitle_div).fadeTo(settings.transition, settings.opacity);
						
						// Calculate adjustement
						atitle_adj = ($(atitle_div).height())/2;
						
						// Apply helper box position elements
						$('.' + settings.classname).css({
							position: 'absolute',
							top: box_top - atitle_adj + settings.top_adjustment + dependentTop,
							left: box_left + settings.left_adjustment + dependentLeft
						//}).fadeIn(settings.transition);
						}).show();
						
						// Supply a default style 
						if(settings.defautstyle) {
							$('.' + settings.classname).css({
								'border': '1px solid #DDDF00',
								'background-color': 'white',
								'font-family': 'verdana',
								'font-size': '10px',
								'background-color': '#FFFFDF',
								'color': '#6E6F00',
								'width': '300px',
								'z-index': '1001',
								'cursor': 'pointer'
							});
							
							$('.' + settings.classname + ' h1').css({
								'font-size': '11px',
								'text-align': 'center',
								'background-color': '#FEFFBF',
								'border-bottom': '1px solid #DDDF00',
								'padding': '0px 3px'
							});
							
							$('.' + settings.classname + ' p').css({
								'padding': '5px 10px'
							});
						}
					},
					
					function() {
						$('.' + settings.classname).remove();
					}
				);
			}
			
			//*************************************************************
			// Supposed to recursively loop through each parent to detect
			// but doesnt do that yet, just finds the first one
			//*************************************************************
			function parentPositionFix(el) {
				var pos = $(el).parent().css('position');
				
				if(pos == 'absolute' || pos == 'relative') {
					adjPos = $(el).parent().position();
					dependentLeft += adjPos.left;
					dependentTop += adjPos.top;
					
					return false;
				}
				
				parentPositionFix($(el).parent());
				
				return true;
			}
		});
		
    };
})(jQuery);
