﻿function activeUrls(text) 
{
    text = text.replace(/\s(http:\/\/[^\s\<]+)(\s)?/gi, ' <a href="$1">$1</a>$2');
    return text.replace(/(^|\s:?)@(\w+)/gi, ' <a href="http://twitter.com/$2">@$2</a>');
}

var addthis_config = {
    username: 'eclectica',
    ui_cobrand: 'Eclectica',
    data_use_flash: false
}

// data_ga_tracker: 'pageTracker'

$(document).ready(function() {

    $.getJSON('/ajax_twitter.aspx', function(data) {

        var output = new Array();

        $.each(data, function(i, item) {

            output.push('<p>' + item.date + ': ' + activeUrls(item.text) + '</p>');

        });

        $('#tweets').html(output.join(''));

    });

    $('div.post img.captionb, div.post img.captionw, div.post img.captionn').each(function(i, item) {
        var img = $(item);
        var cls = img.attr('class');
        var caption = img.attr('alt');

        img.after('<div class="' + cls + '"><p>' + caption + '</p></div>');
    });

    $('div.item').each(function() {

        if ($(this).attr('id')) {
            $('#' + $(this).attr('id') + ' .addthis_toolbox a').attr('addthis:title', title[$(this).attr('id')])
                                                 		   .attr('addthis:url', url[$(this).attr('id')])
                                                 		   .attr('addthis:description', description[$(this).attr('id')]);
        }
    });
    
});