// this script uses jQuery to pull Artist information via AJAX from Last.fm
(function($) {
    $.fn.getArtistBio = function(value) {
        var url = 'http://ws.audioscrobbler.com/2.0/?';
        var method = 'method=artist.getInfo';
        var apiKey = '&api_key=cb30fdc198b07e4734015f63f2f0d538';
        var json = '&format=json&callback=?';
        var artist = '&artist=' + value;
        var request = url + method + artist + apiKey + json
        var $this = $(this);

        $.getJSON(request, function(data) {

            if (data.error != null) {
                $this.find('[class=lfm_bio]').replaceWith('<div class=\'lfm_bio\'>Biography for ' + value + ' is unavailable.</div>');
            }
            else {
                $this.find('[class=lfm_bio]').replaceWith('<div class=\'lfm_bio\'>' + data.artist.bio.summary + '</div>');
            }
        });
    };
})(jQuery);
 
(function($) {
    $.fn.getSong = function(valueArtist, valueTrack) {
        var url = 'http://ws.audioscrobbler.com/2.0/?';
        var method = 'method=track.getInfo';
        var apiKey = '&api_key=cb30fdc198b07e4734015f63f2f0d538';
        var json = '&format=json&callback=?';
        var artist = '&artist=' + valueArtist;
        var track = '&track=' + valueTrack;
        var request = url + method + artist + track + apiKey + json;
        var $this = $(this);

        $.getJSON(request, function(data) {

            if (data.error != null) {
                // do a search and pick best match here?!
                alert(data.message);
            }
            else {
                if (data.track.wiki != null) {
                    $this.find('[class=lfm_bio]').append(data.track.wiki.summary);
                }
            }
        });
    };
})(jQuery);
