/* aqPaging v1.1 - Paging function with next and previous ranges.
   Copyright (C) 2008 Paul Pham <http://aquaron.com/~jquery/aqPaging>

   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(jQuery){
jQuery.fn.aqPaging = function (options) {
   var opts = jQuery.extend({ },jQuery.fn.aqPaging.defaults,options);

   return this.each(function(){
	
	
	
		
      if (opts.pages <= 1) return false;

      if (!jQuery('.aqPaging',this).length) {
         jQuery.fn.aqPaging.defaults.uniqID++;
         jQuery('<div class="aqPaging" id="aqPaging_'
            +jQuery.fn.aqPaging.defaults.uniqID+'"><\/div>')
            .appendTo(this);

         jQuery.fn.aqPaging.defaults.cbs[jQuery.fn.aqPaging.defaults.uniqID]
            = opts.cb;

         jQuery('.aqPaging',this).addClass(opts.css);
      }

      var $pager = jQuery('.aqPaging',this);
      var pid = $pager.attr('id');
 
      var s = 1, e = opts.pages;
      var html = '';

      var offset = (opts.current > opts.max) ? 1 : 0;

      if (opts.pages > opts.max) {
         if (opts.current > opts.max)
            s = opts.max*parseInt((opts.current-offset)/opts.max);

         if (opts.current-offset+opts.max < opts.pages) 
            e = s + opts.max + offset;
      }

      for (var p=s; p<=e; p++)
         html += '<a href="javascript:void(0)" onclick="jQuery.fn.aqPaging.flip(\''+pid+'\''
            +','+p+','+opts.pages+');">' + p + '<\/a> ';

      $pager.html(html);

      if (opts.current >= s && opts.current-opts.max > 0) {
         $pager.prepend('<a href="javascript:void(0)" onclick="jQuery.fn.aqPaging.flip(\''
            +pid+'\''+','+(s-opts.max+1)+','+opts.pages +')">' 
            + (s-opts.max+1) + '<\/a> <i>&hellip;<\/i> ');
      }
      if ((opts.current-offset+opts.max) <= opts.pages && e != opts.pages) {
         $pager.append('<i>&hellip;<\/i>'
            +' <a href="javascript:void(0)" onclick="jQuery.fn.aqPaging.flip(\''+pid+'\''+','
            +opts.pages+','+opts.pages +')">'+opts.pages+'<\/a> ');
      }

      var hi = ((opts.current-1)%opts.max) + ((offset+1)*offset);
	  if (opts.css) {
		 $pager.find('a').removeClass(); 
         $pager.find('a').addClass(opts.aCss)
         .not(':eq('+hi+')').hover(
            function() { jQuery(this).removeClass(); jQuery(this).addClass(opts.hiCss) },
            function() { jQuery(this).removeClass();  jQuery(this).addClass(opts.loCss) }
         );
         $pager.find('i').addClass(opts.iCss);
         
		 $pager.find('a').eq(hi).removeClass();
		 $pager.find('a').eq(hi).addClass(opts.hiCss);
      } else
	  {
		  	
         $pager.find('a').removeClass('aqPagingHi')
            .eq(hi).addClass('aqPagingHi');
	  }
      if (opts.flip)
         jQuery.fn.aqPaging.flip(pid,opts.current,opts.pages);
   });
};

jQuery.fn.aqPaging.flip = function(id,p,total) {
	
   var idx = id.replace(/aqPaging_/,'');
   var func = jQuery.fn.aqPaging.defaults.cbs[idx];
   if (func) func(p);
   jQuery('#'+id).parent().aqPaging({current: p, pages: total});
   return false;
};

jQuery.fn.aqPaging.defaults = {
   cbs: [], pages: 0, current: 0, max: 10, uniqID: 0, flip: false,
   css: "pager_css",
   hiCss: "pager_hiCss",
   loCss: "pager_loCss",
   aCss: "pager_aCss",
   iCss: "pager_iCss"
};
})(jQuery);

