/*

This function checks for the existence of an element with the ID 'articleList'.
If this element exists, the readArticle behavior is added to any links it contains.

This function is triggered when the page loads.
The addLoadEvent.js file is required for this.

*/

var currentArticleId;

addLoadEvent(addArticleEvents);

function addArticleEvents() {
	if (!document.getElementById) return false;
	if (!document.getElementById('articleList')) return false;
	var menu = document.getElementById('articleList');
	var items = menu.getElementsByTagName('a');
	for (var i=0;i<items.length;i++) {
		items[i].onclick = function() {
			return readArticle(this);
		}
		items[i].onkeypress = items[i].onclick;
	}
}


function readArticle(input) {
    if (!document.getElementById) return true;
    
    if (input.getAttribute("class") == "active") {
        return false;
    }
    
    input.setAttribute("class", "sel");
    var url = "getArticle.php" + input.getAttribute('href');
    loadXMLDoc(url);
    return false;
}


function showArticle(text) {
    var parent = document.getElementById("tissue");
    var articleNode = document.getElementById("article");
    
    parent.removeChild(articleNode);
    
    var newNode = document.createElement("div");
    newNode.id = "article";
    newNode.innerHTML = text;
    
    parent.appendChild(newNode);
    
    tweakCurrentArticle();
}


function tweakCurrentArticle() {
    if (!document.getElementById("articleList")) return false;
    var articleList = document.getElementById("articleList");
    var listItems = articleList.getElementsByTagName("li");
    var listItem;
    var itemArticleId;
    for (var i=0;i<listItems.length;i++) {
        listItem = listItems[i];
        switch (listItem.firstChild.getAttribute("class")) {
            case "active":
                listItem.firstChild.removeAttribute("class");
                break;
            case "sel":
                listItem.firstChild.setAttribute("class","active");
                break;
        }
    }
}