/*****************
Non-universal paging function (readable version).
Parameter:
selector		A unique identifier for all related containers. It is used as one of two request parameters, too. (?[selector]=...)
nrOfItems		The total number of pages.
parameter		A parameter value whichs value will be passed as the second request parameter (&parameter=[parameter])
*****************/
function Paging(selector, nrOfItems, parameter) {
	this.overallRange = nrOfItems;
	this._selector = selector;
	this.defaultParameter = parameter;
	
	this.rangeSize = 10;
	this._current = 1;
	this._currentRange = [1,10];
	this._lastLinkIndex = 0;
	this.capMinus10 = "-10";
	this.capPlus10 = "+10";
	this.capPrevious = "< ZURÜCK";
	this.capNext = "WEITER >";
	
	this._updateRange = function() {
		var rangeStart = Math.round(this._current - this.rangeSize/2);
		var rangeEnd = rangeStart + this.rangeSize - 1;
		while (rangeStart < 1) {
			rangeStart++;
			if (rangeEnd < this.overallRange) {
				rangeEnd++;
			}
		}
		while (rangeEnd > this.overallRange) {
			rangeEnd--;
			if (rangeStart > 1) {
				rangeStart--;
			}
		}
		this._currentRange = [rangeStart,rangeEnd];
	};
	
	this._inRange = function(key) {
		return (key >= this._currentRange[0] && key <= this._currentRange[1]) ? true : false;
	};
	
	this._isMinus10able = function() {
		return (this._current - 10) >= 1 ? true : false;
	};
	
	this._isPlus10able = function() {
		return (this._current + 10) <= this.overallRange ? true : false;
	};
	
	this._isMinusable = function() {
		return (this._current - 1) >= 1 ? true : false;
	};
	
	this._isPlusable = function() {
		return (this._current + 1) <= this.overallRange ? true : false;
	};
	
	this.getLinks = function() {
		var result = '';
		this._lastLinkIndex = 0;
		if (this._isMinus10able()) {
			result += '<li class="pagingMinus10 bg2"><a class="linkstyle" href="###" name="minus10"> '+ this.capMinus10 +' </a></li>';
		}
		if (this._isMinusable()) {
			result += '<li class="pagingPrev bg2"><a class="linkstyle" href="###" name="prev"> '+ this.capPrevious +' </a></li>';
		}
		result += '<li class="pagingNumbers"><ul>';
		for (var i = 1; i <= this.overallRange; i++) {
			if (this._inRange(i)) {
				this._lastLinkIndex = i;
				result += '<li><a class="linkstyle" href="###" name="'+i+'">'+i+'</a>|</li>';
			}
		};
		
		
		result += '</ul></li>';
		if (this._isPlus10able()) {
			result += '<li class="pagingPlus10 bg2"><a class="linkstyle" href="###" name="plus10"> '+ this.capPlus10 +' </a></li>';
		}
		if (this._isPlusable()) {
			result += '<li class="pagingNext bg2"><a class="linkstyle" href="###" name="next"> '+ this.capNext +' </a></li>';
		}
		return result;
	};
	
	this.setCurrent = function(value) {
		this._current = value;
		this._updateRange();
	};
	
	this.getCurrent = function() {
		return this._current;
	};
	
	this.setTargetPage = function(linkName) {
		var result = Number(linkName);
		switch (linkName) {
			case 'prev':
				result = this.getCurrent() - 1;
				break;
			case 'next':
				result = this.getCurrent() + 1;
				break;
			case 'minus10':
				result = this.getCurrent() - 10;
				break;
			case 'plus10':
				result = this.getCurrent() + 10;
				break;
		}
		return result;
	};
	
	this.getData = function(page) {
		var result = {};
		result['uebersicht_'+this._selector] = String(page - 1) +  ':1:0:0';
		result.parameter = this.defaultParameter;
		return result;
	};
	
	this.setLinkTargets = function() {
		var localScope = this;
		$('#pagination-' + this._selector).find('a').click(function() {
			var page = localScope.setTargetPage($(this).attr('name'));
			var data = localScope.getData(page);
			$.ajax({
				url: '/ajax_pagination.php',
				data: data,
				type: 'GET',
				success: function(data) {
					localScope.setCurrent(page);
					localScope.setContent(data);
					localScope.setLinks();
					localScope.setLinkTargets();
					localScope.setActiveStyles(page);
				}
			});
		});
	};
	
	this.setLinks = function() {
		$('#pagination-' + this._selector).html(this.getLinks());
		$('#pagination-' + this._selector).find('a[name="'+this._lastLinkIndex+'"]').parent().html('<a class="linkstyle" href="###" name="'+this._lastLinkIndex+'">'+this._lastLinkIndex+'</a>');
		this.adjustLinkBarPosition();
	};
	
	this.setContent = function(content) {
		$('#ajax-page-content-' + this._selector).html(content);
	};
	
	this.setActiveStyles = function(page) {
		$('#pagination-' + this._selector).find('a[name="'+page+'"]').parent().addClass('pagingActive');
		$('#pagination-' + this._selector).find('a[name="'+page+'"]').parent().addClass('list-symbol');
		var separatorChar = this._lastLinkIndex == page ? "" : "|";
		$('#pagination-' + this._selector).find('a[name="'+page+'"]').parent().html('<span>'+page+'</span>'+separatorChar);
	};
	
	this.setInitialLinkBarPosition = function() {
		var visibleItems = Number(this.overallRange) < 11 ? Number(this.overallRange) : 10;
		$('#pagination-' + this._selector).find('.pagingNumbers').css('margin-left',-visibleItems*14);
	};
	
	this.adjustLinkBarPosition = function() {
		var $allLinks = $('#pagination-' + this._selector).find('.pagingNumbers');
		var numberBarWidth = $allLinks.width();
		$('#pagination-' + this._selector).find('.pagingNumbers').css('margin-left',-numberBarWidth/2);
	};
	
	this.setLinks();
	this.setLinkTargets();
	this.setActiveStyles(this.getCurrent());
	this.setInitialLinkBarPosition();
};

