/*
#####################################################
# 
# PagedList 1.0
#
# Hand-crafted by Losource (losource.net)
#
#####################################################
*/

(function($) {
	$.fn.pagedlist = function(options) {		
		// Default options
		var defaults = {
			pagesize: 6,
			itemInView: 0,
			navElementClass: 'paged-list-nav',
			navBackClass: 'paged-list-back-link',
			navMoreClass: 'paged-list-more-link',
			backText: '&lt;&lt; Back',
			moreText: 'More &gt;&gt;'
		};
		
		// Merge with user-defined options
		$.fn.pagedlist.options = $.extend(defaults, options);
		
		return this.each(function() {
			var options = $.fn.pagedlist.options;
			var pagedlist = $(this);
			var listTotal = $(pagedlist).children('li').hide().addClass('hidden').length;
			var listPages = Math.ceil(listTotal/options.pagesize);
			var currentPage = 0;
			
			if (options.itemInView) {
				var iInView = $(pagedlist).find('li').index($(options.itemInView));
				currentPage = Math.floor((iInView)/options.pagesize);
			}

			if (listPages>0) {
				//add the controls
				var elNavRow = $('<li class="' + options.navElementClass + '"></li>');
				var elBack = $('<a class="' + options.navBackClass + '" href="#">' + options.backText + '</a>');
				var elMore = $('<a class="' + options.navMoreClass + '" href="#">' + options.moreText + '</a>');
				$(elNavRow).appendTo(this);
				$(elBack).appendTo(elNavRow);
				$(elMore).appendTo(elNavRow);
				
				changePage(pagedlist, currentPage, options.pagesize, listTotal, elBack, elMore, options.navElementClass);
				
				$(elBack).click(function() {
					currentPage = currentPage - 1;
					changePage(pagedlist, currentPage, options.pagesize, listTotal, elBack, elMore, options.navElementClass);
					return false;
				});
				
				$(elMore).click(function() {
					currentPage = currentPage + 1;
					changePage(pagedlist, currentPage, options.pagesize, listTotal, elBack, elMore, options.navElementClass);
					return false;
				});
			}	
		});
	};
	
	/*
		Internal functions
	*/
	
	function changePage(list, newpage, pagesize, totalitems, elBack, elMore, navElementClass) {
		var listPages = Math.ceil(totalitems/pagesize);
		var startItem = newpage*pagesize;
		var endItem = (newpage*pagesize) + (pagesize-1);
		
		//iterate through showing and hiding the right things
		$(list).children('li').each(function(i) {
			if (!$(this).hasClass(navElementClass)) {
				if ((i>=startItem)&&(i<=endItem)) {
					$(this).addClass('visible').removeClass('hidden');
				} else {
					$(this).addClass('hidden').removeClass('visible');
				}
				
				$(list).children('.hidden').hide();
				$(list).children('.visible').fadeIn();	
				
				
			}
		});
		
		//deal with the controls
		if (newpage==0) {
			$(elBack).hide();
		} else {
			$(elBack).fadeIn();
		}
		
		if (newpage==listPages-1) {
			$(elMore).hide();
		} else {
			$(elMore).fadeIn();
		}
	}
	
	// Persistent options object
	$.fn.pagedlist.options = {};
	
})(jQuery); 	