
var perPage = 5;

$(document).ready(function() {
	$("div.listings").each(function () {
		var listings = $("div.listing", this).get();
		
		var id = 1;
		if(location.hash.length > 1) {
			id = parseInt(location.hash.substring(1));
		}
		
		var blockCount = showBlockSelection(listings, id);
		
		if(blockCount <= 1)
			return;
		
		createPagers(this, blockCount, id - 1);
	});
});


// Creates both the top and bottom "pager"
function createPagers(listingsDiv, blockCount, selected) {
	$('div.topPager', listingsDiv).remove();
	$('div.bottomPager', listingsDiv).remove();
	
	var topPager = createPager(
		'topPager', 	//elementID
		blockCount, 	//blockCount
		selected,		//selected
		true			//setName
	);
		
	var bottomPager = createPager(
		'bottomPager', 	//elementID
		blockCount, 	//blockCount
		selected,		//selected
		false			//setName
	);
			
	$(listingsDiv).prepend(topPager);
	$(listingsDiv).append(bottomPager);
}

// Creates a pager (Page: 1 2 3 4)
function createPager(elementID, blockCount, selected, setName) {
	var pager = document.createElement('div');
	pager.className = 'pager ' + elementID;
	pager.appendChild(document.createElement('span'))
		.appendChild(document.createTextNode('Page: '));
	
	var selectedInt = parseInt(selected); 
	
	// Create prev link
	var prev = $(pager.appendChild(document.createElement('a')));
	prev.append(document.createTextNode('<'));
	prev.addClass('previous');
	if(selectedInt > 0) {
		prev.attr('href', '#' + selectedInt);
		prev.bind('click', pagerClick);
	} else {
		prev.addClass('selected');
	}
	pager.appendChild(document.createTextNode(' '));
	
	// Create pages
	for(x = 0; x < blockCount; x++) {
		var id = x + 1;
		var link = $(pager.appendChild(document.createElement('a')));
		link.append(document.createTextNode((id)));
		if(x != selected) {
			link.attr('href', "#" + id);
			link.bind('click', pagerClick);
		} else {
			link.addClass('selected');
		}
		if(setName) {
			link.attr('name', id);
			link.get(0).id = id; // Required for IE.
		}
		pager.appendChild(document.createTextNode(' '));
	}
	
	// Create next link
	var next = $(pager.appendChild(document.createElement('a')));
	next.append(document.createTextNode('>'));
	next.addClass('next');
	if(selectedInt < blockCount - 1) {
		next.attr('href', '#' + (selectedInt + 2));
		next.bind('click', pagerClick);
	} else {
		next.addClass('selected');
	}
	
	
	return pager;
}

// Shows blocks that are on the right "page" (selectedBlock),
// hides all others.  Returns the number of "pages" found.
function showBlockSelection(listings, selectedBlock) {
	var blockCount = 0;
	var m = listings.length;
	var listing;
	
	for(var x=0; x<m; x++) {
		if((x % 5) == 0) {
			blockCount++;
		}
		listing = $(listings[x]);
		if(blockCount == selectedBlock) {
			listing.css({display:'block'});
		} else {
			listing.css({display:'none'});
		}
	}
	
	return blockCount;
}

// Fired when a number in a "pager" is clicked.
// Displays the appropriate page and adjusts the pagers.
function pagerClick() {
	var parent = this.parentNode;
	while(parent != null && parent.className != 'listings')
		parent = parent.parentNode;
	if(parent == null)
		return false;
	
	var href = this.getAttribute('href');
	var selectedBlock = href.substring(1);
	
	var listings = $("div.listing", parent).get();
	var blockCount = showBlockSelection(listings, selectedBlock);
	
	createPagers($(parent), blockCount, selectedBlock - 1);
	
	return true;
}
