// viostream.pagination.js 
// version: 1.0
// requires:
//      - viostream.js

Viostream.Paginate = function(recordCount, pageSize, totalPages, currentPageIndex, categoryId) {
    /// <summary>
    /// Builds the list of pagination links on the page
    /// </summary>

    var PageLink = function(content, catID, pageIndex, aClassName, liClassName) {
        // creates and returns the li containing the right link
        var a = document.createElement("a");
        a.href = "javascript:void(0);"

        $(a).click(function() {
            var section = catID + "," + pageIndex + "," + Viostream.SortOrder + "," + Viostream.MediaType;
            try {
                Viostream.CurrentPageIndex = pageIndex;
                $.history.load(section);
            } catch (e) {
                Viostream.LoadCategoryFromGuid(section, 0);
            }
            return false;
        }).append($cT(content)).addClass(aClassName);

        /*var li = document.createElement("LI");
        li.appendChild(a);
        li.className = liClassName;*/
        $(a).addClass(liClassName);
        return a;
    };

    try {
        if (recordCount > pageSize) {  //only draw pagination controls if the total number of records > the page size
            var ul = $cE("div");

            //build first link
            //ul.appendChild(PageLink("<<", categoryId, 0, "", "first"));

            //build previous link
            var previousIndex = (currentPageIndex >= 1) ? currentPageIndex - 2 : 0;
            ul.appendChild(PageLink("< PREVIOUS", categoryId, previousIndex, "", "previous"));

            //build page links
            //            for(var j=0;j<totalPages;j++){
            //                ul.appendChild(PageLink(j+1, categoryId, j, (j+1==currentPageIndex ? "current" : ""), "" ));
            //            }
            var pgLoc = document.createElement("span");
            pgLoc.innerHTML = currentPageIndex + " of " + totalPages;
            ul.appendChild(pgLoc);

            //build next link

            var nextIndex = (currentPageIndex < totalPages) ? currentPageIndex : totalPages -1;
            ul.appendChild(PageLink("NEXT >", categoryId, nextIndex, "", "next"));

            //build last link
            //ul.appendChild(PageLink(">>", categoryId, totalPages-1, "", "last"));
            $("div.pagination-controls").empty().append(ul).show();
        } else {
            $("div.pagination-controls").empty().hide();
        }
    }
    catch (e) {
        if (this.isDebug) {
            alert("paginate)" + e.message);
            throw e;
        }
    }
};
