// Domain setup - EDIT THIS to the server you're getting the files from
var domName = "http://www.securitor.com.au";
function fixGuid(theLink) { // remove the domain name if it's there
	return theLink.replace(domName,'');
}
var isTimed = '';
var matchWord = '';
//BASE AJAX function
function showData(datafile,regular,category){
	if (!datafile) { datafile = "/rss/securitor-news-feed.xml"; }
	isTimed = regular;
	matchWord = category; // single keyword expected
	loadXmlDoc(datafile,handleData);
}

//MAIN UTILS - RSS parser and helper
function podData() { //helper util to indicate weeks/months should be split
	handleData("yes");
}
function handleData(){				
	if (isTimed == 'latest') isTimed = '';
	if (req.readyState == 4 || req.readyState == 0) {		
		var theFeed = req.responseXML;		
		var bigString = "";
		elem = theFeed.getElementsByTagName("item");
		var howMany = isTimed ? elem.length : Math.min(5,elem.length);
		var Aweek=0;
		var Lday = 10; // make it higher than possible to start with
		var Amonth = null;
		var Nmonth = null;
		var Lmonth = null;
		var Ayear  = null;
		var Lyear  = null;
		var Ldate  = null;
		var dDiff  = null;
		if (elem.length) {
			if (isTimed == 'yearly') {
				bigString = '<ul class="newsArchive">';
			} else {
				bigString = '<ul class="latestNewsList">';
			}
			if (isTimed == 'daily') { bigString += "\n<h3>This week</h3>\n"; }
			for (ii=0;ii<howMany;ii++) {
				iTitle = iDesc = iLink = iPDF = iTime = littleString = iTarget = iWords = ""; // clear vars
				if (elem[ii].getElementsByTagName("title")[0]) {
					iTitle = elem[ii].getElementsByTagName("title")[0].firstChild.data;
				}
				if (elem[ii].getElementsByTagName("description")[0]) {
					iDesc  = elem[ii].getElementsByTagName("description")[0].firstChild.data;
				}
				if (elem[ii].getElementsByTagName("guid")[0]) {
					iLink  = fixGuid(elem[ii].getElementsByTagName("guid")[0].firstChild.data);
					if (iLink.match('.pdf')) { iTarget = ' target="_blank"'; }
				}
				if (elem[ii].getElementsByTagName("pubDate")[0]) {
					iDate  = elem[ii].getElementsByTagName("pubDate")[0].firstChild.data;
				}
				if (elem[ii].getElementsByTagName("itunes:duration")[0]) {
					iTime  = elem[ii].getElementsByTagName("itunes:duration")[0].firstChild.data;
				} else if (elem[ii].getElementsByTagName("duration")[0]) { // Opera uses just the local name, not the namespace
					iTime  = elem[ii].getElementsByTagName("duration")[0].firstChild.data;
				}
				if (elem[ii].getElementsByTagName("link")[0]) {
					iPDF   = fixGuid(elem[ii].getElementsByTagName("link")[0].firstChild.data);
					littleString = " | <a href=\""+iPDF+"\" target=\"_blank\" class=\"PDFlink\">Read the transcript</a>";
				}
				if (matchWord && elem[ii].getElementsByTagName("keywords")[0]) {
					iWords  = elem[ii].getElementsByTagName("keywords")[0].firstChild.data.toLowerCase().match(matchWord.toLowerCase()); // check if category is in keywords
				}			
				if (!matchWord || iWords) { //(matchWord && iWords.match(matchWord))) { // this match has been moved up into preceding code
					if (isTimed) {
						iDO = parseDate(iDate,1);
						Tday = iDO.getDay();
						Amonth = iDO.getMonth();
						Ayear  = iDO.getFullYear();
						if (Ldate) {
							dDiff = Ldate.getTime()/(1000*60*60*24) - iDO.getTime()/(1000*60*60*24); // epoch days since last
						}
						Nmonth = Months[Amonth];
						if (Lday<=Tday && isTimed == 'daily' && dDiff > 1) { // must be a different week
							if (Aweek == 0 && dDiff < 5.9) {
								bigString += "\n<h3>Last week</h3>\n";
								Aweek++;
							} else if (Aweek < 9) { // start the monthly divs
								bigString += "\n<h3>Older podcasts</h3>\n\n<ul>\n";
								bigString += "<li><a href=\"#"+Mths[Amonth]+Ayear+"\" onclick=\"showdiv('"+Mths[Amonth]+Ayear+"')\"><b>"+Nmonth+" "+Ayear+"</b></a>\n";
								bigString += "<div class=\"highlander\" name=\""+Mths[Amonth]+Ayear+"\" id=\""+Mths[Amonth]+Ayear+"\">\n";
								Lmonth = Nmonth;
								Aweek = 9;
							}
						}
						if (isTimed != 'daily') { Aweek++; }
						Lday = Tday;
						Ldate = iDO;
						if (isTimed == 'yearly') {
							if (Lyear != Ayear) {
								Lyear = Ayear;
								if (bigString.match('cufon')) {
									bigString += "\n</li>\n";
								}
	                            bigString += "\n<li><h2 class=\"cufon\">"+Ayear+"</h2>\n";
							}
						} else {
							if (Aweek>1 && Lmonth != Nmonth) { // start new month li + div and close off the last
								Lmonth = Nmonth;
								if (bigString.match('showdiv')) {
									bigString += "\n</div>\n</li>\n";
								} else if (isTimed == 'adhoc') {
									bigString += "\n\n<ul>\n";
								} else {
									bigString += "\n<h3>Older podcasts</h3>\n\n<ul>\n";
								}
								bigString += "<li><a href=\"#"+Mths[Amonth]+Ayear+"\" onclick=\"showdiv('"+Mths[Amonth]+Ayear+"')\"><b>"+Nmonth+" "+Ayear+"</b></a>\n";
								bigString += "<div class=\"highlander\" name=\""+Mths[Amonth]+Ayear+"\" id=\""+Mths[Amonth]+Ayear+"\">\n";
							}
						}
					}
					if (iLink.indexOf('mp3') > 0) { /* podcast */
					
						bigString += "\n<p><b>"+iTitle+"</b><br /><span class=\"articleDate\">"+parseDate(iDate)+"</span><br />\n"+iDesc+"<br />";
						bigString += "\n<a href=\""+iLink+"\" target=\"_blank\" class=\"audioLink\">Listen to podcast ("+iTime+")</a>"+littleString+"</p>\n";
						
					} else { /* article instead of podcast */
						if (isTimed) {
							bigString += "\n<dt>"+parseDate(iDate)+"</dt>\n<dd><a href=\""+iLink+"\""+iTarget+">"+iTitle+"</a></dd>\n";
						} else {
							bigString += "\n<li><h2 class=\"cufon\"><a href=\""+iLink+"\""+iTarget+">"+iTitle+"</a></h2>\n<p class=\"dateLine\">"+parseDate(iDate)+"</p>\n<p>\n"+iDesc+"</p>\n<p><a class=\"continueReading\" href=\""+iLink+"\" target=\"_blank\">Continue reading...</a>\n</p>\n</li>";
						}
					}
				}
			}
			if (isTimed && bigString.match('showdiv')) { bigString += "\n</div>\n</li>\n</ul>\n"; } // close divs and list
			bigString += "</ul>";
		} else {
			bigString = "We are updating this page with new content, please check back in 10 minutes."
		}
		document.getElementById('theFeed').innerHTML = bigString;
		// script ASSUMES you have a div with the id "theFeed" into which to insert the generated output
	}
}