// ----------------------------------------------------------------------------
// 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 pageElement2 = Array();

	var paginatorId2 = '';

	var currentPage2 = 1; // current page, default 1

	var allItems2 = 0; // no. of repeating items in the container where paginator is applied

	var lastPage2 = 1; // last page, default 1

	//--- Attributes that can be changed according to use ---

	var startPage2 = 1; // start page

	var itemsPerPage2 = 5; // no. of items you want to show on one page

	var firstPageSymbol2 = '<<'; // to indicate First Page

	var previousPageSymbol2 = '前へ'; // to indicate Previous Page

	var nextPageSymbol2 = '次へ'; // to indicate Next Page

	var lastPage2Symbol2 = '>>'; // to indicate Last Page

	var separator2 = ' | '; // To separate paginator's items

	var paginatorPosition2 = 'bottom'; // where you want the paginator to be. Accepted values are 'top','bottom','both'

	var paginatorStyle2 = 1; // To define which style of paginator you need.
	// 1 - for << | < | 1 | 2 | 3 | > | >>
	// 2 - for << | < | 1/8 | > | >>
	// 3 - for < | 1 | 2 | 3 | >
	// 4 - for < | >
    
	var enablePageOfOption2 = false; // it shows on which are you currently, i.e. Page 3 of 6 Page(s), if turned true
    
	var enableGoToPage2 = false; // shows a drop down of all pages for go/jump to any page user want to go, if turned true. Useful incase there are large no. of pages
    
    var textGoToPage2 = 'Go to'; // text for above option. You can change it to 'Jump to Page' or anything you like. The above option needs to turned on for this.
    
	var enableSelectNoItems2 = false; // if you want to change items per page on the fly.
    
    var textSelectNoItems2 = 'Items Per Page'; // text for above option. You can change it to 'Change No. of tag/page' or anything you like. The above option needs to turned on for this.

	var paginatorValues2 = Array(5,10,15,20,25,30); // list of values for above option (enableSelectNoItems2).

    var anchorLink2 = '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----------------------------------------------------
jQuery.fn.extend({
    pagination2: function () {
        paginatorId2 = this;
        switch (paginatorPosition2) {
        case 'top':
            {
                paginatorId2.before('<div class="paginator2"></div>');
                break
            }
        case 'bottom':
            {
                paginatorId2.after('<div class="paginator2"></div>');
                break
            }
        case 'both':
            {
                paginatorId2.before('<div class="paginator2"></div>');
                paginatorId2.after('<div class="paginator2"></div>');
                break
            }
        default:
            {
                paginatorId2.after('<div class="paginator2"></div>')
            }
        }
        initPaginator2()
    },
    depagination2: function () {
        $('.paginator2').remove();
        paginatorId2.children().show()
    }
});

function initPaginator2() {
    if (itemsPerPage2 < 1) itemsPerPage2 = 5;
    allItems2 = paginatorId2.children().length;
    if (allItems2 % itemsPerPage2 == 0) lastPage2 = parseInt(allItems2 / itemsPerPage2);
    else lastPage2 = parseInt(allItems2 / itemsPerPage2) + 1;
    if ((startPage2 < 1) || (startPage2 > lastPage2)) startPage2 = 1;
    if(startPage2 != lastPage2) appendContent2(startPage2, 1)
}
function appendContent2(a, b) {
    if (a < 0) {
        if (a == -1) a = currentPage2 - 1;
        else a = currentPage2 + 1
    }
    currentPage2 = a;
    till = (currentPage2 - 1) * itemsPerPage2;
    if (!b) {
        createPaginator2();
				paginatorId2.children().hide();
				paginatorId2.children().slice(till, itemsPerPage2 + till).show();
				window.scroll(0,0);
/*        paginatorId2.fadeOut("medium", function () {
            createPaginator2();
            paginatorId2.children().hide();
            paginatorId2.children().slice(till, itemsPerPage2 + till).show();
            paginatorId2.fadeIn("medium")
        })*/
    } else {
        createPaginator2();
        paginatorId2.children().hide();
        paginatorId2.children().slice(till, itemsPerPage2 + till).show()
    }
}
function changePaginator2(a) {
    itemsPerPage2 = a;
    if (allItems2 % itemsPerPage2 == 0) lastPage2 = parseInt(allItems2 / itemsPerPage2);
    else lastPage2 = parseInt(allItems2 / itemsPerPage2) + 1;
    appendContent2(1)
}
function createPaginator2() {
    $(".paginator2").html("");
    var a = '';
    var b = '';
    var c = '';
    var d = '';
    var e = ' Page ' + currentPage2 + ' of ' + lastPage2 + ' Page(s) ';
    var f = ' ' + textGoToPage2 + ' <select onchange="appendContent2(this.value);" >';
    var g = ' ' + textSelectNoItems2 + ' <select onchange="itemsPerPage2=Number(this.value);initPaginator2();" >';
    for (var i = 0; i < paginatorValues2.length; i++) {
        if (itemsPerPage2 == paginatorValues2[i]) g += '<option value="' + paginatorValues2[i] + '" selected="selected">' + paginatorValues2[i] + '</option>';
        else g += '<option value="' + paginatorValues2[i] + '">' + paginatorValues2[i] + '</option>'
    }
    g += '</select>';
    if (currentPage2 == 1) {
        style = '<a href="' + anchorLink2 + '" class="inactive first" title="First Page">' + firstPageSymbol2 + '</a>' + separator2;
        a = b = style;
        style = '<a href="' + anchorLink2 + '" class="inactive prev" title="Previous Page">' + previousPageSymbol2 + '</a>' + separator2;
        a += style;
        b += style;
        c += style;
        d += style
    } else {
        style = '<a href="' + anchorLink2 + '" class="active first" onclick="appendContent2(1);" title="First Page">' + firstPageSymbol2 + '</a>' + separator2;
        a = b = style;
        style = '<a href="' + anchorLink2 + '" class="active prev" onclick="appendContent2(-1);" title="Previous Page">' + previousPageSymbol2 + '</a>' + separator2;
        a += style;
        b += style;
        c += style;
        d += style
    }
    for (var i = 1; i <= lastPage2; i++) {
        if (i == currentPage2) {
            a += '<a href="' + anchorLink2 + '" class="inactive" title="Page ' + i + '">' + i + '</a>' + separator2;
            b += '<a href="' + anchorLink2 + '" class="inactive" title="Page ' + i + '">' + i + '/' + lastPage2 + '</a>' + separator2;
            c += '<a href="' + anchorLink2 + '" class="inactive" title="Page ' + i + '">' + i + '</a>' + separator2;
            f += '<option value="' + i + '" selected="selected">' + i + '</option>'
        } else {
            style = '<a href="' + anchorLink2 + '" class="active" onclick="appendContent2(' + i + ');" title="Page ' + i + '">' + i + '</a>' + separator2;
            a += style;
            c += style;
            f += '<option value="' + i + '">' + i + '</option>'
        }
    }
    f += '</select>';
    if (currentPage2 == lastPage2) {
        style = '<a href="' + anchorLink2 + '" class="inactive next" title="Next Page">' + nextPageSymbol2 + '</a>';
        a += style;
        b += style;
        c += style;
        d += style;
        style = separator2 + '<a href="' + anchorLink2 + '" class="inactive last" title="Last Page">' + lastPage2Symbol2 + '</a>';
        a += style;
        b += style
    } else {
        style = '<a href="' + anchorLink2 + '" class="active next" onclick="appendContent2(-2);" title="Next Page">' + nextPageSymbol2 + '</a>';
        a += style;
        b += style;
        c += style;
        d += style;
        style = separator2 + '<a href="' + anchorLink2 + '" class="active last" onclick="appendContent2(' + lastPage2 + ');" title="Last Page">' + lastPage2Symbol2 + '</a>';
        a += style;
        b += style
    }
    switch (paginatorStyle2) {
    case 1:
        style = a;
        break;
    case 2:
        style = b;
        break;
    case 3:
        style = c;
        break;
    case 4:
        style = d;
        break;
    default:
        style = a
    }
    if (enablePageOfOption2) style += '<span class="inactive" title="Page Information">' + e + '</span>';
    if (enableGoToPage2) style += '<span class="inactive" title="Select Page">' + f + '</span>';
    if (enableSelectNoItems2) style += '<span class="inactive" title="Select no. of items per page">' + g + '</span>';
    $(".paginator2").html(style)
}
