$(function() {
    if ($('.feed').length>0) {
        $('.feed').css('height', $('.cause_about').height()>150?$('.cause_about').height():150);
        $('.feed').tweet({
           username:cause_twitter,
           count:20,
           loading_text:'Loading tweets...'
        });
        $('.feed').bind('loaded', function() {
            setTimeout('scroll_feed()', 5000);
        });
    }
    $('.twitter_publish').click(function() {
        window.open('http://twitter.com/?status=Check out @'+cause_twitter+'\'s page on JustCoz - http://justcoz.org/'+cause_twitter);
    });
    $('.dialog_x').click(function() {
        floater();
    });
    $('.hp_add_button').click(function() {
        floater($('.add_cause_dialog'), true);
    });
    try {
        $('.the_content').linkUrl().linkUser().linkHash();
    } catch(e) {
        
    }
});

function scroll_feed()
{
    if ($('.feed').height()<$('.feed').get(0).scrollHeight) {
        var s_item = $('.feed ul li').eq(0);
        item_h = s_item.height()+parseInt(s_item.css('padding-bottom'))+parseInt(s_item.css('margin-bottom'));
        $('.feed').animate({
                scrollTop:item_h
            }, {
                duration:1000,
                complete:function() {
                    $('.feed ul li').eq(0).detach().appendTo('.feed ul');
                    $('.feed').scrollTop(0);
                    setTimeout('scroll_feed()', 5000);
                }
            });
    }
}

function floater(item, show, timeout, fade)
{
    if (item && item.length==0)
        return;
    if (show) {
        $('.full_overlay').show();
        var floater_offset = item.offset();
        var floater_height = item.height()+parseInt(item.css('padding-top'))+parseInt(item.css('padding-bottom'));
        var floater_top = ($('body').height()-floater_height)/2;
        if (floater_top < 10) floater_top = 10;
        floater_top += $(document).scrollTop();
        var floater_width = item.width()+parseInt(item.css('padding-left'))+parseInt(item.css('padding-right'));
        var floater_left = ($('body').width()-floater_width)/2;
        if (floater_left < 0) floater_top = 0;
        //var top = ($('body').height()-(item.height()+parseInt(item.css('padding-top'))+parseInt(item.css('padding-bottom'))))/2+$(document).scrollTop();
        //var left = ($('body').width()-(item.width()+parseInt(item.css('padding-left'))+parseInt(item.css('padding-right'))))/2;
        //if (top<20) top=20;
        //if (left<0) left=0;
        item.show().offset({top:floater_top, left:floater_left});// css('top', top).css('left', left).show();
        //item.css('top', ($('body').height()-item.height())/2).css('left', ($('body').width()-item.width())/2).show();
        if (timeout) {
            setTimeout('floater(false, false, false, 300)', timeout);
        }
    } else {
        if (fade) {
            $('.full_overlay').fadeOut(fade);
            $('.floater').fadeOut(fade);
        } else {
            $('.full_overlay').hide(fade);
            $('.floater').hide(fade);
        }
    }
}

