(function() {
  var cs;

  cs = window.CS || (window.CS = {});

  cs.Data || (cs.Data = {});

  cs.Views || (cs.Views = {});

  cs.Views.HomeView = Backbone.View.extend({
    initialize: function(options) {
      _.bindAll(this);
      this.$el = $(this.el);
      this.appModel = options.appModel;
      this.id = this.$el.data('id');
      this.appModel.bind('site-preloader-animation-in-complete', this.getTwitterStatus);
      this.renderCoffeePriceIndex();
      this.getHomepageData();
      this.setupMailchimp();
    },
    getTwitterStatus: function() {
      var accounts;
      accounts = ['supreme1993'];
      this.twitterHandle = accounts[Math.floor(Math.random() * accounts.length)];
      $.ajax({
        url: 'http://api.twitter.com/1/statuses/user_timeline.json',
        data: {
          screen_name: this.twitterHandle,
          include_rts: 1
        },
        dataType: 'jsonp',
        success: this.haveTweets
      });
    },
    haveTweets: function(data) {
      var _this = this;
      _.each(data, function(tweet) {
        if (tweet.in_reply_to_screen_name === null && _this.tweetModel === null) {
          _this.tweetModel = new Backbone.Model();
          _this.tweetModel.set(tweet);
        }
      });
      if (this.tweetModel != null) {
        this.renderTweet();
      } else {
        this.tweetModel = new Backbone.Model();
        this.tweetModel.set(data[0]);
        this.renderTweet();
      }
    },
    renderTweet: function() {
      var $tweet, $twitter;
      $twitter = this.$('#twitter');
      $twitter.removeClass('loading');
      $twitter.append("    <div class=\"tweet\">      <p>" + (bbb.utils.linkifyTweetText(this.tweetModel.get('text'))) + "</p>    </div>    <a class=\"follow\" href=\"http://twitter.com/" + this.twitterHandle + "\" target=\"_blank\">Follow <span class=\"handle\">@" + this.twitterHandle + "</span></a>    ");
      $twitter.addClass('loaded');
      $tweet = $twitter.find('.tweet p');
      $tweet.css({
        marginTop: 0 - ($tweet.height() / 2)
      });
    },
    renderCoffeePriceIndex: function() {
      var $el;
      $el = $('#coffee-price-index');
      $el.removeClass('loading');
      $el.html("    <h4>Coffee Price Index</h4>    <p class=\"row\">      Supreme Blend      <span class=\"price number\">" + cs.Data.coffeePriceIndex.supreme + "</span>    </p>    <p class=\"row\">      C Market      <span class=\"price number\">" + cs.Data.coffeePriceIndex.cmarket + "</span>    </p>    <p class=\"row\">      Fair Trade      <span class=\"price number\">" + cs.Data.coffeePriceIndex.fairtrade + "</span>    </p>    <p class=\"what\">      ($USD) <a href=\"/how-we-buy-coffee\">What is this?</a>          </p>    ");
    },
    getHomepageData: function() {
      return;
      $.ajax({
        url: "" + cs.Data.index,
        data: {
          id: this.id,
          json: 'get_page',
          custom_fields: 'home_feature'
        },
        dataType: 'json',
        success: this.hazData
      });
    },
    hazData: function(data) {},
    setupMailchimp: function() {
      var $email, $name;
      var _this = this;
      $email = this.$('#mc_mv_EMAIL');
      $email.val('email');
      $name = this.$('#mc_mv_FNAME');
      $name.val('first name');
      _.each([$email, $name], function($el) {
        $el.bind('focus', function(e) {
          if ($el.val() === 'email' || $el.val() === 'first name') $el.val('');
        });
        $el.bind('blur', function(e) {
          if ($el.val() === '') {
            if ($el === $email) $el.val('email');
            if ($el === $name) $el.val('first name');
          }
        });
      });
    }
  });

}).call(this);

