$(function(){
  var page = 1;
  var twitter = $('.twitter');
  
  $('.tweet').addClass('clickable');
  
  $('.clickable').live('click', function(){
    var url = $(this).find('a.store').attr('href');
    document.location.href = url;
  });
  
  $('.tweets').wrap('<div class="tweets_scroll"><div class="tweets_container"></div></div>');
  var container = $('.tweets_scroll');
  var inner = $('.tweets_container');
  // position relative to make scroll work correctly in ie6 and 7
  container.css('position','relative').css('overflow', 'auto');
  //inner.css('position','relative').css('width', container.width());
  //container.css('overflow', 'auto');
  var more_tweets = $('<div class="more_tweets">view more</div>');
  more_tweets.css('position', 'relative'); // fixes missing text in ie7
  inner.append(more_tweets);
  container.css('height', container.outerHeight());

  more_tweets.click(function(){
    more_tweets.html('loading...');
    page += 1;
    $.get('/tweets?page='+page, function(data){
      var new_tweets = $(data).addClass('clickable');
      $('.tweets').append(new_tweets);
      var offset = inner.outerHeight() - container.height();
      container.animate({scrollTop:offset}, 600);
      more_tweets.html('view more');
    });
    return false;
  });
});

