// ==UserScript==
// @name          4chan Quote Inliner
// @namespace     http://spacetaken.net/44d/
// @description   Adds inline quote expansion to 4chan - designed to work with 4chan extension - without it unquoted images wont be inline-expandable
// @include       http://*.4chan.org/*/res/*
// ==/UserScript==

//use the unsafe window for lots of stuff because it is far simpler that way

getFragment = function(fullURL){
	return fullURL.substring(fullURL.lastIndexOf('#')+1);
}

removeFragment = function(fullURL){
	var ind = fullURL.lastIndexOf("#");
	return (ind == -1) ? fullURL : fullURL.substring(0, ind);		
}

unsafeWindow.contractImage = function(aElement, thumbHTML){
	thumbHTML = decodeURI(thumbHTML);
	var repAElem = document.createElement("a");
	//expand image when clicked again
	repAElem.href = aElement.href;
	repAElem.setAttribute("onclick", "return expandImage(this);");
	repAElem.innerHTML = thumbHTML;
	aElement.parentNode.replaceChild(repAElem, aElement);
	return false;
}

unsafeWindow.expandImage = function(aElement){
	var fullImgPath = aElement.href.replace("cb-nws/", "");
	var repAElem = document.createElement("a");
	//shrink image when clicked again
	repAElem.href = fullImgPath;
	repAElem.setAttribute("onclick", "return contractImage(this, '" + encodeURI(aElement.innerHTML) + "');");
	repAElem.innerHTML = '<img src="'+fullImgPath+'" style="border: 0px none; margin-left: 20px; margin-right: 20px;">';
	aElement.parentNode.replaceChild(repAElem, aElement);
	return false;
}

window.setupAutoExpandImages = function(repTable){
	//setup click-to-expand images
	var imgHrefs = document.evaluate(
		"descendant::a/img[contains(@src, '/thumb/')]",
		repTable,
		null,
		XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
		null);
	for(var i=0; i<imgHrefs.snapshotLength; i++){
		var cur = imgHrefs.snapshotItem(i).parentNode;	
		cur.setAttribute("onclick", "return expandImage(this);");
	}
	//also deal with thumbnail-not-available
	var tnaHrefs = document.evaluate(
		"descendant::a/span[@class='tn_reply']",
		repTable,
		null,
		XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
		null);
	for(var i=0; i<tnaHrefs.snapshotLength; i++){
		var cur = tnaHrefs.snapshotItem(i).parentNode;	
		cur.setAttribute("onclick", "return expandImage(this);");
	}
}

unsafeWindow.showInlineQuote = function(aElement, inlineTableId){
	aElement.innerHTML = " &lt;";
	aElement.setAttribute("onclick", "return hideInlineQuote(this, '"+inlineTableId+"');");
	document.getElementById(inlineTableId).style.display = "block";
	return false;
}

unsafeWindow.hideInlineQuote = function(aElement, inlineTableId){
	aElement.innerHTML = " &gt;";
	aElement.setAttribute("onclick", "return showInlineQuote(this, '"+inlineTableId+"');");
	document.getElementById(inlineTableId).style.display = "none";
	return false;	
}

unsafeWindow.inlineQuote = function(aElement){
	var id = getFragment(aElement.href);
	var quoteId = "inline_quote_"+(nextInlineQuoteId++);
	
	//setup click-to-hide-quote link
	newAElement = document.createElement('a');
	newAElement.href = aElement.href;
	newAElement.innerHTML = " &lt;";
	newAElement.style.textDecoration = "none";
	newAElement.setAttribute("onclick", "return hideInlineQuote(this, '"+quoteId+"');");
	aElement.parentNode.replaceChild(newAElement, aElement);
	
	var inlineQuoteTable = document.createElement('table');
	inlineQuoteTable.innerHTML = "<tr><td>"+allReplies[id]+"</td></tr>";
	inlineQuoteTable.id = quoteId;
	inlineQuoteTable.setAttribute("class", "reply");
	inlineQuoteTable.style.borderStyle = "dashed";
	inlineQuoteTable.style.borderWidth = "1px";
	inlineQuoteTable.style.margin = "2px";
	
	var pNode = newAElement.parentNode;
	pNode.parentNode.insertBefore(inlineQuoteTable, pNode.nextSibling);
	setupAutoExpandImages(inlineQuoteTable);
	
	return false;
}

//check not on 404 etc page
if(document.getElementById("navtop")){
		
	var allReplies = new Array();	//reply id => reply td innerHTML
	var nextInlineQuoteId = 0;

	//setup click-to-display-quote-above-this-post
	var quotesHrefs = document.evaluate(
		"//a[@class='quotelink']",
		document,
		null,
		XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
		null);		
	for(var i=0; i<quotesHrefs.snapshotLength; i++){
		var cur = quotesHrefs.snapshotItem(i);
		var id = getFragment(cur.href);
		var quotedTD = document.getElementById(id);
		//check if this link is to external thread / thread head - dont add inline quote link if so
		if(!quotedTD){
			var desc = document.getElementById('nothread'+id) ? ' - Thread Head' : ' - External Thread';
			var dispElem = document.createTextNode(desc);
			cur.parentNode.appendChild(dispElem);
		}
		else{
			var aElement = document.createElement('a');
			aElement.innerHTML = " &gt;";
			aElement.href = cur.href;
			aElement.style.textDecoration = "none";
			cur.parentNode.insertBefore(aElement, cur.nextSibling);
			aElement.setAttribute("onclick", "return inlineQuote(this);");
		}		
	}
	
	//read replies into array
	var rps = document.evaluate(
		"//td[@class='reply' or @class='replyhl']",
		document,
		null,
		XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
		null);
	for(var i=0; i<rps.snapshotLength; i++){
		var cur = rps.snapshotItem(i);
		if(!allReplies[cur.id])
			allReplies[cur.id] = cur.innerHTML;
	}
	
}
