<?xml version="1.0" encoding="UTF-8" ?>
<Module>
	<ModulePrefs 
		title="Super Digg" 
		title_url="http://www.digg.com"
		directory_title="Super Digg - Links Directly To Stories"
		description="A Customizable Digg feed using the Digg API to give you full control and links 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. Very configurable and customizable!" 
		author="Matt Kruse" 
		author_email="gadget@mattkruse.com" 
		screenshot="http://www.javascripttoolbox.com/gadget/superdigg/superdigg.png" 
		thumbnail="http://www.javascripttoolbox.com/gadget/superdigg/superdigg_thumb.png"
		author_affiliation="Matt Kruse" 
		author_location="IL, USA" 
		author_link="http://www.JavascriptToolbox.com/" 
		height="20"
		> 
		<Require feature="dynamic-height" /> 
		<Require feature="setprefs" /> 
	</ModulePrefs>
		<UserPref name="url" datatype="hidden" default_value="http://services.digg.com"/>
		<UserPref name="endpoint" display_name="Endpoint:" default_value="/stories/popular"/>
		<UserPref name="appkey" display_name="AppKey:" default_value="http://igoogle.com"/>
		<UserPref name="num" display_name="Max Items to show (1-100):" default_value="50"/>
		<UserPref name="newwindow" display_name="Open Links In A New Window:" datatype="bool" default_value="true" />
		<UserPref name="fontsize" display_name="Font Size:" default_value="80%"/>
		<UserPref name="favicon" display_name="Display Site Icon:" datatype="bool" default_value="true"/>
		<UserPref name="refresh" display_name="Refresh Interval (minutes):" default_value="5" />
		<UserPref name="lasturl" display_name="Last Seen URL (Advanced!):" default_value=""/>
		<UserPref name="tpl" display_name="Item Template (Advanced!):" default_value="&lt;div class=item&gt;&lt;a href=&quot;%link%&quot; title=&quot;Right-Click for Details!&quot; class=&quot;%classname%&quot; target=&quot;%target%&quot; oncontextmenu=&quot;_toggle(this.nextSibling);resize();return false&quot;&gt;%bullet%%title%&lt;/a&gt;&lt;div style=&quot;display:none&quot;&gt;%thumbnail%&lt;b&gt;URL&lt;/b&gt;:%link%&lt;br&gt;&lt;b&gt;Description&lt;/b&gt;:%description%&lt;br&gt;&lt;b&gt;Topic&lt;/b&gt;:%topic.name%&lt;br&gt;&lt;b&gt;Diggs&lt;/b&gt;:%diggs%&lt;br&gt;&lt;b&gt;&lt;a href=&quot;%href%&quot; target=&quot;blank&quot;&gt;Comments:&lt;/a&gt;&lt;/b&gt; %comments%&lt;br&gt;&lt;b&gt;Submit Date&lt;/b&gt;:%submit_date%&lt;/div&gt;&lt;/div&gt;"/>
		<Content type="html"> 
		<![CDATA[<script type="text/javascript">
var prefs = new _IG_Prefs();
var interval = null;
var new_last_url = "";
var last_url = prefs.getString('lasturl');

// Set the status message
function msg(txt) {
	var status = _gel("status");
	status.innerHTML = txt;
	status.style.display = "block";
	_gel("display").style.display = "none";
}

// Start the refresh cycle
function startRefresh() {
	interval = setInterval(refresh, (60000 * prefs.getInt('refresh')));
}
function clearRefresh() {
	if (interval) {
		clearInterval(interval); 
	}
}

// Load content
function load() {
	var url = prefs.getString("url");
	url += prefs.getString("endpoint");
	url += "?count="+prefs.getInt("num");
	url += "&appkey="+escape(prefs.getString("appkey"))+"?d="+(new Date()).getTime();
	url += "&type=json&size=s";
	_IG_FetchContent(url,render);
}

// Trigger a refresh of content
function refresh() {
	msg("Refreshing...");
	resize();
	load();
}

// Create the content for an entry
function createEntry(index, story, alternate) {
	var tpl = prefs.getString('tpl');
	var favicon = "&#187;&nbsp;";
	if (prefs.getBool("favicon") && story.link && story.link.search(/^(https?:\/\/[^\/]+)/)>=0) {
		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(/\%index\%/g, index);
	tpl = tpl.replace(/\%bullet\%/g,favicon); 
	tpl = tpl.replace(/\%classname\%/g, "item" + ((alternate)?" alternate":""));
	tpl = tpl.replace(/\%target\%/g, (prefs.getBool('newwindow')?"_blank":"") );
	var thumbnail = "";
	if (story.thumbnail && story.thumbnail.src) {
		thumbnail = '<img src="'+story.thumbnail.src+'" style="float:left;" height="'+story.thumbnail.height+'" width="'+story.thumbnail.width+'" hspace="5">';
	}
	tpl = tpl.replace(/\%thumbnail\%/g, thumbnail);
	// Replace all story properties
	tpl = tpl.replace(/\%([^\%]+)\%/g, 
		function(){
			var prop = arguments[1];
			var o = story;
			// Allow for nested properties like topic.short_name
			if (prop.indexOf(".")>0) {
				var prop2 = prop.split(".");
				if (prop2 && prop2[0]) {
					o = story[prop2[0]];
					prop = prop2[1];
				}
			}
			return _hesc(""+((o&&o[prop])?o[prop]:""));
		}
	);
	return tpl;
}

// Render the display from the FetchContent response
function render(response) {
	var json = eval("("+response+")");
	if (!json || !json.stories || !json.stories.length>0) {
		retry("Information is temporarily unavailable.");
		return;
	}
	_gel("status").style.display = "none";
	_gel("clear").style.display="block";
	var count = 0;
	var html = "";
	var hide = false;
	for (var i = 0; i < json.stories.length; i++) {
		var story = json.stories[i];
		if (i==0) {
			new_last_url = story.link;
		}
		if (!hide && story.link==last_url) {
			hide = true;
		}
		if (!hide) {
			if (count==0) {
				_gel("display").style.display = "block";
			}
			html += createEntry(i,story,!!(count++%2==1));
		}
	}
	_gel("entries").innerHTML = html;
	if (count==0) {
		msg('No new stories. <a href="#" onclick="clearRefresh();startRefresh();refresh();return false;">Refresh Now</a>');
	}
	resize();
}

// Call to resize container when content changes
function resize() {
	_IG_AdjustIFrameHeight();
}

// Try to load content again after a delay in case of error
function retry(msgText) {
	if (msgText) { msg(msgText); }
	clearRefresh();
	setTimeout(refresh,10*1000);
}

// Clear items and mark them as read
function clearAll() {
	prefs.set('lasturl',new_last_url);
	last_url = new_last_url;
	_gel("clear").style.display = "none";
	_gel("display").style.display = "none";
	clearRefresh();
	startRefresh();
	refresh();
};

// Init
_IG_RegisterOnloadHandler(function(){startRefresh();load();});

document.write("<style>#status,#display{font-size:"+prefs.getString("fontsize")+";}</style>");
</script>
<style>
a.item {
	display:block;
	text-decoration:none;
	color:black;
}
a.alternate { background-color:#EDF1E6; }
a:hover { background-color:#B2D281; }
div.item, div.item a {
	white-space:nowrap;
	overflow:hidden;
	text-overflow:ellipsis;
}
div.item div {
	font-size:90%;
	white-space:normal;
	overflow:visible;
	border:1px solid #1B5790;
	background-color:#E5ECF3;
}
div.clear {
	display:none;
	float:right;
	cursor:pointer;
	border:1px solid #ccc;
	border-color:#ccc #666;
	background-color:#E7F1D7;
	font-size:80%;
	padding:0;
	margin:0;
}
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="clearAll()">Clear</div>
<div id="entries"></div>
</div>]]></Content> 
</Module>
