/* 
Michael's Rolling Archives 
originally by Michael Heilemann - http://binarybonsai.com
*/

function rollArchive(direction, section, category, pagecount, items_per_page) {

	if (direction == 1 || direction == -1) {
		// Update the variables
		position = position + (direction*offset);
		pagenumber += direction;
	} else if (direction == 'home') {
		// Reset the variables
		pagenumber = 1; 	// Just an indicator for the page counter
		offset = 1; 		// Posts to move per increment + starting position
		position = 1; 		// Change this according to your Reading Options Blog Pages
	}

	$(section+'rollingcontent').innerHTML = '<div class="rollloading"></div>'; // Insert Loading Spinner
	$(section+'rollpages').innerHTML = +pagenumber+' / '+pagecount; // Update Page Count

	// Make sure we don't allow people to go below page 1
	if (pagenumber < '2') {
		$(section+'rollnext').className = 'inactive';
		$(section+'rollnext').onclick = null;
	} else {
		$(section+'rollnext').className = null;
		$(section+'rollnext').onclick = function() { rollArchive(-1, section, category, pagecount, items_per_page); };
	}

	// Make sure we don't allow people to go above pagecount.
	if (pagenumber >= pagecount) {
		$(section+'rollprevious').className = 'inactive';
		$(section+'rollprevious').onclick = null;
	} else {
		$(section+'rollprevious').className = null;
		$(section+'rollprevious').onclick = function() { rollArchive(1, section, category, pagecount, items_per_page); };
	}

	// This part requires Prototype (http://prototype.conio.net/)
	// parameters: 'category_name='+category+'&paged='+position+'&items_per_page='+items_per_page,

	new Ajax.Updater(section+'rollingcontent', 'http://noscope.com/wp-content/themes/pannotia/meta/ajaxarchives.php', {
	method: 'get',
	parameters: 'items_per_page='+items_per_page+'&category_name='+category+'&paged='+position,
	onFailure: rollError
	});
}

function rollError() {
	$(section+'rollingcontent').innerHTML = 'AJAX Archives error.';
	}

function initRollArchive(section, category, pagecount, items_per_page) {
	// Is only run onload.
	if (pagecount > 0) {
		// There are posts
		$(section+'rollprevious').onclick = function() { rollArchive(1, section, category, pagecount, items_per_page); };

		rollArchive('home', section, category, pagecount, items_per_page);
	} else {
		// No archived posts. Turn everything off.
		$(section+'rollingcontent').innerHTML = 'There are no archived posts.';

		$(section+'rollprevious').className = 'inactive';
		$(section+'rollprevious').onclick = null;
		$(section+'rollnext').className = 'inactive';
		$(section+'rollnext').onclick = null;
	}
}

// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog, added by Safirul Alredha.
function addLoadEvent(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function'){
        window.onload = func;
        } else {
                window.onload = function(){
                oldonload();
                func();
                }
        }
}

//addLoadEvent(initRollArchive);  // run initRollArchive onLoad