/************************************************************************************************************
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;

/* Don't change anything below here */


var ajax_tooltipObj = new Array();
var ajax_tooltipObj_iframe = new Array();

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


function ajax_showTooltip(externalFile,inputObj,popup_width,popup_height,ttname,direction)
{
	if(!ajax_tooltipObj[ttname])	/* Tooltip div not created yet ? */
	{
		ajax_tooltipObj[ttname] = document.createElement('DIV');
		ajax_tooltipObj[ttname].style.position = 'absolute';
		ajax_tooltipObj[ttname].id = 'ajax_tooltipObj_' + ttname;		
		ajax_tooltipObj[ttname].style.display='none';
		document.body.appendChild(ajax_tooltipObj[ttname]);

		if (direction == 'right') {		
			var leftDiv = document.createElement('DIV');	/* Create arrow div */
			leftDiv.className='ajax_tooltip_arrow';
			leftDiv.id = 'ajax_tooltip_arrow_' + ttname;
			ajax_tooltipObj[ttname].appendChild(leftDiv);
		}	
		var contentDiv = document.createElement('DIV'); /* Create tooltip content div */
		contentDiv.className = 'ajax_tooltip_content';
		if (direction == 'right') {
			contentDiv.style.left='18px';
		} else {
			contentDiv.style.right='-1px';
		}
		if (popup_width) contentDiv.style.width = popup_width;
		if (popup_height) contentDiv.style.height = popup_height;
		ajax_tooltipObj[ttname].appendChild(contentDiv);
		contentDiv.id = 'ajax_tooltip_content_' + ttname;
		if (direction != 'right') {		
			var leftDiv = document.createElement('DIV');	/* Create arrow div */
			leftDiv.className='ajax_tooltip_arrowflip';
			leftDiv.id = 'ajax_tooltip_arrow_' + ttname;
			ajax_tooltipObj[ttname].appendChild(leftDiv);
		}	
		if(ajax_tooltip_MSIE){	/* Create iframe object for MSIE in order to make the tooltip cover select boxes */
			ajax_tooltipObj_iframe[ttname] = document.createElement('<IFRAME frameborder="0">');
			ajax_tooltipObj_iframe[ttname].style.position = 'absolute';
			ajax_tooltipObj_iframe[ttname].border='0';
			ajax_tooltipObj_iframe[ttname].frameborder=0;
			ajax_tooltipObj_iframe[ttname].style.backgroundColor='#FFF';
			ajax_tooltipObj_iframe[ttname].src = 'javascript:\'<html></html>\';';
			contentDiv.appendChild(ajax_tooltipObj_iframe[ttname]);
			ajax_tooltipObj_iframe[ttname].style.left = '0px';
			ajax_tooltipObj_iframe[ttname].style.top = '0px';
		}

			
	}
	// Find position of tooltip
	ajax_tooltipObj[ttname].style.display='block';
	ajax_loadContent('ajax_tooltip_content_' + ttname, externalFile);
	if(ajax_tooltip_MSIE){
		ajax_tooltipObj_iframe[ttname].style.width = ajax_tooltipObj[ttname].clientWidth + 'px';
		ajax_tooltipObj_iframe[ttname].style.height = ajax_tooltipObj[ttname].clientHeight + 'px';
	}

	ajax_positionTooltip(inputObj, ttname, direction, popup_width);
}

function ajax_positionTooltip(inputObj, ttname, direction, popup_width) {
	var leftPos;
	var topPos;
	var elem = document.getElementById('ajax_tooltipObj_' + ttname);	
	if (direction == 'right') {
		leftPos = (ajaxTooltip_getLeftPos(inputObj) + inputObj.offsetWidth);
		topPos = ajaxTooltip_getTopPos(inputObj);
		/*
		var rightedge=ajax_tooltip_MSIE? document.body.clientWidth-leftPos : window.innerWidth-leftPos
		var bottomedge=ajax_tooltip_MSIE? document.body.clientHeight-topPos : window.innerHeight-topPos
		*/
		//var tooltipWidth = document.getElementById('ajax_tooltip_content_' + ttname).offsetWidth +  document.getElementById('ajax_tooltip_arrow_' + ttname).offsetWidth; 
		// Dropping this reposition for now because of flickering
		//var offset = tooltipWidth - rightedge; 
		//if(offset>0)leftPos = Math.max(0,leftPos - offset - 5);
		//ajax_tooltipObj[ttname].style.left = leftPos + 'px';
		//ajax_tooltipObj[ttname].style.top = topPos + 'px';	
	} else {
		clog("lp:" + ajaxTooltip_getLeftPos(inputObj));
		clog("pw:" + parseInt(popup_width));
		var arrow = 20;
		//leftPos = (ajaxTooltip_getLeftPos(inputObj) - parseInt(popup_width)- (arrow*2));
		leftPos = (ajaxTooltip_getLeftPos(inputObj) - arrow);
		topPos = ajaxTooltip_getTopPos(inputObj);
	}
	elem.style.left = leftPos + 'px';
	elem.style.top = topPos + 'px';	
}


function ajax_hideTooltip(ttname)
{
	ajax_tooltipObj[ttname].style.display='none';
}

function ajaxTooltip_getTopPos(inputObj)
{		
  var returnValue = inputObj.offsetTop;
  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;
}

