/**
*	pagination.js - Plik zawiera klasę do stronicowania wyników za pomocą JavaScript
*
*	Wymagana biblioteka jQuery
*	
	PRZYKŁAD UŻYCIA
	
	w sekcji head:

	<script type="text/javascript">
	<!--
		$(document).ready(function() {
			var bmpag = new bmPagination('results1', 5);
		});
	//-->
	</script>


	w body:

	<div class="results1">
		<div class="nav"></div>
		<div class="items">
			<div>Wynik 1</div>
			<div>Wynik 2</div>
			<div>Wynik 3</div>
			<div>Wynik 4</div>
		</div>
		<div class="nav"></div>
	</div>
	
	
	
*	W przypadku problemu z ogarnięciem elementów paginacji, można nadać każdemu z nich taką samą klasę, np. class="pag_element".
*	Wówczas przy tworzeniu obiektu paginacji w konstruktorze jako 4 parametr należy podać nazwę klasy elementu paginacji.
*	Element obejmujący wszystkie paginowane elementy nie potrzebuje już klasy "items".

	PRZYKŁAD
	
	w sekcji head:

	<script type="text/javascript">
	<!--
		$(document).ready(function() {
			var bmpag = new bmPagination('results1', 5, null, 'pag_element');
		});
	//-->
	</script>


	w body:

	<div class="results1">
		<div class="nav"></div>
		
		<div class="pag_element">Wynik 1</div>
		<div class="pag_element">Wynik 2</div>
		<div class="pag_element">Wynik 3</div>
		<div class="pag_element">Wynik 4</div>
			
		<div class="nav"></div>
	</div>
	
	
	
*	POLE PRZEJDŹ DO STRONY - wystarczy stworzyć pole tekstowe o klasie "pagination-go-to-page"
*	i element do kliknięcia (przycisk, link, obrazek) o klasie "submit-arrow".
*	Oba te elementy muszą znajdować się wewnątrz bloku o klasie określonej w parametrze className.
	
*/

// Czwarty parametr jest opcjonalny - gdy jest używany, elementy do paginacji muszą mieć określoną klasę
function bmPagination(className, itemsPerPage, visibleLinks, itemClassName) {
	// --- CONFIG ----
	this.prevPageStr = '« Poprzednia strona';
	this.nextPageStr = 'Następna strona »';
	// --- end CONFIG ---
	
	this.className = className;
    this.itemsPerPage = itemsPerPage;
	this.visibleLinks = visibleLinks;
	this.pagesNumber;
	this.currentPage;
	this.itemClassName = itemClassName;
	this.itemsNumber = itemClassName ? $('.' + this.itemClassName).size() : $('.' + this.className + ' .items').children().size();

	
	this.init = function() {
		this.pagesNumber = Math.ceil(this.itemsNumber / this.itemsPerPage);
		this.setPage(0);
		
		var bmpagTmp = this;
		$('.' + bmpagTmp.className + ' .nav a').live('click', function(e) {
			e.preventDefault();
			var page = $(this).attr('page');
			if(page === 'prev') {
				bmpagTmp.prevPage(bmpagTmp.currentPage);
			} else if (page === 'next') {
				bmpagTmp.nextPage(bmpagTmp.currentPage);
			} else {
				bmpagTmp.setPage($(this).attr('page'));
			}
		});
		
		$('.' + bmpagTmp.className + ' .submit-arrow').live('click', function(e) {
			e.preventDefault();
			bmpagTmp.setPage($('.' + bmpagTmp.className + ' .pagination-go-to-page').val() - 1);
		});
	};
	
    this.setPage = function(page) {
		if(page >= 0 && page < this.pagesNumber) {
			this.currentPage = page;
			updateItemsVisibility(this);
			updateNav(this);
		}
    };
	
	this.prevPage = function(page) {
		if(this.currentPage > 0) {
			this.currentPage--;
			updateItemsVisibility(this);
			updateNav(this);
		}
	};
	
	this.nextPage = function(page) {
		if(this.currentPage < this.pagesNumber - 1) {
			this.currentPage++;
			updateItemsVisibility(this);
			updateNav(this);
		}
	};
	
	
	// --- Private functions ---
	
	function updateNav(bmpag) {
		var navHtml = getNav(bmpag);
		$('.' + bmpag.className + ' .nav').html(navHtml);
	}
	
	function getNav(bmpag) {
		var aLinks = prepareVisibleLinks(bmpag);
	
		var result = '';
		if(bmpag.currentPage > 0) {
			result += '<a class="prev" page="prev" href="#">' + bmpag.prevPageStr + '</a>';
		} else {
			result += '<span class="prev">' + bmpag.prevPageStr + '</span>';
		}
		
		result += ' <span>|</span> ';
		var first = true;
		for(var i in aLinks) {
			if(first) {
				first = false;
			} else {
				result += ' ';
			}
			if((aLinks[i-1] && aLinks[i] - aLinks[i-1] > 1) || (aLinks[i+1] && aLinks[i+1] - aLinks[i] > 1)) {
				result += ' <span class="current">...</span> <span>|</span> ';
			}
			if(aLinks[i] - 1 == bmpag.currentPage) {
				result += ' <span class="current">' + aLinks[i] + '</span> <span>|</span> ';
			} else {
				result += '<a page="' + (aLinks[i] - 1) + '" href="#">' + aLinks[i] + '</a> <span>|</span> ';
			}
		}
		
		if(bmpag.currentPage < bmpag.pagesNumber - 1) {
			result += '<a class="next" page="next" href="#">' + bmpag.nextPageStr + '</a>';
		} else {
			result += '<span class="next">' + bmpag.nextPageStr + '</span>';
		}
		return result;
	}
	
	function updateItemsVisibility(bmpag) {
		$.each($((itemClassName ? ('.' + bmpag.itemClassName) : ('.' + bmpag.className + ' .items > *'))), function(index, value) {
			var firstToShow = bmpag.currentPage * bmpag.itemsPerPage;
			var lastToShow = firstToShow + bmpag.itemsPerPage - 1;
			$(value).hide().find('img[toload]').each(function() {
				$(this).attr('src', '/_items/_new/img/small-loading.gif');
			});
			if(index >= firstToShow && index <= lastToShow) {
				$(value).show().find('img[toload]').each(function() {
					$(this).attr('src', $(this).attr('toload'));
				});				
			} else {
				$(value).hide();
			}
		});
	}
	
	function prepareVisibleLinks(bmpag) {
		var aResult = new Array();
		if(!bmpag.visibleLinks) {
			bmpag.visibleLinks = 7;
		}
		if(bmpag.visibleLinks < 5) {
			bmpag.visibleLinks = 5;
		}
		
		if(bmpag.visibleLinks >= bmpag.pagesNumber) {
			for(var i = 1; i <= bmpag.pagesNumber; i++) {
				aResult.push(i);
			}
		} else {
			var surroundingsCount = bmpag.visibleLinks - 2;
			var surroundingsBegin = parseInt(bmpag.currentPage) + 1 - Math.floor((surroundingsCount - 1)/2);
			var surroundingsEnd = parseInt(bmpag.currentPage) + 1 + parseInt(Math.ceil((surroundingsCount - 1)/2));
			
			if(surroundingsBegin < 2) {
				surroundingsBegin = 2;
				surroundingsEnd = surroundingsBegin + surroundingsCount - 1;
			}
			if(surroundingsEnd > bmpag.pagesNumber - 1) {
				surroundingsEnd = bmpag.pagesNumber - 1;
				surroundingsBegin = surroundingsEnd - surroundingsCount + 1;
			}
			
			aResult.push(1);
			for(var i = surroundingsBegin; i <= surroundingsEnd; i++) {
				aResult.push(i);
			}
			aResult.push(parseInt(bmpag.pagesNumber));
		}
		
		return aResult;
	}
	this.init();
}

