
if ('undefined' == typeof(DR)) DR = {};
if ('undefined' == typeof(DR.Bonanza)) DR.Bonanza = {};
if ('undefined' == typeof(DR.Bonanza.Grid)) DR.Bonanza.Grid = {};

DR.Bonanza.Grid.Paging = function (config) {
	var grid = $(config.grid);
	var items = grid.getElements('a');
	items.each(function(el, index) { el.setStyle('display', 'none'); });
	var itemsPerPage = config.itemsPerPage ? config.itemsPerPage : 12;
	var currentPage = 0;
	var numberOfPages = Math.ceil(items.length / itemsPerPage);

	var container = (config.container 
			? $(config.container) 
			: grid.parentNode.insertBefore(new Element('div', {'class': 'paging', 'style': 'display: none;'}), grid.nextSibling));
	var pageEls = new Array();
	for(var index = 0; index < numberOfPages; ++index) {
		pageEls.push(new Element('li', {}).adopt(new Element('a', {'href': 'javascript:void(0)', 'html': index +1, 'pageNumber': index +1, 'events': {
			'click': function (event, page) { go(page); }.bindWithEvent(this, index +1)
		}})));
	}
	var previousEl, nextEl, showingEl;
	
	container.adopt(
	//<div class="showing">Showing <span class="num">12</span> af <span class="num">52</span></div>
	//new Element('span',{'class': 'num', 'html': '5'})

		showingEl = new Element('div', {'class': 'showing', 'html': ''}),

		previousEl = new Element('div', {'class': 'previous'}).adopt(new Element('a', {'href': 'javascript:void(0)', 'html': '&nbsp', 'events': {'click': previous}})),
		nextEl = new Element('div', {'class': 'next'}).adopt(new Element('a', {'href': 'javascript:void(0)', 'html': '&nbsp', 'events': {'click': next}})),
		//new Element('ul', {'class': 'pages'}).adopt(pageEls),
		new Element('div', {'class': 'clearfloat', 'html': '&nbsp;'})
	);
	
	// Start of by going to page one
	go(1);
	
	
	
	// Internal methods
	function next () {
		go(currentPage +1)
	}
	
	function previous () {
		go(currentPage -1)
	}
	
	function go (page) {
		page = Math.max(Math.min(page, numberOfPages), 1); // Normalize page num
		var i;
		
		if (currentPage) {
			// Hide current page items
			for (i = ((currentPage -1) * itemsPerPage); i < (currentPage * itemsPerPage); ++i) {
				if ('undefined' == typeof(items[i])) break;
				items[i].style.display = 'none';
			}
			pageEls[currentPage -1].removeClass('current');
		}

		currentPage = page;

		// Show new page items
		for (i = ((currentPage -1) * itemsPerPage); i < (currentPage * itemsPerPage); ++i) {
			if ('undefined' == typeof(items[i])) break;
			items[i].style.display = 'block';
		}

		// Update paging
		//previousEl.class = currentPage>1 ? 'previous' : 'noprevious';		
		previousEl.style.backgroundPosition = currentPage > 1 ? '0px 50%' : '-48px 50%';
		nextEl.style.backgroundPosition = currentPage < numberOfPages ? '-24px 50%' : '-72px 50%';
		showingEl.innerHTML= 'Viser '+currentPage+' / '+ Math.max(numberOfPages, 1);
		//previousEl.style.display = currentPage > 1 ? 'block' : 'none';
		//	nextEl.style.display = currentPage < numberOfPages ? 'block' : 'none';
		//pageEls[currentPage -1].addClass('current');
		
		if (1 > numberOfPages) container.dispose();
	}
	
	
	return { // Start public interface
		itemsPerPage: itemsPerPage,
		currentPage: currentPage,
		numberOfPages: numberOfPages,
		
		next: next,
		previous: previous,
		go: go
		
	}; // End public
}; // End class

