
var proxyUrl = "proxy.php?url="
var newsFeed = {
	name: "news",
	url: "http://feeds2.feedburner.com/OneMoreThingNieuws",
	items: null,
}
var podcastFeed = {
	name: "podcast",
	url: "http://feeds2.feedburner.com/OneMoreThing?format=xml",
	items: null,
}
var httpFeedRequest = null;		// The current XMLHttpRequest

function goto(url) {
	location.href = url;
}

function mail(subject,body) {
	location.href = 'mailto:?subject=' + subject + '&body=' + body;
}

function loadFeed(name) {
	if (name=="news") {
		if (newsFeed.items) {
			setArticles();
		} else {
			fetchFeed(newsFeed);
		}
	} else {
		if (podcastFeed.items) {
			setPodcasts();
		} else {
			fetchFeed(podcastFeed);
		}
	}
}

function fetchFeed(feed) {
    // Abort any pending request before starting a new one
    if (httpFeedRequest != null) {
        httpFeedRequest.abort();
        httpFeedRequest = null;
    }
    httpFeedRequest = new XMLHttpRequest();
    // Function callback when feed is loaded
    httpFeedRequest.onload = function (e) {
        var feedRootElement;
        if (httpFeedRequest.responseXML) feedRootElement = httpFeedRequest.responseXML.documentElement;
        // Request is no longer pending
        httpFeedRequest = null;
        // Process the loaded document
        processFeedDocument(feed,feedRootElement);
    }
    httpFeedRequest.overrideMimeType("text/xml");
	httpFeedRequest.open("GET",proxyUrl+feed.url,true);
	httpFeedRequest.setRequestHeader("Cache-Control", "no-cache");
    // Send the request asynchronously
    httpFeedRequest.send(null);
}

function processFeedDocument(feed,doc) {
	if (feed.name == "news") {
		parseNewsFeed(doc);
		setArticles();
	} else {
		parsePodcastFeed(doc);
		setPodcasts();
	}
}

function parseNewsFeed(doc) {
	newsFeed.items = new Array;
	var items = doc.getElementsByTagName("item");
	for (var i=0; i<items.length; i++) {
		var item = items[i];
		var title = item.getElementsByTagName("title")[0].textContent;
		var category = item.getElementsByTagName("category");
		if (category.length == 1) {
			category = category[0].textContent;
		} else {
			category = "Nieuws";
		}
		var creator = item.getElementsByTagName("creator");
		if (creator.length == 1) {
			creator = creator[0].textContent;
		} else {
			creator = "unkown";
		}
		var content = item.getElementsByTagName("description")[0].textContent;
		content = content.replace("Dit artikel gaat nog verder, klik op \"Lees hier meer of reageer\" om verder te lezen",
		"Klik op \"Meer\" voor het origineel artikel.");
		content = reformatArticleContent(content);
		var link = item.getElementsByTagName("origLink")[0].textContent;
		newsFeed.items[newsFeed.items.length] = {
			title: title,
			category: category,
			creator: creator,
			content: content,
			link: link,
		}
	}
}

function reformatArticleContent(htmlContent) {
	htmlContent = htmlContent.replace(/<img width='1'.*feedsportal.*\/>/g,""); // remove spam
	htmlContent = htmlContent.replace(/<img/g,"<img width=284");
	htmlContent = htmlContent.replace(/<center>/g,"");
	htmlContent = htmlContent.replace(/<\/center>/g,"");
	htmlContent = htmlContent.replace(/<ul>/g,"<br/>");
	htmlContent = htmlContent.replace(/<\/ul>/g,"<br/");
	htmlContent = htmlContent.replace(/<li>/g," - ");
	htmlContent = htmlContent.replace(/<\/li>/g,"");
	return htmlContent;
}

function setArticles() {
	var xhtmlList = "<div class=\"iList\"><ul class=\"iArrow\">";
	for (var i=0; i<newsFeed.items.length; i++) {
		var article = newsFeed.items[i];
		xhtmlList = xhtmlList +
		"<li><a href=\"#_Article\" onclick=\"setArticle("+i+");\">" +
		"<em>" +
		article.title +
		"</em>" +
		"<small>" +
		article.category +
		"</small>" +
		"</a></li>";
	}
	xhtmlList = xhtmlList + "</ul></div>";
	var newsDiv = document.getElementById("newsDiv");
	newsDiv.innerHTML = xhtmlList;
}

function setArticle(index) {
	var article = document.getElementById("waArticle");
	article.setAttribute("title",newsFeed.items[index].category);
	var articleTitle = document.getElementById("articleTitle");
	articleTitle.innerHTML = newsFeed.items[index].title;
	var articleCreator = document.getElementById("articleCreator");
	articleCreator.innerHTML = "Geschreven door " + newsFeed.items[index].creator;
	var articleContent = document.getElementById("articleContent");
	articleContent.innerHTML = newsFeed.items[index].content;
	var readMoreButton = document.getElementById("readMoreButton");
	readMoreButton.setAttribute("onclick","goto('" + newsFeed.items[index].link + "')");
	var mailButton = document.getElementById("mailButton");
	mailButton.setAttribute("onclick","mail('" + escape(newsFeed.items[index].title) + "','" + escape(newsFeed.items[index].link) + "')");
}

function parsePodcastFeed(doc) {
	podcastFeed.items = new Array;
	var items = doc.getElementsByTagName("item");
	//for (var i=0; i<items.length; i++) {
	for (var i=0; i<10; i++) {
		var item = items[i];
		var title = item.getElementsByTagName("title")[0].textContent;
		title = title.replace("One More Thing p","P");
		var subtitle = item.getElementsByTagName("subtitle")[0].textContent;
		var description = item.getElementsByTagName("description")[0].textContent;
		var link = item.getElementsByTagName("link")[0].textContent;
		//var image = item.getElementsByTagName("image")[0].textContent;
		if (link.charAt(link.length-1)=="a") {
			var image = "img/m4a.png";
		} else {
			var image = "img/m4v.png";
		}
		podcastFeed.items[podcastFeed.items.length] = {
			title: title,
			subtitle: subtitle,
			description: description,
			link: link,
			image: image,
		}
	}
}

function setPodcasts() {
	var xhtmlList = "<div class=\"iList\"><h2>Laatste Podcasts</h2><ul class=\"iArrow\">";
	for (var i=0; i<podcastFeed.items.length; i++) {
		var podcast = podcastFeed.items[i];
		xhtmlList = xhtmlList +
		"<li><a href=\"" +
		podcast.link +
		"\" rev=\"media\");\"><img src=\""+
		podcast.image +
		"\"/><em>" +
		podcast.title +
		"</em><small>" +
		podcast.subtitle +
		"</small></a></li>";
	}
	xhtmlList = xhtmlList + "</ul><h2>Oudere Podcasts</h2>" +
	"<div class=\"iMore\"><a href=\"...\">Toon oudere podcasts</a></div>" +
	"</div>";
	var podcastsDiv = document.getElementById("podcastsDiv");
	podcastsDiv.innerHTML = xhtmlList;
}