<?xml version="1.0" encoding="UTF-8" ?>
<Module>
	<ModulePrefs 
		title="Fark" 
		title_url="http://www.fark.com"
		directory_title="Fark.com Customizable News Feed"
		description="A Customizable feed from Fark.com. The items link directly to the story, not to the Fark page. You can also mark all the entries as 'read' and clear your list so you only see new items." 
		author="Matt Kruse" 
		author_email="gadget@mattkruse.com" 
		screenshot="http://www.javascripttoolbox.com/gadget/fark/fark.png" 
		thumbnail="http://www.javascripttoolbox.com/gadget/fark/fark_thumb.png"
		author_affiliation="Matt Kruse" 
		author_location="IL, USA" 
		author_link="http://www.JavascriptToolbox.com/" 
		height="50"
		>
		<Require feature="dynamic-height" /> 
		<Require feature="setprefs" /> 
	</ModulePrefs>
		<UserPref name="num" display_name="Items to show (1-25):" default_value="20"/>
		<UserPref name="format" display_name="Item Format:" default_value="&lt;div class=item&gt;%COMMENTS%&lt;a href=&quot;%URL%&quot; title=&quot;%URL%&quot; class=&quot;%A_CLASSNAME%&quot; target=&quot;%TARGET%&quot;&gt;%BULLET%%TITLE%&lt;/a&gt;&lt;/div&gt;" />
		<UserPref name="topiccss" display_name="Topic Label CSS:" default_value="font-style:italic;" />
		<UserPref name="favicon" display_name="Show site favicon:" datatype="bool" default_value="true" />
		<UserPref name="showcomments" display_name="Show comments link:" datatype="bool" default_value="true" />
		<UserPref name="showtag" display_name="Show topic tag:" datatype="bool" default_value="true" />
		<UserPref name="newwindow" display_name="Open Links In A New Window:" datatype="bool" default_value="true" />
		<UserPref name="altbgcolor" display_name="Alt Row Bg Color:" default_value="#f6f6f6" />
		<UserPref name="refresh" display_name="Refresh Interval (minutes):" default_value="5" />
		<UserPref name="filter" datatype="list" display_name="Filter Out Items Containing:"/>
		<UserPref name="lastseen" display_name="Last Seen URL:" />
		<Content type="html"> 
		<![CDATA[ 
<script type="text/javascript">
var p = new _IG_Prefs();
var fark = new FarkGadget();
function FarkGadget() {
	var me = this;
	this.url = "http://pipes.yahoo.com/pipes/pipe.run?_id=3d402b0f609abc3a6227815d6fe4525b&_render=json";
	this.filter = p.getString('filter');
	this.format = p.getString('format');
	this.favicon = p.getBool('favicon');
	this.showcomments = p.getBool('showcomments');
	this.link = p.getString('link');
	this.hideregexobj = null;
	if (this.filter) { 
		this.hideregexobj = new RegExp(this.filter,"i"); 
	}
	this.interval = null;
	this.lastseen = p.getString('lastseen');
	this.firstlink = null;
	this.num = p.getInt('num');
	this.loaded=false;
	this.topicss = p.getString('topiccss');
	this.msg = function(txt) {
		var status = _gel("status").innerHTML;
		status.innerHTML = txt;
		status.style.display = "block";
		_gel("display").style.display = "none";
	};
	this.startRefresh = function() {
		this.interval = setInterval(function(){me.refresh()}, (60000 * p.getInt('refresh')));
	};
	this.clearRefresh = function() { clearInterval(this.interval); };
	this.init = function() { 
		this.startRefresh();
		this.load();
	};
	this.load = function() {
		var url = this.url;
		_IG_FetchContent(url,function(){me.render.apply(me,arguments)},this.num,false);
	};
	this.refresh = function() {
		this.load();
	};
	this.addEntry = function(index, item, alternate) {
		var tpl = this.format;
		var id = 0;
		tpl = tpl.replace(/\%URL\%/g, _hesc(item.link));
		tpl = tpl.replace(/\%COMMENT_URL\%/g, _hesc(item.comments));
		var title = _hesc(item.title).replace(/^\s*(\[[^\]]+\])/,'<span class="topic">$1</span>');
		var favicon = '<img src="'+item.favicon+'" border="0" align="absmiddle" style="display:none;height:16px;width:16px;" onload="this.style.display=\'inline\'" onerror="this.style.display=\'none\'">&nbsp;';
		tpl = tpl.replace(/\%BULLET\%/g,(this.favicon)?favicon:""); 
		tpl = tpl.replace(/\%A_CLASSNAME\%/g, "item"+((alternate)?" alternate":""));
		tpl = tpl.replace(/\%TITLE\%/g, title);
		tpl = tpl.replace(/\%TARGET\%/g, (p.getBool('newwindow')?"_blank":"") );
		var comments = "";
		if (this.showcomments) {
			comments = '<a href="'+item.comments+'" title="View comment thread" target="_blank"><img src="http://groups.google.com/groups/img/3/favicon.ico" border="0" style="display:inline; float:left; width:16px; margin-right:3px;"></a>';
		}
		tpl = tpl.replace(/\%COMMENTS\%/g, comments);
		_gel("entries").innerHTML += tpl;
		return tpl;
	};
	this.render = function(json) {
		var obj = eval("("+json+")");
		if (!this.loaded && !obj) {this.retry("Information is temporarily unavailable.");return;}
		if (!this.loaded && (!obj.value || !obj.value.items || obj.value.itemslength == 0)) {
			this.retry("Information is temporarily unavailable.");return;
		}
		_gel("status").style.display = "none";
		if (obj.value.items) {
			_gel("entries").innerHTML = "";
			_gel("clear").style.display="block";

			var hide = false;
			var count = 0;
			this.firstlink = obj.value.items[0].link;
			var items = obj.value.items;
			for (var i = 0; i < items.length; i++) {
				var item = items[i];
				if (!hide && item.link==this.lastseen) {
					hide = true;
				}
				if (!hide && i<this.num) {
					if (this.hideregexobj!=null && item.title && item.title.search(this.hideregexobj)>=0) {
						continue;
					}
					if (count==0) {
						_gel("display").style.display = "block";
					}
					this.addEntry(i,item,!!(count++%2==1));
				}
			}
		}
		this.loaded = true;
		_IG_AdjustIFrameHeight();
	};
	this.retry = function(msg) {
		if (msg) { this.msg(msg); }
		this.clearRefresh();
		setTimeout(function(){me.refresh()},5*1000);
	};
	this.clear = function() {
		p.set('lastseen',this.firstlink);
		this.lastseen = this.firstlink;
		_gel("clear").style.display = "none";
		_gel("display").style.display = "none";
		this.clearRefresh();
		this.startRefresh();
		this.refresh();
	};
	_IG_RegisterOnloadHandler(function(){me.init()});
}
document.writeln("<style>span.topic { "+p.getString('topiccss')+" }</style>");
if (!p.getBool('showtag')) {
	document.writeln("<style>span.topic { display:none; }</style>");
}
</script>
<style>
a.item {
	display:block;
	text-decoration:none;
	color:black;
	font-size:smaller;
}
a.alternate {
	background-color:#eee;
}
a:hover {
	background-color:#666699;
	color:white;
}
div.clear {
	display:none;
	float:right;
	cursor:pointer;
	border:2px solid #ccc;
	border-color:#ccc #666 #666 #ccc;
	background-color:#666699;
	font-size:10px;
	color:white;
	margin-left:10px;
}
div.status {
	font-size:smaller;
	color:#666;
	font-style:italic;
}
</style>
<div id="status">Loading...</div>
<div id="display" style="display:none;"><div id="clear" class="clear" title="Click to mark all items as read" onclick="fark.clear()">Mark All Read</div><div id="entries"></div></div>
     ]]> 
   </Content> 
</Module>
