document.tweets = $A()

function add_tweets(tw) {
  $A(tw).each(function(t) {
    document.tweets.push(t)
  })
}

function sort_tweets() {
  document.tweets = document.tweets.sortBy(function(tweet) { return new Date(tweet.created_at).getTime() });
  if(!Prototype.Browser.IE) {
    document.tweets = document.tweets.reverse();
  }
}

function setup_tweets() {
  sort_tweets()
  var container = $('tweets')
  for(var i = 0; i < 5; i++) {
    if(i < document.tweets.length) {
      var tweet = document.tweets[i]
      var li = new Element('li')
      li.insert(new Element('img', {'src': tweet.user.profile_image_url}))
      var h4 = new Element('h4')
      h4.update(new Element('a', {'href': 'http://twitter.com/' + tweet.user.name, 'target': '_blank'}).update(tweet.user.name))
      li.insert(h4)
      li.insert(new Element('p').update(tweet.text))
      container.insert(li)
    }
  }
}

    var FeedPresenter = Class.create({
      initialize: function(post, li) {
        this.post = post;
        this.li = li;
      },
      insertPost: function() {
        if (this.post.type == "photo") this.photo();
        else if (this.post.type == "link") this.link();
        else if (this.post.type == "status") this.status();
      },
      photo: function() {
        this.li.insert(new Element('img', {
          src: "http://imageresizer.steam-bank.com/48x48^?url=#{shot}&quality=61&crop=48x48&gravity=center".interpolate({shot: this.post.picture})
        }).addClassName('profile_image'));
        this.li.insert(new Element('p').addClassName('message').update(this.post.message.truncate(150)));
        this.li.addClassName('photo')
      },
      link: function() {
        this.li.insert(new Element('img', {
            src: 'https://graph.facebook.com/93731884481/picture'
        }).addClassName('profile_image'));
        if(this.post.description) {
        this.li.insert(new Element('p').addClassName('message').update(this.post.description.truncate(150)));
        } else {
          this.li.insert(new Element('p').addClassName('message').update(this.post.message.truncate(150)));
        }
        this.li.addClassName('link');
      },
      status: function() {
        this.li.insert(new Element('img', {
          src: 'https://graph.facebook.com/93731884481/picture'
        }).addClassName('profile_image'));
        this.li.insert(new Element('p').addClassName('message').update(this.post.message.truncate(150)));
        this.li.addClassName('status');
      },
      insertReadMore: function() {
        this.li.insert(new Element('a', {
            'href': 'http://www.facebook.com/pages/Rosemount-Australian-Fashion-Week/93731884481',
            'target': '_blank'
        }).update("Read more..."));
      }
    });
    
    function add_feed(fbposts) {
        if (fbposts) {
            var container = $('facebook_feed');
            var fbfeed = $A(fbposts['data']);
            for (var i = 0; i < 6; i++) {
                if (i < fbfeed.length) {
                    var post = fbfeed[i];
                    var li = new Element('li');
                    var feedPresenter = new FeedPresenter(post, li);
                    feedPresenter.insertPost();
                    feedPresenter.insertReadMore();
                    
                    $('facebook_feed').insert(li);
                }
            }
        }
    }