function open_popup(url, title, width, height) {
    if (typeof(width) == 'undefined')
        var width = 700;
    if (typeof(height) == 'undefined')
        var height = 400;
    var top = (screen.height/2) - (height/2);
    var left = (screen.width/2) - (width/2);
    window.open(url, title, 'toolbar=no,top='+top+',left='+left+',width='+width+',height='+height+',scrollbar=yes,resizable=yes');
}

function share_twitter(url, msg) {
    url = 'http://contenido.com.br' + url;
    open_popup('http://twitter.com/?status=' + encodeURIComponent(url), 'twitter', 800, 400);
    return false;
}

function share_orkut(url, title, msg) {
    url = 'http://contenido.com.br' + url;
    open_popup('http://promote.orkut.com/preview?nt=orkut.com&tt=' + encodeURIComponent(title) + '&du=' + encodeURIComponent(url) + '&cn=' + encodeURIComponent(msg), 'orkut', 800, 400);
    return false;
}

function share_facebook(url, msg) {
    url = 'http://contenido.com.br' + url;
    open_popup('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(url) + '&bodytext=&topic=', 'facebook');
    return false;
}

function share_email(url, msg) {
    url = 'http://contenido.com.br' + url;
    open_popup('/indicar-postagem/?url=' + encodeURIComponent(url) + '&msg=' + encodeURIComponent(msg), 'indicar por email');
    return false;
}

function video_by_email(title) {
    var url = document.location.href;
    open_popup('/indicar-video/?url=' + encodeURIComponent(url) + '&titulo=' + encodeURIComponent(title), 'indicar por email');
    return false;
}

$(document).ready(function() {
    $('body').append('<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/_contenido_.json?callback=show_twitts&count=4"></script>');
});

function show_twitts(twitters) {
    var statusHTML = [];
    for (var i=0; i<twitters.length; i++) {
        var username = twitters[i].user.screen_name;
        var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
            return '<a href="'+url+'">'+url+'</a>';
        }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
            return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
        }).replace(/\B#([_a-z0-9]+)/ig, function(search) {
            return search.charAt(0)+'<a href="http://twitter.com/#search?q='+search.substring(1)+'">'+search.substring(1)+'</a>';
        });
        statusHTML.push('<div><h2>'+relative_time(twitters[i].created_at)+'</h2><p>'+status+'</p></div>');
    }
    document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
}

function two_digits(x) {
    x = '' + x;
    if(x.length == 1)
        return '0' + x;
    else
        return x;
}

function relative_time(time_value) {
    var values = time_value.split(" ");
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3] + " +0000";
    var epoch = Date.parse(time_value);
    var parsed_date = new Date();
    parsed_date.setTime(epoch);
    return two_digits(parsed_date.getDate()) + '-' + two_digits(parsed_date.getMonth() + 1) + '-' + parsed_date.getFullYear() + ' ' + two_digits(parsed_date.getHours()) + ':' + two_digits(parsed_date.getMinutes());
}

