/************************************************************************************************************
Ajax tooltip
Copyright (C) 2006  DTHMLGoodies.com, Alf Magne Kalleland

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

Alf Magne Kalleland, 2006
Owner of DHTMLgoodies.com
	
************************************************************************************************************/	




/* Custom variables */

/* Offset position of tooltip */
var x_offset_tooltip = 5;
var y_offset_tooltip = 0;
var ajax_tooltipWidth = 290;
var ajax_arrowWidth = 20;
var ajax_tooltipDelay = 1000;
var ajax_timeout_ID = null;

/* Don't change anything below here */


var ajax_tooltipObj = false;
var ajax_tooltipObj_iframe = false;

var ajax_tooltip_MSIE = false;

if(navigator.userAgent.indexOf('MSIE')>=0)ajax_tooltip_MSIE=true;

function ajax_showTooltip(externalFile,inputObj)
{
	if(!ajax_tooltipObj)	/* Tooltip div not created yet ? */
	{
		ajax_tooltipObj = document.createElement('DIV');
		ajax_tooltipObj.style.position = 'absolute';
		ajax_tooltipObj.id = 'ajax_tooltipObj';		
		document.body.appendChild(ajax_tooltipObj);

		var leftDiv = document.createElement('DIV');	/* Create arrow div to left*/
		leftDiv.className='ajax_tooltip_arrow';
		leftDiv.id = 'ajax_tooltip_arrow';
		ajax_tooltipObj.appendChild(leftDiv);
		
		var contentDiv = document.createElement('DIV'); /* Create tooltip content div */
		contentDiv.className = 'ajax_tooltip_content';
		ajax_tooltipObj.appendChild(contentDiv);
		contentDiv.id = 'ajax_tooltip_content';
		
		if(ajax_tooltip_MSIE){	/* Create iframe object for MSIE in order to make the tooltip cover select boxes */
			ajax_tooltipObj_iframe = document.createElement('<IFRAME frameborder="0">');
			ajax_tooltipObj_iframe.style.position = 'absolute';
			ajax_tooltipObj_iframe.border='0';
			ajax_tooltipObj_iframe.frameborder=0;
			ajax_tooltipObj_iframe.style.backgroundColor='#FFF';
			ajax_tooltipObj_iframe.src = 'about:blank';
			contentDiv.appendChild(ajax_tooltipObj_iframe);
			ajax_tooltipObj_iframe.style.left = '0px';
			ajax_tooltipObj_iframe.style.top = '0px';
		}

			
	}
	// Find position of tooltip
	var arrowDiv = document.getElementById('ajax_tooltip_arrow');	/* Create arrow div to left*/
	if (ajax_point_to_left(inputObj)) {
		arrowDiv.className='ajax_tooltip_arrow';
	}
	else {
		if (ajax_tooltip_MSIE) {
			arrowDiv.className='ajax_tooltip_arrow_right_IE';
		}
		else {
			arrowDiv.className='ajax_tooltip_arrow_right';
		}
	}
	ajax_loadContent('ajax_tooltip_content',externalFile);
	if(ajax_tooltip_MSIE){
		ajax_tooltipObj_iframe.style.width = ajax_tooltipObj.clientWidth + 'px';
		ajax_tooltipObj_iframe.style.height = ajax_tooltipObj.clientHeight + 'px';
	}

	ajax_positionTooltip(inputObj);
	if (ajax_timeout_ID!=null)
		clearTimeout(ajax_timeout_ID);
	ajax_timeout_ID = setTimeout("ajax_tooltipObj.style.display='block';", ajax_tooltipDelay);
//	ajax_tooltipObj.style.display='block';
}

function ajax_positionTooltip(inputObj)
{
	var leftPos = (ajaxTooltip_getLeftPos(inputObj) + inputObj.offsetWidth);
	var topPos = ajaxTooltip_getTopPos(inputObj);
	
	var maxrightPos=ajax_tooltip_MSIE? document.body.clientWidth+document.body.scrollLeft : window.innerWidth+window.pageXOffset;
	var maxbottomPos=ajax_tooltip_MSIE? document.body.clientHeight+document.body.scrollTop : window.innerHeight+window.pageYOffset;

//	var tooltipWidth = document.getElementById('ajax_tooltip_content').offsetWidth +  document.getElementById('ajax_tooltip_arrow').offsetWidth; 

	var alt_leftPos = leftPos - inputObj.offsetWidth; //get the position of the left side of the target object (image)
	var farright_edge = leftPos + ajax_tooltipWidth;

//	window.status = "leftPos="+leftPos+", alt_leftPos="+alt_leftPos+", farrightedge="+farright_edge+", ajax_tooltipwidth="+ajax_tooltipWidth+", maxright="+maxrightPos+", offsetWidth="+inputObj.offsetWidth+", contentWidth="+document.getElementById('ajax_tooltip_content').offsetWidth+", getLeftPos="+ajaxTooltip_getLeftPos(inputObj)+", arrowWidth="+document.getElementById('ajax_tooltip_arrow').offsetWidth; 
	
	if (farright_edge > maxrightPos) {
		leftPos = alt_leftPos - ajax_tooltipWidth - ajax_arrowWidth;  //shift left pos to place the tooltip window to left of object
	}
	
	ajax_tooltipObj.style.left = leftPos + 'px';
	ajax_tooltipObj.style.top = topPos + 'px';	
	
}

function ajax_point_to_left(inputObj)
{
	var ajax_tooltip_on_rightside = false; // if you put the tooltip on the right side, the arrow points to left
	var leftPos = (ajaxTooltip_getLeftPos(inputObj) + inputObj.offsetWidth);
	
	var maxrightPos=ajax_tooltip_MSIE? document.body.clientWidth+document.body.scrollLeft : window.innerWidth+window.pageXOffset;

	var alt_leftPos = leftPos - inputObj.offsetWidth; //get the position of the left side of the target object (image)

	var farright_edge = leftPos + ajax_tooltipWidth;
	if (farright_edge > maxrightPos) {
		ajax_tooltip_on_rightside = false;
	}
	else {		ajax_tooltip_on_rightside = true; }
	
/*
if (ajax_tooltip_on_rightside) {
		window.status = "tooltip on right=true, leftPos="+leftPos+", alt_leftPos="+alt_leftPos+", farrightedge="+farright_edge+", tooltipwidth="+tooltipWidth+", maxright="+maxrightPos+", offsetWidth="+inputObj.offsetWidth; 
	}
	else {
		window.status = "tooltip on right=false, leftPos="+leftPos+", alt_leftPos="+alt_leftPos+", farrightedge="+farright_edge+", tooltipwidth="+tooltipWidth+", maxright="+maxrightPos+", offsetWidth="+inputObj.offsetWidth; 

	}
*/

	return ajax_tooltip_on_rightside;		
}


function ajax_hideTooltip()
{
	ajax_tooltipObj.style.display='none';
	if (ajax_timeout_ID!=null)
		clearTimeout(ajax_timeout_ID);
}

function ajaxTooltip_getTopPos(inputObj)
{		
  var returnValue = 0;
  if (ajax_tooltip_MSIE) {
			 returnValue += inputObj.offsetTop; // skip first offset top for mozilla
  }
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML') returnValue += inputObj.offsetTop;
  }
  return returnValue;
}

function ajaxTooltip_getLeftPos(inputObj)
{
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
  }
  return returnValue;
}