/*	=============================
	various Theatre of Noise code

	Released under GPL and free to use unless otherwise noted. 
	(c) 2006-7 Robin Parmar
*/

// spits out associate links for three Amazon sites
// there is one item for every day of the week
function AmazonLinks() {	
	var title = new Array( 
				"The Intelligence of Evil",
				"Magnitude by Wild Swans",
				"The End Of Violence",
				"The Prisoner",
				"3 Women",
				"Double vie de Véronique",
				"Dalek I Love You",
				"Systems Of Romance",
				"2067 by Rheostatics"
				);
	var link = new Array( 
				"2007/03/jean-baudrillard-how-to-disappear",
				"2007/03/wild-swans-catalogue-completed",
				"2007/07/end-of-violence-reconsidered",
				"2007/01/prisoner-revisited",
				"2006/11/robert-altman-rip",
				"2006/04/vronique",
				"2007/04/dalek-i-love-you-discography-updated",
				"2006/04/re-appearance-of-quiet-men",
				"2006/10/rheostatics-one-last-time"
				);
	var produs = new Array( 
				"1845203348",
				"B000O5BPDW",
				"0792844009",
				"B000FOQ03C",
				"B0001GH5TW",
				"B000I2J75O",
				"B000O5BPDC",
				"B000FDF28M",
				"B0004Z34ZY"
				);
	var produk = new Array( 
				"1845203348",
				"B000O5BPDW",
				"B000E8OKA4",
				"B0002VF64C",
				"B000E1ZABE",
				"B000EHPOQE",
				"B000O5BPDC",
				"B000EU1PWS",
				"B0004Z34ZY"
				);

	// get day of week (0-7)
	// var today = new Date();
	// var item = today.getDay();

	item = getRandom(title.length) -1;

	var us = {dom: ".com", id: "theatreofnois-20", ext: "us", asin: produs[item]};
	var ca = {dom: ".ca", id: "theatreofno0d-20", ext: "ca", asin: produs[item]};
	var uk = {dom: ".co.uk", id: "theatreofnois-21", ext: "uk", asin: produk[item]};

	var root = 'http://www.theatreofnoise.com/'
	var s1 = '<a href="http://www.amazon';
	var s2 = '/exec/obidos/ASIN/';
	var s3 = '/ref=nosim/';
	var s4 = '" class="amazon"><img src="http://robinparmar.com/blog/amazon';
	var s5 = '.jpg" class="amazon" border="0" width="100" height="25" alt="buy from Amazon" title="thank you for helping this site!"/></a><span class="spacer">&nbsp;</span>';

	var s = '<p>Buy my personal recommendation or <i>any other</i> items through the Amazon links to support this site. Today I suggest: <a href="' + root + link[item] +'.html">' + title[item] + '</a> (text link to article).</p><p>'

	with (uk){s += s1 + dom + s2 + asin + s3 + id + s4 + ext + s5;}
	with (us){s += s1 + dom + s2 + asin + s3 + id + s4 + ext + s5;}
	with (ca){s += s1 + dom + s2 + asin + s3 + id + s4 + ext + s5;}

	s += '</p>'

	document.write(s);
}


/*	==================
	Oblique Strategies
	(not Brian Eno's but my own)

	Released under GPL and free to use unless otherwise noted. 
	(c) 2005-6 Robin Parmar
*/

function ObliqueStrategies() {	
	os = new Array( 
		"A shadow is cast upon the wall.",
		"A sketch is good enough for now.",
		"Add a catalyst.",
		"Adopt an arbitrary conformity.",
		"Ending is beginning.",
		"Ask google.",
		"Be imagistic.",
		"Be more self-reflexive.",
		"Betray your instincts.",
		"Beyond a level of complexity, spontaneous assembly occurs. [De Landa]",
		"Choose a colour you dislike.",
		"Close your eyes.",
		"Cook yourself a meal.",
		"Copy your last action.",
		"Delegate the most important process.",
		"Delete your favourite thing.",
		"Do what is easy.",
		"Do what is necessary.",
		"Drop a beat.",
		"Even out the tone.",
		"Find a good person to kick your ass.",
		"Find a new context.",
		"Find an unlikely collaborator.",
		"Follow that whim.",
		"Get some fresh air.",
		"Hug someone.",
		"Increase cohesion.",
		"Increase tension.",
		"It's not that important.",
		"Lie down and breath.",
		"Light some incense.",
		"Look to the borderlines.",
		"Make more concrete.",
		"Meet face-to-face.",
		"Minimalism / maximalism.",
		"More boring.",
		"Nothing is something.",
		"Obliterate the most recent.",
		"Picture your childhood home.",
		"Seek the original.",
		"Separate the parts.",
		"Slice into equal pieces.",
		"Slow down.",
		"Spark a fire.",
		"Spiral like a nautilus shell.",
		"Stand on one leg.",
		"Stop and drink tea.",
		"Take fragmentary notes.",
		"Take the tangent to the centre segments. [Alphaville]",
		"The snail carries its home.",
		"There are no rules.",
		"There are places we go when we travel.",
		"There is nothing to say.",
		"Turn it inside out.",
		"Turn it into a ritual.",
		"Twice as fast and half as long.",
		"We have come to the war zone.",
		"What are you doing next Saturday?",
		"What haven't you noticed?",
		"Where is the outside?",
		"Write a computer programme.",
		"Write a surprise letter."
	);

	item = os[getRandom(os.length) -1];
	document.write(item);
}


/*	=======================
	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);
	}
}