var shownId = "";


function getWindowHeight()
{
    var windowHeight = 0;
    if (typeof(window.innerHeight) == 'number') {
            windowHeight = window.innerHeight;
    }
    else {
            if (document.documentElement && document.documentElement.clientHeight) {
                    windowHeight = document.documentElement.clientHeight;
            }
            else {
                    if (document.body && document.body.clientHeight) {
                            windowHeight = document.body.clientHeight;
                    }
            }
    }
    return windowHeight;
}


window.addEvent('resize', function(){
    rePosition();
})


window.addEvent('scroll', function(){
    rePosition();
})

function rePosition()
{
    var pos = window.getScroll();
    $$(".pw_blackoverlay").setStyle('top',pos.y );
    
    //Center div in middle of screen
    var windowHeight = getWindowHeight();
    if (windowHeight > 0)
    {
            var contentElement = $("lightbox" + shownId);
            if (contentElement!=null)
            {
                var contentHeight = contentElement.offsetHeight;
                //if (windowHeight - contentHeight > 0)
                //{
                        var newPos = pos.y + (((windowHeight - contentHeight) / 2)) + 'px';
                        //alert(newPos);
                        contentElement.setStyle("top",newPos);
                //}
                //else
                //{
                    //contentElement.setStyle("position",'static');
                //}
            }
    }
}


function showLightBox(id,optionalCallbackOnClose)
{
    window.document.body.insertBefore(document.getElementById('lightbox' + id),window.document.body.firstChild);
    window.document.body.appendChild(document.getElementById('fade' + id),window.document.body.firstChild);
    
    
    $('fade' + id).style.display = 'block';
    $('lightbox' + id).style.display = 'block';
    
    shownId = id;
    
    $('closeCB' +id).value = (typeof optionalCallbackOnClose !="undefined") ? optionalCallbackOnClose : "";
    
    window.addEvent('keyup', function(event)
    {
        if (event.key=='esc') hideLightBox();
    })
    
    rePosition();
}

function hideLightBox()
{
    if ($('closeCB' + shownId).value !=  "")
    {
        var cbFuncName = $('closeCB' + shownId).value;
        var func = eval(cbFuncName);
        func();
    }
    
    $$('.pw_blackoverlay').setStyle('display','none');
    $$('.pw_box').setStyle('display','none');
}
