/*
Create an ArticleTypeGeneric namespace.
*/
var ATG = new Object();

/*
Initialise the image pager.
*/
ATG.Initialise = function (indexControlId, imageCount, maxDisplaySize)
{
	// Find the input control specifed and store a reference to it, we use it to store the index of
	// the image currently being viewed
	ATG.indexControl = document.getElementById(indexControlId);
	
	// Store the number of images available for viewing and the number we can display at any one time
	ATG.imageCount = imageCount;
	ATG.maxDisplaySize = maxDisplaySize;
	
	// Display the pager
	var pager = document.getElementById('pager');
	pager.className = 'pager';
	
	ATG.UpdatePageLinks();
}

/*
Get the index of the image currently being viewed.
*/
ATG.GetCurrentIndex = function ()
{
	return parseInt(ATG.indexControl.value);
}

/*
Set the index of the image currently being viewied.
*/
ATG.SetCurrentIndex = function (index)
{
	ATG.indexControl.value = index;
}

/*
Update the links to each page so that they look correct.
*/
ATG.UpdatePageLinks = function ()
{
	var current = ATG.GetCurrentIndex();

	// Calculate how many pages we will display
	var count = Math.min(ATG.maxDisplaySize, ATG.imageCount);

	// Calculate the maximum size each half can be
	var half = (ATG.maxDisplaySize - 1) / 2;

	// Calculate the first page we will display
	var first = Math.max(0, Math.min(current - half, ATG.imageCount - count));

	// Calculate the last page we will display
	var last = first + count - 1;

	// Update each of the pages
	for(var i = 0; i < ATG.imageCount; i++)
	{
		var page = document.getElementById('page' + i);
		
		if(i == current)
		{
			page.className = "current";
		}
		else if(i < first || i > last)
		{
			page.className = "disabled";
		}
		else
		{
			page.className = "";
		}
	}

	// Show and hide the previous button
	var prevPage = document.getElementById('prevPage');
	prevPage.className = (current > 0) ? "" : "hidden";
	
	// Show and hide the next button
	var nextPage = document.getElementById('nextPage');
	nextPage.className = (current < (ATG.imageCount - 1)) ? "" : "hidden";
}

/*
Update the images so that only the current one is displayed.
*/
ATG.UpdateImages = function()
{
	var current = ATG.GetCurrentIndex();
	
	// Update each of the image, caption and spec sets
	for(var i = 0; i < ATG.imageCount; i++)
	{
		// Try to locate the controls, some may not exist
		var hero = document.getElementById('hero' + i);
		var caption = document.getElementById('caption' + i);
		var spec = document.getElementById('spec' + i);
		
		if(i == current)
		{
			// Display the controls
			if(hero != undefined) hero.className = "";
			if(caption != undefined) caption.className = "caption";
			if(spec != undefined) spec.className = "spec";
		}
		else
		{
			// Hide the controls
			if(hero != undefined) hero.className = "hidden";
			if(caption != undefined) caption.className = "caption hidden";
			if(spec != undefined) spec.className = "spec hidden";
		}
	}
}

/*
Sets the currently displayed page to the given value.
*/
ATG.SetPage = function (index)
{
	ATG.SetCurrentIndex(index);
	
	ATG.UpdatePageLinks();
	ATG.UpdateImages();
}

/*
Sets the currently displayed page to the previous one.
*/
ATG.PreviousPage = function ()
{
	var current = ATG.GetCurrentIndex();

	if(current > 0)
	{
		ATG.SetCurrentIndex(current - 1);
	}
	
	ATG.UpdatePageLinks();
	ATG.UpdateImages();
}

/*
Sets the currently displayed page to the next one.
*/
ATG.NextPage = function ()
{
	var current = ATG.GetCurrentIndex();

	if(current < (ATG.imageCount - 1))
	{
		ATG.SetCurrentIndex(current + 1);
	}
	
	ATG.UpdatePageLinks();
	ATG.UpdateImages();
}

/*
Displays the full article body.
*/
ATG.ShowBody = function ()
{
	var body = document.getElementById('body');
	var readFull = document.getElementById('readFull');
	
	body.className = "";
	readFull.className = "hidden";
}

