$(function(){
    $('#social_aggregation').dsTab({activeSelector:'li', after:TruncateSocial});
    $('#main_rotational').append('<a class="btn prev" href="">Previous Slide</a>');
    $('#main_rotational').append('<a class="btn next" href="">Next Slide</a>');
    $('#main_rotational div.slides').cycle({
        timeout: 5000
        , pause: 1
        , next: '#main_rotational a.next'
        , prev: '#main_rotational a.prev'
    });

    Shadowbox.init();
    $('#social_aggregation').delegate('a.shadowbox_photo', 'click', function(e){
        e.preventDefault();

        Shadowbox.open({
            content: $(this).attr('href')
            , player: 'img'
        });
    });
    

    $('#social_aggregation').delegate('a.shadowbox_video', 'click', function(e){
        e.preventDefault();

        Shadowbox.open({
            content: $(this).attr('href')
            , player: 'swf'
            , width: 853
            , height: 505
        });
    });

    PullSocial();
});

function TruncateSocial(clickedLink, shownBox){
    $('div.video', shownBox).each(function(){$(this).dsTruncate({boxToTruncate: 'p.summary'})});
    $('div.blog', shownBox).each(function(){$(this).dsTruncate({boxToTruncate: 'p.summary'})});
}

function PullSocial(){
    $.getJSON('/action/pull_social.php', LoadSocial)
}

function LoadSocial(data){
    $('#social_all_box div.video').each(function(i){
        if (i%2) {
            var item = data.videos[Math.floor(i/2)];
        } else {
            var item = data.videos_knox[Math.floor(i/2)];
        }

        var el = $(this);
        el.html('<span class="'+((item.user=='enjoyknoxville' || item.user=='Enjoy Knoxville')?'knox_tag':'tag')+'">Video</span><a class="img_wrap shadowbox_video" href="'+item.url+'"><img alt="" src="'+item.thumb+'" /></a><h2>'+item.title+'</h2><p class="summary">'+item.description+'</p><div class="source"><a href="'+item.href+'">source</a></div>');
        var share = $('<div class="share"><a href="">Share</a></div>').appendTo(el);
        addthis.button(share[0], {}, {url: item.href, title: item.title});
        $(this).dsTruncate({boxToTruncate: 'p.summary'});
    });
    
    $('#social_videos_box div.video').each(function(i){
        if (i%2) {
            var item = data.videos[Math.floor(i/2)];
        } else {
            var item = data.videos_knox[Math.floor(i/2)];
        }

        var el = $(this);
        el.html('<span class="'+((item.user=='enjoyknoxville' || item.user=='Enjoy Knoxville')?'knox_tag':'tag')+'">Video</span><a class="img_wrap shadowbox_video" href="'+item.url+'"><img alt="" src="'+item.thumb+'" /></a><h2>'+item.title+'</h2><p class="summary">'+item.description+'</p><div class="source"><a href="'+item.href+'">source</a></div>');
        var share = $('<div class="share"><a href="">Share</a></div>').appendTo(el);
        addthis.button(share[0], {}, {url: item.href, title: item.title});
    });

    $('#social_all_box div.tweet').each(function(i){
        var item = data.tweets[i];

        var text = item.text.replace(/http:\/\/([^ ]+)/g, '<a href="http://$1" target="_blank">http://$1</a>');
        text = text.replace(/@([^ ]+)/g, '@<a href="http://twitter.com/$1" target="_blank">$1</a>');
        text = text.replace(/#([^ ]+)/g, '<a href="http://twitter.com/#search?q=%23$1" target="_blank">#$1</a>');

        var el = $(this);
        el.html('<span class="'+(item.user=='enjoyknoxville'?'knox_tag':'tag')+'">Tweet</span><p class="text"><a class="self" href="http://twitter.com/'+item.user+'" target="_blank">'+item.user+'</a><span> </span>'+(item.retweeted ? 'RT ' : '')+text+'</p><p class="meta"><a href="http://twitter.com/'+item.user+'/status/'+item.id_str+'">'+item.date+'</a> via '+item.source+(item.reply_to && item.reply_to_id ? ' <a href="http://twitter.com/'+item.reply_to+'/status/'+item.reply_to_id+'">in reply to '+item.reply_to+'</a>' : '')+'</p>');
        var share = $('<div class="share"><a href="">Share</a></div>').appendTo(el);
        addthis.button(share[0], {}, {url: 'http://twitter.com/'+item.user+'/status/'+item.id_str, title: 'Tweet from '+item.user+(item.reply_to?' to '+item.reply_to:'')});
    });
    
    $('#social_tweets_box div.tweet').each(function(i){
        var item = data.tweets[i];

        var text = item.text.replace(/http:\/\/([^ ]+)/g, '<a href="http://$1" target="_blank">http://$1</a>');
        text = text.replace(/@([^ ]+)/g, '@<a href="http://twitter.com/$1" target="_blank">$1</a>');
        text = text.replace(/#([^ ]+)/g, '<a href="http://twitter.com/#search?q=%23$1" target="_blank">#$1</a>');

        var el = $(this);
        el.html('<span class="'+(item.user=='enjoyknoxville'?'knox_tag':'tag')+'">Tweet</span><p class="text"><a class="self" href="http://twitter.com/'+item.user+'" target="_blank">'+item.user+'</a><span> </span>'+(item.retweeted ? 'RT ' : '')+text+'</p><p class="meta"><a href="http://twitter.com/'+item.user+'/status/'+item.id_str+'">'+item.date+'</a> via '+item.source+(item.reply_to && item.reply_to_id ? ' <a href="http://twitter.com/'+item.reply_to+'/status/'+item.reply_to_id+'">in reply to '+item.reply_to+'</a>' : '')+'</p>');
        var share = $('<div class="share"><a href="">Share</a></div>').appendTo(el);
        addthis.button(share[0], {}, {url: 'http://twitter.com/'+item.user+'/status/'+item.id_str, title: 'Tweet from '+item.user+(item.reply_to?' to '+item.reply_to:'')});
    });

    $('#social_all_box div.photo').each(function(i){
        var item = data.photos[i];
        if (item) {
            var el = $(this);
            el.html('<span class="'+(item.user=='enjoyknoxville'?'knox_tag':'tag')+'">Photo</span><a class="img_wrap shadowbox_photo" href="'+item.src+'"><img alt="" src="'+item.thumb+'" /></a><div class="source"><a href="'+item.url+'">src</a></div>');
            var share = $('<div class="share"><a href="">Share</a></div>').appendTo(el);
            addthis.button(share[0], {}, {url: item.url, title: item.title});
        }
    });
    
    $('#social_photos_box div.photo').each(function(i){
        var item = data.photos[i];

        if (item) {
            var el = $(this);
            el.html('<span class="'+(item.user=='enjoyknoxville'?'knox_tag':'tag')+'">Photo</span><a class="img_wrap shadowbox_photo" href="'+item.src+'"><img alt="" src="'+item.l_url+'" /></a><div class="source"><a href="'+item.url+'">src</a></div>');
            var share = $('<div class="share"><a href="">Share</a></div>').appendTo(el);
            addthis.button(share[0], {}, {url: item.url, title: item.title});
        }
    });

    $('#social_all_box div.blog').each(function(i){
        var item = data.blogs[i];
        var el = $(this);
        el.html('<span class="knox_tag">Blog</span><h2><a href="'+item.link+'">'+item.title+'</a></h2><p class="summary">'+item.summary+'</p><p class="more"><a href="'+item.link+'">Continue Reading &raquo;</a></p>');
        var share = $('<div class="share"><a href="">Share</a></div>').appendTo(el);
        addthis.button(share[0], {}, {url: item.link, title: item.title});
        el.dsTruncate({boxToTruncate: 'p.summary'});
    });
    
    $('#social_blog_box div.blog').each(function(i){
        var item = data.blogs[i];
        var el = $(this);
        el.html('<span class="knox_tag">Blog</span><h2><a href="'+item.link+'">'+item.title+'</a></h2><p class="summary">'+item.summary+'</p><p class="more"><a href="'+item.link+'">Continue Reading &raquo;</a></p>');
        var share = $('<div class="share"><a href="">Share</a></div>').appendTo(el);
        addthis.button(share[0], {}, {url: item.link, title: item.title});
    });
}

