var permalink = false;

function init() {

	if (requestedauthor != -1) {									// person permalink through URL found
		permalink = true;											// set flag so we can control load process
	}

	list_people('rating','desc',0);

	populate_sidebar();

	topnav_init();
}

window.onload = init;



function list_people(sortby,sortdir,pagestart) {
	// request main people list
	if (permalink) {
		pl = 'yes';
	} else {
		pl = 'no';
	}
	new Ajax('/ajax/retrieve_peoplelist.php', {
		postBody: Object.toQueryString({sortby: sortby, sortdir: sortdir, pagestart: pagestart, permalink: pl}),
		update: 'peoplemaincontent',
		evalScripts: false,
		onComplete: function(){ 
			if (permalink) {
				showpersoninfo(requestedauthor);
				requestedauthor = -1;
				permalink = false;
			} else {
				$('PLlistmaster').setStyle('display', 'block');		// show full people list
			}
		}
	}).request();
}


function populate_sidebar() {
	// request content for sidebar modules
	new Ajax('/ajax/retrieve_sidebar.php', {
		postBody: Object.toQueryString({modules: 'author_rated,author_discussed,author_busy,story_rated,place_popular'}),
		update: 'sidebar',
		evalScripts: false,
		onComplete: function(){ nothing='examplecode'; }	// todo: do we need a function call on AJAX success?
	}).request();
}


var full_story_open = false;
var full_story_open_id = -1;

// called from links around each story excerpt in the big panel
function showpersoninfo(pid) {
	if (!full_story_open) {
		$('PLlistmaster').setStyle('display', 'none');		// hide existing stories

		// set up the big edit/view panel
		var div = new Element('div');
		div.setProperty('id', 'fullstory');
		div.injectInside('peoplemaincontent');

		full_story_open = true;

		// request full story
		new Ajax('/ajax/retrieve_person.php', {
			postBody: Object.toQueryString({person: pid}),
			update: 'fullstory',
			evalScripts: false,
			onComplete: function(){ nothing='examplecode'; }	// todo: do we need a function call on AJAX success?
		}).request();

		full_story_open_id = pid;								// remember id to allow for scroll down after full story close
		new Fx.Scroll(window).toTop();

	}

}


function clear_fullstory() {
	pb = document.getElementById('peoplemaincontent');
	olddiv = document.getElementById('fullstory');
	pb.removeChild(olddiv);
}


function fullstory_back() {
	clear_fullstory();
	$('PLlistmaster').setStyle('display', 'block');			// show list of people
	new Fx.Scroll(window).toElement('bigstory'+full_story_open_id);
	full_story_open = false;
	full_story_open_id = -1;
}


