/*
 * Evil Icons <%= EvilIcons::VERSION %>
 * http://evil-icons.io
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 * Copyright 2014-<%= Date.today.year %> Alexander Madyankin <alexander@madyankin.name>, Roman Shamin <al4emist.artway@gmail.com>
 */

(function(document){
  "use strict";

  function renderSprite() {
    var sprite = "<%= sprite("sprite.svg").gsub('"', '\"') %>";
    document.querySelector("body").insertAdjacentHTML("afterbegin", sprite);
  }

  function icon(name, options) {
    var options = options || {};
    var size    = options.size ? "icon--" + options.size : "";
    var klass   = "icon icon--" + name + " " + size + " " + (options.class || "");


    var icon =  "<svg class='icon__cnt' width='0' height='0'>" +
                  "<use xlink:href='#" + name + "-icon' />" +
                "</svg>";

    var html =  "<div class='" + klass + "'>" +
                  wrapSpinner(icon, klass) +
                "</div>";

    return html;
  }

  function wrapSpinner(html, klass) {
    if (klass.indexOf("spinner") > -1) {
      return "<div class='icon__spinner'>" + html + "</div>";
    } else {
      return html;
    }
  }

  function renderIcons() {
    var render = true;
    var icons = document.querySelectorAll("[data-icon]");

    for (var i = 0; i < icons.length; i++) {
      var currentIcon = icons[i];
      var name        = currentIcon.getAttribute("data-icon");
      var options = {
        class:  currentIcon.className,
        size:   currentIcon.getAttribute("data-size")
      };

      currentIcon.insertAdjacentHTML("beforebegin", icon(name, options));
      currentIcon.parentNode.removeChild(currentIcon);
    }
  }

  document.addEventListener("DOMContentLoaded", function() {
    renderSprite();
    renderIcons();
  });

})(window.document);
