/* aqPaging v2.0
   Paging function with next and previous ranges.
   Copyright (C) 2011 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 _o = $.extend({
		border: '#e8e8e8',
		fg: '#00A94F',
		bg: '#e8e8e8',
		padding: '2px 5px',
		margin: '0 2px',
		pages: 0, max: 10, current: 1
	},$options);

	var _draw = function($ob) {
		$ob.empty();

		var _s = 1,
			_e = _o.pages,
			_p,
			_offset = (_o.current > _o.max) ? 1 : 0;

		if (_o.pages > _o.max) {
			if (_o.current > _o.max) {
				_s = _o.max *
					parseInt((_o.current - _offset) / _o.max, 10);
			}

			if (_o.current - _offset + _o.max < _o.pages) {
				_e = _s + _o.max + _offset;
			}
		}

		for (_p = _s; _p <= _e; _p++) {
			$('<a\/>')
				.attr('href', '#' + _p)
				.text(_p)
				.appendTo($ob);
		}

		if (_o.current >= _s && _o.current - _o.max > 0) {
			$ob.prepend('<a href="#1">1<\/a><i>&hellip;<\/i>');
		}

		if ((_o.current - _offset + _o.max) <= _o.pages &&
			_e !== _o.pages) {
			$ob.append('<i>&hellip;<\/i>' +
				'<a href="#' + _o.pages + '">' + _o.pages + '<\/a>');
		}

		$('<br\/>')
			.css({ float: 'none', clear: 'both' })
			.appendTo($ob);

		$('A, I', $ob)
		.css({
			
			padding: _o.padding,
			margin: _o.margin
		})
		.filter('A')
		.css({
			border: '1px solid ' + _o.border,
			textDecoration: 'none',
			color: _o.fg,
			backgroundColor: _o.bg
		})
		.hover(
			function() {
				$(this).css({backgroundColor: _o.fg, color: _o.bg});
			},
			function() {
				if ($(this).attr('href') !== '#' + _o.current) {
					$(this).css({backgroundColor: _o.bg, color: _o.fg});
				}
			}
		)
		.unbind('click').bind('click', function() {
			_o.current = $(this).text();
			$ob.trigger('highlight');

			if ($.isFunction(_o.cb)) {
				_o.cb(_o.current);
			}
			return false;
		});
	};

	return this.each(function() {
		if (_o.pages <= 1) {
			return false;
		}

		var _ob = $('.aqPaging', this);

		if (!_ob.length) {
			_ob = $('<div\/>')
				.addClass('aqPaging')
				.appendTo(this);
		}

		_ob.bind('highlight', function() {
			_draw(_ob);
			$('A', _ob)
				.css({ backgroundColor: _o.bg, color: _o.fg})
				.filter('A[href="#' + _o.current + '"]')
				.css({ backgroundColor: _o.fg, color: _o.bg});
		})
		.trigger('highlight');
	});
};

})(jQuery);

