function LinkClickCallback (event)
{
    poper.Launch(this.src);
    event.returnValue = false;
}

var poper =
{
    divObj : null,
    backDivObj : null,
    loadImgObj : null,
    imgObj : null,
    animInt : null,
    
    Init : function ()
    {
        // Create the div
        poper.divObj = document.createElement("div");
        poper.divObj.className = "Popup";
        poper.divObj.style.display = "none";
        document.body.appendChild(poper.divObj);
        poper.loadImgObj = new Image();
        poper.loadImgObj.src = "images/loading.gif";
        poper.divObj.style.width = poper.loadImgObj.width+"px";
        poper.divObj.style.height = poper.loadImgObj.height+"px";
        poper.divObj.appendChild(poper.loadImgObj);
        poper.Center();
        poper.backDivObj = document.createElement("div");
        poper.backDivObj.className = "PopupBack";
        poper.backDivObj.id = "PopupBack";
        poper.backDivObj.style.display = "none";
        document.body.appendChild(poper.backDivObj);
        changeOpac(0,poper.backDivObj);
        
        AddEventHandler("click", poper.Stop, poper.divObj);
        AddEventHandler("click", poper.Stop, poper.backDivObj);
        
        links = document.getElementsByTagName("a");
        for (var i = 0; i < links.length; ++i)
            if (links[i].className == "imgpop")
                links[i].onclick =  function () {poper.Launch(this.href);return false};
    },
    
    Stop : function ()
    {
        if (animInt) 
        {
            clearTimeOut(aniInt);
            animInt = null;
        }
        poper.divObj.style.display = "none";
        if (poper.imgObj) 
        {
            poper.divObj.removeChild(poper.imgObj);
            delete poper.imgObj;
            poper.imgObj = null;
        }
        poper.divObj.style.width = poper.loadImgObj.width+"px";
        poper.divObj.style.height = poper.loadImgObj.height+"px";
        poper.loadImgObj.style.display = "inline";
        poper.Center();
        poper.FadeOutBack();
        
    },
    
    Center : function ()
    {
        var curWidth = parseInt(poper.divObj.style.width.substring(0,poper.divObj.style.width.length-2));
        var curHeight = parseInt(poper.divObj.style.height.substring(0,poper.divObj.style.height.length-2));
        if (isNaN(curWidth)) curWidth = 0;
        if (isNaN(curHeight)) curHeight = 0;
        poper.divObj.style.left = ((wndWidth() - Math.floor(curWidth))/2) + "px";
        poper.divObj.style.top = ((wndHeight() - Math.floor(curHeight))/2) + "px";
    },
    
    FadeInBack : function ()
    {
        poper.backDivObj.style.display= "block";
        
        if (poper.backDivObj.style.opacity*100 < 50)
        {
            changeOpac(poper.backDivObj.style.opacity*100+10, poper.backDivObj);
            setTimeout("poper.FadeInBack()",50);
        }
    },
    
    FadeOutBack : function ()
    {
        
        if (poper.backDivObj.style.opacity*100 > 0)
        {
            changeOpac(poper.backDivObj.style.opacity*100-10, poper.backDivObj);
            setTimeout("poper.FadeOutBack()",50);
        }
        else
        {
            poper.backDivObj.style.display = "none";
        }
    },
    
    Anim : function ()
    {
        if (poper.imgObj)
        {
            var curWidth = 0;
            curWidth = parseInt(poper.divObj.style.width.substring(0, poper.divObj.style.width.length-2));
            var curHeight = 0;
            curHeight = parseInt(poper.divObj.style.height.substring(0, poper.divObj.style.height.length-2));
            if (curWidth < poper.imgObj.width) 
            {
              if (curWidth + 50 > poper.imgObj.width)
                curWidth = poper.imgObj.width;
              else
                curWidth += 50
            }
            if (curHeight < poper.imgObj.height)
            {
                if (curHeight+50 > poper.imgObj.height)
                  curHeight = poper.imgObj.height;
                else
                  curHeight += 50;
            }
            
            poper.divObj.style.width = curWidth + "px";
            poper.divObj.style.height = curHeight + "px";
            
            if (curWidth == poper.imgObj.width && curHeight == poper.imgObj.height)
            {
                poper.loadImgObj.style.display = "none";
                poper.imgObj.style.display = "inline";
                animInt = null;
            }
            else animInt = setTimeout("poper.Anim()", 20);
            poper.Center();
        }
    },
    
    Launch : function (imgUrl)
    {
        poper.FadeInBack();
        poper.divObj.style.display = "block";
        poper.imgObj = new Image();
        poper.imgObj.src = imgUrl;
        poper.imgObj.style.display = "none";
        poper.divObj.appendChild(poper.imgObj);
        AddEventHandler("load", function(event){poper.Anim();}, poper.imgObj);
        poper.Center();
    }
};

AddEventHandler("load", poper.Init, window);

