/*
var currentScript = (function (e) {
    if (e.nodeName.toLowerCase() == 'script') return e;
    if (e.lastChild == null) return e;
    return arguments.callee(e.lastChild);
})(document);
//alert(currentScript.nodeName);
*/

if ( window.jQuery === undefined ) {
    var xhr = null;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    xhr.open('GET', '/js/extlib/jquery/jquery-1.3.2.min.js', false);
    xhr.send(null);
    eval(xhr.responseText);
    jQuery.noConflict();
}

(function() {
    var j$ = jQuery;

    var element = document.createElement('div');
    element.innerHTML = '<div id="rankingApps" style="padding:3px"><h3 style="margin:5px 0;text-align:center;font-size:10pt"><a href="http://www.rainbowapps.com/"><img src="http://www.rainbowapps.com/image/global/logo_header.png" width="120" /></a><br /><a style="text-decoration:none" href="http://www.rainbowapps.com/software/ranking/user" target="_blank">Pick Up &#x30a2;&#x30d7;&#x30ea;</a></h3><ol style="margin:0;padding:0;list-style:none;border-top:1px dotted #CCC"></ol><p style="margin:0;text-align:center;font-size:9pt"><a style="color:#999" href="http://www.rainbowapps.com" target="_blank">by RainbowApps</a></p></div>';
    j$('#raRankingApps').after(element);

    var widget = j$(element).css({ width: '144px' });
    var base = 'http://www.rainbowapps.com';
    var api = 'http://api.rainbowapps.com/software/pickup';

    var app = j$('<li style="clear:left;float:none;border:none;border-bottom:1px dotted #CCC;margin:0;padding:5px 0"><a class="icon"><img style="float:left;margin:0 5px 0 0;border:none;padding:0" /></a><p style="margin:0;overflow:hidden;line-height:1"><strong style="color:#666;font-size:10pt"></strong> <a class="count" style="padding:2px;background-color:#EEE;font-size:8pt"></a><br /><a style="font-size:10pt" class="name"></a></p><div style="clear:left"></div></li>');

    j$.getJSON(api + '?cb=?', function(data) {
        j$.each(data.rows, function(i, item) {
            if ( 10 <= i++ ) return;
            var li = app.clone();
            li.find('.icon, .name').attr('href', base + '/software/' + item.software_id + '/');
            li.find('.count').attr('href', base + '/software/' + item.software_id + '/belonging').html(item.count_tweet + 'tweets');
            li.find('.name').html(item.software_name);
            li.find('img').attr({
                    'src': item.picture_uri,
                    'width': 50,
                    'height': 50,
                    'alt': item.software_name,
                    'title': item.software_name
                });
            widget.find('ol').append(li);
        });
    });

})();
