(function() {
  var cs;

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

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

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

  cs.Views.AppView = Backbone.View.extend({
    events: {
      'click a': 'goingSomewhere'
    },
    speed: 460,
    offset: 60,
    ease: 'easeOutExpo',
    initialize: function(options) {
      var _this = this;
      this.appModel = options.appModel;
      _.bindAll(this);
      this.$el = $(this.el);
      this.$preloader = $('#site-preloader');
      $(window).bind('load', function() {
        setTimeout(_this.animateIn, 200);
      });
    },
    animateIn: function(e) {
      var _this = this;
      if (window.location.pathname !== cs.Data.index) {
        $('html, body').scrollTop(126);
      }
      this.appModel.trigger('site-preloader-animate-in');
      this.$('#site-preloader .message').animate({
        opacity: 0
      }, this.speed * 2);
      _.each(this.$('.bg-column'), function(el, index) {
        $(el).delay(index * _this.offset).animate({
          height: 0
        }, _this.speed);
      });
      setTimeout(function() {
        _this.$preloader.addClass('hide');
        cs.Data.loaded = true;
        _this.appModel.trigger('site-preloader-animation-in-complete');
      }, this.speed + (this.$('.bg-column').length + 1) * this.offset);
    },
    goingSomewhere: function(e) {
      var $el;
      if (e.target.nodeName !== 'A') {
        $el = $(e.target).parents('a');
      } else {
        $el = $(e.target);
      }
      if ($el.attr('target') === '_blank') return;
      if ($el.attr('href') === '#') return;
      if (e.metaKey || e.shiftKey || e.ctrlKey) return;
      if ($el.attr('href').indexOf('http://') >= 0 && $el.attr('href').indexOf(cs.Data.index) < 0) {
        return;
      }
      if ($el.attr('href').indexOf('mailto:') >= 0) return;
      e.preventDefault();
      this.href = $el.attr('href');
      this.animateOut();
    },
    animateOut: function() {
      var _this = this;
      if ($('html').hasClass('ie8')) {
        window.location = this.href;
        return;
      }
      this.$preloader.removeClass('hide');
      this.$('#site-preloader .message').animate({
        opacity: 1
      }, this.speed * 2);
      _.each(this.$('.bg-column'), function(el, index) {
        $(el).delay(index * _this.offset).animate({
          height: window.innerHeight
        }, _this.speed);
      });
      setTimeout(function() {
        window.location = _this.href;
      }, this.speed + this.$('.bg-column').length * this.offset);
    }
  });

}).call(this);

