function Generic() {};

Generic.prototype.getWindowWidth = function() {
	return (window.innerWidth ? window.innerWidth : document.body.clientWidth);
}

Generic.prototype.getWindowHeight = function() {
	return (window.innerHeight ? window.innerHeight : document.body.clientHeight);
}

Generic.prototype.getMouseX = function(e) {
	if(e.pageX) {
		return e.pageX;
	}
	return e.clientX + 
		(document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
}

Generic.prototype.getMouseY = function(e) {
	if(e.pageY) {
		return e.pageY;
	} 
    
	return e.clientY + 
		(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);    
}

Generic.prototype.getWindowXOffset = function() {
	return (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
}

Generic.prototype.getWindowYOffset = function() {
	return (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
}

Generic.prototype.getPosition = function(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
		return [curleft, curtop];
	}
	return false;
}

Generic.prototype.getSize = function(obj) {
	if(obj.clientWidth) {
		return [obj.clientWidth, obj.clientHeight];
	}
	if(obj.offsetWidth) {
		return [obj.offsetWidth, obj.offsetHeight];
	} else if(obj.clip.width) {
		return [obj.clip.width, obj.clip.height];
	}
}

Generic.prototype.getSrcElement = function(e) {
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;

	return targ;
}

Generic.prototype.isCssActive = function() {
	bodyStyle = window.getComputedStyle(document.body, '');
	return bodyStyle.backgroundImage != 'none';
}

function toggle(id) {
	obj = document.getElementById(id);
	if(obj.style.display == 'block') {
		obj.style.display = 'none';
	}
	else {
		obj.style.display = 'block';
	}

}

function hide(id) {
	obj = document.getElementById(id);
	obj.style.display = 'none';
}

/**
 * The tooltip class provides a set of functions to show footnotes as if it were tooltips.
 * A mouseover on a label will trigger the tooltip to show up. After moving the mouse out of the label
 * or the tooltip itself, the tooltip will disappear after a short delay ({@link Tooltip#timeout}). It's
 * also possible to provide a 'Close' button.
 */
function Tooltip() {
	this.currentId = '';
	this.timeout = 500;
	this.timeoutObj = null;
	this.g = new Generic();
}

/**
 * Indicates that the mouse has been moved over a label, so we want to display a tooltip.
 * @param {Event} e The event which initiated this function call.
 */
Tooltip.prototype.activateLabel = function(e) {
	obj = this.g.getSrcElement(e);
	obj.title = '';
	
	ttobj	= document.getElementById('tt_' + obj.id.substring(6));

	// Only update close the current tooltip if it's another.
	// Only place the tooltip if it's not already active.
	if(this.currentId != ttobj.id) {
		this.closeCurrent();
		
		this.currentId = ttobj.id;
		this.placeObject(ttobj, this.g.getMouseX(e), this.g.getMouseY(e));
	}
	clearTimeout(this.timeoutObj);
}

/**
 * Close the currently visible tooltip, if any.
 */
Tooltip.prototype.closeCurrent = function() {
	if(this.currentId != '') {
		obj = document.getElementById(this.currentId);
		obj.style.visibility = 'hidden';
		obj.style.top = 0;
		obj.style.left = 0;
		this.currentId = '';
	}
}

/**
 * Indicates the mouse has been moved out of the area of the label, or the tooltip itself.
 * This function will start a timer, to close the tooltip after a short delay.
 */
Tooltip.prototype.mouseOut = function() {
	clearTimeout(tooltip.timeoutObj);
	if(tooltip.currentId != '') {
		tooltip.timeoutObj = setTimeout("tooltip.closeCurrent();", tooltip.timeout);
	}
}

/**
 * Indicates the mouse has been moved over the tooltip itself, so the timer will be cleared and 
 * the tooltip will stay visible. 
 */
Tooltip.prototype.mouseOver = function() {
	clearTimeout(tooltip.timeoutObj);
}

/**
 * Place the tooltip, calculate the coordinates by using the mouse coordinates.
 * @param obj The object (tooltip) to be placed.
 * @param {Integer} mouse_x The x-coordinate of the mouse wrt. to the left side of the screen.
 * @param {Integer} mouse_y The y-coordinate of the mouse wrt. to the upper side of the screen.
 */
Tooltip.prototype.placeObject = function(obj, mouse_x, mouse_y) {		
		var size = this.g.getSize(obj);

		var element_width = size[0];
	  var element_height = size[1];
		var dangerousPosition = false;
		var contentPos = this.g.getPosition(document.getElementById('contentTopLeft'));
		
		// Margin between mouse and tooltip
		var mouseMrg = 10;
		// Margin between tooltip and border of the screen
		var borderMrg = 20;
		
		
		posX = mouse_x - element_width - mouseMrg;
		if(posX < this.g.getWindowXOffset + borderMrg) {
			posX = this.g.getWindowXOffset + borderMrg;
			dangerousPosition = true;
		}

		posY = mouse_y - mouseMrg - element_height;
		if(posY < this.g.getWindowYOffset() + borderMrg) {
			if(!dangerousPosition) {
				posY = this.g.getWindowYOffset() + borderMrg;
			}
			else {
				posY = mouse_y + mouseMrg;
			}
		}
		
		obj.style.left = (posX - contentPos[0]) + 'px';
		obj.style.top = (posY - contentPos[1]) + 'px'; 
		
		obj.style.visibility = 'visible';
}

/**
 * Indicates the tooltip must be closed immediately, because the user clicked the 'Close' button.
 * @param {Event} e The event which initiated this function call.
 */
Tooltip.prototype.close = function(event) {
	this.currentId = '';
	clearTimeout(this.timeoutObj);
	
	obj = this.g.getSrcElement(event);
	obj.blur();
	obj.parentNode.parentNode.style.visibility = 'hidden';
	
	return false;
}

Tooltip.prototype.mayFollow = function() {
	return !tooltip.g.isCssActive();
}

/**
 * Create a tooltip from the DIV with the specified id.
 * @param {String} id The id of the DIV which should be transformed into a tooltip.
 */
Tooltip.prototype.create = function(id) {
	obj = document.getElementById(id);
	obj.className = 'tooltipJS';
	obj.onmouseover = this.mouseOver;
	obj.onmouseout = this.mouseOut;
	contents = obj.innerHTML;
	obj.innerHTML = 
		'<div class="tl"></div><div class="tr"></div><div class="top"></div>' + 
		'<div class="cnt"><div class="spacer"></div>' + contents + '</div>' +
		'<div class="bl"></div><div class="br"></div><div class="bottom"></div>' +
		'<div class="closeBtn"><a href="#ttSrc_' + id.substring(3) + '" onclick="return tooltip.close(event);"></a></div>';
}

// Create an instance of the tooltip function collection.
var tooltip = new Tooltip();

//window.onload = initTooltips;
function initTooltips() {
	if(document.styleSheets[0].insertRule) {
		document.styleSheets[0].insertRule('a.footnoteText sup { display: none; }', 0);
	}
	else {
		document.body.innerHTML += '<style type="text/css">a.footnoteText sup { display: none; }</style>';
	}
}
