// tooltip component
var tooltip = new Object();
// Timeout_id array
var timeout_id = null;
var showing = false;
var eventPosX = null;
var eventPosY = null;
// Set content for Tooltip via Ajax call
// Parameters:  * url         : full url path, including parameters, for Ajax call
//              * containerid : ID of the tooltip element
//              * key         : key used for caching Tooltip ***** need to be defined thoroughly
// Return: TRUE if successful, FALSE if not
tooltip.setTooltipContent = function(url, containerid, key) {
	var success = false;
	//if the key is in cache
	//if(jsVars.global.ajaxPopCache[key] != null) {
	//	$(containerid).innerHTML = jsVars.global.ajaxPopCache[key];
	//	success = true;
	//} else {
		//if not, make ajax call
		new Ajax.Request(url, {
				method: 'get',
				onSuccess: function(transport) {
					//put the response text into cache and present it on the tooltip element
					//jsVars.global.ajaxPopCache[key] = transport.responseText;
					$(containerid).innerHTML = transport.responseText;
					success = true;
				},
				onFailure: function(transport) {
					$(containerid).innerHTML = transport.status;
				}
		});
	//}
	return success;
}




// Initialize tooltip
// Parameters:   * url         : full url path, including parameters, for Ajax call
//               * e           : event on the parent element of tooltip element
//		 * containerid : ID of the tooltip element
//		 * delay       : time delay - miliseconds - e.g: 1000, 2000, etc...
// Return: nothing
tooltip.init = function(containerid, delayOut) {
	$(containerid).onmouseout = function() {
		tooltip.hide(containerid, delayOut);
	}
	$(containerid).onmouseover = function() {
		clearTimeout(timeout_id);
		tooltip.display(containerid);
	}
}


// Show Tooltip
// Parameters:   * url		   : full url path, including parameters, for Ajax call
//               * ev  		   : event on the parent element of tooltip element
//		 		 * containerid : ID of the tooltip element
//		 		 * delay       : time delay - miliseconds - e.g: 1000, 2000, etc...
// Return: nothing
tooltip.show = function(url, ev, containerid, delay)  {

	var e = ev || window.event;
	eventPosX = Event.pointerX(e);
	eventPosY = Event.pointerY(e)  ;
	timeout_id = setTimeout("tooltip.showTooltip('" + url + "','" + e + "','" + containerid + "',false);", delay);
	//tooltip.showTooltip(url, e, containerid);
	//$('debug').innerHTML = $('debug').innerHTML + "<br /> * TimeOut_ID_OnMouseOver = " + timeout_id;
	//$('debug').innerHTML = $('debug').innerHTML + "<br /> * e = " + Event.pointerX(e);
}

tooltip.showText  = function (text, ev, containerid, delay ){
	var e = ev || window.event;
	eventPosX = Event.pointerX(e);
	eventPosY = Event.pointerY(e)  ;
	//text = text.replace (/\'/,"\\'");
	//text = text.replace (/\"/,'\\"');
	tooltip.textDisplay = text;
	timeout_id = setTimeout("tooltip.showTooltip('" + "','" + e + "','" + containerid + "', true);", delay);
}


tooltip.showTooltip = function(url, ev, containerid, isText) {
	var e = ev || window.event;
	if (!document.all&&!document.getElementById)
		return;
	if (showing == false) {
		showing = true;
		tooltip.locate(e, containerid);
		//tooltip.setTooltipContent(url, containerid, 'key');
		if ( !isText ){
			if (tooltip.setTooltipContent(url, containerid, 'key')) {
				tooltip.locate(e, containerid);
			}
		}
		else {
			$(containerid).innerHTML = tooltip.textDisplay;
			tooltip.locate(e, containerid);
		}
	}
}


// Locate the Tooltip
// Parameters:   * ev	       : event on the parent element of tooltip element
//		 		 * containerid : ID of the tooltip element
// Return: nothing
tooltip.locate = function(ev, containerid) {
	var e = ev || window.event;
	if (showing == false) return false;
	var myWidth = 0;
	var myHeight = 0;

	var tooltip_com=$(containerid);

	var eventX = parseInt(Event.pointerX(e))>0?Event.pointerX(e):eventPosX;
	var eventY = parseInt(Event.pointerY(e))>0?Event.pointerY(e):eventPosY;



	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;
	}

	if (eventX - 20 + $(containerid).getWidth() > myWidth)
		tooltip_com.style.left = (eventX - $(containerid).getWidth()) + "px";
	else
		tooltip_com.style.left = eventX - 20 + "px";

	if (eventY + 15 + $(containerid).getHeight() > myHeight)
		tooltip_com.style.top = (eventY - 15 - $(containerid).getHeight()) + "px";
	else
		tooltip_com.style.top = eventY + 15 + "px";

	//----- Fix for wrapping -------//
	tooltip_com.style.whiteSpace = "nowrap";
	//----- End Fix ------//

	/*if (eventX + $(containerid).getWidth() > myWidth)
		tooltip_com.style.left = (eventX - $(containerid).getWidth()) + "px";
	else
		tooltip_com.style.left = eventX + "px";

	if (eventY + $(containerid).getHeight() > myHeight)
		tooltip_com.style.top = (eventY - $(containerid).getHeight()) + "px";
	else
		tooltip_com.style.top = eventY + "px";*/

	tooltip.display(containerid);
}

// Display tooltip
tooltip.display = function(containerid) {
	var tooltip_com=$(containerid);
	//tooltip_com.style.opacity = "0";
	tooltip_com.style.display = "block";
	new Rico.Effect.FadeTo(containerid,0.8,500,5);
}

tooltip.stopTimeout = function() {
	//$('debug').innerHTML = $('debug').innerHTML + "<br /> * TimeOut_ID_OnMouseOut = " + timeout_id;
	clearTimeout(timeout_id);
}

// Hide tooltip
tooltip.hide = function(containerid, delay) {
	timeout_id = setTimeout("tooltip.hideTooltip('" + containerid + "');", delay);
}

tooltip.hideTooltip = function (containerid) {
	showing = false;
	setTimeout("tooltip.turnoff('"+containerid+"');",100);
	new Rico.Effect.FadeTo(containerid,0,200,5);
	//new Rico.Effect.Position(containerid,-100,-100,10,9);

}

tooltip.turnoff = function(containerid) {
	$(containerid).setStyle({
	  'display': 'none'
	});
}

