// Load FeedMagnet
var server = 'my.feedmagnet.com';
ready = function(fx) {
    if (typeof $FM !== 'undefined' && typeof $FM.ready === 'function') { $FM.ready(fx); }
    else { window.setTimeout(function() { ready.call(null, fx); }, 50); }
};
var fmjs = document.createElement('script');
fmjs.src = ('https:' === document.location.protocol ? 'https://' : 'http://') + server + '/embed.js';
fmjs.setAttribute('async', 'true'); document.documentElement.firstChild.appendChild(fmjs);
ready(function($, _) {
    
    var feed = $FM.Feed(['sxsw']).connect('new_update', function(self, data) {
        var update = data.update.data;
        
        // sometimes we don't have a permalink, in which case we just want the text with out the <a /> tag
        var permalink;
        if (update.permalink) {
            permalink = '<p class="info"><a target="_blank" href="' + update.permalink + '">' + _(update.timestamp).pretty_time() + ' via ' + update.channel + '</a></p>'; 
        } else {
            permalink = '<p class="info">' + _(update.timestamp).pretty_time() + ' via ' + update.channel + '</p>';
        }
        
        // build the HTML
        var html = '';
        html += '<div class="update">';
        html +=     '<div class="avatar">';
        if (update.author.profile) html += '<a target="_blank" href="' + update.author.profile + '">';
        html +=         '<img src="' + update.author.avatar + '" alt="' + update.author.alias + '" />';
        if (update.author.profile) html += '</a>';
        html +=     '</div>';
        html +=     '<div class="content">';
        var i;
        for (i = 0; i < update.photos.length; i++) {
            if (update.photos[i].role == "photo") html += '<a target="_blank" href="' + update.photos[i].permalink + '"><img class="photo" src="' + update.photos[i].local_url + '165" /></a>';
        }
        html +=         '<p class="text">' + update.text + '</p>';
        for (i = 0; i < update.links.length; i++) {
            html += '<p class="link">&rsaquo; <a target="_blank" href="' + update.links[i].short_url + '">' + update.links[i].domain + '</a><br/></p>';
        }
        html +=         permalink;
        html +=     '</div>';
        html += '</div>';
        data.update.html = html;
    });
    
    var el = $FM.Element('.feed').connect('updates_placed', {
        'strong': true,
        'callback': function(self, data) {
            data.$new_updates.slideDown();
            for (var i = 10; i < data.updates.length; i++) {
                data.updates[i]._el.remove();
            }
        	while (data.updates.length > 10) { data.updates.pop(); }
        }
    }).display(feed);
    
    feed.poll({'wait': 5});
    
    // use this if we want to totally hide the pie chart
    //$('.pie').remove();
    //$('.feed').css({'top': 200});
    
    // hide the pie chart altogether if we are on IE
    if(navigator.appName == 'Microsoft Internet Explorer') {
        $('.pie').remove();
        $('.feed').css({'top': 200});
        
    // otherwise, let's draw the pie chart
    } else {
        feed.count({
            'detail': 'source',
            'success': function(self, data) {
                console.log(data);
                $('.pie').pie({
                    'data': [
                        {'value': data.response.groups['sxsw'].sources[1].counts.within_the_last_day, 'label': "Interactive"},
                        {'value': data.response.groups['sxsw'].sources[0].counts.within_the_last_day, 'label': "Film"},
                        {'value': data.response.groups['sxsw'].sources[2].counts.within_the_last_day, 'label': "Music"}
                    ]
                });
            }
        });
    }
    
    
});
