var infinateScroll = false;
$(document).ready(function(){
    $('#homeHeader').click(function(e){
        e.preventDefault();
        window.open('http://itunes.apple.com/gb/app/beermap/id313367328?mt=8','_blank');
    });

    $('#timelineMode').click(function(){
        expandTimeline();
    });

    if($('.timelineItem').length>=20){
        infinateScroll = true;
        $(window).scroll(function(){
            if  (infinateScroll && 
                    (($(document).height() - $(window).height()) -$(window).scrollTop())<10){
                expandTimeline();
                infinateScroll = false;
            }
        });
    }
});

function expandTimeline(){
    var id = $('.timelineItem:last').attr('id').replace(/r/,'');
    $('#refreshing').removeClass('hidden');
    $('#timelineMode').addClass('hidden');
    $.get('/index/timeline/',{maxId:id},function(data) {
        $('#refreshing').addClass('hidden');
        $('#timelineMode').removeClass('hidden');
        if(data==0){
            $('.timelineMore').html('No more tastes!');
        } else {
            infinateScroll = true;
            $('.timelineItems').append(data);
        }
    });
}

