<?xml version="1.0" encoding="UTF-8" ?>
<Module>
	<ModulePrefs 
		title="Digg Home" 
		title_url="http://www.digg.com"
		directory_title="Digg.com Feed via piggfeed.com"
		description="A Customizable Digg feed using the improved RSS feed from piggfeed.com as the source. The items link directly to the story, not to the Digg 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/digg/digg.png" 
		thumbnail="http://www.javascripttoolbox.com/gadget/digg/digg_thumb.png"
		author_affiliation="Matt Kruse" 
		author_location="IL, USA" 
		author_link="http://www.JavascriptToolbox.com/" 
		/> 
		<UserPref name="url" display_name="URL:" default_value="http://piggfeed.com/piggfeed.php?category=all&amp;embed=true&amp;minimal=true"/>
		<UserPref name="num" display_name="Items to show (1-100):" default_value="20"/>
		<UserPref name="newwindow" display_name="Open Links In A New Window:" datatype="bool" default_value="true" />
		<UserPref name="filter" datatype="list" display_name="Filter Out Items Containing:"/>
		<UserPref name="refresh" display_name="Refresh Interval (minutes):" default_value="5" />
		<UserPref name="lastseen" display_name="Last Seen URL:" />
		<Content type="html-inline"> 
		<![CDATA[ 
<script type="text/javascript">
var digg = new DiggGadget();
function DiggGadget() {
	var me = this;
	this.id=__MODULE_ID__;
	var p = this.p = new _IG_Prefs(this.id);
	this.url = p.getString('url');
	this.filter = p.getString('filter');
	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.itemList={};
	this.highlightNewItemsTimeout=null;
	this.loaded=false;
	this.msg = function(txt) {
		_gel("FEED"+this.id+"statusmsg").innerHTML = txt;
		_gel("FEED"+this.id+"status").style.display = "block";
		_gel("FEED"+this.id+"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;
		url += "&number="+this.num;
		url += "&random="+new Date().getTime();
		_IG_FetchFeedAsJSON(url,function(){me.render.apply(me,arguments)},this.num,false);
	};
	this.refresh = function() {
		_gel("m_"+this.id).style.backgroundColor = "#E7F1D7";
		if (this.highlightNewItemsTimeout!=null) {
			clearTimeout(this.highlightNewItemsTimeout);
		}
		this.load();
	};
	this.addEntry = function(index, url, title, summary, date, alternate) {
		var tpl = '<div class=item><a href="%URL%" title="%SUMMARY%" class="%A_CLASSNAME%" target="%TARGET%">%BULLET%%TITLE%</a></div>';
		tpl = tpl.replace(/\%INDEX\%/g, index).replace(/\%ID\%/g, this.id).replace(/\%URL\%/g, _hesc(url));
		var title = _hesc(title);
		var classname = "item";
		if (this.loaded && !this.itemList[title]) {
			classname += " new"+this.id;
		}
		if (alternate) {
			classname += " alternate";
		}
		this.itemList[title] = true;
		if (url.search(/^(https?:\/\/[^\/]+)/)>=0) {
			var favicon = '<img src="'+RegExp.$1+'/favicon.ico" border="0" align="absmiddle" style="height:16px;width:16px;" onerror="this.style.visibility=\'hidden\'">&nbsp;';
			tpl = tpl.replace(/\%BULLET\%/g,favicon); 
		}
		else {
			tpl = tpl.replace(/\%BULLET\%/g,"");
		}
		tpl = tpl.replace(/\%A_CLASSNAME\%/g, classname);
		tpl = tpl.replace(/\%TITLE\%/g, title);
		tpl = tpl.replace(/\%TARGET\%/g, (p.getBool('newwindow')?"_blank":"") );
		tpl = tpl.replace(/\%SUMMARY\%/g, '');
		_gel("FEED"+this.id+"entries").innerHTML += tpl;
		return tpl;
	};
	this.render = function(obj) {
		_gel("m_"+this.id).style.backgroundColor = "";
		if (!this.loaded && !obj) {this.retry("Information is temporarily unavailable.");return;}
		if (!this.loaded && obj.ErrorMsg && obj.ErrorMsg != "") {this.retry(obj.ErrorMsg);return;}
		if (!this.loaded && (!obj.Entry || obj.Entry.length == 0)) {
			this.retry("Information is temporarily unavailable.");return;
		}
		_gel("FEED"+this.id+"status").style.display = "none";
		if (obj.Entry) {
			_gel("FEED"+this.id+"entries").innerHTML = "";
			_gel("FEED"+this.id+"clear").style.display="block";

			var hide = false;
			var count = 0;
			this.firstlink = obj.Entry[0].Link;
			for (var i = 0; i < obj.Entry.length; i++) {
				if (!hide && obj.Entry[i].Link==this.lastseen) {
					hide = true;
				}
				if (!hide) {
					if (this.hideregexobj!=null && obj.Entry[i].Title && obj.Entry[i].Title.search(this.hideregexobj)>=0) {
						continue;
					}
					if (count==0) {
						_gel("FEED"+this.id+"display").style.display = "block";
					}
					this.addEntry(i,obj.Entry[i].Link,obj.Entry[i].Title,obj.Entry[i].Summary,obj.Entry[i].Date,!!(count++%2==1));
				}
			}
			if (count==0) {
				if (this.allowclear && !this.group) {
					_gel("FEED"+this.id+"clear").style.display="none";
				}
			}
		}
		this.loaded = true;
		setTimeout(function(){me.unhighlightNewItems();}, 10 * 1000);
	};
	this.unhighlightNewItems = function() {
		if (this.highlightNewItemsTimeout!=null) {
			clearTimeout(this.highlightNewItemsTimeout);
		}
		var entries = _gel('FEED'+this.id+'entries').getElementsByTagName('A');
		if (entries) {
			for (var i=0; entries && i<entries.length; i++) {
				var a = entries[i];
				if (a && typeof(a.className)=="string") {
					a.className = a.className.replace(new RegExp("\s*new"),'');
				}
			}
		}
	};
	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("FEED"+this.id+"clear").style.display = "none";
		_gel("FEED"+this.id+"display").style.display = "none";
		this.clearRefresh();
		this.startRefresh();
		this.refresh();
	};
	_IG_RegisterOnloadHandler(function(){me.init()});
}
</script>
<style>
#m___MODULE_ID__ { 
	border:2px solid #1b5790; 
}
#m___MODULE_ID__ .modboxin {
	padding:1px;
}
#m___MODULE_ID__ h2.modtitle {
	background-color:#1b5790;
	background-image:url(http://www.JavascriptToolbox.com/gadget/digg/digg.gif);
	background-position: top center;
	background-repeat:no-repeat;
	color:white;
}
#m___MODULE_ID___h a {
	color:white;
	font-size:smaller;
}
#m___MODULE_ID__ h2.modtitle td {
	font-size:smaller;
}
#m___MODULE_ID__ a.item {
	display:block;
	text-decoration:none;
	color:black;
}
#m___MODULE_ID__ a.alternate {
	background-color:#f6f6f6;
}
#m___MODULE_ID__ a:hover {
	background-color:#B2D281;
}
#m___MODULE_ID__ a.new {
	background-color:#ffffcc !important;
}
#m___MODULE_ID__ div.item, 
#m___MODULE_ID__ div.item * {
	white-space:nowrap;
	overflow:hidden;
	text-overflow:ellipsis;
}
#m___MODULE_ID__ div.clear {
	display:none;
	float:right;
	cursor:pointer;
	border:1px solid #ccc;
	border-color:#ccc #666;
	background-color:#E7F1D7;
	font-size:smaller;
}
#m___MODULE_ID__ div.status {
	font-size:smaller;
	color:#666;
	font-style:italic;
}
#m___MODULE_ID__ a.ddbox {background-image:url('/ig/images/arrow_green.gif') !important}
#m___MODULE_ID__ a.delbox {background-image:url('/ig/images/x_green.gif') !important}
#m___MODULE_ID__ a.minbox {background-image:url('/ig/images/min_green.gif') !important}
#m___MODULE_ID__ a.maxbox {background-image:url('/ig/images/max_green.gif') !important}
#m___MODULE_ID__ a.ddbox:hover {background-image:url('/ig/images/arrow_green_highlight.gif') !important}
#m___MODULE_ID__ a.delbox:hover {background-image:url('/ig/images/x_green_highlight.gif') !important}
#m___MODULE_ID__ a.minbox:hover {background-image:url('/ig/images/min_green_highlight.gif') !important}
#m___MODULE_ID__ a.maxbox:hover {background-image:url('/ig/images/max_green_highlight.gif') !important
</style>
<div id="FEED__MODULE_ID__status">
<div id="FEED__MODULE_ID__msg"><table cellspacing="0" cellpadding="0" border="0" height="100%" width="100%"><tr><td>
	<div id="FEED__MODULE_ID__statusmsg" class="status">Loading...</div>
</td></tr></table>
</div>
</div>

<div id="FEED__MODULE_ID__display" style="display:none;">
	<div id="FEED__MODULE_ID__clear" class="clear" title="Click to mark all items as read" onclick="digg.clear()">Clear</div>
	<div id="FEED__MODULE_ID__entries"></div>
</div>
     ]]> 
   </Content> 
</Module>
