var nGalleryImageIndex = 0; 

function ClosePopupFrame()
{
    var pPopupFrame = getObject("GallerPopupFrame");
    if (pPopupFrame)
    {
        document.body.removeChild(document.getElementById('PopupBG'));    
        document.body.removeChild(pPopupFrame);
        hideFixMSIE('GallerPopupFrame');
    }
}

function ShowImageGallery()
{
    strImageSrc = arGalleryImages[nGalleryImageIndex];

    ClosePopupFrame();
    
    pPopupBG = document.createElement('DIV');      
    pPopupBG.id = 'PopupBG';  
    pPopupBG.className = 'popupbg';                          
    pPopupBG.style.width = getDocWidth() + 'px';
    pPopupBG.style.height = getDocHeight() + 'px';
    setOpacity(pPopupBG, 5);
    pPopupBG.onclick = ClosePopupFrame;
    
    pPopupBG.innerHTML = '&nbsp'; 
                                
    document.body.appendChild(pPopupBG);
    
    pPopupFrame = document.createElement('DIV');    
    pPopupFrame.id = 'GallerPopupFrame';
    pPopupFrame.className = 'gallerypopup'; 
    
    pPopupFrame.innerHTML = '<img id="popupgalleryimg" src="' + strImageSrc + '" onclick="ClosePopupFrame();" onload="OnImageLoad(this)" style="padding:2px;" />';        
 
    if(arGalleryImages.length > 1)
    {
        pPopupFrame.innerHTML += '<br /><img src="' + strRootPath + 'images/gallery/previous.gif" width="100" height="35" onclick="ShowPreviousImage();" onmouseover="GalleryButtonOver(this);" onmouseout="GalleryButtonOver(this);" /><img src="' + strRootPath + 'images/gallery/next.gif" width="100" height="35" onclick="ShowNextImage();" onmouseover="GalleryButtonOver(this);" onmouseout="GalleryButtonOver(this);" />';
    } 
    document.body.appendChild(pPopupFrame); 
    
    InitImageIndex(strImageSrc); 
}

function OnImageLoad(pImage)
{                                           
    var pPopupFrame = getObject("GallerPopupFrame");
    if (pPopupFrame)
    {
        var x = (getFrameWidth() / 2) - (pImage.width / 2) + getScrollX();
        var y = (getFrameHeight() / 2) - (pImage.height / 2) + getScrollY();  
                  
        pPopupFrame.style.top = y + 'px';
        pPopupFrame.style.left = x + 'px';
        
        pPopupFrame.style.visibility = "visible";
    
        resizeFixMSIE('GallerPopupFrame');
    }         
} 

function ShowNextImage()
{
    if(nGalleryImageIndex + 2 > arGalleryImages.length)    
    {
        nGalleryImageIndex = 0;    
    }
    else
    {
        nGalleryImageIndex++;    
    }
    getObject('popupgalleryimg').src = arGalleryImages[nGalleryImageIndex];
    // ResizePopupFrame();
}

function ShowPreviousImage()
{
    if(nGalleryImageIndex - 1 < 0)    
    {
        nGalleryImageIndex = arGalleryImages.length - 1;    
    }
    else
    {
        nGalleryImageIndex--;    
    }
    getObject('popupgalleryimg').src = arGalleryImages[nGalleryImageIndex];
    // ResizePopupFrame();        
}

function InitImageIndex(strImageSrc)
{
    for (i = 0; i < arGalleryImages.length; i++)    
    {
        if(arGalleryImages[i] == strImageSrc)    
        {
            nGalleryImageIndex = i;
            break;
        }
    }
}

function GalleryButtonOver(pImage)
{
    strImageSrc = pImage.src;     
    if(strImageSrc.match(/_over\.gif$/))
    {        
        strImageSrc = strImageSrc.replace(/_over\.gif$/, '.gif');    
    }
    else
    {       
        strImageSrc = strImageSrc.replace(/\.gif$/, '_over.gif');
    } 
    pImage.src = strImageSrc;     
}

