require.config({
  baseUrl: '/assets/js',
  paths: {
    jquery: 'jquery-1.10.2.min',
    bootstrap: 'bootstrap.min',
  },
  shim: {
    bootstrap: ['jquery'],
  },
  waitSeconds: 0,
});

stogram = function () {
  var self = this;
  self.plugins = {};
  self.init = function (container) {
    $('[data-selected]').each(function (idx) {
      var v = $(this).data('selected');
      if ($(this).prop('tagName') == 'SELECT') {
        $('option', this).each(function () {
          if ($(this).val() == v) {
            $(this).prop('selected', 1);
          }
        });
      } else if ($(this).hasClass('radio')) {
        $('[type=radio]', this).each(function () {
          if ($(this).val() == v) {
            $(this).prop('checked', 1);
          }
        });
      } else if ($(this).hasClass('checkbox')) {
        v = v.split(',');
        $('[type=checkbox]', this).each(function () {
          if (v.include($(this).val())) {
            $(this).prop('checked', 1);
          }
        });
      }
    });

    require(['bootstrap'], function () {
      $('[data-toggle="tooltip"]').tooltip();
      $('[data-spy="affix"]').affix();
    });

    $('[data-plugin]', container).each(function (idx) {
      var plugin = $(this).attr('data-plugin');
      var opts = $(this).data();

      opts.el = $(this);
      if (!opts.id) {
        opts.id = 'st-plugin-' + plugin + '-' + idx;
        $(this).attr('data-id', opts.id);
      }
      self.plugin(plugin, opts);
    });

    $('a').each(function () {
      var href = $(this).attr('href');
      var url = window.location.href;
      if ($(this).attr('role') != 'nav') {
        return;
      }

      if (
        (url == href ||
          (url.indexOf(href) >= 0 && href != window.location.origin + '/' && !$(this).data('exact')) ||
          (href == '/' && url == window.location.origin + '/')) &&
        $(this).attr('role') == 'nav'
      ) {
        $(this).addClass('active');
        $(this).parent().addClass('active');
      }
    });

    // 处理Loading

    setTimeout(function () {
      $('[role="loading"]').removeClass('unload').addClass('completed');
      $(window).trigger('scroll');
      setTimeout(function () {
        $('[role="loading"] .spinner').remove();
      }, 800);
    }, 0);

    $('a[href]').on('click', function (e) {
      var href = $(this).attr('href');
      var target = $(this).attr('target');
      var pathname = window.location.pathname;

      if (target) {
        return true;
      }
      if (href.includes('#')) {
        var uri = href.split('#');
        if (uri[0] == pathname) {
          return true;
        }
      }
      if (href.substr(0, 1) != '#' && href.substr(0, 11) != 'javascript:' && href.substr(0, 4) != 'tel:' && href.substr(0, 7) != 'mailto:') {
        setTimeout(function () {
          $('[role="loading"]').addClass('unload');
        }, 0);
        setTimeout(function () {
          window.location = href;
          setTimeout(function () {
            $('[role="loading"]').remove();
          }, 1000);
        }, 100);
        return false;
      }
      return true;
    });
  };

  self.plugin = function (name, opts, callback) {
    var fa = name.match(/\-(\w)/g);
    var plugin = {};

    if (fa) {
      for (var x in fa) {
        func = name.replace(fa[x], fa[x].toUpperCase()).replace('-', '');
      }
    }

    var func = eval('self.' + func);
    if (func) {
      func(el, opts);
    } else {
      require(['stogram/plugins/' + name], function (plugin) {
        plugin = new plugin(opts);
        if (callback) {
          callback(plugin);
        }

        self.plugins[opts.id] = plugin;
      });
    }
  };

  var num = 0;
  self.message = function (msg, timeout, type) {
    num++;
    var id = 'message-' + num;
    var className = 'message-' + (type ? type : 'info');
    if (!$('body>.message').length) {
      $('body').append('<div class="message"></div>');
    }
    $('body>.message').empty();
    $('body>.message').append('<section id="' + id + '" class="' + className + '">' + msg + '</section>');
    var el = $('#' + id);
    setTimeout(function () {
      el.addClass('move-enter');
    }, 100);
    setTimeout(
      function () {
        el.remove();
      },
      timeout ? timeout : 5000
    );

    return {};
  };

  self.message.loading = function (msg, timeout) {
    self.message(msg, timeout, 'loading');
  };

  self.message.info = function (msg, timeout) {
    self.message(msg, timeout, 'info');
  };

  self.message.error = function (msg, timeout) {
    self.message(msg, timeout, 'error');
  };

  self.message.success = function (msg, timeout) {
    self.message(msg, timeout, 'success');
  };

  self.message.warning = function (msg, timeout) {
    self.message(msg, timeout, 'warning');
  };

  self.dialog = function (params) {
    self.plugin('dialog', params, function (dialog) {
      dialog.open();
    });
  };

  self.init('body');
  return self;
};

var stogram = new stogram();
window.stogram = stogram;
