﻿    function GetDocumentHeight()
    {
        return Math.max(
        Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
        Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
        Math.max(document.body.clientHeight, document.documentElement.clientHeight)
        );
    }

    function GetDocumentWidth()
    {
        return Math.max(
        Math.max(document.body.scrollWidth, document.documentElement.scrollWidth),
        Math.max(document.body.offsetWidth, document.documentElement.offsetWidth),
        Math.max(document.body.clientWidth, document.documentElement.clientWidth)
        );
    }

    function findPosY(obj)
    {
        var curtop = 0;
        if(obj.offsetParent)
            while(1)
            {
              curtop += obj.offsetTop;
              if(!obj.offsetParent)
                break;
              obj = obj.offsetParent;
            }
        else if(obj.y)
            curtop += obj.y;
        return curtop;
    }
    
    function findPosX(obj)
    {
        var curtop = 0;
        if(obj.offsetParent)
            while(1)
            {
              curtop += obj.offsetLeft;
              if(!obj.offsetParent)
                break;
              obj = obj.offsetParent;
            }
        else if(obj.x)
            curtop += obj.x;
        return curtop;
    }
    function scrollY()
    {
        var ScrollTop = document.body.scrollTop;

        if (ScrollTop == 0)
        {
            if (window.pageYOffset)
                ScrollTop = window.pageYOffset;
            else
                ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
        }
        
        return ScrollTop;
    }

    function scrollX()
    {
        var ScrollLeft = document.body.scrollLeft;

        if (ScrollLeft == 0)
        {
            if (window.pageYOffset)
                ScrollLeft = window.pageXOffset;
            else
                ScrollLeft = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
        }
        
        return ScrollLeft;
    }

    function showPopup(popupDivId, leftBorderId, rightBorderId, arrowId, mainId, position, thumbnailId)
	{
	    var popupDiv = document.getElementById( popupDivId );
	    var leftBorder = document.getElementById( leftBorderId );
	    var rightBorder = document.getElementById( rightBorderId );
	    var arrow = document.getElementById( arrowId );
	    var main = document.getElementById( mainId );
	    var thumbnail = document.getElementById( thumbnailId );
	    
        /***********************************************************
        The following if...else positions the popup horizontally
        depending on the position of the popup (left or right).
        There is a bit of tampering with the pixels through trial
        and error to get the position just right.
        ***********************************************************/
        if ( position == 'left' )
        {
            popupDiv.style.left = findPosX( thumbnail ) - main.offsetWidth - arrow.offsetWidth - scrollX() + 8 + 'px';
            popupDiv.style.width = main.offsetWidth + arrow.offsetWidth + 'px';

            if ( main.style.left == '' )
            {
                main.style.left = main.style.left + 14 + 'px';
            }
        }
        else if ( position == 'right' )
        {
            popupDiv.style.left = findPosX( thumbnail ) + thumbnail.offsetWidth - 10 - scrollX() + 'px';
            
            if ( main.style.left == '' )
            {
                if ( navigator.appName == 'Netscape' )
                {
                    //On firefox, move the main area the width of the arrow
                    main.style.left = main.style.left + arrow.offsetWidth - 15 + 'px';
                }
    
                main.style.left = main.style.left - 15 + 'px';
            }
        }
        
        /***********************************************************
        The following if...else positions the popup vertically
        depending on the browser.  There is a bit more pixel 
        tampering.
        ***********************************************************/
        if ( navigator.appName == 'Netscape' )
        {
            var height = thumbnail.style.height;
            
            height = height.replace( 'px', '' )
            
            if ( main.offsetHeight <= thumbnail.offsetHeight )
            {
                popupDiv.style.top = findPosY( thumbnail ) + ( ( thumbnail.offsetHeight - main.offsetHeight ) / 2 ) - height / 2 + 'px';
            }
            else if ( main.offsetHeight > thumbnail.offsetHeight )
            {
                popupDiv.style.top = findPosY( thumbnail ) - ( ( main.offsetHeight - thumbnail.offsetHeight ) / 2 ) - height / 2 + 'px';
            }
        }
        else
        {
            if ( main.offsetHeight <= thumbnail.offsetHeight )
            {
                popupDiv.style.top = findPosY( thumbnail ) + ( ( thumbnail.offsetHeight - main.offsetHeight ) / 2 ) + 'px';
            }
            else if ( main.offsetHeight > thumbnail.offsetHeight )
            {
                popupDiv.style.top = findPosY( thumbnail ) - ( ( main.offsetHeight - thumbnail.offsetHeight ) / 2 ) + 'px';
            }
        }
        
        arrow.style.top = ( ( main.offsetHeight - arrow.offsetHeight ) / 2 ) + 'px';
        
        //Remove the total height of the header and footer
        leftBorder.style.height = main.offsetHeight - 50 + 'px';
        rightBorder.style.height = main.offsetHeight - 50 + 'px';

		popupDiv.style.visibility = "visible";
    }

	function hidePopup(popupDivId)
	{
		var popupDiv = document.getElementById( popupDivId )
	
		popupDiv.style.visibility = "hidden";
	}
