/* aqPaging v1.0 - 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($){
$.fn.aqPaging = function (options) {
   var opts = $.extend({ },$.fn.aqPaging.defaults,options);

   return this.each(function(){
      if (opts.pages <= 1) return false;

      if (!$('.aqPaging',this).length) {
         $.fn.aqPaging.defaults.uniqID++;
         $('<div class="aqPaging" id="aqPaging_'
            +$.fn.aqPaging.defaults.uniqID+'"><\/div>')
            .appendTo(this);

         $.fn.aqPaging.defaults.cbs[$.fn.aqPaging.defaults.uniqID]
            = opts.cb;

         $('.aqPaging',this).css(opts.css);
      }

      var $pager = $('.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;
      }
	  if(opts.current == 1){
	  		html+= '<a href="#navigation-list" class="aqPreviousInactive"><\/a>';
	  }else{
	  		previousNum = opts.current - 1;
	  		html+= '<a href="#navigation-list" class="aqPrevious" onclick="$.fn.aqPaging.flip(\'' + pid + '\'' + 
			',' + 
			previousNum + 
			',' +
			opts.pages +
			');" id="pageClick-' + 
			previousNum + 
			'">' + 
			'' +
			'<\/a>';
	  }
	   if (opts.current >= s && opts.current-opts.max > 0) {
         html += ('<div class="link-container"><a href="#navigation-list" onclick="$.fn.aqPaging.flip(\''
            +pid+'\''+','+(s-opts.max+1)+','+opts.pages +')">' 
            + (s-opts.max+1) + '<\/a></div> ');
      }
	  if((opts.current - 3) > 0) html += '<div class="link-container-more">...</div>';
      for (var p = s; p <= e; p++) 
	  	
	  	if (opts.current == p) {
	  		html += '<div class="link-container"><a href="#navigation-list" class="aqPagingHi" onclick="$.fn.aqPaging.flip(\'' + pid + '\'' +
	  		',' +
	  		p +
	  		',' +
	  		opts.pages +
	  		');" id="pageClick-' +
	  		p +
	  		'">' +
	  		p +
	  		'<\/a> </div>';
	  	}else{
			if (((p > (opts.current - 3)) && (p < (opts.current + 3))) || ((opts.current == 1) && p < 6)) {
				html += '<div class="link-container"><a href="#navigation-list" onclick="$.fn.aqPaging.flip(\'' + pid + '\'' +
				',' +
				p +
				',' +
				opts.pages +
				');" id="pageClick-' +
				p +
				'">' +
				p +
				'<\/a></div> ';
			}
		}
	 	if((opts.current + 2) < opts.pages) html += '<div class="link-container-more">...</div>';
		
      if ((opts.current-offset+opts.max) <= opts.pages && e != opts.pages) {
         html+=''
            +' <div class="link-container"><a href="#navigation-list" onclick="$.fn.aqPaging.flip(\''+pid+'\''+','
            +opts.pages+','+opts.pages +')">'+opts.pages+'<\/a></div> ';
      }
	 if(opts.current == opts.pages){
	  		html+= '<a href="#navigation-list" class="aqNextInactive"><\/a>';
	  }else{
	  		previousNum = opts.current + 1;
	  		html+= '<a href="#navigation-list" class="aqNext" onclick="$.fn.aqPaging.flip(\'' + pid + '\'' + 
			',' + 
			previousNum + 
			',' +
			opts.pages +
			');" id="pageClick-' + 
			previousNum + 
			'">' + 
			'' +
			'<\/a>';
	  }
		
      $pager.html(html);

     

      var hi = ((opts.current-1)%opts.max) + ((offset+1)*offset);
      if (opts.css) {
         $pager.find('a').css(opts.aCss)
         .not(':eq('+hi+')').hover(
            function() { $(this).css(opts.hiCss) },
            function() { $(this).css(opts.loCss) }
         );
         $pager.find('i').css(opts.iCss);
         $pager.find('a').eq(hi).css(opts.hiCss);
      } else
        

      if (opts.flip)
         $.fn.aqPaging.flip(pid,opts.current,opts.pages);
   });
};

$.fn.aqPaging.flip = function(id,p,total) {
   var idx = id.replace(/aqPaging_/,'');
   var func = $.fn.aqPaging.defaults.cbs[idx];
   if (func) func(p);
   $('#'+id).parent().aqPaging({current: p, pages: total});
   $('#pageClick-' + p).addClass('aqPagingHi');
   return false;
};

$.fn.aqPaging.defaults = {
   cbs: [], pages: 0, current: 0, max: 10, uniqID: 0, flip: false,
   css: { font: '12px' },
   hiCss: {fontWieght: 'normal' },
   loCss: { },
   aCss: {  },
   iCss: {  }
};
})(jQuery);