/*
 *  jQuery Sweet Pagination 0.1
 *  http://tutorialzine.com/2010/05/sweet-pages-a-jquery-pagination-solution/
 *  
 *  Author: Martin Angelov
 *  
 *  Modified by: Drew Wells
 *  Change Log
 *  05/07/2010: Refactored to accept jQuery collections
 *          Moved variables to top, as JS would do this anyway
 *          Removed some unneccessary code
 *	         Fixed li to fill pages even with low amount of text
 *
 *  	Tested: FF3.6, IE7/8
 * 
 */
(function($){

	// Creating the sweetPages jQuery plugin:
	$.fn.sweetpages = function(options){
		var opts = $.extend({}, $.fn.sweetpages.defaults, options);
		return this.each(function(){
			var $ul = $(this), // The plugin works best for unordered lists, althugh ols would do just as well:
				$li = $ul.find('li'),
				pagesNumber = Math.ceil((($li.length - opts.lastPage) / opts.perPage) + opts.lastPage), //calculate the number of pages
				$swControls,
				maxWidth = 0,
				maxHeight = 0,
				$swPage,
				$swSlider,
				$hyperLinks,
				totalWidth = 0,
				currentPosition = 0,
				lastPage;
			//Normalize css
			$ul.css({
				'margin': 0,
				'list-style': 'none outside none',
				'overflow': 'hidden'
			});	 
			// If the pages are less than two, do nothing:
			if (pagesNumber < 2) {
				return this;
			}
			
			$li.each(function(){
				// Calculating the height of each li element, and storing it with the data method:
				//	true makes it include margin
				var el = $(this);
				el.data('height', el.outerHeight(true));
			});
			
			// Creating the controls div:
			swControls = ['<div class="swControls"><a class="navPage" href="">Back</a>'];
			//reduction added to eliminate li off of last page
			for (var i = 0,start,end; i < pagesNumber; i++) {
				// Slice a portion of the lis, and wrap it in a swPage div:
				// Slice them one at a time if they are a lastPage
				if (i >= pagesNumber - opts.lastPage) {
				console.log(pagesNumber,$li.length-i)
					$li.slice($li.length - i, $li.length - i + 1 ).wrapAll('<div class="swPage" />');
				}
				else {
					$li.slice(i * opts.perPage, (i + 1) * opts.perPage).wrapAll('<div class="swPage" />');
				}
				
				// Adding a link to the swControls div:
				swControls.push('<a href="" class="swShowPage">' + (i + 1) + '</a>');
			}
			swControls.push('<a class="navPage" href="">Next</a>');
			$ul.append(swControls.join(''));
			// Center the control div:
			$swControls = $(".swControls");
			$swControls.css({
				'position': 'absolute',
				'left': '50%'
			});
			
			
			$swPage = $ul.find('.swPage').css({
				'float': 'left',
				'width': $ul.width()
			});
			//Loop through each page finding the necessary page height and full page width including hidden portion(s)
			$swPage.each(function(){
				var elem = $(this),
					tmpHeight = elem.height();
				
				if ( tmpHeight > maxHeight){
					maxHeight = tmpHeight;
				} 
				if( elem.outerWidth() > maxWidth ){
					maxWidth = elem.width();
				}
				totalWidth += elem.outerWidth();
			});
			
			//Set the page dimensions based on content in li
			$ul.height(maxHeight);
			$ul.width(maxWidth);
			
			$swPage.wrapAll('<div class="swSlider" />');
			//Force the li to be the size of the internal dimensions of the page, taking into account any padding or margins on the div
			$li.width( $swPage.width() - ($li.outerWidth(true) - $li.width()) );
			$swSlider = $ul.find('.swSlider');
			$swSlider.append('<div style="clear:both;" />').width(totalWidth); //Fix issues with IE not cleanly flipping pages
			
			$hyperLinks = $ul.find('a.swShowPage');
			// Mark the first link as active the first time this code runs:
			$hyperLinks.eq(0).addClass('active');
			
			//Display changes after you make it position absolute, don't assign the margin until absolute has been drawn
			$swControls.css({
				'margin-left': -$swControls.width() / 2
			});
			$hyperLinks.click(function(e){
				// If one of the control links is clicked, slide the swSlider div 
				// (which contains all the pages) and mark it as active:
				$(this).addClass('active').siblings().removeClass('active');
				currentPosition = -(parseInt($(this).text()) - 1) * $ul.width()
				$swSlider.stop().animate({
					'margin-left': currentPosition
				}, 'slow');
				return false;
			});
			
			$(".navPage").bind('click',function(ev){
				var nextPosition = currentPosition,
					pageWidth = $swPage.width(),
					linkPosition = (currentPosition == 0) ? 0 : -(currentPosition / pageWidth );
				if( this.innerHTML == 'Next' ){
					if( currentPosition + totalWidth > pageWidth ){ //Prevent next on last page
						nextPosition -= pageWidth;
						linkPosition += 1;
					}
				} else if( currentPosition < 0 ) {
						nextPosition += pageWidth;
						linkPosition -= 1;
				}
				if( nextPosition != currentPosition ){
					currentPosition = nextPosition;
					$hyperLinks.removeClass('active').eq(linkPosition).addClass('active');
					$swSlider.stop().animate({
						'margin-left': nextPosition
					},'slow');
				}
				
				return false;
			});
			
		});
	}
	
	$.fn.sweetpages.defaults = {
		perPage: 3,
		lastPage: 0 //tell sweetpages to seperate the last li into its own page
	};
})(jQuery);

