﻿var DELICIOUS_ICON = 'http://www.1x1.jp/blog/wp-content/delicious.gif';

function delicious_count ( url ) {
    if ( ! url ) url = location.protocol+'//'+location.hostname+location.pathname;
    var encurl = encodeURIComponent(url);
    var delicious_html = '<a href="http://del.icio.us/url/?url='+encurl+'"'+
        ' title="del.icio.us">'+
        '<span id="delicious_count_'+encurl+'"'+
        ' style="font-size:10px; vertical-align:top;'+
        ' font-weight:bold; text-decoration:underline;"></span></a> ';
    document.write( delicious_html );

    var delicious_script = function () {
        var script = document.createElement( 'script' );
        script.type = 'text/javascript';
        script.charset = 'UTF-8';
        script.src = 'http://badges.del.icio.us/feeds/json/url/data?'+
            'url='+encurl+'&callback=delicious_callback';
        document.body.appendChild( script );
    };

    if ( window.Event && Event.observe ) {
        Event.observe( window, 'load', delicious_script, false ); 
    } else {
        delicious_script();
    }
}

function delicious_callback( data ) {
    if ( ! data ) return;
    var first = data[0];
    if ( ! first ) return;

    var encurl = encodeURIComponent(first.url);
    var span = document.getElementById( 'delicious_count_'+encurl );
    if ( ! span ) return;

    var text = document.createTextNode( first.total_posts+' people ' )
    span.appendChild( text );
}

