var currentPage = 1;

function Keen2Know_initListings() {
	$$('.listings_next').each(function(element) {
		Event.observe(element, 'click', Keen2Know_showNextListingsPage)
	});
	
	$$('.listings_previous').each(function(element) {
		Event.observe(element, 'click', Keen2Know_showPreviousListingsPage)
	});

	$('listings-dd').observe('change', Keen2Know_showListings);
	$('listings-available').observe('change', Keen2Know_showListings);

	Keen2Know_showListings();
}

function Keen2Know_showListings() {


	if (null != $('previous-page-link')) {
		if (1 == currentPage) { 
			$('previous-page-link').hide();
			$('currentPage').hide();
		} else {
			$('previous-page-link').show();
			$('currentPage').update('Page: ' + currentPage);
			$('currentPage').show()
		}
	}
	
	var catVal = $('categoryId');

	if (null == catVal)
		var categoryId = 195;
	else
		var categoryId = catVal.value;

	var sortVal = $('listings-dd');

	if (null == sortVal)
		var currentSort = 'points';
	else
		var currentSort = sortVal.value

	var availableFilterVal = $('listings-available');

	if (null == availableFilterVal)
		var currentAvailableFilter = true;
	else
		var currentAvailableFilter = availableFilterVal.checked
	
	$$('#listings').each(function(element) {
		element.update('<div id="listing-loader"><img src="/img/ajax-loader.gif" /></div>')
	});

	$$('#listings').each(function(element) {
		var aU = new Ajax.Updater(
			element, 
			'/listings/?page=' + currentPage + '&sort=' + currentSort + '&categoryId=' + categoryId + '&availableFilter=' + currentAvailableFilter, 
			{ method: 'get', evalScripts: true, onComplete: Keen2Know_setupFavoriteLinks }
		);
	});
}

function Keen2Know_showNextListingsPage(event) {
	currentPage++;
	Keen2Know_showListings();
	if (event) Event.stop(event);
}

function Keen2Know_showPreviousListingsPage(event) {
	currentPage--;
	Keen2Know_showListings();
	if (event) Event.stop(event);
}

Event.observe(window, 'load', Keen2Know_initListings);	

