// Frames buster
if(top.location != self.location){
	top.location = self.location;
}

// Check for AJAX support
if(!tmt.net.isSupported()){
	window.location.replace("no_ajax.htm");
}

// Fix image flickering in IE on CSS rollovers
try {
  document.execCommand("BackgroundImageCache", false, true);
} 
catch(err){}

var loadingTextNode = document.createTextNode("Loading, please wait...");

function loadFeed(feedUrl, category, showMessage){
	var contentDiv = document.getElementById("content");
	if(showMessage){
		contentDiv.replaceChild(loadingTextNode, contentDiv.firstChild);
	}
	
	tmt.net.httpRequest(feedUrl, 
							function(){
								var contentDOM = parseFeed(this.response.responseXML, category);
								contentDiv.replaceChild(contentDOM, contentDiv.firstChild);
							}
						);
}

function loadDoc(docUrl){
	var contentDiv = document.getElementById("content");
	tmt.net.httpRequest(docUrl, 
							function(){
								var contentDOM = parseDoc(this.response.responseXML);
								contentDiv.replaceChild(contentDOM, contentDiv.firstChild);
							}
						);
}

/*
Since pages will be served as application/xhtml+xml, 
we can't just serialize the XML files and set the innerHTML of the target div. 
Instead we traverse the XML's DOM and create a new XHTML tree out of its data
*/

function parseFeed(xmlDOM, category){
	var rootNode = document.createElement("div");
	var itemsNodes = xmlDOM.getElementsByTagName("item");
	var itemsCounter = 0;
	rootNode.setAttribute("id", "feedContent");
	for(var i=0; i<itemsNodes.length; i++){
    	var intemCat = itemsNodes[i].getElementsByTagName("category")[0].firstChild.nodeValue
		// Check for category
		if(intemCat != category){
			continue;
		}
		var blockNode = document.createElement("div");
		// Title and link into <h3><a>
		var headerNode = document.createElement("h3");
		var titleTextNode = document.createTextNode(itemsNodes[i].getElementsByTagName("title")[0].firstChild.nodeValue);
		// Check if there is a link
		if(itemsNodes[i].getElementsByTagName("link").length > 0){
			var linkNode = document.createElement("a");
			var linkUrl = itemsNodes[i].getElementsByTagName("link")[0].firstChild.nodeValue;
			linkNode.appendChild(titleTextNode);
			linkNode.setAttribute("href", linkUrl);
			headerNode.appendChild(linkNode);
		}
		else{
			headerNode.appendChild(titleTextNode);
		}
		blockNode.appendChild(headerNode);
		// Date into <em>
		var dateTextNode = document.createTextNode(formatDate(itemsNodes[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue));
		var dateNode = document.createElement("em");
		dateNode.appendChild(dateTextNode);	
		headerNode.appendChild(dateNode);
		// Append description
		var descriptionNode = document.createTextNode(itemsNodes[i].getElementsByTagName("description")[0].firstChild.nodeValue);
		blockNode.appendChild(descriptionNode);
		// Append the whole div/item
		rootNode.appendChild(blockNode);
		itemsCounter ++;
	}
	if(itemsCounter == 0){
		return document.createTextNode("Sorry, no content");
	}
	return rootNode;
}

function parseDoc(xmlDOM){
	var rootNode = document.createElement("div");
	var paraNodes = xmlDOM.getElementsByTagName("para");
	// Turn any <para> into <p>
	for(var i=0; i<paraNodes.length; i++){
		var pNode = document.createElement("p");
		var pTextNode = document.createTextNode(paraNodes[i].firstChild.nodeValue);
		pNode.appendChild(pTextNode);
		rootNode.appendChild(pNode);
	}
	return rootNode;
}

function formatDate(dateStr){
	var dateObj = new Date(dateStr);
	var displayDate = " (";
	displayDate += dateObj.getFullYear() + "-";
	var monthStr = dateObj.getMonth() + 1;
	if(monthStr.toString().length == 1){
		monthStr = "0" + monthStr.toString();
	}
	var dayStr = dateObj.getDate();
	if(dayStr.toString().length == 1){
		dayStr = "0" + dayStr.toString();
	}
	displayDate += monthStr + "-";
	displayDate += dayStr + ") ";
	return displayDate;
}

function fedInit(){
	loadFeed("rss/news.xml", "news");
}

function panelsInit(){
	var navBar = tmt.get("navBar");
	navBar.tmtExpandpanelContainer = tmt.widget.expandpanel.containerFactory(navBar);
	var linkBar = tmt.get("linkBar");
	linkBar.tmtExpandpanelContainer = tmt.widget.expandpanel.containerFactory(linkBar);
}

tmt.addEvent(window, "load", panelsInit);
tmt.addEvent(window, "load", fedInit);