/*
	Blogger Recent Comments
	v1.05

	Based on the Farrago Recent Comments Hack v1.03
	boggerhacks.blogspot.com
	(c) 2004 Ebenezer Orthodoxy 

	Statement: I would GPL the code if the original author would. 
	Mods by: Robin Parmar
	Visit: noisetheatre.blogspot.com/
*/

// our class
function RecentComments() {
	// options to change
	this.displayAmount = 10;
	this.displayTemplate = '<li>[name]:<br/>[title]</li>';
	this.displayPre = '<ul>';
	this.displayPost = '</ul>';
	this.displayLink = true;

	// properties
	this.comments = new Array();
	this.title = '';
	this.itemurl = '';

	// methods
	this.SetTemplate= rcSetTemplate;
	this.SetAmount	= rcSetAmount;
	this.SetLink	= rcSetLink;
	this.SetPrePost	= rcSetPrePost;

	this.SetTitle	= rcSetTitle;
	this.SetUrl		= rcSetUrl;

	this.SortDate	= rcSortDate;
	this.AddComment	= rcAddComment;
	this.Display	= rcDisplay;

	// change this line to your date converter method
	this.DateConvert= rcDateConvert;
}

// simple property setters: these are used by process
function rcSetTitle(x) {
	this.title = document.getElementById(x).innerHTML;
}
function rcSetUrl(x) {
	this.itemurl = x;
}

// these are used by user to customise
function rcSetTemplate(x) {
	this.displayTemplate = x;
}
function rcSetAmount(x) {
	this.displayAmount = x;
}
function rcSetLink(x) {
	if (x==0) {
		this.displayLink = false;
	} else {
		this.displayLink = true;
	}
}
function rcSetPrePost(x, y) {
	this.displayPre = x;
	this.displayPost = y;
}

// date format converter
// insert your own here depending on the format you use for comment dates
// this one converts from:
// 		01 November, 2005 16:35
// to:
//		11/01/2005 16:35:00
function rcDateConvert(dt) {
	var s = dt.split(' ');
	var d = s[0];
	var m = s[1];
	var y = s[2];
	var t = s[3];

	var MonthHash = new Array();
	MonthHash['January']	= '01';
	MonthHash['February']	= '02';		
	MonthHash['March']		= '03';	
	MonthHash['April']		= '04';
	MonthHash['May']		= '05';
	MonthHash['June']		= '06';	
	MonthHash['July']		= '07';
	MonthHash['August']		= '08';
	MonthHash['September']	= '09';
	MonthHash['October']	= '10';
	MonthHash['November']	= '11';
	MonthHash['December']	= '12';

	// trim off comma
	m = m.substring(0, m.length-1);

	return MonthHash[m] + '/' + d  + '/' + y + ' ' + t + ':00';
}

// default converter: does nothing
// use if your comment date format is:
//   mm/dd/yyyy hh:mm:ss
function rcDateConvertDefault(dt) {
	return dt;
}

// given a date string this returns a sorted representation
function rcSortDate(strDate) {
	strDate = this.DateConvert(strDate)

	var d = new Date(strDate);

	var day = '' + d.getDate();
	if (day.length==1) {
		day = '0' + day;
	}
	var month = '' + (d.getMonth()+1);
	if (month.length==1) {
		month = '0' + month;
	}
	var hour = '' + d.getHours();
	if (hour.length==1) {
		hour = '0' + hour;
	}
	var min = '' + d.getMinutes();
	if (min.length==1) {
		min = '0' + min;
	}
	var sec = '' + d.getSeconds();
	if (sec.length==1) {
		sec = '0' + sec;
	}
	var sortDate = '' + d.getFullYear() + month + day + hour + min + sec;
	return sortDate;
}

// adds to global comments array
function rcAddComment(title, url, id, a, datestamp) {
	var author = a;
	var expt = '';
	var st = '';

	// grab content of our hidden layer containing all items
	var html = document.getElementById('comm' + id).innerHTML;

	// strip out whitespace 
	while (html.indexOf("\n") > -1) {
		html = html.replace("\n", "");
	}
	while (html.indexOf(" />") > -1) {
		html = html.replace(" />", "/>");
	}
	while (html.indexOf(" <a/>") > -1) {
		html = html.replace(" <a/>", "<a/>");
	}

	var htmll = html.toLowerCase();
	var pos1 = htmll.lastIndexOf('<br><a></a>posted by');
	var pos2 = htmll.lastIndexOf('<br><a></a><a></a>');
	var pos3 = htmll.lastIndexOf('<br/><a/><a/>');
	var pos4 = htmll.lastIndexOf('<br/><a></a><a></a>');
	var aoffset = pos1 + 6;

	if (pos3 > -1) {
		pos2 = pos3;
	}
	if (pos4 > -1) {
		pos2 = pos4;
	}
	if (pos2 > -1) {
		pos1 = pos2;
		aoffset = htmll.lastIndexOf('<a><b> </b></a>');
		if (aoffset == -1) {
			aoffset = htmll.lastIndexOf('<a><b></b></a>') - 1;
		}
	}

	if (pos1 > -1) {
		author = html.substr(aoffset+15, html.length-1);
		expt = html.substr(0, pos1-4);
	} else {
		expt = html;
	}
	expt = expt.replace(/(<([^>]+)>)/ig, "");

	if (expt.length > 50) {
		expt = expt.substr(0, 50);
		if (expt.lastIndexOf(' ') > -1) {
			expt = expt.substr(0, expt.lastIndexOf(' '));
		}
		expt += '...';
	}
	expt = expt.replace('"', "\"");
	expt = expt.replace("'", "\'");

	author = author.replace("<A ", "<a ");
	if (!this.displayLink) {
		author = author.replace(/(<([^>]+)>)/ig, "");
	}

	// build a template string of HTML
	st = this.displayTemplate.replace('[name]', author);
	st = st.replace('[title]', '<a title="' + expt + '" href="' + url + '#c' + id + '">' + title + '</a>');

	// prefix with date for sorting purposes
	st = this.SortDate(datestamp) + st;

	// accumulate on our array
	this.comments.push(st);
}

function rcDisplay() {
	// most recent comments first
	this.comments.sort();
	this.comments.reverse();

	if (this.displayPre.length >0) {
		document.write(this.displayPre);
	}

	for (i=0; i<10 && i < this.comments.length && i < this.displayAmount; i++) {
		var s = this.comments[i];

		// strips off date prefix
		s = s.substr(14, s.length-1); 
		document.write(s);
	}

	if (this.displayPost.length >0) {
		document.write(this.displayPost);
	}
}