//requires sarissa

var 
	articles,
	totalArticles,
	totalPages,
	currentPage = 1,
	articlesPerPage = 10,
	req;

function loadArchive()
{
	var	req = new XMLHttpRequest();
	req.open("POST", "http://www.australiandiamondcompany.com.au/_dev/_api.asp?article", false);
	req.setRequestHeader("Content-Type", "text/xml");
	req.send('<?xml version="1.0" encoding="UTF-8"?><call><caller><user/></caller><args><article><feature_num_lt>1</feature_num_lt></article></args></call>');
	var response = new DOMParser().parseFromString(req.responseText, "application/xml");
	articles = response.getElementsByTagName('article');
	totalArticles = articles.length;
	totalPages = Math.ceil(totalArticles / articlesPerPage);
}

function renderArchiveList(elem)
{
	var list = document.createElement("ul");
	var currentArticle = ((currentPage - 1) * articlesPerPage) + 1;
	var articleCount = Math.min(articlesPerPage, totalArticles - ((currentPage - 1) * 10));
	
	for(var i = 0; i < articleCount; i++)
	{
		var li = document.createElement('li');
		var a = document.createElement('a');
		a.className = 'archiveLink';
		a.href = getArchiveLinkURL(currentArticle);
		a.appendChild(document.createTextNode(getArchiveLinkText(currentArticle)));
		li.appendChild(a);
		list.appendChild(li);
		currentArticle++;
	}
	if(typeof elem.innerHTML != "undefined")
	{
		elem.innerHTML = list.innerHTML;
	}
	else
	{
		return list.innerHTML;
	}
}

function renderArchivePagingControl(elem)
{
	var control = document.createElement('div');
	if(currentPage > 1)
	{
		var a = document.createElement('a');
		a.className = 'archivePagingLink';
		a.appendChild(document.createTextNode('<'));
		a.href='javascript:gotoArchivePage(' + (0 + currentPage - 1) + ');';
		control.appendChild(a);
	}
	else
	{
		control.appendChild(document.createTextNode(' '));
	}
	control.appendChild(document.createTextNode(' Page ' + currentPage + ' '));
	if(currentPage < totalPages)
	{
		var a = document.createElement('a');
		a.className = 'archivePagingLink';
		a.appendChild(document.createTextNode('>'));
		a.href='javascript:gotoArchivePage(' + (1 + currentPage) + ');';
		control.appendChild(a);
	}
	else
	{
		control.appendChild(document.createTextNode(' '));
	}
	if(elem.innerHTML)
	{
		elem.innerHTML = control.innerHTML;
	}
	else
	{
		return control.innerHTML;
	}
}

function renderArchive()
{
	renderArchiveList(document.getElementById('archiveList'));
	renderArchivePagingControl(document.getElementById('archivePagingControl'));
}

function gotoArchivePage(num)
{
	currentPage = Math.max(1, Math.min(totalPages, num));
	renderArchive();
}

function getArchiveLinkURL(num)
{
	var article = articles[num - 1];
	return 'news_article.asp?id=' + article.getElementsByTagName('article_id')[0].firstChild.nodeValue;
}

function getArchiveLinkText(num)
{
	var article = articles[num - 1];
	return article.getElementsByTagName('title')[0].firstChild.nodeValue;
}

window.onload = function()
{
	loadArchive();
	renderArchive();
}