// JavaScript Document

var isIE = false;
var req;

function loadXMLDoc(url) {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
			isIE = true;
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

function processReqChange() 
{
    // only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here...
			response  = req.responseXML.documentElement;
			buildRecentLinksList(response.getElementsByTagName('channel')[0]);
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}

function buildRecentLinksList(result)
{
	var recentDiv = document.getElementById("recentLinksList");
	var recentHeading = document.createElement("h2");
	var listElement = document.createElement("ul");
	var recentLinks = result.getElementsByTagName("item");
	// loop through links and
	// add each link to the recent links list
	for (var i = 0; i < recentLinks.length; i++)
	{
		listElement.appendChild(buildRecentLinkItem(recentLinks[i]));
	}
	
	clearRecentLinksList(recentDiv);
	recentHeading.innerHTML = "-- Recently on LiNKY --<a href='http://www.mylinky.com/rss.php' alt='feed' title='rss feed'><img src='/images/feed.png' /></a>";
	recentDiv.appendChild(recentHeading);
	recentDiv.appendChild(listElement);
}

function clearRecentLinksList(list)
{
	list.innerHTML = "";
}

function buildRecentLinkItem(content)
{
	var listItem, aTag, title, user, timestamp;
	listItem = document.createElement("li");
	aTag = document.createElement("a");
	title = document.createTextNode(getElementTextNS("", "title", content, 0));
	separator1 = document.createTextNode(" - ");
	separator2 = document.createTextNode(" - ");
	user = document.createElement("span");
	timestamp = document.createElement("span");
	user.appendChild(document.createTextNode(getElementTextNS("dc", "creator", content, 0)));
	timestamp.appendChild(document.createTextNode(getElementTextNS("dc", "date", content, 0)));
	aTag.href = getElementTextNS("", "link", content, 0);
	aTag.target = "_blank";
	user.className = "username";
	timestamp.className = "timestamp";
	aTag.appendChild(title);
	listItem.appendChild(aTag);
	listItem.appendChild(separator1);
	listItem.appendChild(user);
	listItem.appendChild(separator2);
	listItem.appendChild(timestamp);
	
	return listItem;
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}









