/*******************************************************************************
 * Module: ToolTips.js Version 1.0
 *********************************
 * Implementation of custom compliant tooltips
 *
  *********************************
 * Requires: DOM_Fixes.js 
 *           ToolTip.css
 *   (required modules have to be referenced in HTML document prior to this
 *    module)
 *********************************
 * Usage:
 * - add call to initToolTips() to the BODY onLoad event.
 * - use the following attributes with html tags to display tooltips:
 *      tooltip='text of the tool tip'
 *      ttclass='custom_style' (optional - if not present default is used)
 *      ttdelay='delay_time_ms' - time in ms to delay the appearance of the 
 *              tooltip, if not present default is used.
 *      ttontime='on_time_ms' - duration in ms the tool tip is being displayed,
 *              if not present the tooltip is shown until the mouse moves out.
 *********************************
 * Copyright (c) 2002, Vlad Krylov, All Rights Reserved.
 *
 * You may not use the code contained in this file without my express written
 * permission.
 * You may not redistribute, sell, or offer this file for download, in any form 
 * or on any medium, without my express written permission. This includes, but 
 * is not limited to, adding it to script archives or bundling and distributing
 * it with other scripts/software
 * You agree to retain the credits and copyright notice in the source code when
 * including it in your own pages. 
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHOR OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.  
 ******************************************************************************/

// Define default delay and style for tooltips.
var ttDefDelay = 500;
var ttDefStyle = 'tooltip';

// variable holding current tooltip node.
var tooltip = null;

/* Function: 	displayToolTip
   Arguments: 	none
	Renders the current tooltip visible.
*/
function displayToolTip()
{ if(tooltip)
     { tooltip.style.display='block';
       var onTime=tooltip.getAttribute('ontime');
       if(onTime) setTimeout('deleteToolTip()',onTime);       
     }
}

/* Function: 	constructToolTip
   Arguments: 	event object
	Constructs tooltip node.
*/
function constructToolTip(e)
{ var target=e.target;
    if(target.nodeName=='#text') target=target.parentNode;
    if(target.getAttribute('tooltip'))
      { if(tooltip) deleteToolTip(e);
        tooltip = document.createElement('DIV');
        document.body.appendChild(tooltip);
        var ttClass=target.getAttribute('ttclass');
        if(! ttClass) ttClass=ttDefStyle;
        tooltip.setAttribute('class',ttClass);
        var ttOnTime=target.getAttribute('ttontime');
        if(ttOnTime) tooltip.setAttribute('ontime',ttOnTime);        
        tooltip.appendChild(
                       document.createTextNode(target.getAttribute('tooltip')));
        moveToolTip(e);
        var ttDelay=target.getAttribute('ttdelay');
        if(! ttDelay) 
            ttDelay=ttDefDelay;
        setTimeout('displayToolTip()',ttDelay);
      }    
}

/* Function: 	constructIEToolTip
   Arguments: 	event object
	Constructs Internet Explorer compatible tooltip node.
*/
function constructIEToolTip(e)
{ var target=e.srcElement;
    if(! target.getAttribute('tooltip')) return;
    if(tooltip) deleteToolTip(e);
    tooltip = document.createElement('DIV');
    document.body.appendChild(tooltip);
    var ttClass=target.getAttribute('ttclass');
    if(! ttClass) ttClass=ttDefStyle;
    tooltip.className=ttClass;
    var ttOnTime=target.getAttribute('ttontime');
    if(ttOnTime) tooltip.setAttribute('ontime',ttOnTime);        
    tooltip.appendChild(
                       document.createTextNode(target.getAttribute('tooltip')));
    moveIEToolTip(e);
    var ttDelay=target.getAttribute('ttdelay');
    if(! ttDelay) 
        ttDelay=ttDefDelay;
    setTimeout('displayToolTip()',ttDelay); 
    return;
}

/* Function: 	deleteToolTip
   Arguments: 	none
	Deletes tooltip node.
*/
function deleteToolTip()
{   if(tooltip)
      { document.body.removeChild(tooltip);
        tooltip=null;
      }
}

/* Function: 	moveToolTip
   Arguments: 	event object
	Moves tooltip to follow the mouse.
*/
function moveToolTip(e)
{   if(tooltip)
      { var dw=document.width;
        var scrollLeft=e.pageX - e.clientX;
        if(e.clientX < 0.5 * dw)
	  { tooltip.style.left=(e.pageX + 15) + 'px';
            tooltip.style.right='';
            tooltip.style.marginLeft='';
            tooltip.style.marginRight=(20 - scrollLeft) + 'px';
          }
        else
          { tooltip.style.right=(dw-(e.pageX - 45)) + 'px';
            tooltip.style.left='';
            tooltip.style.marginLeft=(scrollLeft + 20) + 'px';
            tooltip.style.marginRight='';
          }
        tooltip.style.top=(e.pageY + 5) + 'px';
      }
    return;
}

/* Function: 	moveIEToolTip
   Arguments: 	event object
	Moves tooltip to follow the mouse in Internet Explorer 
        compatible manner.
*/
function moveIEToolTip(e)
{   if(tooltip)
      { var dw=document.body.clientWidth;
        var mpX=e.clientX+document.body.scrollLeft;
        var mpY=e.clientY+document.body.scrollTop;
        if(e.clientX < 0.5 * dw)
	  { tooltip.style.left=(mpX + 15) + 'px';
            tooltip.style.right=(20 - document.body.scrollLeft) + 'px';
          }
        else
          { tooltip.style.right=(dw-(e.clientX-15)) + 'px';
            tooltip.style.left='';
          }
        tooltip.style.top=(mpY + 5) + 'px';
      }
}


/* Function: 	initIEToolTips
   Arguments: 	document node
	For Internet Explorer iterates attachs events to the node if tooltip
        attribute is defined and then iterates node children calling itself. 
*/
function initIEToolTips(node)
{   if(node.nodeType==1 && node.getAttribute('tooltip'))
      {   node.attachEvent('onmouseover',constructIEToolTip);
          node.attachEvent('onmouseout',deleteToolTip);
          node.attachEvent('onmousemove',moveIEToolTip);
      }
    for(var i=0; i<node.childNodes.length; i++)
        initIEToolTips(node.childNodes[i]);
    
}

/* Function: 	initToolTips
   Arguments: 	none
	If browser is Internet Explorer calls IE specific initialization
        function, otherwise adds event listeners to the document. 
*/
function initToolTips()
{   if(browser=='ie')
      { initIEToolTips(document.body);
      }
    else
      { document.addEventListener('mouseover',constructToolTip,true);
        document.addEventListener('mouseout',deleteToolTip,true);
        document.addEventListener('mousemove',moveToolTip,true);
      }
}
/***********************************************
* Cool DHTML tooltip script II- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var offsetfromcursorX=12 //Customize x offset of tooltip
var offsetfromcursorY=10 //Customize y offset of tooltip

var offsetdivfrompointerX=10 //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY=14 //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

document.write('<div id="dhtmltooltip"></div>') //write out tooltip DIV
document.write('<img id="dhtmlpointer" style="visibility:hidden" src="' + readCookie('IV_JCT') + '/ABP/include/images/TooltipPointer.gif" />') //write out pointer image

var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

var pointerobj=document.all? document.all["dhtmlpointer"] : document.getElementById? document.getElementById("dhtmlpointer") : ""

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thewidth, thecolor){
if (ns6||ie){
if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
tipobj.innerHTML=thetext
enabletip=true
return false
}
}

function positiontip(e){
if (enabletip){
var nondefaultpos=false
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20
var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20

var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX
var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY

var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-1) : -1000

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth){
//move the horizontal position of the menu to the left by it's width
tipobj.style.left=curX-tipobj.offsetWidth+"px"
nondefaultpos=true
}
else if (curX<leftedge)
tipobj.style.left="5px"
else{
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetfromcursorX-offsetdivfrompointerX+"px"
pointerobj.style.left=curX+offsetfromcursorX+"px"
}

//same concept with the vertical position
if (bottomedge<tipobj.offsetHeight){
tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px"
nondefaultpos=true
}
else{
tipobj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px"
pointerobj.style.top=curY+offsetfromcursorY+"px"
}
tipobj.style.visibility="visible"
if (!nondefaultpos)
pointerobj.style.visibility="visible"
else
pointerobj.style.visibility="hidden"
}
}

function hideddrivetip(){
if (ns6||ie){
enabletip=false
tipobj.style.visibility="hidden"
pointerobj.style.visibility="hidden"
tipobj.style.left="-1000px"
tipobj.style.backgroundColor=''
tipobj.style.width=''
}
}

document.onmousemove=positiontip

// functie om breedte van tooltips te beperken zodat ze niet buiten het scherm kunnen vallen
$(window).ready(function() {
	var tooltips = $('.tooltipAlg img');

	for (var i = 0; i <= tooltips.length; i++)
	{
        $(tooltips[i]).mouseenter(function() 
        {
            // het span element zorgt voor de tooltip
            var spanElement = $(this).parent().find('span');
            // resetten van css attributen
            spanElement.css('width', '');
            spanElement.css('white-space', 'nowrap');

            var widthOfTooltip = spanElement.width();
            var leftPositionOfTooltip = spanElement.offset().left;

            var overflowWidth = (widthOfTooltip + leftPositionOfTooltip) - $(window).width();

            if (overflowWidth > 0) {
                // mogelijke scrollbar meenemen in berekening
                var maxWidth = (widthOfTooltip - overflowWidth) - 30;

                spanElement.css('width', maxWidth + 'px');
                // voorkomen dat de white-space setting de breedte niet in acht neemt
                spanElement.css('white-space', 'normal');
            }
        });
		
        $(tooltips[i]).mouseleave(function()
        {
            // het span element zorgt voor de tooltip
            var spanElement = $(this).parent().find('span');			
            spanElement.css('white-space', 'normal');
        });
    }
});
