document.write('<script type="text/javascript" src="http://media.digg.com/js/loader/229/JS_Libraries,jquery|JS_Libraries,jquery-noconflict|jquery-dom"></script>');
document.write('<link rel="stylesheet" type="text/css" media="all" href="http://digg.com/css/widget.css" />');

digg_width = typeof digg_width == 'string' ? digg_width : '300px';
digg_height  = typeof digg_height  == 'string' ? digg_height  : '350px';
digg_border  = typeof digg_border  == 'number' ? digg_border  : 1;
digg_target  = typeof digg_target  == 'number' ? '_blank' : '';
digg_count = typeof digg_count  == 'number' ? 'no-digg-count'  : '';
digg_description = typeof digg_description  == 'number' ? ''  : 'no-digg-description';
digg_theme = typeof digg_theme  == 'string' ? digg_theme  != 'digg-widget-unstyled' ? 'digg-widget '+digg_theme : digg_theme  : 'digg-widget digg-widget-theme1';
digg_title = typeof digg_title  == 'string' ? digg_title  : 'All popular stories in All Topics';
digg_custom_header = typeof digg_custom_header  == 'string' ? 'background: '+digg_custom_header+';'  : '';
digg_custom_border = typeof digg_custom_border  == 'string' ? 'border-color: '+digg_custom_border+';'  : '';
digg_custom_link = typeof digg_custom_link  == 'string' ? digg_custom_link  : '';
digg_custom_hoverlink = typeof digg_custom_hoverlink  == 'string' ? digg_custom_hoverlink  : '';
digg_custom_footer = typeof digg_custom_footer  == 'string' ? ' style="color: '+digg_custom_footer+'"'  : '';
digg_id = typeof digg_id  == 'string' ? ''+digg_id+''  : 'digg-widget-container';

document.write('<div id="'+digg_id+'" class="'+digg_theme+'" style="width: '+digg_width+'; border-width: '+digg_border*7+'px; '+digg_custom_border+'">');
document.write('<div class="digg-widget-header" style="'+digg_custom_header+'"><h1><a href="http://digg.com/?OTC-widget">Digg</a></h1><div><a href="http://digg.com/tour?OTC-widget">What is Digg?</a></div></div>');
document.write('<div class="digg-widget-topic">'+digg_title+'</div>');
document.write('<ul style="height: '+digg_height+';" class="'+digg_count+' '+digg_description+'"></ul>');
document.write('<div class="digg-widget-footer"><a href="http://digg.com/?OTC-widget"'+digg_custom_footer+'>Powered by Digg\'s Users</a></div></div>');

function diggwb(obj) {
    if (!digg || !digg.$) setTimeout(function() { diggwb(obj); }, 200); //hack for IE not loading scripts that are included via document.write until it decides too
    digg.$('#'+digg_id+' ul').html('');
    var tpl = function() {
	  if (this.thumbnail == undefined) {
		return [
				'li', {}, [
						   'a', { 'href': this.href+'?OTC-widget', 'class': 'digg-count' }, this.diggs+' <span>diggs</span>',
						   'h3', {}, [
									  'a', { 'href': this.href+'?OTC-widget', 'target':  digg_target, 'style': 'color: '+digg_custom_link+' ' }, this.title
									 ],
						   'p', {}, this.description
						  ]
			   ];
	  } else {
		return [
				'li', {}, [
						   'a', { 'href': this.href+'?OTC-widget', 'class': 'digg-count' }, this.diggs+' <span>diggs</span>',
						   'h3', {}, [
									  'a', { 'href': this.href+'?OTC-widget', 'target':  digg_target, 'style': 'color: '+digg_custom_link+' ' },
									  [
									   'img', {'src': this.thumbnail.src, 'alt': '', 'width': '30', 'height': '30', 'class': 'digg-thumbnail'}, '',
									   'span', {}, this.title
									  ],
									 ],
						   'p', {}, this.description
						  ]
			   ];
	  }
	};
    if(!obj) {
        digg.$('#'+digg_id+' ul').html('We were unable to retrieve matching stories from Digg. Please refresh the page to try again.');
    }
    if(!obj.stories || obj.stories.length == 0) {
        digg.$('#'+digg_id+' ul').html('Currently, there are no recent stories of this type on Digg.');
    }
    if (obj.stories) {
        for (var i = 0 ; i < obj.stories.length ; i++) {
            if(obj.stories[i].diggs > 10000) {
                obj.stories[i].diggs = Math.floor(obj.stories[i].diggs/1000)+'K+';
            }
            digg.$('#'+digg_id+' ul').tplAppend(obj.stories[i], tpl);
        }                 
    }
    if(digg_custom_hoverlink != '') {
        digg.$('#'+digg_id+' ul a').hover(function(){digg.$(this).css('color',  digg_custom_hoverlink)}, function() {digg.$(this).css('color',  digg_custom_link)});
    }
}
