/* Pop Tip
 *
 */
var popTipContainer;
var popTipContent;
var popTipActiveAreas = new Array();

// You simply *must* run this function, in the body of the script
// and not inside any other tags (other than the <script> tag)
function popTipInit()
{
	document.write( '<div id="popTipContainer" class="popTipPointTopLeft" style="top:0px;left:0px;">' + 
			'<div class="popTipTop"></div>' +
			'<div id="popTipContent"></div>' + 
			'<div class="popTipBottom"></div>' +
		'</div>' );
	popTipContainer = document.getElementById( 'popTipContainer' );
	popTipContent = document.getElementById( 'popTipContent' );
	closePopTip();
}



function popTipIsOpen()
{
	return popTipContainer.style.visibility == 'visible';
}


function popTipMouseAwayMonitor( e )
{
	// get the moose pos
	var mouseX = 0;
	var mouseY = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		mouseX = e.pageX;
		mouseY = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		mouseX = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		mouseY = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	
	// check each 'active area' to see if the mouse is in any of them
	// if it isn't, then close the thingy
	var inAnyArea = false;
	var area, areaLeft, areaRight, areaTop, areaBottom = null;
	for ( var areaNum=0; areaNum<popTipActiveAreas.length; areaNum++ )
	{
		// introduce a small tolerance value just to be nice
		area = popTipActiveAreas[ areaNum ];
		areaLeft = area[ 0 ] - 5;
		areaRight = area[ 1 ] + 5;
		areaTop = area[ 2 ] - 5;
		areaBottom = area[ 3 ] + 5;
		
		if ( mouseX>=areaLeft && mouseX<=areaRight && mouseY>=areaTop && mouseY<=areaBottom  )
		{
			inAnyArea = true;
			break;
		}
	}
	
	if ( !inAnyArea )
	{
		closePopTip();
	}
}

// width should be a number - it will make the CONTENT div that many pixels wide
// note, jobby, that handing in null will let it be just whatever the hell size it wants to be
function openPopTip( targetElement, html, width )
{
	if ( popTipIsOpen() )
	{
		closePopTip();
	}
	if ( !isNaN( width ) )
	{
		popTipContent.style.width = width + 'px';
	}
	else
	{
		popTipContent.style.width = 'auto';
	}
	popTipContent.innerHTML = html;
	applyPopTipPosition( targetElement );
	popTipContainer.style.visibility = 'visible';
	document.onmousemove = popTipMouseAwayMonitor;
}



function closePopTip()
{
	document.onmousemove = null;
	popTipContainer.style.visibility = 'hidden';
	popTipContainer.style.top = '0px';
	popTipContainer.style.left = '0px';
	popTipContent.innerHTML = '';
}

function applyPopTipPosition( targetElement )
{
	// reset the position first
	popTipActiveAreas = new Array();
	popTipContainer.style.top = '0px';
	popTipContainer.style.left = '0px';
	popTipContainer.className = 'popTipPointTopLeft';
	
	// we get the top and left edge co-ordindates
	var top = targetElement.offsetTop;
	var left = targetElement.offsetLeft;
	var currentElement = targetElement;
	var i = 0;
	var isie7= ( navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;
	while ( currentElement = currentElement.offsetParent )
	{
		top += currentElement.offsetTop;
		left += currentElement.offsetLeft;
		if ( isie7 && currentElement.currentStyle.position=='relative' )
		{
			left -= currentElement.currentStyle.marginLeft.replace(/px/g, "") * 1;
		}
		i++;
	}

	// now get the width and height of the target element
	var width = targetElement.offsetWidth;
	var height = targetElement.offsetHeight;
	
	// using these to work out the classname
	var popTipClassVertical = 'Top';
	var popTipClassHorizontal = 'Left';
	
	// now we need to work out how big the popTipDiv is going to be
	var popTipWidth = popTipContainer.offsetWidth;
	var popTipHeight = popTipContainer.offsetHeight;
	
	// by default, the pop tip will go in the middle of the target element
	var popTipLeft = left + ( width / 2 );
	var popTipTop = top + height;

	// we need to know the size of the windae
	if ( typeof( window.innerWidth ) == 'number' )
	{
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	}
	else if ( document.documentElement 
		&& ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
	{
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	// we also need ot know how much we've scrolled down
	// default it to 0
	var yScroll = 0;
	if( typeof( window.pageYOffset ) == 'number' )
	{
		yScroll = window.pageYOffset;
	}
	else if( document.body && document.body.scrollTop )
	{
		yScroll = document.body.scrollTop;
	}
	else if( document.documentElement && document.documentElement.scrollTop )
	{
		yScroll = document.documentElement.scrollTop;
	}
	
	
	// let's add a little safety margin on to the width to account for scrollbars and shit,
	// and also increase the allowed height to account fo scrolling
	windowWidth -= 20;
	windowHeight -= 20;
	windowHeight += yScroll;
	
	// the popTip will be below the target element with the same 'left' value
	// but perhaps this will make it overlap the bottom or right of the screen :(
	// in these cases, we must adjust
	if ( popTipLeft + popTipWidth > windowWidth )
	{
		popTipLeft -= popTipWidth;
		popTipClassHorizontal = 'Right';
	}
	
	if ( popTipTop + popTipHeight > windowHeight )
	{
		popTipTop -= ( popTipHeight + height );
		popTipClassVertical = 'Bottom';
	}

	// right now, the very edge of the popup box is at the middle of the target
	// this will make it difficult to get nice looking pointers on the popups, so we'll move it 15px to one side
	// so that there's room for a nice graphic
	popTipLeft = ( popTipClassHorizontal=='Left' ) ? popTipLeft - 15 : popTipLeft + 15;

	popTipContainer.className = 'popTipPoint' + popTipClassVertical + popTipClassHorizontal;
	popTipContainer.style.top = popTipTop + 'px';
	popTipContainer.style.left = popTipLeft + 'px';
	
	// make active areas
	var tipArea = new Array( popTipLeft, popTipLeft + popTipWidth, popTipTop, popTipTop + popTipHeight );
	var targetElementArea = new Array( left, left + width, top, top + height );
	popTipActiveAreas[ 0 ] = tipArea;
	popTipActiveAreas[ 1 ] = targetElementArea;
}

