//
// (c) 2005 Photozig, Inc. All rights reserved.
//

var PhotosPerPage = 12; // Each page holds 12 photos. Photo info comes from albuminfo.js
var PhotosPerRow = 4; // Each row of photos holds 4 photos.
var CurPage = 0; // Current page.
var AlreadyProcessed = 0; // The Konqueror browser calls PzProcessTime everytime we move to the
                          // next photo. We use this flag to avoid side effects due to that.

// Replaces current page passing page number as parameter.
function ReplacePage(Index)
{
    CurPage = Index;
    var Base = Index * PhotosPerPage;

    for (var I = 0; I < PhotosPerPage; I++)
    {
        var suffix = "" + parseInt(((I / PhotosPerRow) + 1), 10) + ((I % PhotosPerRow) + 1);
        var thumbName = "photoThumb" + suffix;
        var thumbCaption = "photoCaption" + suffix;

        if (Base + I >= photoFiles.length)
        {
            changeText(thumbName, "&nbsp;");
            changeText(thumbCaption, "&nbsp;");
        }
        else
        {
            if (photoTitles[Base + I] == "")
            {
                var title = "";
            }
            else
            {
                var title = photoTitles[Base + I];
            }

            changeText(thumbName,
                       "<div class='shadow'><div>" +
                       "<a href='javascript:OpenPhoto(" + (Base + I) + ");'>" +
                       "<img src='Thumbs/" + photoFiles[Base + I] +
                       "' alt='" + photoFiles[Base + I] + "' " +
                       "width='" + thumbWidths[Base + I] + "' " +
                       "height='" + thumbHeights[Base + I] + "' border='0' />" +
                       "</a></div></div>");
            changeText(thumbCaption,
                       "<a href='javascript:OpenPhoto(" + (Base + I) + ");' class='caption'>" +
                       title + "</a>");
        }
    }

    var lastPhoto = Base + PhotosPerPage;
    if (lastPhoto > photoTitles.length)
    {
        lastPhoto = photoTitles.length;
    }
    changeText("viewingPhotos", "Photos " + (Base + 1) + " - " + lastPhoto + " of " + photoTitles.length);

    var Min = Index - 10;
    if (Min < 0)
    {
        Min = 0;
    }
    if (Min > 0)
    {
        PageLinks += "... ";
    }
    var PageLinks = "Page: ";
    var pageCount = parseInt((photoTitles.length - 1) / PhotosPerPage) + 1;
    for (i = Min; i < Min + 15 && i < pageCount; i++)
    {
        PageLinks += "<a href='javascript:ReplacePage(" + i + ");'>";
        if (i == Index)
        {
            PageLinks += "<u>" + (i + 1) + "</u>";
        }
        else
        {
            PageLinks += (i + 1);
        }
        PageLinks += "</a>&nbsp;";
    }
    if (Min + 15 < pageCount)
    {
        PageLinks += "...";
    }
    changeText("pageLinks", PageLinks);

    HideOrShowElements(Base == 0, [ "arrowPrevious1", "textPrevious1", "arrowPrevious2", "textPrevious2" ]);
    HideOrShowElements(Base + PhotosPerPage >= photoFiles.length, [ "arrowNext1", "textNext1", "arrowNext2", "textNext2" ]);
}

function NextPage()
{
    if ((parseInt(CurPage) + 1) * PhotosPerPage >= photoFiles.length)
    {
        // We are at the last page
        return;
    }
    ReplacePage(parseInt(CurPage) + 1);
}

function PrevPage()
{
    if (CurPage > 0)
    {
        ReplacePage(parseInt(CurPage) - 1);
    }
}

function PlaySlides()
{
    self.location.href = "photodetails.html?slides=" + CurPage * PhotosPerPage;
}

function OpenPhoto(photoNum)
{
    SetCookie("CurPhoto", photoNum);

    // If cookies working, use cookies
    if (CookieVal("CurPhoto") == photoNum)
    {
        self.location.href = "photodetails.html";
    }
    else
    {
        // If not, pass on URL (user will have a hard time with back/forth buttons).
        self.location.href = "photodetails.html?photo=" + photoNum;
    }
}

function OpenFirstPhotoInPage()
{
    OpenPhoto(CurPage * PhotosPerPage);
}

// Initialize the script
function InitializeScript()
{
    var CurPhoto = 0;

    if (AlreadyProcessed == 1)
    {
        return;
    }
    AlreadyProcessed = 1;

    var templateDir = "../Templates" + templatesVersion + "/" + albumTemplate + "/";
    if (document.getElementById("mainTemplate").href != templateDir + "template.css")
    {
        document.getElementById("mainTemplate").href = templateDir + "template.css";
    }
    document.getElementById("arrowPrevious1").src = templateDir + "images/previous1.gif";
    document.getElementById("arrowPrevious2").src = templateDir + "images/previous1.gif";
    document.getElementById("arrowNext1").src = templateDir + "images/next1.gif";
    document.getElementById("arrowNext2").src = templateDir + "images/next1.gif";
    document.getElementById("logo").src = templateDir + "images/photozigAlbumsLogo.gif";

    CurPage = CookieVal("CurPage");
    if (!CurPage || CurPage == "null" || CurPage >= photoFiles.length / PhotosPerPage)
    {
        QueryString_Parse();
        CurPage = parseInt(QueryString("page"));
        if (!CurPage)
        {
            CurPage = 0;
        }
    }

    ReplacePage(CurPage); // Force to load the page
}

function FinalizeScript()
{
    SetCookie("CurPage", CurPage);
    SetCookie("PhotosPerPage", PhotosPerPage);
    SetCookie("LastAlbumURL", document.URL, true);
}

