// ==UserScript==
// @name			4chan Cooliris Integration
// @namespace		http://www.nyoron.co.uk/
// @description	Does what it says on the tin - adds Cooliris support to 4chan
// @include		http://boards.4chan.org/*
// @exclude		http://boards.4chan.org/f/*
// @resource		ci_img http://www.nyoron.co.uk/img/cooliris-launch.png
// ==/UserScript==

var VERSION = 0.2;

// $x() from http://wiki.greasespot.net/Code_snippets#XPath_helper
function $x() {
	var x = '',          // default values
	node = document,
	type = 0,
	fix = true,
	i = 0,
	toAr = function(xp) {      // XPathResult to array
		var final = [], next;
		while(next = xp.iterateNext())
			final.push(next);
		return final;
	},
	cur;
	while(cur = arguments[i++])      // argument handler
		switch(typeof cur) {
			case "string": x += (x == '') ? cur : " | " + cur; continue;
			case "number": type = cur; continue;
			case "object": node = cur; continue;
			case "boolean": fix = cur; continue;
		}
	if(fix) {      // array conversion logic
		if(type === 6) type = 4;
		if(type === 7) type = 5;
	}
	if(!/^\//.test(x)) x = "//" + x;         	 // selection mistake helper
	if(node !== document && !/^\./.test(x)) x = "." + x;  // context mistake helper
	var temp = document.evaluate(x, node, null, type, null); //evaluate!
	if(fix)
		switch(type) {                              // automatically return special type
			case 1: return temp.numberValue;
			case 2: return temp.stringValue;
			case 3: return temp.booleanValue;
			case 8: return temp.singleNodeValue;
			case 9: return temp.singleNodeValue;
		}
	return fix ? toAr(temp) : temp;
}

var head = document.getElementsByTagName('head')[0];
if(!head) {
	// Can't do anything :(
} else {
	GM_addStyle('#ci_button { background:transparent url(' + GM_getResourceURL('ci_img') + ') no-repeat top center; height: 24px; width: 24px; position:fixed; right: 1px; bottom: 2px; z-index:32767; cursor: pointer; } #ci_button:hover { background-position: bottom center; }');
	
	var ci_div = document.createElement('div');
	ci_div.id = 'ci_button';
	
	var pageUri = document.location.pathname.split('/');
	var link;
	
	if(pageUri.length === 4) { // Thread page
		link = 'http://www.nyoron.co.uk/thread.php?ver=' + VERSION + '&url=' + document.location.href;
	} else if(pageUri.length === 3) { // Board page
		var threadLinks = $x('//a[text()="Reply"]', XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
		var threads = new Array();
		for(i = 0; i < threadLinks.length; i++) {
			threads.push(/res\/(\d+)$/.exec(threadLinks[i].href)[1]);
		}
		link = 'http://www.nyoron.co.uk/thread.php?ver=' + VERSION + '&url=http://' + document.location.hostname + '/' + pageUri[1] + '/res/' + threads[0] + '&threads=' + threads.join(':');
	}
	
	if(link) {
		var rss = $x('//link[@title="RSS feed"]', head, XPathResult.FIRST_ORDERED_NODE_TYPE);
		if(!rss) {
			rss = document.createElement('link');
			rss.type = 'application/rss+xml';
			rss.href = link;
			rss.title = 'RSS feed';
			rss.rel = 'alternate';
			head.appendChild(rss);
		} else {
			rss.href = link;
		}
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.src = 'http://lite.piclens.com/current/piclens_optimized.js';
		head.appendChild(script);
		document.body.appendChild(ci_div);
	}
	
	var w = unsafeWindow;
	
	function waitForPicLensLite() {
		if(typeof(w.PicLensLite) === 'undefined') {
			window.setTimeout(waitForPicLensLite, 100);
		} else {
			ci_div.addEventListener('click', function() { w.PicLensLite.start(null, { maxScale: 1.0 }) }, true);
		}
	}
	
	waitForPicLensLite();
}
