var stopDeactivate=0;
var neglectDeactivate=0;
var divCounter=0;
var uniqueDivName="ttDivTp";

function getInnerWidth() {if (window.innerWidth) return window.innerWidth;if (document.body.offsetWidth) return document.body.offsetWidth;return 0;}
function getInnerHeight(){if (window.innerHeight) return window.innerHeight;if (document.body.offsetHeight) return document.body.offsetHeight;return 0;}
function getPageLocationTop(){if (window.pageYOffset) return window.pageYOffset;if (document.body.scrollTop) return document.body.scrollTop;return 0;}
function isNum(s){if (s!=null && s!="") {var thetruth=1;for (var i=0;i<s.length;i++){var currChar=s.substring(i,i+1);if (currChar!="-" && currChar!="." && currChar!="0" && currChar!="1" && currChar!="2" && currChar!="3" && currChar!="4" && currChar!="5" && currChar!="6" && currChar!="7" && currChar!="8" && currChar!="9") thetruth=-1;}if (thetruth!=-1) return s;}return "false";}

function getDivNumber()
{
    return divCounter;
}
function getUniqueDivName()
{
    return uniqueDivName;
}
function divNumberInc()
{
    divCounter++;
}

function imgpopup(img)
{
    var parentDiv=img.parentNode;
    var neglectTimeout=3000;
    
    for (var i=0;i<parentDiv.attributes.length;i++)
    {
        var parseTest=isNum(parentDiv.attributes[i].nodeValue);
        if (parseTest!="false")
        {
            var parsedNum=(parentDiv.attributes[i].nodeValue-0);
            if (parentDiv.attributes[i].nodeName.toLowerCase()=="inactivity-timeout")
            {
                neglectTimeout=Math.floor(parsedNum*1000);
            }
        }
    }
    
    var innerWin;
    for (var i=0;i<parentDiv.childNodes.length;i++)
    {
        try {
            if (parentDiv.childNodes[i].className.toLowerCase()=="popupwin")
            {
                innerWin=parentDiv.childNodes[i];
            }
        }
        catch (e) {}
    }
    
    var top=0;
    var left=0;
    var height=0;
    var width=0;
    var overflow=0;
    
    //takes into account Tables that will throw off the spacing
    var imgW=img;
    var bt=0;
    var bl=0;
    var bodyElement=document.getElementsByTagName("body")[0];
    while (imgW!=bodyElement)
    {
        //alert("type="+imgW.tagName+" top="+imgW.offsetTop+" left="+imgW.offsetLeft);
        if (imgW.tagName.toLowerCase()=="table" || imgW.tagName.toLowerCase()=="td")
        {
            bt+=(imgW.offsetTop-0);
            bl+=(imgW.offsetLeft-0);
        }
        imgW=imgW.parentNode;
    }
    
    // BASEPOINT
    top+=bt;
    left+=bl;
    
    var timeouttime="500";
    for (var i=0;i<innerWin.attributes.length;i++)
    {
        // Get all number-based parameters
        var parseTest=isNum(innerWin.attributes[i].nodeValue);
        if (parseTest!="false")
        {
            var parsedNum=(innerWin.attributes[i].nodeValue-0);
            if (innerWin.attributes[i].nodeName.toLowerCase()=="offset-right")
            {
                left+=parsedNum;
            }
            if (innerWin.attributes[i].nodeName.toLowerCase()=="offset-down")
            {
                top+=parsedNum;
            }
            if (innerWin.attributes[i].nodeName.toLowerCase()=="height")
            {
                height=parsedNum;
            }
            if (innerWin.attributes[i].nodeName.toLowerCase()=="width")
            {
                width=parsedNum;
            }
            if (innerWin.attributes[i].nodeName.toLowerCase()=="expiration")
            {
                timeouttime=Math.floor(parsedNum*1000);
            }
        }
        
        // Get all text-based parameters
        if (innerWin.attributes[i].nodeName.toLowerCase()=="overflow")
        {
            if (innerWin.attributes[i].nodeValue.toLowerCase()=="top")
                overflow=1;
            if (innerWin.attributes[i].nodeValue.toLowerCase()=="bottom")
                overflow=-1;
        }
    }
    
    var html=innerWin.innerHTML;
    
    top+=img.offsetTop;
    left+=img.offsetLeft;
    
    var winHeight=getInnerHeight();
    var winScrollTop=getPageLocationTop();
    
    if ((top+height)>(winHeight+winScrollTop)) // if the popupwin will dip below the browser window
    {
        if ((top-height)<winScrollTop) // special case: refer to overflow specification
        {
            if (overflow==-1)
                top=winHeight+winScrollTop-height-5;
            if (overflow==0)
                top=Math.floor((winHeight-height)/2)+winScrollTop;
            if (overflow==1)
                top=winScrollTop+5;
        }
        else
        {
            top-=height;
        }
    }
    if (top<winScrollTop) // if the popup window will generate above the browser window but can be moved down the thumbnail to fit better
    {
        if ((top+img.offsetHeight)>=winScrollTop)
            top=winScrollTop+5; // doesn't really matter what the height of the thumbnail is, as long as we can see it we should put the popup 5px from the top of the browser window
    }
    else if ((top+height)>(winHeight+winScrollTop)) // if the popup window will generate above the browser window but can be moved down the thumbnail to fit better
    {
        if ((img.offsetTop)<=(winScrollTop+winHeight))
            top=winHeight+winScrollTop-height-5; // doesn't really matter what the height of the thumbnail is, as long as we can see it we should put the popup 5px from the bottom of the browser window
    }
    
    
    var winWidth=getInnerWidth();
    
    if ((left+width)>(winWidth)) // if the popupwin will dip to the right of the browser window
    {
        if ((left-width)<0) // no overflow specification for this, to impliment, copy vertical code above and replace vertical variables with horizontal ones
        {
            left=Math.floor((winWidth-width)/2);
        }
        else
        {
            left-=width;
        }
    }
    if (left<0) // if the popup window will generate to the right of the browser window but can be moved across the thumbnail to fit better
    {
        if ((left+img.offsetLeft)>=0)
            left=5; // doesn't really matter what the width of the thumbnail is, as long as we can see it we should put the popup 5px from the left of the browser window
    }
    else if ((left+width)>(winWidth)) // if the popup window will generate to the right of the browser window but can be moved across the thumbnail to fit better
    {
        if ((img.offsetLeft)<=(winWidth))
            left=winWidth-width-5; // doesn't really matter what the height of the thumbnail is, as long as we can see it we should put the popup 5px from the right side of the browser window
    }
    
    
    
    var d=document.getElementById("toolTip");
    d.style.display="inline";
    var h='<div id="ctCurrOpenDivToolTipWindow"'
        +' style="position:absolute;'
        +'z-index:100;'
        +'top:'+top+'px;'
        +'left:'+left+'px;'
    if (width!=0)
        h+='width:'+width+'px;';
    if (height!=0)
        h+='height:'+height+'px;';
    h+='"'
        +' onmouseover="javascript:prolong();"'
        +' onmouseout="javascript:deactivate('+timeouttime+');">'
        +html
        +'</div>';
    d.innerHTML=h;
    neglectDeactivate++;
    timeneglect(neglectTimeout);
}

function killImgPopup(){document.getElementById("toolTip").innerHTML="";document.getElementById("toolTip").style.display="none";}
function exDeactivate() {stopDeactivate--;if (stopDeactivate<=0) {stopDeactivate=0;killImgPopup();}}
function prolong(){stopDeactivate++;}
function deactivate(timeout) {if (stopDeactivate>0) {setTimeout("exDeactivate()",timeout);}}
function timeneglect(neglectTimeout) {setTimeout("neglectdeactivate()",neglectTimeout);}
function neglectdeactivate(){neglectDeactivate--;if (neglectDeactivate<=0 && stopDeactivate==0){neglectDeactivate=0;killImgPopup();}}

document.write("<div id=\"toolTip\"></div>");