var DAND = DAND || {};

DAND.songkick = (function($) {
    
    // settings
    var s = {
        
        $calendar: false,
        apiKey: 'T8hQpFO9A6z9C3lV',
        userId: 643609
        
    };
    
    
    function init() {
        
        s.$calendar = $('#sk-calendar');
        
        if (s.$calendar.length) {
            getEvents();
        }
        
    }
    
    
    function getEvents() {

        $.ajax({
            
            type: 'GET',
            url: 'http://api.songkick.com/api/3.0/artists/' + s.userId + '/calendar.json?jsoncallback=?&apikey=' + s.apiKey,
            dataType: 'jsonp',
            success: renderEvents
            
        });
        
    }
    
    
    function renderEvents(r) {
        
        var template = '<li><a href="{url}"><strong>{date}</strong>: {title} / {location}</a></li>',
            markup = [];
        
        $.each(r.resultsPage.results.event, function(i, event) {
            var item = template,
                date = event.start.date.split('-');
                
            date = date[2] + '.' + date[1] + '.' + date[0];
            
            item = item.replace('{date}', date);
            item = item.replace('{url}', event.uri);
            item = item.replace('{title}', event.displayName);
            item = item.replace('{location}', event.location.city);
            
            markup.push(item);
        });

        s.$calendar.append('<ul>' + markup.join('') + '</ul>');
    }
    
    
    $(init);
    
}(jQuery));
