/**
 * Create ToolTip object
 * @type  ToolTip
 */
var tooltip = null;

/**
 * Initializes ToolTip module
 * @type  ToolTip
 */
tooltip = new ToolTip();

/**
 * ToolTip module
 * @constructor
 * @author   Sperg Ádám
 * @version  1.01
 */
function ToolTip() {
	
	/**
	 * Clien width
	 * @type  Number
	 */
	var myWidth = 0;
	
	/**
	 * Clien height
	 * @type  Number
	 */
	var myHeight = 0;
	
	/**
	 * Mouse X position
	 * @type  Number
	 */
	var mouseX = 0;
	
	/**
	 * Mouse Y position
	 * @type  Number
	 */
	var mouseY = 0;
	
	/**
	 * Screen margin for mouse position
	 * @type  Number
	 */
	var mouseMargin = 15;
	
	/**
	 * Tooltip Box Width
	 * @type  String
	 */
	var toolTipWidth = '350px';
	
	/**
	 * Get client width and height
	 */
	function getClientWH() {
		if(typeof( window.innerWidth ) == 'number' ) {
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
	}
	
	/**
	 * Get element by id
	 * @param   String  pId  DOM element id
	 * @return  Object  DOM element
	 */
	function getElt(pId) {
		return (document.getElementById ? document.getElementById(pId)
			: document.all ? document.all[pId]
			: null);
	}
	
	/**
	 * Move toolTip
	 * @param  Event
	 */
	function moveToolTip(e) {
		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;
		}
		if (mouseX < 0) {
			mouseX = 0;
		}
		if (mouseY < 0) {
			mouseY = 0;
		}
		var tooltip = getElt('toolTipBox');
		tooltip.style.width = tooltip.offsetWidth + 'px';
		tooltip.style.height = tooltip.offsetHeight + 'px';
		if(e.clientX > (myWidth - tooltip.offsetWidth - mouseMargin - 20)) {
			tooltip.style.left = (mouseX - tooltip.offsetWidth - mouseMargin) + 'px';
		} else {
			tooltip.style.left = mouseX + mouseMargin + 'px';
		}
		if(e.clientY > (myHeight - tooltip.offsetHeight - mouseMargin - 5)) {
			tooltip.style.top = (mouseY - tooltip.offsetHeight - mouseMargin) + 'px';
		} else {
			tooltip.style.top = mouseY + mouseMargin + 'px';
		}
		return true;
	}
	
	/**
	 * Show toolTip
	 * @param  Object  pObj      Caller element
	 * @param  String  pTitle    Title of the toolTip
	 * @param  String  pContent  Content of the toolTip
	 */
	this.show = function(pObj, pTitle, pContent) {
		if (navigator.appName.indexOf('Microsoft') == -1) {
			document.captureEvents(Event.MOUSEMOVE);
		}
		document.onmousemove = moveToolTip;
		if(!document.attachEvent) {
			pObj.addEventListener('mouseout', hide, false);
		} else {
			pObj.attachEvent('onmouseout', hide); 
		}
		if(pObj.title) {
			pObj.title = '';
		}
		if(pObj.alt = '') {
			pObj.alt = '';
		}
		getClientWH();
		pObj.style.cursor = 'help';
		var tooltip = getElt('toolTipBox');
		var tooltipHeader = getElt('toolTipHeader');
		var tooltipContent = getElt('toolTipContent');
		tooltipHeader.innerHTML = pTitle ? pTitle : '';
		tooltipContent.innerHTML = pContent ? pContent : '';
		tooltip.style.visibility = 'visible';
	}
	
	/**
	 * Hide toolTip
	 */
	function hide() {
		if (navigator.appName.indexOf('Microsoft') == -1) {
			document.captureEvents(Event.MOUSEMOVE);
		}
		document.onmousemove = null;
		var tooltip = getElt('toolTipBox');
		tooltip.style.width = toolTipWidth;
		tooltip.style.height = '';
		tooltip.style.cursor = 'default';
		tooltip.style.visibility = 'hidden';
		var content = getElt('toolTipContent');
		content.className = 'toolTipNormal';
	}
}

