// ==UserScript==
// @name          Favicon with Fresh Reader
// @namespace     http://libelabo.jp/
// @description   A script to add favicons next to links on Fresh Reader 
// @include       http://example.com/feedshow*
// @version       0.1
// ==/UserScript==

(function() {
	
	var showBlankIcon = false;
	
	// apply the function to each element found by the path
	function forEachMatch(path, f, root) {
		var root = (root == null) ? document : root;
		var matches = root.evaluate(
			path, root, null,
			XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
		for (var i = 0; i < matches.snapshotLength; i++)
			f(matches.snapshotItem(i),matches.snapshotItem(i));
	}
	
	function add_favicon(link,node) {
		var favicon = document.createElement('img');
		favicon.setAttribute('class', 'entry-favicon');
		favicon.src = "http://" + link.hostname + "/favicon.ico";
		favicon.width = 16;
		favicon.alt   ="";
		favicon.style.border = "0";
		
		if( showBlankIcon ){
			var g = link.parentNode;
			
			var favicon_container = document.createElement('div');
			favicon_container.style.cssFloat = 'left';
			favicon_container.style.minWidth = '16px';
			favicon_container.style.minHeight = '16px';
			favicon_container.style.marginRight = '5px';
			favicon_container.style.backgroundImage = 'url("chrome://global/skin/icons/folder-item.png")';
			
			favicon_container.appendChild(favicon);
			node.parentNode.insertBefore(favicon_container,node);
		}
		else{
			favicon.style.marginRight = "1ex";
			node.parentNode.insertBefore(favicon,node);
		}
	}
	
	forEachMatch('//div[@class="content"]/div[@class="itemtitle"]/a[@href] | //div[@class="main"]/h2/a',add_favicon);
	
})();