//
// (c) 2005 Photozig, Inc. All rights reserved.
//

var AlbumsPerPage = 12; // Album info comes from albumsinfo.js
var AlbumsPerRow = 4;
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.
var BlogStyle = false;

function CommentCountForGroup(groupno)
{
    if (typeof(commentGroups) == "undefined")
    {
        return 0;
    }
    for (var I = 0; I < commentGroups.length; I++)
    {
        if (commentGroups[I] == groupno)
        {
            return commentCounts[I];
        }
    }
    return 0;
}

function DoComment(groupno)
{
    OpenComment(userName, groupno);
}

// Replaces current page passing page number as parameter
function ReplacePage(PageNum)
{
    var Base = albumFiles.length - 1 - PageNum * AlbumsPerPage;

    for (var I = 0; I < AlbumsPerPage; I++)
    {
        var suffix = "" + parseInt(((I / AlbumsPerRow) + 1), 10) + ((I % AlbumsPerRow) + 1);
        var thumbName = "albumThumb" + suffix;
        var thumbCaption = "albumCaption" + suffix;
        if (BlogStyle)
        {
            var thumbDescr = "albumDescription" + suffix;
            var thumbDate = "albumDate" + suffix;
        }
        var Index = Base - I;

        if (Index < 0)
        {
            changeText(thumbName, "&nbsp;");
            changeText(thumbCaption, "&nbsp;");
            if (BlogStyle)
            {
                changeText(thumbDescr, "&nbsp;");
                changeText(thumbDate, "&nbsp;");
            }
        }
        else
        {
            if (albumTitles[Index] == "")
            {
                var title = "Album #" + (Index + 1);
            }
            else
            {
                var title = albumTitles[Index];
            }
            var Descr = albumDescriptions[Index];
            var P = Descr.indexOf("<");
            if (P >= 0)
            {
                Descr = Descr.substring(0, P);
            }
            if (BlogStyle)
            {
                var Extra = "<a href='" + albumLinks[Index] + "' class='small'>View photos</a>";
            }
            else
            {
                var Extra = "";
            }
            changeText(thumbName,
                       "<table width='160' height='109' border='0' cellspacing='0' cellpadding='0'>" +
                       "<tr align='center' valign='middle'>" +
                       "<td class='my_album'>" +
                       "<a href='" + albumLinks[Index] + "'>" +
                       "<img src='" + albumFiles[Index] + "' " +
                       "alt='" + Descr + "' " +
                       "width='" + coverWidths[Index] + "' " +
                       "height='" + coverHeights[Index] + "' border='0' ></a></td>" +
                       "</tr></table>" + Extra);
            changeText(thumbCaption,
                       "<a href='" + albumLinks[Index] + "' class='caption'>" + title + "</a>");
            if (BlogStyle)
            {
                if (InMyPhotozigWebsite)
                {
                    changeText(thumbDescr, albumDescriptions[Index] + "<br><br>" +
                                           "<a class='small' href='javascript:DoComment(\"" + groupNumbers[Index] + "\");'>" +
                                           CommentCountForGroup(groupNumbers[Index]) +
                                           "&nbsp;comments</a>");
                }
                else
                {
                    changeText(thumbDescr, albumDescriptions[Index]);
                }
                changeText(thumbDate, "<div class='date-header'>" + albumDates[Index] + "</div>");
            }
        }
    }
    HideOrShowElements(PageNum == 0, [ "arrowPrevious1", "textPrevious1" ]);
    HideOrShowElements(Base - AlbumsPerPage < 0, [ "arrowNext1", "textNext1" ]);
}

function NextPage()
{
    if ((CurPage + 1) * AlbumsPerPage >= albumFiles.length)
    {
        // We are at the last page
        return;
    }
    ReplacePage(++CurPage);
}

function PrevPage()
{
    if (CurPage > 0)
    {
        ReplacePage(--CurPage);
    }
}

function OpenLastViewedAlbum()
{
    if (CookieVal("LastAlbumURL") == 0)
    {
        var Base = CurPage * AlbumsPerPage;

        if (Base < albumLinks.length)
        {
            self.location.href = albumLinks[Base];
        }
    }
    else
    {
        self.location.href = CookieVal("LastAlbumURL");
    }
}

// Initialize the script
function InitializeScript(ABlogStyle)
{
    if (AlreadyProcessed == 1)
    {
        return;
    }
    AlreadyProcessed = 1;
    BlogStyle = ABlogStyle;

    var templateDir = "Templates" + templatesVersion + "/PzAlbums/";
    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("arrowNext1").src = templateDir + "images/next1.gif";
    document.getElementById("logo").src = templateDir + "images/photozigAlbumsLogo.gif";

    // Extract html parameters
    CurPage = CookieVal("CurAlbumPage");
    QueryString_Parse();
    if (!CurPage || CurPage == "" || CurPage >= albumFiles.length / AlbumsPerPage)
    {
        CurPage = parseInt(QueryString("page"));
        if (!CurPage)
        {
            CurPage = 0;
        }
    }

    var jump = null;
    var link = null;

    // In blog mode we check for group parameter to jump to the desired directly.
    var group = QueryString("group");
    if (group != null)
    {
        var J = 0;
        for (var I = groupNumbers.length - 1; I >= 0; I--)
        {
            if (groupNumbers[I] == group)
            {
                jump = "#a" + parseInt(parseInt(J) % AlbumsPerPage + 1);
                CurPage = parseInt(J / AlbumsPerPage);
                link = albumLinks[I];
                break;
            }
            J++;
        }
    }

    ReplacePage(parseInt(CurPage)); // Force to load the page
    if (jump != null)
    {
        if (BlogStyle)
        {
            location.hash = jump;
        }
        else
        {
            self.location.href = link;
        }
    }
}

function FinalizeScript()
{
    SetCookie("CurAlbumPage", CurPage);
}

