// JavaScript Document

google.load("feeds", "1");

var theArticle = [];

function hasLab(cList, lab) {
	if (!cList.length)
		return false;
	var foundIt = false;
	for (var i = 0; i < cList.length; i ++)
		foundIt = foundIt || (cList[i] == lab);
	return foundIt;
}

function doFeedLoad(n, feedURL) {
	var feed = new google.feeds.Feed(feedURL);
	feed.setNumEntries(n);
	feed.load(function(result) {
		if (!result.error) {
			//clear the "Teaser" section
			var teasers = document.getElementById('teaser');
			while (teasers.hasChildNodes())
				teasers.removeChild(teasers.firstChild);
			teaserH = document.createElement('h1')
			teaserH.appendChild(document.createTextNode('Recent News Articles '));
			teaserL = document.createElement('a');
			teaserL.setAttribute('href', 'http://newsblog.verdoncollege.school.nz/');
			teaserL.setAttribute('title', 'See all news articles');
			teaserL.appendChild(document.createTextNode('>>'));
			teaserH.appendChild(teaserL)
			teasers.appendChild(teaserH);

			//build the arrays of articles
			var i, artStore, heading, article, dateline, theDate, featured = -1, thisEntry, articleText, tLink, tSnip, articleID = 0;
			for (i=0; i < result.feed.entries.length; i++) {
				thisEntry = result.feed.entries[i];
				
				//add the article to the teaser list
				heading = document.createElement('h2');
				tLink = document.createElement('a');
				tSnip = document.createElement('p');
				tLink.setAttribute('href', thisEntry.link);
				tLink.setAttribute('artID', articleID);
				tLink.onclick = displayArticle;
				tLink.appendChild(document.createTextNode(thisEntry.title));
				tSnip.appendChild(document.createTextNode(thisEntry.contentSnippet));
				heading.appendChild(tLink);
				teasers.appendChild(heading);
				teasers.appendChild(tSnip);
				
				//build up a div for plugging in later on
				if (hasLab(thisEntry.categories, 'Featured') && (featured == -1)) {
					featured = articleID;
				}
				artStore = document.createElement('div');
				theDate = new Date(thisEntry.publishedDate);
				heading = document.createElement('h1');
				heading.appendChild(document.createTextNode(thisEntry.title));
				dateline = document.createElement('p');
				dateline.appendChild(document.createTextNode(formatDate(theDate, 'd MMM y')));
				dateline.className = 'date';
				articleText = document.createElement('div');
				articleText.innerHTML = '<p>' + thisEntry.content + '</p><div style="clear:both"></div>';
				artStore.appendChild(heading);
				artStore.appendChild(dateline);
				artStore.appendChild(articleText);
				smartenIt(artStore);
				theArticle[articleID++] = artStore;
			}
			if (featured > -1) {
				displayArticle(featured);
			}
			smartenIt(teasers);
		}
	});
}

function displayArticle(fID) {
	var doJump = true;
	if (typeof(fID) == 'number') {
		//we have been given a featured ID
		aID = fID;
		doJump = false;
	}
	else{
		//we've been clicked
		aID = this.getAttribute('artID');
	}
	
	//remove the existing article (if any) and replace it with the one in the array
	var article = document.getElementById('articleTarget');
	while (article.hasChildNodes()) {
		article.removeChild(article.firstChild);
	}
	article.appendChild(theArticle[aID]);
	if (doJump) {
		scroll(0,0) //jump to the top of the article
	}
	return false; //stop the link from heading to the blog
}