(function() {
  var cs;

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

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

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

  cs.Views.NavigationView = Backbone.View.extend({
    events: {
      'focus #s': 'searchActions',
      'blur #s': 'searchActions'
    },
    initialize: function(options) {
      var dontUseScrollyNav;
      _.bindAll(this);
      this.$el = $(this.el);
      if ($.browser.msie && $.browser.version < 8) {
        dontUseScrollyNav = true;
      } else {
        $(window).bind('scroll', this.onScroll);
      }
      this.setupLinks();
      this.addLogo();
    },
    onScroll: function(e) {
      var y,
        _this = this;
      if ($('html').hasClass('webkit')) {
        y = $('body').scrollTop();
      } else {
        y = $('html').scrollTop();
      }
      if (y > 124 && !this.$el.hasClass('small')) {
        this.$el.addClass('small');
        this.$('a#small-logo').css({
          opacity: 0
        });
        this.$('a#small-logo').animate({
          opacity: 1
        }, 200);
        this.$('.small-nav').css({
          opacity: 0
        });
        this.$('.small-nav').animate({
          opacity: 1
        }, 200);
      }
      if (y < 124 && this.$el.hasClass('small')) {
        this.$el.removeClass('small');
        this.$('.nav-list').css({
          opacity: 0
        });
        _.each(this.$('.nav-list'), function(el, index) {
          var $el;
          $el = $(el);
          $el.delay(index * 60).animate({
            opacity: 1
          }, 200);
        });
      }
    },
    addLogo: function() {
      var n, total;
      if (window.location.pathname === cs.Data.index) {
        this.$('a#big-logo').css({
          backgroundImage: "url('" + cs.Data.templateDir + "/_/images/logo.nav.normal.png')"
        });
      } else {
        total = 4;
        n = Math.ceil(Math.random() * total);
        this.$('a#big-logo').css({
          backgroundImage: "url('" + cs.Data.templateDir + "/_/images/logo.nav." + n + ".png')"
        });
      }
    },
    setupLinks: function() {
      var _this = this;
      _.each(this.$('.nav-list'), function(el, index) {
        var $el, $first;
        $el = $(el);
        $first = $el.find('ul li:first a');
        $el.prepend("<h4 class=\"title\"><a href=\"" + ($first.attr('href')) + "\">" + ($first.text()) + "</a></h4>        <span class=\"dash\">&mdash;</span>");
        _this.$('.small-nav').append("<li><a href=\"" + ($first.attr('href')) + "\">" + ($first.text()) + "</a></li>");
        $first.parents('li').remove();
      });
    },
    searchActions: function(e) {
      var $el;
      $el = $(e.target);
      switch (e.type) {
        case 'focusin':
          if ($el.val() === 'Search') $el.val('');
          break;
        case 'focusout':
          if ($el.val() === '') $el.val('Search');
      }
    }
  });

}).call(this);

