// Update link classes based on selection; populate article list

function showArticlesBySubject()
{
	document.getElementById('bySubject').className = "methodOff";
	document.getElementById('byAuthor').className = "methodOn";
	document.getElementById('byIssue').className = "methodOn";
	showArticles("subject", 0);
}

// Update link classes based on selection; populate article list
function showArticlesByAuthor()
{
	document.getElementById('bySubject').className = "methodOn";
	document.getElementById('byAuthor').className = "methodOff";
	document.getElementById('byIssue').className = "methodOn";
	showArticles("author", 1);
}

// Update link classes based on selection; populate article list
function showArticlesByIssue()
{
	document.getElementById('bySubject').className = "methodOn";
	document.getElementById('byAuthor').className = "methodOn";
	document.getElementById('byIssue').className = "methodOff";
	showArticles("issue", 2);
}

// Populate article list
function showArticles(t, i)
{
	var o = document.getElementById("articleIndexContainer");
	
	// Record type of data being shown
	o.entryType = t;
	o.entryTypeId = i;

	// Build from cache
	if(o.cache && o.cache[o.entryTypeId])
	{
		// Build entry
		clearElement(o);
		var result = o.cache[o.entryTypeId];
		for(var i = 0; i < result.length; i++)
			addEntry(o, result[i][0], result[i][1]);
		return;
	}
	
	var url = useAsynchronousCalls
			? interfaceURL+"?oc_id="+oc_id+"&type="+t
			: interfaceURL+"?oc_id="+oc_id+"&type="+o.parentNode.entryType+"&id="+o.entryId
	;
	new Ajax.Request(url, {
		method : 'get',
		asynchronous : useAsynchronousCalls,
		onSuccess : function(transport) {
			parseArticlesResult(o, transport.responseText);
		},
		onFailure : function(transport) {
			alert("Unable to load data.");
		}
	});
	if(useAsynchronousCalls)
	{
		setElementText(o, "Loading...");
	} else {
		//parseArticlesResult(o, httpRequest.responseText);// httpRequest doesn't exist.  Don't care; won't use.
	}
}

// Parse response
function parseArticlesResult(o, t)
{
	var result = String(t).evalJSON(!useEval);

	if(cacheEntries)
	{
		if(o.cache == undefined)
			o.cache = new Array(3);
		o.cache[o.entryTypeId] = result;
	}

	// Build entry
	clearElement(o);
	for(var i = 0; i < result.length; i++)
		addEntry(o, result[i][0], result[i][1]);
}

// Add entry to article index container
function addEntry(o, i, t)
{
	var outerDiv = document.createElement("div");
	var titleDiv = document.createElement("div");
	var contentDiv = document.createElement("div");
	var contentTable = document.createElement("table");
	var contentTbody = document.createElement("tbody");
	var plusMinusSpan = document.createElement("span");

	setElementText(plusMinusSpan, "+");
	plusMinusSpan.id = "plusMinusSpan"+i;
	
	titleDiv.appendChild(plusMinusSpan);
//	if(i == 0)
//		t = "Unspecified "+o.entryType
	titleDiv.appendChild(document.createTextNode(t));
	titleDiv.className = "articleTitle";
	titleDiv.onclick = function() { toggleEntry(this) };
	
	contentTbody.id = "contentTable"+i;
	contentTbody.setAttribute("cellspacing", "2");
	contentTable.appendChild(contentTbody);
	contentDiv.appendChild(contentTable);
	contentDiv.className = "articleContent";
	
	outerDiv.appendChild(titleDiv);
	outerDiv.appendChild(contentDiv);

	outerDiv.id = "articleEntry"+i;
	outerDiv.entryId = i;
	outerDiv.isOpened = false;
	outerDiv.isLoaded = false;
	outerDiv.className = "articleEntry";
	outerDiv.style.height = "22px";
	outerDiv.onmouseover = function() { rollOverEntry(this) };
	outerDiv.onmouseout = function() { rollOutEntry(this) };
	
	o.appendChild(outerDiv);
}

// Load entry content
function loadEntry(o)
{
	if(o.isLoaded)
		return;
	
	
	var url = useAsynchronousCalls
			? interfaceURL+"?oc_id="+oc_id+"&type="+o.parentNode.entryType+"&id="+o.entryId
			: interfaceURL+"?oc_id="+oc_id+"&type="+o.parentNode.entryType+"&id="+o.entryId
	;
	new Ajax.Request(url, {
		method : 'get',
		asynchronous : useAsynchronousCalls,
		onSuccess : function(transport) {
			parseEntryResult(o, transport.responseText);
		},
		onFailure : function(transport) {
			alert("Unable to load data.");
		}
	});
	if(useAsynchronousCalls)
	{
		setAsLoading(o);
	} else {
		//parseArticlesResult(o, httpRequest.responseText);// httpRequest doesn't exist.  Don't care; won't use.
	}
}

// Parse httpRequest.responseText
function parseEntryResult(o, r)
{
	var t = document.getElementById("contentTable"+o.entryId);
	var result = String(r).evalJSON(!useEval);

	// Build entry
	clearElement(t);
	addEntryTitle(t);
	for(var i = 0; i < result.length; i++)
		addEntryRow(t, result[i][0], result[i][1], result[i][2], result[i][3], result[i][4]);
				
	o.isLoaded = true;
}

// Set "loading" text in cell
function setAsLoading(o)
{
	var contentCell;
	var contentRow = document.createElement("tr");
	var t = document.getElementById("contentTable"+o.entryId);
	
	clearElement(t);

	contentCell = document.createElement("th");
	setElementText(contentCell, "Loading...");
	contentRow.appendChild(contentCell);

	contentRow.setAttribute("colspan", "4");
	t.appendChild(contentRow);
}

// Add headings to table
function addEntryTitle(o)
{
	var contentCell;
	var contentRow = document.createElement("tr");
	
	contentCell = document.createElement("th");
	setElementText(contentCell, "Title");
	contentCell.style.width = "40%";
	contentRow.appendChild(contentCell);

	contentCell = document.createElement("th");
	setElementText(contentCell, "Subject");
	contentCell.style.width = "23%";
	contentRow.appendChild(contentCell);

	contentCell = document.createElement("th");
	setElementText(contentCell, "Author(s)");
	contentCell.style.width = "16%";
	contentRow.appendChild(contentCell);

	contentCell = document.createElement("th");
	setElementText(contentCell, "Issue");
	contentCell.style.width = "16%";
	contentRow.appendChild(contentCell);

	o.appendChild(contentRow);
}

// Add an article row to the content table
function addEntryRow(o, i, a, b, c, d)
{
	var row = document.createElement("tr");
	var cell, url;
	
	url = document.createElement("a");
	setElementText(url, a);
	url.setAttribute("title", "Click to see Details...");
	url.href = "#";
	url.articleId = i;
	url.articleTitle = a;
	url.articleSubject = b;
	url.articleAuthor = c;
	url.articleIssue = d;
	url.href = "article.html";
	url.onclick = function() {
		var w = window.open("","","width=400,height=300,resizable=yes,toolbar=no,directories=no,location=no,status=yes,menubar=no,scrollbars=no");
		w.document.open();
		w.document.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Article</title><link rel="stylesheet" type="text/css" href="'+stylesheetURL+'"></head><body><div class="articleDetails"><h1>'+this.articleTitle+'</h1><h2>'+this.articleAuthor+'</h2><p><span>Subject:</span> '+this.articleSubject+'<br /><span>Issue:</span> '+this.articleIssue+'</p></div></body></html>');
		w.document.close();
		return false;
	};
	cell = document.createElement("td");
	cell.appendChild(url);
	row.appendChild(cell);

	cell = document.createElement("td");
	setElementText(cell, b);
	row.appendChild(cell);

	cell = document.createElement("td");
	setElementText(cell, c);
	row.appendChild(cell);

	cell = document.createElement("td");
	setElementText(cell, d);
	row.appendChild(cell);

	o.appendChild(row);
}

// Toggle entry open or closed; load content if opening, destroy content if closing
function toggleEntry(o)
{
	// If the element is tweening open or closed, stop the tween
	if(o.parentNode.interval >= 0)
		clearInterval(o.parentNode.interval);
		
	if(o.parentNode.isOpened)
	{
		// Destroy content and set to "not loaded" if caching is disabled
		if(!cacheEntries)
		{
			clearElement(document.getElementById("contentTable"+o.parentNode.entryId));
			o.parentNode.isLoaded = false;
		}
		
		// Set to "not open"
		o.parentNode.isOpened = false;
		setElementText(document.getElementById("plusMinusSpan"+o.parentNode.entryId), "+");
		// Tween closed
		o.parentNode.interval = setInterval("tweenEntry('"+o.parentNode.id+"',"+entryClosedHeight+",'-')", 50);
	}
	else
	{
		// Start asynchronous load
		loadEntry(o.parentNode);
		
		// Set to "open"
		o.parentNode.isOpened = true;
		setElementText(document.getElementById("plusMinusSpan"+o.parentNode.entryId), "-");
		// Tween open
		o.parentNode.interval = setInterval("tweenEntry('"+o.parentNode.id+"',"+entryOpenedHeight+",'+')", 50);
	}
}


function tweenEntry(i, k, l)
{
	// Get element and current height
	var o = document.getElementById(i);
	var j = parseInt(o.style.height, 10);
	
	// Add or remove height
	if(l == "+")
		j += 20;
	else
		j -= 20;
	
	// Stop tween if the desired height is reached
	if((l == "+" && j >= k) || (l == "-" && j <= k))
	{
		j = k;
		clearInterval(o.interval);
		o.interval = -1;
	}
	
	// Set the height
	o.style.height = j+"px";
}

// Roll over entry
function rollOverEntry(o)
{
	o.className = "articleEntryOver";
}

// Roll out of entry
function rollOutEntry(o)
{
	o.className = "articleEntry";
}

// Remove all content and add text node
function setElementText(o, t)
{
	if(t == "")
		t = " ";
	var text = document.createTextNode(t);
	while(o.firstChild) o.removeChild(o.firstChild);
		o.appendChild(text);
}

// Remove all content
function clearElement(o)
{
	while(o.firstChild) o.removeChild(o.firstChild);
}
