/* Release 5.2 */

/*@FILE INFORMATION
----------------------------------------
Author:		United Airlines MileagePlus Team 2010 
File:		tooltip.js 
Created:	5/24/10 2:34 PM
Updated:	6/8/10 8:29 AM

/*@END--------------------------------*/


Tooltip = {
	
	//PUBLIC
	//returns void
	register: function(/*array*/labels, /*object*/leaf, /*object*/options){
		if(options){
			var offsetLeft = options.offsetLeft || 0,
				offsetTop = Math.abs(options.offsetTop) || 0,
				pinTo = options.pinTo || null,
				charCount = options.charCount || null,
				relativeParent = options.relativeParent || document.body,
				stemWidth = options.stemWidth || 0,
				stemHeight = options.stemHeight || 0;
		
		}
		
		
		//Register label events
		labels
			.bind('mouseover', function(evt){
				var label = $(this),
					labelOffset = label.offset(),
					labelTop = labelOffset.top,
					labelLeft = labelOffset.left,
					labelWidth = label.outerWidth(),
					labelHeight = label.outerHeight(),
					labelHorizontalMidpoint = labelLeft + (labelWidth / 2),
					relativeParentLeft,
					relativeParentWidth,
					relativeParentRight,
					leafWidth,
					leafHeight,
					leafTop,
					leafLeft,
					leafRight,
					stem = leaf.find('.tooltip_stem'),
					stemHorizontalPosition,
					stemVerticalPosition,
					body = leaf.find('.tooltip_body'),
					message = label.attr('tooltip_message') || 'Tooltip Message';
					
				
				//Relative parent
				if(relativeParent.nodeName){
					relativeParentLeft = 0;
					relativeParentWidth = relativeParent.clientWidth;
					relativeParentRight = relativeParentWidth;
					
				}else{
					relativeParentLeft = relativeParent.offset().left;
					relativeParentWidth = relativeParent.outerWidth();
					relativeParentRight = relativeParentLeft + relativeParentWidth;
				
				}
				
				
				//Is pinTo defined
				if(pinTo){
					var	icon = label.find(pinTo),
						iconOffset = icon.offset(),
						iconLeft = iconOffset.left,
						iconTop = iconOffset.top,
						iconWidth = icon.outerWidth(),
						iconHeight = icon.outerHeight(),
						iconHorizontalMidpoint = iconLeft + (iconWidth / 2);
					
				}
				
			
				//Set tooltip message
				body.html(message);
				
				
				//Set a maxwidth based on character count if defined
				if(charCount){
					if(Tooltip.charCountSetsWidth(message, charCount)){
						body.css('width', Tooltip.getCharCountWidth(charCount));
						
					}else{
						body.css('width', 'auto');
					
					}
				
				}
				
				
				//Set leaf position			
				leaf
					.css('top', function(){
						leafHeight = leaf.outerHeight();

						//Display leaf below label
						if(labelTop - leafHeight - offsetTop < $('body').scrollTop()){
							leafTop = labelTop + labelHeight;
							stem.insertBefore(body);
							stem.addClass('tooltip_stem_top');
							stemVerticalPosition = 0;
							return leafTop + offsetTop;
						
						//Display leaf above label
						}else{
							leafTop = labelTop - leafHeight;
							stem.insertAfter(body);
							stem.removeClass('tooltip_stem_top');
							stemVerticalPosition = 'bottom';
							return leafTop - offsetTop;
						
						}
					
					})
					.css('left', function(){
						//Pin leaf to midpoint of icon
						if(pinTo){
							leafLeft = iconHorizontalMidpoint + offsetLeft;
						
						//Pin leaf to midpoint of label
						}else{
							leafLeft = labelHorizontalMidpoint + offsetLeft;
					
						}
						leafWidth = leaf.outerWidth();
						leafRight = leafLeft + leafWidth;
						
						//Does leafs right edge extend beyond relative parents right edge
						if(leafRight > relativeParentRight){
							leafLeft -= (leafRight - relativeParentRight + 5);
							leafRight = leafLeft + leafWidth;
							
							//Snap leaf to label icons horizontal midpoint
							if(pinTo){
								
								//Flip stem image horizontally
								if(iconHorizontalMidpoint + stemWidth > leafRight){
									stemHorizontalPosition = iconHorizontalMidpoint - leafLeft - stemWidth;
									if(stemVerticalPosition === 'bottom'){
										stemVerticalPosition = -stemHeight * 2;
										
									}else{
										stemVerticalPosition = -stemHeight;
									
									}
								
								//Display stem image default state
								}else{
									stemHorizontalPosition = iconHorizontalMidpoint - leafLeft;
								
								}
							
							//Snap leaf to label horizontal midpoint
							}else{
								stemHorizontalPosition = labelHorizontalMidpoint - leafLeft;
							
							}
						
						//Display leaf default state
						}else{
							stemHorizontalPosition = Math.abs(offsetLeft);
						
						}
						stem.css('background-position', stemHorizontalPosition + ' ' + stemVerticalPosition);
						return leafLeft;
					
					})
					.addClass('tooltip_show');
			
			})
			.bind('mouseout', function(){
				leaf
					.removeClass('tooltip_show')
					.css('left', -9999)
					.css('top', -9999);
			
			});
	
	},
	
	
	//PRIVATE 
	//returns Boolean
	charCountSetsWidth: function(/*string*/message, /*integer*/charCount){
		var z=message, y=charCount, r, a, l, i;
		
		//Replace block-level elements with delimeter
		r = /<\/?(?:[bp]re?|p|h[1-6]|[uod]?li?|d[tdi]v?|blockquote|address)(?:.|\s)*?>/gi;
		z = z.replace(r, '@');
		
		//Remove leading and trailing delimeters
		r = /^@*|@*$/g;
		z = z.replace(r, '');
		
		//Remove duplicate delimeters
		r = /(?:\s*@\s*)+/g;
		z = z.replace(r, '@');
		
		//Remove remaining elements
		r = /<(?:.|\s)*?>/g;
		z = z.replace(r, '');
		
		a = z.split('@');
		l = a.length;
		i = l - 1;
		do{
			if(a[i].length > y) return true;
		
		}while(i--);
		return false;	
	
	},
	
	
	//PRIVATE
	//returns Integer
	getCharCountWidth: function(/*integer*/charCount){
		var d = document,
			b = d.body,
			s = d.createElement('span'),
			i = charCount - 1,
			w;
		
		s.style.visibility = 'hidden';
		s.style.position = 'absolute';
		s.style.top = -9999;
		s.style.left = -9999;
		do{
			s.appendChild(d.createTextNode('W'));
		
		}while(i--);
		b.appendChild(s);
		w = s.offsetWidth;
		b.removeChild(s);
		return w;
	
	}

}


//INITIALZIE TOOLTIP
$(document).ready(function(){
	Tooltip.register($('.tooltip'), $('#mp_tooltip'), {
		offsetLeft:-50,
		stemWidth:25,
		stemHeight:26,
		charCount:30,
		pinTo:'sup'
	
	});

});















