var mPosX;
var mPosY;
var zIndex = 10;
var tipOpen = new Array;

$(document).ready(function(){ 

	// this conditional creates ensures all the html 
	// is loaded before attempting to traverse
	// the file and apply event handlers to it

	// this script relies on the notion
	
	// establish if there are tooltips on the page
		
	$('.tooltip').hoverIntent( showTip, hideTip);
});

function showTip(e) {

			var tooltipname = '#tooltip_' + $(this).attr("id");
			var thisLink = $(this);
			
			// access a div with the id that 
			// matches tooltipname
			// place it at the mouse position
			// and fade it in
			
			if ($(tooltipname).css('display') == 'none') {
			
				zIndex++;
				
				$(tooltipname).prepend('<p class="closeLink"><a href="javascript:;" class="switchToolTipClose">[close]</a></p>');
				
				$(tooltipname + ' a.switchToolTipClose').bind('click', function(e) {
					fadeTip (tooltipname);
				});
				
				$(tooltipname).css({
					'position' : 'absolute',
					'top' : e.pageY,
					'left' : e.pageX - document.getElementById('wrapper').offsetLeft,
					'z-index' : zIndex
				});
				
				$(tooltipname).fadeIn('fast');
			
			}
			
}


function hideTip() {

	var tooltipname = '#tooltip_' + $(this).attr("id");
	
	var actionName = (tooltipname.substring(1));
	
	tipOpen[actionName] = setTimeout(function() {
		fadeTip (tooltipname);
	}, 4000)
			
}

function fadeTip (tooltipname) {

	var actionName = (tooltipname.substring(1));

	$(tooltipname + ' a.switchToolTipClose').unbind('click');
	$(tooltipname).fadeOut('slow', function() {
		clearTimeout(tipOpen[actionName]);
		$(tooltipname + ' p.closeLink').remove();
	});
}

