// Developer Settings
var imgClose = "http://www.foodprofile.com/WEB-AFSProfile/Images/btn-close.gif";
var imgPrev = "http://www.foodprofile.com/WEB-AFSProfile/Images/btn-prev.gif";
var imgNext = "http://www.foodprofile.com/WEB-AFSProfile/Images/btn-next.gif";

//Script Variables
var iImageWidth = 0;
var iWindowWidth = 0;
var iWindowHeight = 0;
var iImageIndex = 1;
var iMaxWidth = (screen.width / 2) - 15;
var iMaxHeight = (screen.height / 2) - 15;
var ns = (navigator.appName.indexOf("Netscape") != -1);

function FindMaxImageWidth(sImageUrl)
{
    var newImg = new Image();
    newImg.src = sImageUrl;
    iImageWidth = newImg.width;
    if (iImageWidth > iMaxWidth)
        iImageWidth = iMaxWidth;
}

function GetWindowHeightWidth()
{
    iWindowWidth = parseInt("0" + document.body.clientWidth, 10);
    if (iWindowWidth == 0)
        iWindowWidth = parseInt("0" + document.documentElement.clientWidth, 10);
    if (iWindowWidth == 0)
        iWindowWidth = parseInt("0" + window.innerWidth, 10);
    if (iWindowWidth == 0)
        iWindowWidth = screen.Width;

    iWindowHeight = parseInt("0" + window.innerHeight, 10);
    if (iWindowHeight == 0)
        iWindowHeight = parseInt("0" + document.documentElement.clientHeight, 10);
    if (iWindowHeight == 0)
        iWindowHeight = parseInt("0" + document.body.clientHeight, 10);
    if (iWindowHeight == 0)
        iWindowHeight = screen.Height;
        
        iMaxHeight = iWindowHeight;        
}

function CreatePopupWindow()
{
    GetWindowHeightWidth();
    var myDiv = document.createElement("div");
    myDiv.innerHTML = "<table cellpadding='0' cellspacing='0' onmouseover='javascript:ShowButtons()' onmouseout='javascript:HideButtons()' id='tblBackground' class='tblBackground' border='0'><tr style='position: absolute;' height='30px'><td width='100px'>&nbsp;</td><td id='tdImageCurrentIndex' class='tdImageCurrentIndex' width='" + (iWindowWidth - 200) + "px'></td><td width='100px' class='tdClose'><img id='btnclose' src='" + imgClose + "' onclick='javascript:HidePopupWindow()'></td></tr><tr><td class='tdPrev' width='100px'><img id='btnprevious' src='" + imgPrev + "' onclick='javascript:ShowPreviousImage()'></td><td width='" + (iWindowWidth - 200) + "px'>&nbsp;</td><td class='tdNext' width='100px'><img id='btnnext' src='" + imgNext + "' onclick='javascript:ShowNextImage()'></td></tr></table><div id='divImageViewer' class='divImageViewer'></div>";
    document.body.appendChild(myDiv);
}

function ShowPopupWindow()
{
    if (document.getElementById("tblBackground") == null)
        CreatePopupWindow();

    SetPopupWindowFloat();

    if (document.getElementById("tblBackground") != null)
    {
        document.getElementById("tblBackground").style.display = "";
        document.getElementById("divImageViewer").style.display = "";
        LoadImage();
    }
}

function HidePopupWindow()
{
    if (document.getElementById("tblBackground") != null)
    {
        document.getElementById("tblBackground").style.display = "none";
        document.getElementById("divImageViewer").style.display = "none";
    }
}

function HideButtons()
{
    document.getElementById("btnprevious").style.display = "none";
    document.getElementById("btnnext").style.display = "none";
    document.getElementById("btnclose").style.display = "none";
}

function ShowButtons()
{
    document.getElementById("btnprevious").style.display = "";
    document.getElementById("btnnext").style.display = "";
    document.getElementById("btnclose").style.display = "";
}

function SetButtonVisibility()
{
    if (iImageIndex <= 1)
        document.getElementById("btnprevious").style.visibility = "hidden";
    else
        document.getElementById("btnprevious").style.visibility = "visible";

    if (iImageIndex >= parseInt((document.getElementById("gallery").childNodes.length),10))
        document.getElementById("btnnext").style.visibility = "hidden";
    else
        document.getElementById("btnnext").style.visibility = "visible";
}

function ShowPreviousImage()
{
    if (iImageIndex > 1)
        iImageIndex = iImageIndex - 1;
    LoadImage();
}

function ShowNextImage()
{
    if (iImageIndex < parseInt((document.getElementById("gallery").childNodes.length),10))
        iImageIndex = iImageIndex + 1;
    LoadImage();
}

function LoadImage()
{
    if (document.getElementById("gallery") != null)
    {
        if (document.getElementById("divImageViewer") != null)
        {
            var sImgName = "galleryImg" + iImageIndex;
            if (document.getElementById(sImgName) != null)
            {
                FindMaxImageWidth("" + document.getElementById(sImgName).getAttribute("href"));
                document.getElementById("divImageViewer").innerHTML = "<center><a href='" + document.getElementById(sImgName).getAttribute("href") + "' target='_NetservImageViewer'><img src='" + document.getElementById(sImgName).getAttribute("href") + "' width='" + iImageWidth +"px'></a></center>";
            }
            document.getElementById("tdImageCurrentIndex").innerHTML = "Image " + iImageIndex + " of " + parseInt((document.getElementById("gallery").childNodes.length), 10) + document.getElementById(sImgName).getAttribute("title");
            SetButtonVisibility();
        }
    }
}

function SetPopupWindowFloat()
{
    var pX, pY;
    if (document.getElementById("tblBackground") != null)
    {
        GetWindowHeightWidth();
        pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
        if (this.sy < 0)
        {
            pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
        }
         
        document.getElementById("tblBackground").style.top = pY + "px";        
        document.getElementById("tblBackground").style.height = iMaxHeight + "px";        

        document.getElementById("divImageViewer").style.top = pY + 40 + "px";
        document.getElementById("divImageViewer").style.left = ((iWindowWidth / 2) - (iMaxWidth / 2)) + "px";
        document.getElementById("divImageViewer").style.height = iMaxHeight + "px";
        document.getElementById("divImageViewer").style.width = iMaxWidth + "px";       
    }
}

function keyPressHandler(e)
{
    var kC = (window.event) ? event.keyCode : e.keyCode;
    var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
    if (kC == Esc)
        HidePopupWindow();
}

window.onscroll = function()
{    
    SetPopupWindowFloat();
}


