(function() {
  var cs;

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

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

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

  cs.Views.PromosView = Backbone.View.extend({
    initialize: function(options) {
      this.options = options;
      this.promos_width = 0;
      _.bindAll(this);
      this.$el = $(this.el);
      this.data = this.$el.data();
      if (this.data.related) {
        this.getRelatedThing();
      } else {
        this.getPostData();
      }
    },
    getPostData: function() {
      var method;
      if (this.data.page) {
        method = 'get_page';
      } else {
        method = 'get_post';
      }
      $.ajax({
        url: "" + cs.Data.index,
        data: {
          id: this.data.id,
          json: method,
          custom_fields: 'excerpt,feature-image,tags,related,post-thumb'
        },
        dataType: 'json',
        success: this.hazData
      });
    },
    hazData: function(data) {
      var _this = this;
      this.model = new Backbone.Model();
      if (this.data.page) {
        this.model.set(data.page);
      } else {
        this.model.set(data.post);
      }
      if ((this.model.get('custom_fields') != null) && (this.model.get('custom_fields')['tags'] != null)) {
        this.tags = this.model.get('custom_fields')['tags'][0].split(',');
      } else {
        this.tags = [];
      }
      _.each(this.tags, function(tag) {
        switch (tag) {
          case 'supreme-location':
            _this.addBigLocationsPromo();
        }
      });
      this.addExtras();
    },
    addExtras: function() {
      var space;
      if (this.promos_width < 960) {
        space = 960 - this.promos_width;
        if (space >= 960) {
          this.addPeoplePromo();
        } else if (space >= 345) {
          this.addLocationsPromo();
        } else {
          this.addShippingPromo();
        }
        this.addExtras();
      } else {
        this.finallyAddClearfix();
      }
    },
    addLocationsPromo: function() {
      var $el, view;
      this.promos_width += 370;
      $el = $("<div class=\"promo loading locations-promo\" data-view=\"LocationsPromoView\"></div>");
      view = new cs.Views.LocationsPromoView({
        appModel: this.options.appModel,
        el: $el
      });
      this.$el.append($el);
    },
    addPeoplePromo: function() {
      var $el, view;
      this.promos_width += 615;
      $el = $("<div class=\"promo large loading people-promo\" data-view=\"PeoplePromoView\"></div>");
      view = new cs.Views.PeoplePromoView({
        appModel: this.options.appModel,
        el: $el
      });
      this.$el.append($el);
    },
    finallyAddClearfix: function() {
      this.$el.append("<div class=\"clearfix\"></div>");
    },
    addBigLocationsPromo: function() {
      var $el, view;
      this.promos_width += 960;
      $el = $("<div class=\"big-locations-promo promo\"></div>");
      view = new cs.Views.BigLocationsPromoView({
        appModel: this.options.appModel,
        el: $el
      });
      this.$el.append($el);
      this.$('.line-title').text('Our Locations:');
    },
    addShippingPromo: function() {
      var $el;
      this.promos_width += 222;
      $el = $("<a href=\"http://www.coffeesupremeshop.com\" target=\"_blank\" class=\"promo shipping-promo\"></a>");
      this.$el.append($el);
    },
    getRelatedThing: function() {
      var id, type;
      type = this.data['relatedType'];
      id = this.data['relatedGuid'];
      id = id.replace("" + window.location.protocol + "//" + window.location.host + "/", "");
      id = id.replace("http://blog.coffeesupremeshop.com/", "");
      id = id.replace("?p=", "");
      id = id.replace("?page_id=", "");
      $.ajax({
        url: "" + cs.Data.index,
        data: {
          id: id,
          json: "get_" + type,
          custom_fields: 'excerpt,feature-image,tags,related,post-thumb'
        },
        dataType: 'json',
        success: this.hasRelatedThing
      });
    },
    hasRelatedThing: function(data) {
      this.related = new Backbone.Model();
      this.related.set({
        type: this.data['relatedType']
      });
      switch (this.data['relatedType']) {
        case 'page':
          this.related.set({
            data: data.page
          });
          break;
        case 'post':
          this.related.set({
            data: data.post
          });
      }
      this.renderRelated();
    },
    renderRelated: function() {
      var $el, view;
      this.promos_width += 370;
      $el = $("<div class=\"promo related-promo loading\"></div>");
      view = new cs.Views.RelatedPromoView({
        appModel: this.options.appModel,
        model: this.related,
        el: $el
      });
      this.$el.append($el);
      this.getPostData();
    }
  });

}).call(this);

