/*

Find blog entries related to the page that you are currently on

*/

// ==UserScript==
// @name			Find relevant blog content
// @namespace		http://www.postgenomic.com/userscripts
// @description		Extracts key terms from current page, matches them to terms in the Postgenomic database
// @include			http://www.nature.com/news*
// ==/UserScript==

function process_page() {
	GM_log("Processing page...");
	var bodytext = getText();
	 	
	// get terms from page using the Yahoo! term extraction API
	
	var args = "appid=xnn&context=" + escape(bodytext) + "&output=json&callback=json_callback";
	
	if (bodytext.length < 100) {
		alert("Sorry, I couldn't work out what this page is about.");
	} else {
		create_loading_bar();
		GM_xmlhttpRequest({
			method: 'POST',
			url: 'http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction',
			headers: {'Content-type': 'application/x-www-form-urlencoded'},
			data: args,
			onload: function(responseDetails) {
				eval(responseDetails.responseText);
			}
		});	
	}
}

function getText() {
	GM_log("Extracting text...");
	
	var bodyText = document.body.innerHTML;
	
	var matches = bodyText.match(/([\w\s\d\,\.\'^_]{64,})/ig);
	
	if (matches.length) {
		return matches.join('\n');
	} else {
		return "";
	}
}

function create_loading_bar() {
	var sidebar = document.createElement("div");	
	sidebar.innerHTML = '<div id="pgrelevant_loading" style="background-color: #FFF9BF; border: 2px dotted #FF6600; border-top: none; padding: 5px; text-align: center;">'
	+ '<h2>Analyzing content... please be patient.</h2>';
	
	document.body.insertBefore(sidebar, document.body.firstChild);
}

function remove_loading_bar() {
	// get rid of the loading_bar
	var loadingBar = document.getElementById("pgrelevant_loading");
	loadingBar.innerHTML = '';
}

function create_bar_top(terms) {
	
	// the API call we need...
	var pgUrl = "http://www.postgenomic.com/api.php?type=posts&format=json&callback=pg_json_callback&order_by=post_freq&term=" + terms;
	var niceUrl = "http://www.postgenomic.com/interface/posts.php?order_by=post_freq&term=" + terms;
	
	var sidebar = document.createElement("div");
	sidebar.innerHTML = '<div id="pgrelevant_bar_top" style="background-color: #FFF9BF; border: 2px dotted #FF6600; border-bottom: none; padding: 5px;">'
	+ '<h2>Blog posts on similar topics</h2>'
	+ '<i style="font-size: 0.8em;">' + terms + '</i>'
	+ '</div>'
	+ '<div style="background-color: #FFF9BF; border: 2px dotted #FF6600; border-top: none; border-bottom: none; padding: 5px; text-align: right;">'
	+ '<a href="' + niceUrl + '">Read more at Postgenomic.com...</a>'
	+ '</div>';
	document.body.insertBefore(sidebar, document.body.firstChild);

	// call the Postgenomic API
	GM_xmlhttpRequest({
		method: 'GET',
		url: pgUrl,
		onload: function(responseDetails) {
			eval(responseDetails.responseText);
		}
	});	
}

function pg_json_callback(obj) {
	// got results from Postgenomic
	GM_log("Got results from Postgenomic...");
	
	var len = obj.length;
	
	var displayText= "";

	for (var i=0; ((i < len) && (i <= 2)); i++) {
		displayText = displayText + '<p><a href="' + obj[i].url + '">' + obj[i].title + '</a><br/>' + obj[i].summary;
	}
		
	var pgResults = document.createElement("div");
	
	if (len <= 0) {
		displayText = "<p>Sorry, we couldn't find any blog posts whose content is similar to that of this page.";
	}
	
	pgResults.innerHTML = '<div>' + displayText + '</div>';
	
	// add results to the top_bar
	document.getElementById("pgrelevant_bar_top").appendChild(pgResults);
	
	remove_loading_bar();
}

function json_callback(obj) {
	// got terms from the current page.
	GM_log("Got some terms...");
	
	// build up a string of those terms...
	var terms = obj.ResultSet.Result.join();
		
	// open up new bar at side of screen
	create_bar_top(terms);
}

GM_log("Relevant blog content script is running.");
GM_registerMenuCommand( "Find relevant blog content", process_page);

// function below is now called by the menu hook.
// process_page();