//Modified version of the javascript for Rowan's homepage

function addLoadEvent(func) //allows multiple events to occur when page is loaded
{
	var oldonload = window.onload;
	if(typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

function createAjaxRequest()
{
	var request;
	
	try
	{
		request = new XMLHttpRequest();
	} 
	catch(trymicrosoft)
	{
		try
		{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(othermicrosoft)
		{
			try
			{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(failed) 
			{
				request = false;
			}
		 }
	}
	
	return request;
}

var storyRotation = false;
var stories = new Array();
var request = false;

//If the "noajax" URL variable doesn't exist
if(window.location.search.search("noajax") == -1)
{
	request = createAjaxRequest();
}

if(request)
{
	addLoadEvent(setThumbnailLinks);
}

//Disable all AJAX-related code
function resetPage()
{
	if(storyRotation)
	{
		clearInterval(storyRotation);
	}
	
	if(document.getElementById('thumbnails'))
	{
		var thumbs = document.getElementById('thumbnails').getElementsByTagName('a');
		for(var i = 0; i < thumbs.length; i++)
		{
			thumbs[i].onclick = '';
			thumbs[i].href += '&noajax=true';
		}
	}
}

function setThumbnailLinks()
{
	if(document.getElementById('thumbnails'))
	{
		var thumbs = document.getElementById('thumbnails').getElementsByTagName('a');
		
		for(var i = 0; i < thumbs.length; i++)
		{
			var storyID = thumbs[i].href;
			storyID = storyID.replace(/.*?id=/,""); //Remove everything before the ID number
			storyID = storyID.replace(/&.*/,""); //Remove everything after the ID number
			setThumb(storyID, thumbs[i]);
		}
		storyRotation = setInterval(setRotation,6000);
	}
}

function setThumb(storyID, thumb)
{
	getStory(storyID);
	thumb.onclick = function() {
		if(storyRotation)
		{
			clearInterval(storyRotation);
		}
		changeStory(storyID, false); 	
		return false; 
	};
}

function setThumbClass(id)
{
	var thumbs = document.getElementById('thumbnails').getElementsByTagName('a');
		
	for(var i = 0; i < thumbs.length; i++)
	{
		thumbs[i].getElementsByTagName('img')[0].className = "";
		var storyID = thumbs[i].href;
		storyID = storyID.replace(/.*?id=/,""); //Remove everything before the ID number
		storyID = storyID.replace(/&.*/,""); //Remove everything after the ID number
		if(storyID == id)
		{
			thumbs[i].getElementsByTagName('img')[0].className = "selectedthumb";
		}
	}
}

//Use Ajax to get a story
function getStory(storyID)
{
	if(!stories['story' + storyID])
	{
		var url = "get_stories.php?id=" + storyID;
		
		var storyrequest = createAjaxRequest();
		storyrequest.open("GET", url, true);
		
		storyrequest.onreadystatechange = function()
		{
			if(storyrequest.readyState == 4)
			{
				if(storyrequest.status == 200)
				{
					var story = storyrequest.responseText;

					if(story)
					{						
						story = eval("(" + story + ")");
						
						stories['story' + storyID] = story.title;
						
						var newimg = document.createElement('img');
						newimg.src = 'images/articles/' + story.image;
						newimg.alt = '';
						newimg.id = storyID + 'image';
						document.getElementById('storyimages').appendChild(newimg); //Load image in hidden div so that the image is already in browser's cache when it's needed by the story rotation
					}
				}
			}
		};
		
		storyrequest.send(null);
	}
}

//Retrieve the new story and replace the current story
function changeStory(storyID, fade)
{
	if(stories['story' + storyID])
	{
		var fadeimage = new Fx.Style($('mainImage'),'opacity', {duration:1000});
		var fadetitle = new Fx.Style($('mainTitle'),'opacity', {duration:1000});
	
		if(fade) //fade out
		{
			fadeimage.start(1,0).chain(function () {this.start(0,1);});
			fadetitle.start(1,0).chain(function () {replaceContent(storyID); this.start(0,1); setThumbClass(storyID);});
		}
		else
		{
			replaceContent(storyID);
			setThumbClass(storyID);
		}
	}
}

function replaceContent(ID)
{
	//Set title
	document.getElementById('mainTitle').childNodes[0].nodeValue = stories['story' + ID];
	//Set image
	document.getElementById('mainImage').getElementsByTagName('a')[0].href = 'article.php?id=' + ID;
	document.getElementById('mainImage').getElementsByTagName('img')[0].src = document.getElementById(ID + 'image').src;
	document.getElementById('mainImage').getElementsByTagName('img')[0].alt = stories['story' + ID];
	//Set link
	document.getElementById('mainLearnMore').getElementsByTagName('a')[0].href = 'article.php?id=' + ID;
}

function setRotation()
{
	//get current story ID.
	var id = document.getElementById('mainImage').getElementsByTagName('a')[0].href;
	id = id.replace(/.*?id=/,""); //Remove everything before the ID number

	//get the next story ID.
	var thumbs = document.getElementById('thumbnails').getElementsByTagName('a');
		
	for(var i = 0; i < thumbs.length; i++)
	{
		var storyID = thumbs[i].href;
		storyID = storyID.replace(/.*?id=/,""); //Remove everything before the ID number
		storyID = storyID.replace(/&.*/,""); //Remove everything after the ID number
		
		if(storyID == id)
		{
			if(i == thumbs.length - 1)
			{
				var nextStory = thumbs[0].href;
			}
			else
			{
				var nextStory = thumbs[i+1].href;
			}
			nextStory = nextStory.replace(/.*?id=/,"");
			nextStory = nextStory.replace(/&.*/,"");
		}
	}

	changeStory(nextStory, true);	
}