var LicGen = (function() {
  return {
    Template: {
      rexp: /{{(\w+)}}/g,
      field_for: function(name) {
        return $('[name=' + name + ']');
      },
      field_size: function(name) {
        return this.field_for(name).attr('size');
      },
      render: function() {
        var tpl = $('#text');
        if (tpl.length === 0) { return; }
        var rendered = tpl.html().replace(this.rexp, function(match, name) {
          LicGen.Template.bind_field(name);
          filler = '<span class="fill ph ' + name + '">';
          for(var i = 0; i < LicGen.Template.field_size(name); i++) { filler = filler + '_'; }
          filler += '</span>';
          return filler;
        });
        tpl.html(rendered);
      },
      bind_field: function(name) {
        this.field_for(name).bind('keyup', function() {
          $('.' + name).removeClass('ph').text($(this).val());
        });
      },
      initialize: function() {
        this.render();
        $('.default').clearField({blurClass: 'default-blur'});
        $('.prefilled').trigger('keyup');
      }
    },
    Selection: {
      initialize: function() {
        //$('form#selection').submit(this.submit);
      },
      submit: function() {
        var val = $('[name=type]').val();
        if (val.length === 0) { return false; }
        $.ajax({
          type: 'get',
          dataType: 'html',
          url: this.action + "/" + val,
          success: function(html) {
            $('#main').html(html);
            LicGen.Template.render();
            $('.default').clearField({blurClass: 'default-blur'});
          },
          error: function(xhr, status, error) {
            console.log(error);
          }
        });
        return false;
      }
    },
    Permalink: {
      initialize: function() {
        $('#permalink').bind('click', this.get);
      },
      params: function() {
        var params = "";
        $('#fields input').each(function() {
          var el = $(this);
          params = params + el.attr('name') + "=" + encodeURIComponent(el.val()) + "&";
        });
        return params;
      },
      get: function() {
        $.ajax({
          type: 'post',
          data: LicGen.Permalink.params(),
          dataType: 'html',
          url: this.href,
          success: function(html) {
            $.facebox(html);
          }
        });
        return false;
      }
    },
    Copy: {
      initialize: function() {
        if ($('#clipboard').length === 0) { return; }
        var clip = new ZeroClipboard.Client();
        var anchor = $('#clipboard');
        clip.setHandCursor(true);
        clip.addEventListener('mouseDown', function(client) {
          clip.setText(LicGen.Copy.trim_lines($('#text').text()));
          anchor.text("Copied!");
          setTimeout(function() {
            anchor.text("Copy");
          }, 1500);
        });
        clip.glue('clipboard');
      },
      trim_lines: function(text) {
        return ($.trim(text) || "").replace(/^\s+(\w)|\s+$/gm, function(match, w) {
          if (typeof w !== 'undefined') {
            return w;
          } else {
            return "";
          }
        }).replace(/\n/gm, "\n\n");
      }
    }
  };
})();

$(function() {
  ZeroClipboard.setMoviePath('/js/ZeroClipboard.swf');
  $.each(LicGen, function(name, module) {
    module.initialize();
  });
});
