/*!
 * jqPagination, a jQuery pagination plugin (obviously)
 *
 * Copyright (C) 2011 Ben Everard
 *
 * http://beneverard.github.com/jqPagination
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *     
 */
(function(a){a.jqPagination=function(c,b){var d=this;d.$el=a(c);d.el=c;d.$input=d.$el.find("input");d.$el.data("jqPagination",d);d.init=function(){d.options=a.extend({},a.jqPagination.defaultOptions,b);if(d.options.max_page===null){if(d.$input.data("max-page")!==undefined){d.options.max_page=d.$input.data("max-page")}else{d.options.max_page=1}}if(d.$input.data("current-page")!==undefined&&d.isNumber(d.$input.data("current-page"))){d.options.current_page=d.$input.data("current-page")}d.$input.removeAttr("readonly");d.setPage();d.$input.live("focus mouseup",function(e){if(e.type==="focusin"){var f=a(this);f.val(f.data("current-page")).select()}if(e.type==="mouseup"){return false}});d.$input.live("blur keydown",function(e){var g=a(this),f=parseInt(d.options.current_page,10);if(e.keyCode===27){g.val(f);g.blur()}if(e.keyCode===13){g.blur()}if(e.type==="focusout"){d.setPage(g.val())}});d.$el.find("a").live("click",function(e){if(!e.metaKey&&!e.ctrlKey){e.preventDefault();d.setPage(a(this).data("action"))}})};d.setPage=function(f){var g=parseInt(d.options.current_page,10),e=parseInt(d.options.max_page,10);if(isNaN(parseInt(f,10))){switch(f){case"first":f=1;break;case"prev":case"previous":f=g-1;break;case"next":f=g+1;break;case"last":f=e;break}}f=parseInt(f,10);if(isNaN(f)||f<1||f>e||f===g){d.setCurrentPage(g);d.setInputValue(g)}else{d.setCurrentPage(f);d.setInputValue(f);d.setLinks(f);d.options.paged(f)}};d.setCurrentPage=function(e){d.options.current_page=e;d.$input.data("current-page",e)};d.setInputValue=function(f){var g=d.options.page_string,e=d.options.max_page;g=g.replace("{current_page}",f).replace("{max_page}",e);d.$input.val(g)};d.isNumber=function(e){return !isNaN(parseFloat(e))&&isFinite(e)};d.setLinks=function(h){var j=d.options.link_string,i=parseInt(d.options.current_page,10),e=parseInt(d.options.max_page,10);if(j!==""){var g=i-1;if(g<1){g=1}var f=i+1;if(f>e){f=e}d.$el.find("a.first").attr("href",j.replace("{page_number}","1"));d.$el.find("a.prev, a.previous").attr("href",j.replace("{page_number}",g));d.$el.find("a.next").attr("href",j.replace("{page_number}",f));d.$el.find("a.last").attr("href",j.replace("{page_number}",e))}};d.init()};a.jqPagination.defaultOptions={current_page:1,link_string:"",max_page:null,page_string:"Page {current_page} of {max_page}",paged:function(){}};a.fn.jqPagination=function(b){return this.each(function(){(new a.jqPagination(this,b))})}})(jQuery);
