/**  
 * @fileoverview This file contains functions and variables for the stories panel.
 * @author Mark Hendrickson <mhendric@bowdoin.edu>
 * @version 1.0
 */

// Directory that stores the stories images
var stories_directory = 'images/stories/';

var active_story = 1;
var current_story;
var current_story_story_url;
var next_story;
var next_story_url;
var showing_story = false;
var prepping_next_story = false;

/**
 * Initialize the stories rotation.
 * @param {Integer} story Starting story ID
 */
function initStories(story)
{
	current_story = story;
	prepNextStory();
}

/**
 * Prepare the next story for display when story changes.
 */
function prepNextStory()
{	
	prepping_next_story = true;
	var ajax = initAjax();
	ajax.open('GET','scripts/get_story.php?current_story='+current_story+'&direction=forward',true);
	ajax.onreadystatechange = function() { prepNextStoryCallback(ajax) };
	ajax.send(null);
}

/**
 * Called exclusively by prepNextStory(); continues the operations of that function.
 * @param {Object} ajax Ajax object
 * @see #prepNextStory
 */
function prepNextStoryCallback(ajax)
{
	if(ajax.readyState == 4)
	{
		if(ajax.status == 200)
		{
			var response = ajax.responseXML;
			var story = response.getElementsByTagName('story')[0];
			var id = elementText(story.getElementsByTagName('id')[0]);
			var text = elementText(story.getElementsByTagName('text')[0]);
			var header = elementText(story.getElementsByTagName('header')[0]);
			var url = elementText(story.getElementsByTagName('url')[0]);
			var filename = elementText(story.getElementsByTagName('filename')[0]);

			if(active_story == 1)
				inactive_story = 2;
			else
				inactive_story = 1;

			var text_div = $('story_text_'+inactive_story);
			text_div.innerHTML = text;

			var header_div = $('story_header_'+inactive_story);
			header_div.innerHTML = header;

			var image = $('story_image_'+inactive_story);
			image.src = stories_directory+filename;

			next_story = id;
			next_story_url = url;

			prepping_forward_story = false;
		}
	}
}

/**
 * Show the next story in the rotation.
 * @param {Boolean} override Indicates whether to override the precondition that another call of the function must have completed
 */
function showStory(override)
{
	if(!showing_story || override)
	{
		showing_story = true;

		if(prepping_forward_story)
			setTimeout("showStory(true)",1000);
		else
		{
			current_story_url = next_story_url;

			var learn_more = $('stories_learn_more_a');
			learn_more.href = current_story_url;

			var stories_container = $('stories_container');
			stories_container.onclick = function (url) { window.location = current_story_url; };

			showStoryProcess();
		}
	}
}

/**
 * Called exclusively by showStory(); continues the operations of that function by fading new story into view.
 * @param {Integer} step Step in the fade-in process
 */
function showStoryProcess(step)
{
	if(!step)
		step = 1;
	
	if(active_story == 1)
		var inactive_story = 2;
	else
		var inactive_story = 1;

	var active_story_div = $('story_'+active_story);
	var inactive_story_div = $('story_'+inactive_story);
	
	if(step <= 20)
	{
		if(step == 1)
			inactive_story_div.style.display = 'block';

		if(current_browser == 'Explorer' && current_os == 'Windows')
			showStoryProcess(21);
		else
		{
			active_story_div.style.opacity = (20-step)/20;
			inactive_story_div.style.opacity = step/20;

			setTimeout('showStoryProcess('+(step+1)+')',35);
		}
	}
	else
	{		
		active_story_div.style.display = 'none';

		if(active_story == 1)
			active_story = 2;
		else
			active_story = 1;
		
		current_story = next_story;
	
		showing_story = false;
		prepNextStory();
	}
}