﻿/******************************************************************************
* Cool DHTML tooltip script- © 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
*******************************************************************************
* Custom Modifications for HDSF (i.e. Fade-in/out effect, and boundry settings): 
*  - Konrad Kyc [2009 © Vantagesoft.ca] - 2009-02-14
******************************************************************************/

var offsetxpoint = 5                    //Customize x offset of tooltip
var offsetypoint = -160                 //Customize y offset of tooltip
var overlayWidth = 300;
var overlayHeight = 115;
var curX = 0;
var curY = 0;

siteBlockPosition = FindPosition(GetElementObjectById("divSiteBlock"));

// Area Boundaries
var topBoundary = 0;//300;
var bottomBoundary = 0;//735
var leftBoundary = 0;//255;
var rightBoundary = 0;//879;
var boundryCalculated = false;

var enabletip = false;
var tipVisible = false;

var tipobj = GetElementObjectById("divTooltip");

function ietruebody()
{
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function Tooltip(title, text)
{
    var htmlText = '<div class="TooltipTitle">' + title + '</div><div class="TooltipText">' + text + '</div>';
    TooltipHtml(htmlText, '', 300);
}

function TooltipHtml(htmlText, backgroundColor, toolTipWidth)
{

    if (ns6||ie)
    {        
        // Calculate Boundries (if not calculated)
        CalculateBoundries();
        
        if (typeof toolTipWidth != "undefined" && toolTipWidth > 0) tipobj.style.width = toolTipWidth + "px";
        if (typeof backgroundColor != "undefined" && backgroundColor != "") tipobj.style.backgroundColor = backgroundColor;

        tipobj.innerHTML = htmlText;
        
        if(BrowserSupportsJQuery() && !tipVisible)
        {
            $("#divTooltip").fadeTo("slow", 0.92);
        }
        
        enabletip = true;
        tipVisible = true;
        
        return false;
    }
}

function CalculateBoundries()
{
    // Calculate Site Block Position (if not calculated)
    CalculateSiteBlockPosition();

    if(!boundryCalculated)        
    {
        var caseStudyGrid = GetElementObjectById("divCaseStudyGrid");
        var caseStudyGridPosition = FindPosition(caseStudyGrid);
        
        topBoundary = caseStudyGridPosition[1] - siteBlockPosition[1];
        bottomBoundary = topBoundary + GetElementHeight(caseStudyGrid);
        leftBoundary = caseStudyGridPosition[0] - siteBlockPosition[0];
        rightBoundary = leftBoundary + GetElementWidth(caseStudyGrid);
        boundryCalculated = true;
    }

}

function PositionTooltip(e)
{
    if (enabletip)
    {
        curX =(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
        curY =(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
        
        // Adjust the coordinates relatively to the Site Block element.
        curX -= siteBlockPosition[0];
        curY -= siteBlockPosition[1];
        
        if(IsOutsideBoundry())
        {
            enabletip = false;
            DisableTooltip();
        }
        else
        {
            //Find out how close the mouse is to the corner of the window
            var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20;
            var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20;

            var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000;

            //if the horizontal distance isn't enough to accomodate the width of the context menu
            if (rightedge < (overlayWidth + 40))
                //move the horizontal position of the menu to the left by it's width
                tipobj.style.left = ie? ietruebody().scrollLeft+event.clientX - tipobj.offsetWidth - 5 - siteBlockPosition[0] + "px" : window.pageXOffset + e.clientX-tipobj.offsetWidth - 5 - siteBlockPosition[0] + "px";
            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 + offsetxpoint + "px";

            // Vertical position should always be the same
            tipobj.style.top = curY + offsetypoint + "px";
            tipobj.style.display = "block";
        }
    }
}

function HideTooltip()
{
    if(IsOutsideBoundry())
    {
        enabletip = false;
        DisableTooltip();
    }
}

function DisableTooltip()
{
    // Only hide if the mouse is outside the bounds
    if(IsOutsideBoundry())
    {   
        if(BrowserSupportsJQuery())
        {
            $("#divTooltip").fadeTo("slow", 0.0, function(){
                tipobj.style.display = "none";
                tipobj.style.left = "-1000px";
                tipobj.style.backgroundColor = '';
                tipobj.style.width='';
                tipVisible = false;
                enabletip = false;           
            });  
        }
        else
        {
            if (ns6||ie)
            {
                enabletip = false;
                tipVisible = false;
                tipobj.style.display = "none";
                tipobj.style.left = "-1000px";
                tipobj.style.backgroundColor = '';
                tipobj.style.width='';
            }
        }
    }
}

function IsOutsideBoundry()
{
    return curX < leftBoundary || curX > rightBoundary || curY < topBoundary || curY > bottomBoundary
}

document.onmousemove = PositionTooltip;
