/*
	This is based on the Google results numbering script from;
	Adam Langley <agl@imperialviolet.org>
	A Firefox Greasemonkey script,
	Version 0.1

	Public Domain
*/

// ==UserScript==
// @name 		Google Search Ratings
// @namespace 		http://www.langenhoven.com/
// @description 	Adds ratings to search results
// @include 		http://www.google.*/search*
// ==/UserScript==

(function() {
	// Search results are in p elements with a class of 'g'
	// This uses XPath to find all such elements and returns a 
	// snapshot. (A snapshot doesnt become invalid after changing
	// the DOM)
	
	var results = document.evaluate("//p[@class='g']", document, null, 
			XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

	for (var i = 0; i < results.snapshotLength; ++i) {
		var result = results.snapshotItem(i);

		//We want to rate by domain, so chop the text down to the domain.
		//First get the plain url
		try {
		var urlname = result.firstChild.nextSibling.getAttribute("href");

		//If you want to rate by URL instead of domainname then simply
		//comment out the following section
		var domname = urlname;
		var domarr = urlname.split("/");
		//We grab section 0 which is the "http" part and section 2
		//because section 1 is just the second / and will be blank
		urlname = domarr[0] + "//" + domarr[2] + "/";

		domname = urlname;

		} catch (error) {
			//simply skip to the next entry
			domname = '';
                }

		if (domname != '') {
		//Remember to add in the items you want in the reverse order
		//that they should appear on the page

		//Put a space between our rating modifiers and the actual link
		var newspan = document.createTextNode("  ");
		result.insertBefore(newspan, result.firstChild);

		// We put the negative rating box in a small-caps red span
		var newspan = document.createElement("a");
                newspan.setAttribute("href", domname + "#negrate");
		newspan.setAttribute("name", "negrate");
		newspan.setAttribute("style", "color:red; font-variant: small-caps;");
		newspan.appendChild(document.createTextNode('[-]'));
		result.insertBefore(newspan, result.firstChild);

		// We put the positive rating box in a small-caps green span
		var newspan = document.createElement("a");
                newspan.setAttribute("href", domname + "#posrate");
		newspan.setAttribute("name", "posrate");
		newspan.setAttribute("style", "color:green; font-variant: small-caps;");
		newspan.appendChild(document.createTextNode('[+]'));
		result.insertBefore(newspan, result.firstChild);

		
		//Take a look to see if this link has been rated before
		var rating = GM_getValue(domname, 0);
		if (rating > 0) {
			//We found a positive rating so display it with the link
			var colspan = document.createElement("span");
			colspan.setAttribute("style", "color:green; font-weight: bold;");
			colspan.appendChild(document.createTextNode('[' + rating + '] '));
			result.insertBefore(colspan, result.firstChild);
			
		} //positive rating

		if (rating < 0) {
			//Negative rating - display with the link
			//This is deliberately handled separately so that we could 
			//possibly delete this domain later on if we felt like it
			var colspan = document.createElement("span");
			colspan.setAttribute("style", "color:red; font-weight: bold;");
			colspan.appendChild(document.createTextNode('[' + rating + '] '));
			result.insertBefore(colspan, result.firstChild);


		} //negative rating

		} //error checking
	} //loop through the results links

	results = null;


	//Add an event listener to trap the mouse clicks so that we can grab our
	//ratings if they come through
	document.addEventListener('click', function(event) {
		// event.target is the element that was clicked

		//Split the name on our divider to see if it is 
		//one of our special clicks. Hopefully I find a
		//better way to do this through the name value
		var click_val = '' + event.target;
		try {
		var click_arr = click_val.split("#");

		if (click_arr.length > 1) {
			//click_arr[0] will contain our domain to check for

			if (click_arr[click_arr.length - 1] == "posrate" ) {
				var rating_count = GM_getValue(click_arr[0], 0);

				//Increment the counter
				rating_count += 1;

				//Save the value
				GM_setValue(click_arr[0], rating_count);

				//Prevent the click from going through 
				event.stopPropagation();
				event.preventDefault();
		  	} //positive rating

			if (click_arr[click_arr.length - 1] == "negrate" ) {
				var rating_count = GM_getValue(click_arr[0], 0);

				//Decrement the counter
				rating_count -= 1;

				//Save the value
				GM_setValue(click_arr[0], rating_count);

				//Prevent the click from going through 
				event.stopPropagation();
				event.preventDefault();
		  	} //negative rating

  		} //found a rating link

		} catch (error) {
			//Shoot straight past on errors
		}

	}, true);


})();





