/**
 * High Gear Media LTD. 2009
 * Created: 3-3-2009
 * Summery: Show Digg main stories for Autos Category.
 */
var $j = jQuery; //avoid conflicts
document.write('<script type="text/javascript" src="http://digg.com/js/jquery-dom.js"></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 (!$j) setTimeout(function() { 
        diggwb(obj);
    }, 200); //hack for IE not loading scripts that are included via document.write until it decides too
    $j('#'+digg_id+' ul').html('');
    var tpl = function() {
        if (this.thumbnail == undefined) {
            this.thumbnail = {
                "src": "/images/website/diggLogoThumb.gif",
                "alt": "Digg Logo"
            };
        }
        else {
            this.thumbnail.alt = "Story Thumbnail"
        }
        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": this.thumbnail.alt,
            "width": "30px",
            "height": "30px",
            "class": "diggImg"
        }, "",
        "span",{}, this.title
        ]
        ],
        'p', {}, this.description
        ]
        ];
    };
    if (!obj) {
        $j('#'+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) {
        $j('#'+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+';
            }
            $j('#'+digg_id+' ul').tplAppend(obj.stories[i], tpl);
        }
    }
    if (digg_custom_hoverlink != '') {
        $j('#'+digg_id+' ul a').hover(function(){
            $j(this).css('color',  digg_custom_hoverlink)
        },  function() {
                $j(this).css('color',  digg_custom_link)
            });
    }
}
