/*	=============================
	various Theatre of Noise code

	Released under GPL and free to use unless otherwise noted. 
	(c) 2006-2010 Robin Parmar
*/


function RelatedPostEntries(json) {
	var homeUrl = 'http://www.theatreofnoise.com/';
	var maxNumberOfPostsPerLabel = 5;
	var TargetElement = 'relatedposts';

	var ul = document.createElement('ul');
	var maxPosts = (json.feed.entry.length <= maxNumberOfPostsPerLabel) ? json.feed.entry.length : maxNumberOfPostsPerLabel;

 	for (var i=0; i<maxPosts; i++) {
  		var entry = json.feed.entry[i];
		var alturl;
		for (var k=0; k<entry.link.length; k++) {
			if (entry.link[k].rel == 'alternate') {
				alturl = entry.link[k].href;
				break;
			}
		}
		var li = document.createElement('li');
		var a = document.createElement('a');
		a.href = alturl;

		if(a.href!=location.href) {
			var txt = document.createTextNode(entry.title.$t);
			a.appendChild(txt);
			li.appendChild(a);
			ul.appendChild(li);
		}
 	}

	for (var l=0; l<json.feed.link.length; l++) {
		if (json.feed.link[l].rel == 'alternate') {
			var raw = json.feed.link[l].href;
			var label = raw.substr(homeUrl.length+13) + ':';

			// for (var k=0; k<20; k++) label = label.replace(" ", "");

			var txt = document.createTextNode(label);
			var h = document.createElement('b');
			h.appendChild(txt);
			var div1 = document.createElement('div');
			div1.appendChild(h);
			div1.appendChild(ul);
			document.getElementById(TargetElement).appendChild(div1);
		}
 	}
}

function CodeHook(url, label, callback) {
	var script = document.createElement('script');
	script.setAttribute('src', url + 'feeds/posts/default/-/' + label + '?alt=json-in-script&callback=' + callback);
	script.setAttribute('type', 'text/javascript');
	document.documentElement.firstChild.appendChild(script);
}



// reviewofweb.com
// <script src="http://www.theatreofnoise.com/feeds/comments/default?alt=json-in-script&amp;callback=RecentComments"/>
function RecentComments(json) {
	var numcomments = 5;
	var showcommentdate = true;
	var showposttitle = true;
	var numchars = 100;

	for (var i=0; i<numcomments; i++) {
		var entry = json.feed.entry[i];
		var alturl;

		if (i == json.feed.entry.length) break;

		for (var k = 0; k < entry.link.length; k++) {
			if (entry.link[k].rel == 'alternate') {
				alturl = entry.link[k].href;
				break;
			}
		}

	alturl = alturl.replace("#", "#comment-");

	var postlink = alturl.split("#");
	postlink = postlink[0];

	var linktext = postlink.split("/");
	linktext = linktext[5];
	linktext = linktext.split(".html");
	linktext = linktext[0];

	var posttitle = linktext.replace(/-/g, " ");
	posttitle = posttitle.link(postlink);

	var commentdate = entry.published.$t;
	var cdyear = commentdate.substring(0,4);
	var cdmonth = commentdate.substring(5,7);
	var cdday = commentdate.substring(8,10);

	var monthnames = new Array();
	monthnames[1] = "Jan";
	monthnames[2] = "Feb";
	monthnames[3] = "Mar";
	monthnames[4] = "Apr";
	monthnames[5] = "May";
	monthnames[6] = "Jun";
	monthnames[7] = "Jul";
	monthnames[8] = "Aug";
	monthnames[9] = "Sep";
	monthnames[10] = "Oct";
	monthnames[11] = "Nov";
	monthnames[12] = "Dec";

	if ("content" in entry) {
		var comment = entry.content.$t;
	} else if ("summary" in entry) {
	  var comment = entry.summary.$t;
	} else {
		var comment = "";
	}

	var re = /<\S[^>]*>/g;
	comment = comment.replace(re, "");
	document.write('<br/>');
	if (showcommentdate == true) document.write('On ' + monthnames[parseInt(cdmonth,10)] + ' ' + cdday + ' ');
	document.write('<a href="' + alturl + '">' + entry.author[0].name.$t + '</a> commented');
	if (showposttitle == true) document.write(' on ' + posttitle);
	document.write('<br/>');

	if (comment.length < numchars) {
		document.write('<i>');
		document.write(comment);
		document.write('</i>');
	} else {
		document.write('<i>');
		comment = comment.substring(0, numchars);
		var quoteEnd = comment.lastIndexOf(" ");
		comment = comment.substring(0, quoteEnd);
		document.write(comment + '...<a href="' + alturl + '">(more)</a>');
		document.write('</i>');}
		document.write('<br/>');
	}
	document.write('<br/>');
}


// spits out code for recent posts
function PostCode() {
	var label = GetCurrentLabel();
	var root = '<script src="http://www.theatreofnoise.com/feeds/';

	if (label=='all') {
		var line = root + 'posts/default/?max-results=5&amp;alt=json-in-script&amp;callback=RecentPostsByLabel"/>';
	} else {
		var line = root + 'posts/summary/-/' + label + '?max-results=5&amp;alt=json-in-script&amp;callback=RecentPostsByLabel"/>';
	}

	document.write('<div>');
	document.write('<h2>recent posts: ' + label + '</h2>');
	document.write(line);
	document.write('</div>');
}

function RecentPostsByLabel(json) {
	document.write('<ul>');
	for (var i=0; i<json.feed.entry.length; i++) {
		var entry = json.feed.entry[i];
		var alturl;
		if (i == json.feed.entry.length) break;
		for (var j=0; j<entry.link.length; j++) {
			if (entry.link[j].rel == 'alternate') {
				alturl = entry.link[j].href; 
				break;
			}
		}
		var entryUrl = json.feed.entry[i].link[j].href;
		var entryTitle = json.feed.entry[i].title.$t;

		var item = '<li>' + entryTitle + ' <a href="' + entryUrl + '">go&gt;&gt;</a></li>';
		document.write(item);
	}
	document.write('</ul>');
}


// retrieves label for current page
function GetCurrentLabel() {
	// this is the format of the page url:
	// http://www.theatreofnoise.com/search/label/sound%20art

	// these labels are expected:
	// photography, music, audio, computers, composition, events, media, politics, design, food, poetry, about

	var url = location.href;
	var label = 'all';

	if (url.indexOf('search/label')>-1) {
		label = url.replace('http://www.theatreofnoise.com/search/label/', '');
		pos = label.indexOf('?')
		if (pos>-1) {
			label = label.substr(0, pos)
		}
	}

	return label;
}


// spits out CSS for appropriate banner images
function BannerByLabel() {
	var label = GetCurrentLabel();

	var m = '#header-m {background-image:url(http://robinparmar.com/blog/assets/b-' + label + '.jpg);}\n'
	var r = '#menu a#m' + GetLabelIndexForMenu(label) + ' {background-position:0 29px !important;}\n'

	document.write("<style type='text/css'>\n" + m + r + "</style>\n");
}

// support function for above
function GetLabelIndexForMenu(label) {
	var LabelHash = new Array();

	LabelHash['all']			= '00';
	LabelHash['photography']	= '01';		
	LabelHash['music']			= '02';	
	LabelHash['audio']			= '03';
	LabelHash['computers']		= '04';
	LabelHash['composition']	= '05';	
	LabelHash['events']			= '06';
	LabelHash['media']			= '07';
	LabelHash['politics']		= '08';
	LabelHash['design']			= '09';
	LabelHash['food']			= '10';
	LabelHash['poetry']			= '11';
	LabelHash['about']			= '12';

	return LabelHash[label];
}

// 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/assets/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);
}



