(function() {
  var cs;

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

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

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

  cs.Views.LocationsPromoView = Backbone.View.extend({
    initialize: function(options) {
      this.options = options;
      _.bindAll(this);
      this.$el = $(this.el);
      this.$el.html("<h4>Our Locations</h4>        <a class=\"link\" href=\"/locations\"></a>        <p class=\"name\"><span class=\"location-name\"></span></p>");
      if (cs.Data.loaded) {
        this.render();
      } else {
        this.options.appModel.bind('site-preloader-animation-in-complete', this.render);
      }
    },
    render: function() {
      var $images;
      var _this = this;
      $images = $("<div class=\"images\"></div>");
      _.each(cs.Data.locations, function(location, index) {
        var $img;
        $img = $("        <img class=\"image-" + index + "\" src=\"" + cs.Data.templateDir + "/locations/" + location.image + "\"/>      ");
        $images.append($img);
        if (index === 0) $img.bind('load', _this.firstImageLoaded);
      });
      this.$el.append($images);
    },
    showLocation: function(n) {
      this.currentLocation = n;
      this.$('.images img').removeClass('visible');
      this.$(".images img.image-" + n).addClass('visible');
      if (this.int != null) clearInterval(this.int);
      this.int = setTimeout(this.showNextLocation, 3000);
      this.$('.location-name').text(cs.Data.locations[n].name);
    },
    showNextLocation: function() {
      if (this.currentLocation > cs.Data.locations.length - 2) {
        this.showLocation(0);
      } else {
        this.showLocation(this.currentLocation + 1);
      }
    },
    firstImageLoaded: function(e) {
      this.$el.removeClass('loading');
      this.showLocation(0);
    }
  });

}).call(this);

