var permalink = false;

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

//	list_stories('date','desc',0);

	permalink_mode();

	populate_sidebar();

	topnav_init();
}

window.onload = init;


function permalink_mode() {
	if (permalink) {
//		$('STlistmaster').setStyle('display','none');
		open_fullstory(requestedstory);
		requestedstory = -1;
		permalink = false;
	}
}


function list_stories(sortby,sortdir,pagestart) {
	// request main story list
	if (permalink) {
		pl = 'yes';
	} else {
		pl = 'no';
	}
	new Ajax('/ajax/retrieve_storylist.php', {
		postBody: Object.toQueryString({sortby: sortby, sortdir: sortdir, pagestart: pagestart, section: 2, permalink: pl}),
		update: 'storymaincontent',
		evalScripts: false,
		onComplete: function(){ 
			if (permalink) {
				open_fullstory(requestedstory);
				requestedstory = -1;
				permalink = false;
			} else {
				$('STlistmaster').setStyle('display', 'block');		// show full stories
			}
		}
	}).request();
}


function populate_sidebar() {
	// request content for sidebar modules
	new Ajax('/ajax/retrieve_sidebar.php', {
		postBody: Object.toQueryString({modules: 'story_rated,story_discussed,story_viewed,author_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 open_fullstory(sid) {
	if (!full_story_open) {
		$('STlistmaster').setStyle('display', 'none');		// hide existing stories

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

		full_story_open = true;

		// request full story
		new Ajax('/ajax/retrieve_story.php', {
			postBody: Object.toQueryString({story: sid, section: 2}),
			update: 'fullstory',
			evalScripts: false,
			onComplete: function(){
				soundstatus = $('soundplayer').getStyle('z-index');
				if (soundstatus != '-1') {
					var so = new SWFObject('/flash/audioplayer.swf','mpl','105','48','8');
					so.addParam('wmode','transparent');
					so.addParam('flashvars','soundurl=/audiodb/'+soundstatus+'.mp3');
					so.write('soundplayer');
				}
			}
		}).request();

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

	}

}


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


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


// we catch opening of the lighttable to scroll window to the top
function show_full_photo_scroll(pid) {
	new Fx.Scroll(window).toTop();
	show_full_photo(pid);
}



