// ----------------------------------------------------------------------------
// Pagination Plugin - A jQuery Plugin to paginate content
// v 1.0 Beta
// Dual licensed under the MIT and GPL licenses.
// ----------------------------------------------------------------------------
// Copyright (C) 2010 Rohit Singh Sengar
// http://rohitsengar.cueblocks.net/
// ----------------------------------------------------------------------------
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ----------------------------------------------------------------------------



//------------ initializing all the values needed in paginator. -----------------

//--- Variables for internal use ----
var pageElement = Array();
var paginatorId = '';
var searchSummaryId = '';
var currentPage = 1; // current page, default 1
var allItems = 0; // no. of repeating items in the container where paginator is applied
var lastPage = 1; // last page, default 1
var itemsPerPage = 1; // no. of items you want to show on one page

//--- Attributes that can be changed according to use ---
var startPage = 1; // start page
var previousPageSymbol = '<'; // to indicate Previous Page
var nextPageSymbol = '>'; // to indicate Next Page
var enablePageOfOption = false; // it shows on which are you currently, i.e. Page 3 of 6 Page(s), if turned true
var anchorLink = 'javascript:void(0);'; // if you want to change href of the paginator anchor text (links for page) to '#' or to something else. As # is append on the address bar upon clicking I used javascript:void(); which is clean.
//-----------functions starts----------------------------------------------------


// function for extending function to dom object. Read more about it in jQuery docs.

jQuery.fn.extend({
    pagination: function() {
        paginatorId = this;
        
        initPaginator(); // calling function to start pagination.
    },

    depagination: function() {
        jQuery('.articlePager').remove(); // removing paginator class.
        paginatorId.children().show(); // show all content of the div where pagination was applied.
    }
});



function initPaginator()  // for initializing the paginator
{
    allItems = paginatorId.children().length; // finding number of total items in content
    
    if (allItems % itemsPerPage == 0) // calculating last page of the paginator
        lastPage = parseInt(allItems / itemsPerPage);
    else
        lastPage = parseInt(allItems / itemsPerPage) + 1;

    if ((startPage < 1) || (startPage > lastPage))	// start page for pagination
        startPage = 1;

    if (lastPage > 1)  // show pagination only if there is more than 1 page.
    {
        paginatorId.after('<div class="articlePager"></div>');
        appendContent(startPage, null, true);
    }
}

function jumpToPage(page, callback) {
    if (currentPage == page) {
        callback();
    }
    else
    {
        appendContent(page,callback);
    }
}

function pag_NextPage() {
    jumpToPage(-2, function() {
        $j.scrollTo(0, 300);
    });
}

function pag_PrevPage() {
    jumpToPage(-1, function() {
        $j.scrollTo(0, 300);
    });
}

// function for appending the content of selected page. called everytime whenever user clicks on any active link of paginator. set effect true/1 for fading effects, false/0 for changing contents for 
function appendContent(page,callback,noEffect) {
    if (page < 0) {

        if (page == -1)
            page = currentPage - 1;
        else
            page = currentPage + 1;
    }

    currentPage = page; // get current page or page selected.
    till = (currentPage - 1) * itemsPerPage;
    if (noEffect == true) {
        createPaginator();   // create new paginator
        paginatorId.children().hide();  // hide all child element of content
        paginatorId.children().slice(till, itemsPerPage + till).show();  // show only those items according to page selected.
    }
    else {
        if (!callback) {
            paginatorId.fadeOut("fast", function() { // fade out the current content 
                createPaginator();  // create new paginator
                paginatorId.children().hide();  // hide all child element of content
                paginatorId.children().slice(till, itemsPerPage + till).show(); // show only those items according to page selected.
                paginatorId.fadeIn("fast"); // use nice fade in effect to show the items of that selected page.
            });
        }
        else {
            paginatorId.fadeOut("fast", function() { // fade out the current content 
                createPaginator();  // create new paginator
                paginatorId.children().hide();  // hide all child element of content
                paginatorId.children().slice(till, itemsPerPage + till).show(); // show only those items according to page selected.
                paginatorId.fadeIn("fast", callback); // use nice fade in effect to show the items of that selected page.
            });
        }
    }
}


function createPaginator()  // for creating the paginator
{
    jQuery(".articlePager").html("");

    var paginatorContents = '<table class="articlePagerTable"><tr><td>';

    var pageOfOption = ' Page ' + currentPage + ' of ' + lastPage;  // for showing page info option

    if (searchSummaryId != "") {
        var startIndex = (currentPage-1) * itemsPerPage + 1;
        var endIndex = startIndex + itemsPerPage - 1;
        if (endIndex > allItems)
            endIndex = allItems;
        var searchSummaryContent = "&nbsp;" + startIndex + "-" + endIndex + " of";
        jQuery("#" + searchSummaryId).html(searchSummaryContent);
    }

    if (currentPage == 1) // for setting paginator style if current page is first page
    {
        paginatorContents += '<div class="articlePagerPrevInactive" title="Previous Page"></div>';
    }
    else // for setting paginator style for first page links
    {
        paginatorContents += '<div onclick="pag_PrevPage();" class="articlePagerPrev" title="Previous Page"></div>';
    }

    paginatorContents += '</td><td><div class="articlePagerSummary" >' + pageOfOption + '</div></td><td>';

    if (currentPage == lastPage) // for setting paginator style if current page is last page
    {
        paginatorContents += '<div class="articlePagerNextInactive" title="Next Page"></div>';
    }
    else // for setting paginator style for last page links
    {
        paginatorContents += '<div onclick="pag_NextPage();" class="articlePagerNext" title="Next Page"></div>';
    }
    paginatorContents += "</td></tr></table>";
    
    jQuery(".articlePager").html(paginatorContents);
}

// --------------------- Plugin Ends ----------------------------

	
