/* Pull out the parent object.
	Take the value of its title attribute
	put it in the tooltip layer and make the layer visible */
function showNiceToolTip( inEvent, inLeft ){
	window.status = "showNiceToolTip";
	var theElement = getElementFromEvent( inEvent );
	
	do{
		if( null == theElement ){
			break;
		}

		var theToolTipLayer = document.getElementById( "toolTipLayer" );

		if( null == theToolTipLayer ){
			break;
		}

		var theHelpText = getHelpText( theElement )

		removeElementText( theToolTipLayer );
		setElementText( theToolTipLayer, theHelpText );
		
		var theParent = theElement.parentElement;
		if( null == theParent ){
			break;	
		}

		theToolTipLayer.style.left = "" + inLeft + "px";

		if( inLeft < 400 ){
			theToolTipLayer.style.textAlign = "right";
		}else{
			theToolTipLayer.style.textAlign = "left";	
		}

		
	}while( false );
}

function removeNiceToolTip( inEvent ){
	var theParent = getElementFromEvent( inEvent );

	if( theParent && inEvent.relatedTarget && (inEvent.relatedTarget.parentNode == theParent) && (inEvent.relatedTarget.nodeType == Node.TEXT_NODE) ){
		//  Do nothing - we're entering the text child node of which is a silly Safari eccentricity!
		// Also the excessive checking is to make sure an exception isn't thrown...
	}else{
		if( theParent ){
			
			var theToolTipLayer = document.getElementById( "toolTipLayer" );
			
			if( theToolTipLayer ){
				removeElementText( theToolTipLayer );	
			}
		}
	}
}


function removeElementText( inElement ){
	for( var theChild = inElement.firstChild ; theChild ; theChild = theChild.nextSibling ){
		inElement.removeChild( theChild );
	}
}

function setElementText( inElement, inText ){
	var theTextNode = document.createTextNode( inText );
	if( theTextNode ){
		inElement.appendChild( theTextNode );
	}else{
		opera.postError( "couldn't create text node" );	
	}
}

// Position an object at a specific pixel coordinate
function shiftTo( inElement, x, y) {
	// equalize incorrect numeric value type
	var units = (typeof inElement.left == "string") ? "px" : 0;
	inElement.left = x + units;
	inElement.top = y + units;
}

// Will be expanded in future to deal with spans that have more than one child
function getHelpText( inElement ){
	var theHelpText = "";
	var theSpanners = inElement.getElementsByTagName( "span" );
	var theMaxIndex = theSpanners.length;
	for( var theIndex = 0 ; theIndex < theMaxIndex ; ++theIndex ){
		if( theSpanners[theIndex].className == "contextualHelp" ){
			theHelpText = theSpanners[theIndex].firstChild.nodeValue;
		}
	}
	
	return theHelpText;
}