var modalEnableDragging = false;
var modal = null;
var mouseX = null;
var mouseY = null;
var modalX = null;
var modalY = null;

function showHideImagesModal(
    show,
    useOverlay,
    modalClientID,
    modalImageClientID)
{
    if ((modalClientID != null) &&
        (modalClientID != ''))
    {
        modal = document.getElementById(modalClientID);
    }
    
    var overlay = document.getElementById('overlay');
    
    if (show)
    {
        if (modal != null)
        {
            modal.style.visibility = 'hidden';
            modal.style.display = '';
        }
        
        if ((modalImageClientID != null) &&
            (modalImageClientID != ''))
        {
            var imgModalImage = document.getElementById(modalImageClientID);
            
            if (imgModalImage != null)
            {
                var imgWidth = getImageWidth(
                    imgModalImage);
                    
                var imgHeight = getImageHeight(
                    imgModalImage);
                    
                modal.style.width = (
                    imgWidth +
                    'px');
                
                modal.style.height = (
                    parseInt(imgHeight + 0) +
                    23 +
                    'px');
            }
        }
        
        if (navigator.userAgent.match('IE'))
        {
            window.scrollX = (
                document.documentElement.scrollLeft + 
                document.body.scrollLeft);
                
            window.scrollY = (
                document.documentElement.scrollTop + 
                document.body.scrollTop);
                
            window.innerWidth = document.documentElement.clientWidth;
            window.innerHeight = document.documentElement.clientHeight;
        }
    
        if (modal != null)
        {
            var centerX = (
                (window.innerWidth - 
                    modal.clientWidth) / 2 + 
                window.scrollX);
                
            var centerY = (
                (window.innerHeight - 
                    modal.clientHeight) / 2 + 
                window.scrollY);
                
            modal.style.top = (
                centerY +
                'px');
                
            modal.style.left = (
                centerX +
                'px');
        }
            
        if ((useOverlay) &&
            (overlay != null))
        {
            overlay.style.height = (
                document.documentElement.scrollHeight +
                'px');
                
            overlay.style.width = (
                document.documentElement.scrollWidth +
                'px');
            
            overlay.style.display = '';
        }
        
        if (modal != null)
        {
            modal.style.visibility = '';
        }
    }
    else
    {
        if ((useOverlay) &&
            (overlay != null))
        {
            overlay.style.display = 'none';
        }

        if (modal != null)
        {
            modal.style.display = 'none';
        }
    }
    
    return false;
}

function modalMouseDown(
    evt)
{
    if (!evt)
    {
        var evt = window.event;
    }
    
    modalEnableDragging = true;
    
    if (modal != null)
    {
        mouseY = (
            evt.pageY ? 
            evt.pageY :
            evt.clientY);
            
        mouseX = (
            evt.pageX ?
            evt.pageX :
            evt.clientX);
            
        modalY = parseInt(
            modal.style.top + 
            0);
            
        modalX = parseInt(
            modal.style.left +
            0);
        
        document.onmousemove = modalMouseMove;
    }
    
    return false;
}

function modalMouseMove(
    evt)
{
    if (!evt)
    {
        var evt = window.event;
    }
    
    if ((modalEnableDragging) &&
        (modal != null))
    {
        var currentMouseY = (
            evt.pageY ? 
            evt.pageY :
            evt.clientY);
                
        var currentMouseX = (
            evt.pageX ?
            evt.pageX :
            evt.clientX);
                
        modal.style.left = (
            modalX +
            currentMouseX -
            mouseX +
            'px');
            
        modal.style.top = (
            modalY +
            currentMouseY -
            mouseY +
            'px');
    }
    
    return false;
}

function modalMouseUp()
{
    document.onmousemove = null;
    
    modalEnableDragging = false;
    
    mouseX = null;
    mouseY = null;
    modalX = null;
    modalY = null;
    
    return false;
}

function getImageWidth(
    image)
{
    var imageWidth = 0;
    
    if (image != null)
    {
        if (image.naturalWidth)
        {
            imageWidth = image.naturalWidth;
        }
        else
        {
            var newImage = new Image();
            
            newImage.src = image.src;
            
            imageWidth = newImage.width;
        }    
    }
    
    return imageWidth;
}

function getImageHeight(
    image)
{
    var imageHeight = 0;
    
    if (image != null)
    {
        if (image.naturalHeight)
        {
            imageHeight = image.naturalHeight;
        }
        else
        {
            var newImage = new Image();
            
            newImage.src = image.src;
            
            imageHeight = newImage.height;
        }    
    }
    
    return imageHeight;
}

document.onmouseup = modalMouseUp;
