


/**
 *	Class InfoTip
 *	=============
 *
 *	A class representing a mouse-tooltip that displays some information when
 *	the mouse is hovered-over, or clicks on a word or a target object in the 
 *	browser. The infotip hides when the mouse moves off of the target word or 
 *	object.
 *
 *	Class (Static) Fields:
 *	----------------------
 *		- static final String DEFAULT_TOOLTIP_ID
 *				The id for the <div> element that displays the tooltip and info
 *
 *		- static final String TOOLTIP_TERM
 *				The term for the default mouseover instruction.
 *
 *		- static final int	TIP_Y_DISPLACEMENT
 *				The number of pixels below the mouse pointer to display the tooltip
 *
 *		- static final int TIP_DISPLAY_WIDTH_MAX
 *				The maximum width of the table that contains the tooltip.
 *
 *		- static final int TIP_DISPLAY_WIDTH_MAX_FACTOR
 *				The somewhat arbitary number to multiply the string-length of
 *				the definition by, in order to arrive at a good general ballpark
 *				width for the infotip bubble (table)
 *
 *
 *	Instance Fields:
 *	---------------
 *		- String term: 
 *				The word to store information about; previously was the lookup key,
 *				but now lookup is done with 'key', and terms need not be unique.
 *
 *		- String definition: 
 *				The information that is stored with the key/term
 *
 *		- String key
 *				The lookup key, must be unique for every term/definition. Previously
 *				the 'term' was used as lookup key, this is newly added field.
 *
 *		- String tooltip_id: 
 *				The 'id' attribute of the <div> element that displays the tooltip.
 *
 */
//function InfoTip( term, definition) {
function InfoTip( term, definition, key) {
	
	this.term 		= term;
	this.definition = definition;
	this.key		= key;
	
	return this;
} 



//
// Class (static) fields
//
InfoTip.DEFAULT_TOOLTIP_ID 				= "infotip";
InfoTip.TOOLTIP_TERM 					= "tooltip";
InfoTip.TOOLTIP_KEY						= "infotipen";
InfoTip.TIP_Y_DISPLACEMENT 				= 20;
InfoTip.TIP_DISPLAY_WIDTH_MAX 			= 280;
InfoTip.TIP_DISPLAY_WIDTH_MAX_FACTOR 	= 5;

InfoTip.list							= null;
InfoTip.tooltip_element 				= null;
InfoTip.tooltip_style 					= null;
var agent   							= navigator.userAgent.toLowerCase();
InfoTip.ns4 							= document.layers;
InfoTip.ns6 							= document.getElementById && !document.all;
InfoTip.ie4 							= document.all;
InfoTip.ie6 							= document.getElementById && !document.layers && (document.compatMode=="CSS1Compat");
InfoTip.ie7 							= document.getElementById && !document.layers && (document.compatMode=="BackCompat") && agent.indexOf('msie')!=-1;
InfoTip.supported_agent 				= ( InfoTip.ns4 | InfoTip.ns6 | InfoTip.ie4 | InfoTip.ie6 | InfoTip.ie7);

//alert( "document.all=" + document.all + "\ndocument.layers=" + document.layers + "\nis IE7:" + InfoTip.ie7);
//
// Class (static) methods
//


/**
 *	public static void init()
 *
 */
InfoTip.init = function() {

	if ( InfoTip.supported_agent ) {
	
		if ( InfoTip.ns4 ) {
			InfoTip.tooltip_element 			= eval( "document." + InfoTip.DEFAULT_TOOLTIP_ID );
			InfoTip.tooltip_style 				= InfoTip.tooltip_element;
			document.captureEvents(Event.MOUSEMOVE | Event.CLICK);
			
		} else {
			InfoTip.tooltip_element 			= document.getElementById( InfoTip.DEFAULT_TOOLTIP_ID );
			InfoTip.tooltip_style 				= InfoTip.tooltip_element.style;
			InfoTip.tooltip_style.visibility 	= "visible";
			InfoTip.tooltip_style.display 		= "none";
		}
		
	    document.onmousemove 				= InfoTip.moveToMouseLoc;
	    document.onclick 					= InfoTip.moveToMouseLoc;
		
		InfoTipList.create_list();
	}

}

window.onload = InfoTip.init;



/** 
 *	public static void hide()
 *
 */
InfoTip.hide = function() {

    if( InfoTip.ns4 ) {
		InfoTip.tooltip_style.visibility = "hidden";
	} else {
		InfoTip.tooltip_style.display = "none";
	}
}




/**
 *	public static void moveToMouseLoc()
 *
 */
InfoTip.moveToMouseLoc = function (e) {
    
	var x;
	var y;
	var px = '';
	var scroll_x = 0;
	var scroll_y = 0;
	
    if ( InfoTip.ns4 || InfoTip.ns6 ) {
        
		x = e.pageX;
		y = e.pageY + InfoTip.TIP_Y_DISPLACEMENT ;
		
		if ( InfoTip.ns6 ) {
			px = "px";
		}
		
    } else {
		x  = event.clientX;
		y  = event.clientY + InfoTip.TIP_Y_DISPLACEMENT;
	}
	
    if ( InfoTip.ie6 && !InfoTip.ns6 ) {
    	scroll_x = document.body.parentNode.scrollLeft;
    	scroll_y = document.body.parentNode.scrollTop;
    }else if ( InfoTip.ie7 ) {
    	scroll_x = document.body.parentNode.scrollLeft;
    	scroll_y = document.body.scrollTop;
    }

	InfoTip.tooltip_style.left = (x + scroll_x) + px;
	InfoTip.tooltip_style.top  = (y + scroll_y) + px;

    return true;
}






/**
 *	public static void display()
 *
 */
//InfoTip.display = function ( term ) {
InfoTip.display = function ( key ) {
	
	//var infotip 			= InfoTip.list.get_infotip( term );
	var infotip 			= InfoTip.list.get_infotip( key );
	var term				= infotip.term;
	var definition 			= '<span class="infotip-definition">' + infotip.definition + '</span>';
	var defLength 			= definition.length;
	var tablewidth 			= (defLength * InfoTip.TIP_DISPLAY_WIDTH_MAX_FACTOR > InfoTip.TIP_DISPLAY_WIDTH_MAX) ? InfoTip.TIP_DISPLAY_WIDTH_MAX : defLength * InfoTip.TIP_DISPLAY_WIDTH_MAX_FACTOR;
	var word_heading 		= (term == InfoTip.TOOLTIP_TERM) ? "" : "<b>" + term + "</b><br />"; // no heading for toolip general default instruction	
	
    infotip.display_infotip( word_heading + definition, tablewidth);

	//alert ("x: " + event.clientX + ", y: " + event.clientY + "\nparent-scrollTop: " + document.body.parentNode.scrollTop + "\nbody-scrollTop: " + document.body.scrollTop)
}



//
// InfoTip instance members
//
InfoTip.prototype.tooltip_id = InfoTip.DEFAULT_TOOLTIP_ID;



/**
 *	public void display_infotip( String msg, int tablewidth )
 *
 */
InfoTip.prototype.display_infotip = function (msg, tablewidth) {

    var content = (
    		  '<div class="infotip-box">'
    		+ '<div class="infotip-message" style="width:' + tablewidth + ';">'
    		+ msg
    		+ '</div></div>'
    );
    
    if( InfoTip.ns4 ) {
      InfoTip.tooltip_style.document.write( content );
      InfoTip.tooltip_style.document.close();
      InfoTip.tooltip_style.visibility = "visible";
    
	} else {
      InfoTip.tooltip_element.innerHTML = content;
      InfoTip.tooltip_style.display 	= 'block'
    }
}

