// Generated by CoffeeScript 1.6.2
(function() {
  var __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  define(["marionette"], function(Marionette) {
    var EditableView, _ref;

    EditableView = (function(_super) {
      __extends(EditableView, _super);

      function EditableView() {
        _ref = EditableView.__super__.constructor.apply(this, arguments);
        return _ref;
      }

      EditableView.prototype.events = {
        "change input": "changed",
        "change textarea": "changed"
      };

      EditableView.prototype.templateHelpers = {
        renderCheckbox: function(value) {
          return (value ? "checked" : "");
        }
      };

      EditableView.prototype.changeSelect = function(evt) {
        var changed, obj, objInst, value;

        changed = evt.currentTarget;
        if (id) {
          value = $(evt.currentTarget).val();
          obj = "{\"" + changed.id + "\":\"" + value.replace(/\n/g, "<br />") + "\"}";
          objInst = JSON.parse(obj);
          return this.model.set(objInst);
        }
      };

      EditableView.prototype.changed = function(evt) {
        var changed, obj, objInst, value;

        return;
        changed = evt.currentTarget;
        if (changed.id) {
          value = void 0;
          obj = void 0;
          if ($(changed).is(":checkbox")) {
            value = $(changed).is(":checked");
            obj = "{\"" + changed.id + "\":" + value + "}";
          } else {
            value = $(changed).val();
            obj = "{\"" + changed.id + "\":\"" + value.replace(/\n/g, "<br />") + "\"}";
          }
          objInst = JSON.parse(obj);
          return this.model.set(objInst);
        }
      };

      EditableView.prototype._bakeForm = function(el, lookups) {
        var labels, mapped, model;

        model = this.model;
        labels = void 0;
        mapped = void 0;
        $(".typeahead", el).typeahead({
          source: function(query, process) {
            return $.get("/ajax/lookup/" + lookups + "/", {
              query: query
            }, (function(data) {
              labels = [];
              mapped = {};
              $.each(data, function(i, item) {
                mapped[item[1]] = item;
                return labels.push(item[1]);
              });
              return process(labels);
            }), "json");
          },
          updater: function(item) {
            this.$element.val(mapped[item][0]);
            model.set(this.$element.attr("id"), mapped[item][0]);
            return item;
          }
        });
        $(".datepicker", el).datepicker({
          format: "dd/mm/yyyy"
        });
        $(".timepicker", el).timepicker();
        return $("textarea.tinymce", this.el).tinymce({
          script_url: "/static/js/libs/tiny_mce/tiny_mce.js",
          mode: "textareas",
          theme: "advanced",
          theme_advanced_toolbar_location: "top",
          theme_advanced_toolbar_align: "left",
          theme_advanced_buttons1: "fullscreen,media,tablecontrols,separator,link,unlink,anchor,separator,preview,separator,bold,italic,underline,strikethrough,separator,bullist,numlist,outdent,indent,separator,undo,redo,separator,image,cleanup,help,separator,code",
          theme_advanced_buttons2: "",
          theme_advanced_buttons3: "",
          auto_cleanup_word: true,
          plugins: "media, table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,print,contextmenu,fullscreen,preview,searchreplace",
          plugin_insertdate_dateFormat: "%m/%d/%Y",
          plugin_insertdate_timeFormat: "%H:%M:%S",
          extended_valid_elements: "a[name|href|target=_blank|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
          fullscreen_settings: {
            theme_advanced_path_location: "top",
            theme_advanced_buttons1: "fullscreen,media, separator,preview,separator,cut,copy,paste,separator,undo,redo,separator,search,replace,separator,code,separator,cleanup,separator,bold,italic,underline,strikethrough,separator,forecolor,backcolor,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,help",
            theme_advanced_buttons2: "removeformat,styleselect,formatselect,fontselect,fontsizeselect,separator,bullist,numlist,outdent,indent,separator,link,unlink,anchor",
            theme_advanced_buttons3: "sub,sup,separator,image,insertdate,inserttime,separator,tablecontrols,separator,hr,advhr,visualaid,separator,charmap,emotions,iespell,flash,separator,print"
          }
        });
      };

      EditableView.prototype._saveChanges = function() {
        var args, error, _results;

        args = arguments;
        if (!this.model.isValid()) {
          if (this.model.errors) {
            _results = [];
            for (error in this.model.errors) {
              $("#group-" + error, this.el).addClass("error");
              _results.push($("#error-" + error, this.el).text(this.model.errors[error]));
            }
            return _results;
          }
        } else {
          return this.model.save(null, {
            success: args[0].success,
            error: args[0].error
          });
        }
      };

      return EditableView;

    })(Marionette.ItemView);
    return EditableView;
  });

}).call(this);
