/************************************************************************************************************
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 = false;
var ajax_tooltipObj_iframe = false;

var ajax_tooltip_MSIE = false;
if(navigator.userAgent.indexOf('MSIE')>=0)ajax_tooltip_MSIE=true;


function ajax_showMultipleTooltip(externalFile, inputObj, width, height, showclose, idPart, topAnchor, inline)
{
    var id = 'ajax_tooltipObj' + idPart;
    var contentId = 'ajax_tooltip_content' + idPart;
    var obj = document.getElementById(id);
    var closedivId = 'ajax_tooltip_close' + idPart;
    var frame = false;
	if(obj == null)	/* Tooltip div not created yet ? */
	{
		obj = document.createElement('DIV');
		if (!inline)
		    obj.style.position = 'absolute';		
		obj.id = id;		
		document.body.appendChild(obj);

		var closeDiv = document.createElement('DIV');	/* Create arrow div */		
		if (showclose)
		{
		    closeDiv.className='ajax_tooltip_close';
		}
		else
		{
		    closeDiv.className='ajax_tooltip_close_hidden';
		}
		closeDiv.id = closedivId;
		closeDiv.innerHTML = '<a href="#" onclick="ajax_hideTooltip2(\'' + id + '\');return false;">Close</a>';		    	    
	    obj.appendChild(closeDiv);
	    		
		
		var contentDiv = document.createElement('DIV'); /* Create tooltip content div */
		contentDiv.className = 'ajax_tooltip_content';
		obj.appendChild(contentDiv);
		
		contentDiv.id = contentId;				
		
		if(ajax_tooltip_MSIE){	/* Create iframe object for MSIE in order to make the tooltip cover select boxes */
			frame = document.createElement('<IFRAME frameborder="1">');
			if (!inline)
			    frame.style.position = 'absolute';
			frame.border='0';
			frame.frameborder=0;
			frame.style.backgroundColor='#000';
			frame.src = 'about:blank';
			contentDiv.appendChild(frame);
			frame.style.left = '0px';
			frame.style.top = '0px';			
		}			
	}
	document.getElementById(contentId).innerHTML = '';
	document.getElementById(contentId).style.height = 'auto';
	if (width)
		obj.style.width = width;
	else
	    obj.style.width = 'auto';
	
	obj.style.height = '0px';
	
	// Find position of tooltip
	obj.style.display='block';
	ajax_loadContent(contentId, externalFile, width, height);
	if(ajax_tooltip_MSIE){
	    frame.style.width = obj.clientWidth + 'px';
	    frame.style.height = obj.clientHeight + 'px';				
	}

    if (!inline)
	    ajax_positionTooltip2(inputObj, width, height, topAnchor, id, contentId, closedivId);
}

function ajax_positionTooltip2(inputObj, width, height, topAnchor, id, contentId, closedivId)
{
    var obj = document.getElementById(id);
	var leftPos = (ajaxTooltip_getLeftPos(inputObj) + inputObj.offsetWidth);
	var topPos = ajaxTooltip_getTopPos(inputObj);
	if (topAnchor)
	    topPos -= height;	
	
	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, tooltipHeight;
	if (width)
	    tooltipWidth = width;
	else
	    tooltipWidth = document.getElementById(contentId).offsetWidth; 
	    
	if (height)
	    tooltipHeight = height;
	else	
	    tooltipHeight = document.getElementById(contentId).offsetHeight +  document.getElementById(closedivId).offsetHeight; 

	
	// Dropping this reposition for now because of flickering
	var offset = rightedge - tooltipWidth; 
	if(offset<0)leftPos = Math.max(0,ajaxTooltip_getLeftPos(inputObj) - tooltipWidth);
		
	if (leftPos == 0)
	    leftPos = ajaxTooltip_getLeftPos(inputObj) - width / 2;
	    	
	obj.style.left = leftPos + 'px';
	obj.style.top = topPos + 'px';		
	
}

function ajax_hideTooltip2(id)
{
    var obj = document.getElementById(id);
	obj.style.display='none';
}


function ajax_showTooltip(externalFile, inputObj, width, height, showclose, topAnchor, inline)
{
	if(!ajax_tooltipObj)	/* Tooltip div not created yet ? */
	{
		ajax_tooltipObj = document.createElement('DIV');
		if (!inline)
		    ajax_tooltipObj.style.position = 'absolute';		
		ajax_tooltipObj.id = 'ajax_tooltipObj';		
		document.body.appendChild(ajax_tooltipObj);

				
		var closeDiv = document.createElement('DIV');	/* Create arrow div */		
		if (showclose)
		{
		    closeDiv.className='ajax_tooltip_close';
		}
		else
		{
		    closeDiv.className='ajax_tooltip_close_hidden';
		}
		closeDiv.id = 'ajax_tooltip_close';
		closeDiv.innerHTML = '<a href="#" onclick="ajax_hideTooltip();return false;">Close</a>';		    	    
	    ajax_tooltipObj.appendChild(closeDiv);
	    		
		
		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="1">');
			if (!inline)
			    ajax_tooltipObj_iframe.style.position = 'absolute';
			ajax_tooltipObj_iframe.border='0';
			ajax_tooltipObj_iframe.frameborder=0;
			ajax_tooltipObj_iframe.style.backgroundColor='#000';
			ajax_tooltipObj_iframe.src = 'about:blank';
			contentDiv.appendChild(ajax_tooltipObj_iframe);
			ajax_tooltipObj_iframe.style.left = '0px';
			ajax_tooltipObj_iframe.style.top = '0px';			
		}			
	}
	document.getElementById('ajax_tooltip_content').innerHTML = '';
	document.getElementById('ajax_tooltip_content').style.height = 'auto';
	if (width)
		ajax_tooltipObj.style.width = width;
	else
	    ajax_tooltipObj.style.width = 'auto';
	
	ajax_tooltipObj.style.height = '0px';
	// Find position of tooltip
	ajax_tooltipObj.style.display='block';
	ajax_loadContent('ajax_tooltip_content', externalFile, width, height);
	if(ajax_tooltip_MSIE){
		ajax_tooltipObj_iframe.style.width = ajax_tooltipObj.clientWidth + 'px';
		ajax_tooltipObj_iframe.style.height = ajax_tooltipObj.clientHeight + 'px';				
	}

    if (!inline)
	    ajax_positionTooltip(inputObj, width, height, topAnchor);
}

function ajax_showTooltipMessage(messageHtml, inputObj, width, height, showclose, topAnchor, inline)
{
	if(!ajax_tooltipObj)	/* Tooltip div not created yet ? */
	{
		ajax_tooltipObj = document.createElement('DIV');
		if (!inline)
		    ajax_tooltipObj.style.position = 'absolute';		
		ajax_tooltipObj.id = 'ajax_tooltipObj';		
		document.body.appendChild(ajax_tooltipObj);

				
		var closeDiv = document.createElement('DIV');	/* Create arrow div */		
		if (showclose)
		{
		    closeDiv.className='ajax_tooltip_close';
		}
		else
		{
		    closeDiv.className='ajax_tooltip_close_hidden';
		}
		closeDiv.id = 'ajax_tooltip_close';
		closeDiv.innerHTML = '<a href="#" onclick="ajax_hideTooltip();return false;">Close</a>';		    	    
	    ajax_tooltipObj.appendChild(closeDiv);
	    		
		
		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 */
			c = document.createElement('<IFRAME frameborder="1">');
			if (!inline)
			    ajax_tooltipObj_iframe.style.position = 'absolute';
			ajax_tooltipObj_iframe.border='0';
			ajax_tooltipObj_iframe.frameborder=0;
			ajax_tooltipObj_iframe.style.backgroundColor='#000';
			ajax_tooltipObj_iframe.src = 'about:blank';
			contentDiv.appendChild(ajax_tooltipObj_iframe);
			ajax_tooltipObj_iframe.style.left = '0px';
			ajax_tooltipObj_iframe.style.top = '0px';			
		}			
	}
	document.getElementById('ajax_tooltip_content').innerHTML = messageHtml;
	document.getElementById('ajax_tooltip_content').style.height = 'auto';
	if (width)
		ajax_tooltipObj.style.width = width;
	else
	    ajax_tooltipObj.style.width = 'auto';
	
	ajax_tooltipObj.style.height = '0px';
	// Find position of tooltip
	ajax_tooltipObj.style.display='block';
	
	if(ajax_tooltip_MSIE){
		ajax_tooltipObj_iframe.style.width = ajax_tooltipObj.clientWidth + 'px';
		ajax_tooltipObj_iframe.style.height = ajax_tooltipObj.clientHeight + 'px';				
	}

    if (!inline)
	    ajax_positionTooltip(inputObj, width, height, topAnchor);
}

function ajax_positionTooltip(inputObj, width, height, topAnchor)
{
	var leftPos = (ajaxTooltip_getLeftPos(inputObj) + inputObj.offsetWidth);
	var topPos = ajaxTooltip_getTopPos(inputObj);
	if (topAnchor)
	    topPos -= height;	
	
	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, tooltipHeight;
	if (width)
	    tooltipWidth = width;
	else
	    tooltipWidth = document.getElementById('ajax_tooltip_content').offsetWidth; 
	    
	if (height)
	    tooltipHeight = height;
	else	
	    tooltipHeight = document.getElementById('ajax_tooltip_content').offsetHeight +  document.getElementById('ajax_tooltip_close').offsetHeight; 

	
	// Dropping this reposition for now because of flickering
	var offset = rightedge - tooltipWidth; 
	if(offset<0)leftPos = Math.max(0,ajaxTooltip_getLeftPos(inputObj) - tooltipWidth);
		
	if (leftPos == 0)
	    leftPos = ajaxTooltip_getLeftPos(inputObj) - width / 2;
	    	
	ajax_tooltipObj.style.left = leftPos + 'px';
	ajax_tooltipObj.style.top = topPos + 'px';		
	
}


function ajax_hideTooltip()
{
	ajax_tooltipObj.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;
}