// Generated by CoffeeScript 1.4.0
(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; },
    __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

  window.Controls.textbox = (function(_super) {

    __extends(textbox, _super);

    function textbox() {
      return textbox.__super__.constructor.apply(this, arguments);
    }

    textbox.prototype.createDom = function() {
      this.dom = $("<div>\n    <input class=\"control textbox " + this.properties.style + "\" \n        type=\"text\" />\n</div>");
      this.input = this.dom.find('input');
      return this.input.val(this.properties.value);
    };

    textbox.prototype.detectUpdates = function() {
      var oldvalue, r, value;
      r = {};
      value = this.input.val();
      oldvalue = this.properties.value || "";
      if (this.properties.type === 'integer') {
        value = parseInt(value);
      }
      if (value !== oldvalue) {
        r.value = value;
      }
      this.properties.value = value;
      return r;
    };

    return textbox;

  })(window.Control);

  window.Controls.passwordbox = (function(_super) {

    __extends(passwordbox, _super);

    function passwordbox() {
      return passwordbox.__super__.constructor.apply(this, arguments);
    }

    passwordbox.prototype.createDom = function() {
      this.dom = $("<div>\n    <input class=\"control textbox " + this.properties.style + "\" \n        type=\"password\" />\n</div>");
      this.input = this.dom.find('input');
      return this.input.val(this.properties.value);
    };

    return passwordbox;

  })(window.Controls.textbox);

  window.Controls.editable = (function(_super) {

    __extends(editable, _super);

    function editable() {
      this.goEditMode = __bind(this.goEditMode, this);

      this.goViewMode = __bind(this.goViewMode, this);
      return editable.__super__.constructor.apply(this, arguments);
    }

    editable.prototype.createDom = function() {
      var icon, _ref,
        _this = this;
      icon = _make_icon(this.properties.icon);
      this.dom = $("<div class=\"control editable\">\n    <div class=\"control label\">" + icon + " <span>" + ((_ref = this.properties.placeholder) != null ? _ref : this.properties.value) + "</span></div>\n    <input class=\"control textbox " + this.properties.style + "\" type=\"text\" value=\"" + (this.properties.value || '') + "\" />\n</div>");
      this.label = this.dom.find('.label');
      this.input = this.dom.find('input');
      this.input.hide();
      this.label.click(this.goEditMode);
      this.input.blur(this.goViewMode);
      return this.input.keyup(function(e) {
        if (e.which === 13) {
          _this.goViewMode();
        }
        return _this.cancel(e);
      });
    };

    editable.prototype.goViewMode = function() {
      var _ref;
      this.label.find('>span').html((_ref = this.properties.placeholder) != null ? _ref : this.input.val());
      this.input.hide();
      return this.label.show();
    };

    editable.prototype.goEditMode = function(e) {
      this.label.hide();
      this.input.show();
      this.input.focus();
      e.stopPropagation();
      return e.preventDefault();
    };

    editable.prototype.detectUpdates = function() {
      var r, value;
      r = {};
      value = this.input.val();
      if (this.properties.type === 'integer') {
        value = parseInt(value);
      }
      if (value !== this.properties.value) {
        r.value = value;
      }
      return r;
    };

    return editable;

  })(window.Control);

  window.Controls.checkbox = (function(_super) {

    __extends(checkbox, _super);

    function checkbox() {
      return checkbox.__super__.constructor.apply(this, arguments);
    }

    checkbox.prototype.createDom = function() {
      this.dom = $("<div class=\"control checkbox\">\n    <input \n        id=\"" + this.properties.uid + "\"\n        type=\"checkbox\" \n        " + (this.properties.value ? 'checked="checked"' : '') + " \n    />\n    <label for=\"" + this.properties.uid + "\">\n        <div class=\"tick\">\n            <i class=\"icon-ok\"></i>\n        </div>\n    </label>\n    <div class=\"control label\">" + this.properties.text + "</div>\n</div>");
      return this.input = this.dom.find('input');
    };

    checkbox.prototype.detectUpdates = function() {
      var checked, r;
      r = {};
      checked = this.input.is(':checked');
      if (checked !== this.properties.value) {
        r.value = checked;
      }
      this.properties.value = checked;
      return r;
    };

    return checkbox;

  })(window.Control);

  window.Controls.dropdown = (function(_super) {

    __extends(dropdown, _super);

    function dropdown() {
      return dropdown.__super__.constructor.apply(this, arguments);
    }

    dropdown.prototype.createDom = function() {
      var i, _fn, _i, _ref,
        _this = this;
      this.dom = $("<div><select class=\"control dropdown\" /></div>");
      this.input = this.dom.find('select');
      this.data = [];
      _fn = function(i) {
        return _this.input.append("<option value=\"" + _this.properties.values[i] + "\" " + (_this.properties.values[i] === _this.properties.value ? 'selected' : '') + ">" + _this.properties.labels[i] + "</option>");
      };
      for (i = _i = 0, _ref = this.properties.labels.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
        _fn(i);
      }
      this.input.select2();
      if (this.properties.server) {
        return this.input.change(function(e) {
          _this.event('change', {});
          return _this.cancel(e);
        });
      }
    };

    dropdown.prototype.detectUpdates = function() {
      var r, value;
      r = {};
      value = this.input.val();
      if (this.properties.type === 'integer') {
        value = parseInt(value);
      }
      if (value !== this.properties.value) {
        console.log(value, this.properties.value);
        r.value = value;
      }
      this.properties.value = value;
      return r;
    };

    return dropdown;

  })(window.Control);

  window.Controls.combobox = (function(_super) {

    __extends(combobox, _super);

    function combobox() {
      return combobox.__super__.constructor.apply(this, arguments);
    }

    combobox.prototype.createDom = function() {
      var i, _fn, _i, _ref,
        _this = this;
      this.dom = $("<div><input class=\"control combobox\" type=\"text\" value=\"" + this.properties.value + "\" /></div>");
      this.input = this.dom.find('input');
      this.data = [];
      _fn = function(i) {
        return _this.data.push({
          label: _this.properties.labels[i],
          value: _this.properties.values[i]
        });
      };
      for (i = _i = 0, _ref = this.properties.labels.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
        _fn(i);
      }
      if (this.properties.separator !== null) {
        this.input.autocomplete({
          source: function(request, response) {
            var vals;
            vals = _this.getVals();
            return response($.ui.autocomplete.filter(_this.data, vals.pop()));
          },
          focus: function() {
            return false;
          },
          select: function(event, ui) {
            var vals;
            vals = _this.getVals();
            vals.pop();
            vals.push(ui.item.value);
            _this.input.val(vals.join(_this.properties.separator));
            return false;
          },
          minLength: 0
        });
      } else {
        this.input.autocomplete({
          source: this.data,
          minLength: 0
        });
      }
      return this.input.click(function() {
        return _this.input.autocomplete('search', '');
      });
    };

    combobox.prototype.getVals = function() {
      return this.input.val().split(this.properties.separator);
    };

    combobox.prototype.detectUpdates = function() {
      var r, value;
      r = {};
      value = this.input.val();
      if (this.properties.type === 'integer') {
        value = parseInt(value);
      }
      if (value !== this.properties.value) {
        r.value = value;
      }
      this.properties.value = value;
      return r;
    };

    return combobox;

  })(window.Control);

  window.Controls.fileupload = (function(_super) {

    __extends(fileupload, _super);

    function fileupload() {
      return fileupload.__super__.constructor.apply(this, arguments);
    }

    fileupload.prototype.createDom = function() {
      var _this = this;
      this.dom = $("<div class=\"control fileupload\">\n    <input type=\"file\" />\n    \n    <div class=\"full-overlay\">\n        <div class=\"content\">\n            <div class=\"inner\">\n                <h1>Upload</h1>\n                <div class=\"pb\"></div>\n            </div>\n        </div>\n    </div>  \n</div>");
      this.progress = new window.Controls.progressbar(this.ui, {}, []);
      this.dom.find('.pb').append($(this.progress.dom));
      this.input = this.dom.find('input')[0];
      this.input.addEventListener('change', function(e) {
        var d, file, xhr;
        file = _this.input.files[0];
        xhr = new XMLHttpRequest();
        xhr.file = file;
        if (xhr.upload) {
          xhr.upload.onprogress = function(e) {
            var done, progress, total;
            done = e.position || e.loaded;
            total = e.totalSize || e.total;
            progress = 1.0 * done / total / 2;
            return _this.progress.setProgress(progress);
          };
        }
        d = new FormData();
        d.append('file', file);
        xhr.open('post', _this.properties.target, true);
        xhr.send(d);
        return $(_this.dom).find('.full-overlay').show();
      }, false);
      return this.dom.find('.full-overlay').hide();
    };

    return fileupload;

  })(window.Control);

  window.Controls.paging = (function(_super) {

    __extends(paging, _super);

    function paging() {
      return paging.__super__.constructor.apply(this, arguments);
    }

    paging.prototype.createDom = function() {
      var i, _i, _ref,
        _this = this;
      this.dom = $("<div class=\"control paging\">\n    <div class=\"control label\">Page:&nbsp;</div>\n    <a class=\"prev control button style-mini\"><i class=\"icon-arrow-left\"></i></a>\n    <select />\n    <a class=\"next control button style-mini\"><i class=\"icon-arrow-right\"></i></a>\n</div>");
      this.select = this.dom.find('select');
      for (i = _i = 0, _ref = this.properties.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
        this.select.append($("<option value=\"" + (i + 1) + "\">" + (i + 1) + "</option>"));
      }
      this.select.val(this.properties.active + 1);
      this.select.select2({
        width: '80px'
      });
      this.prev = this.dom.find('.prev');
      this.next = this.dom.find('.next');
      if (this.properties.active === 0) {
        this.prev.hide();
      }
      if (this.properties.active === this.properties.length - 1) {
        this.next.hide();
      }
      if (!this.properties.length) {
        this.dom.hide();
      }
      this.prev.click(function() {
        return _this.set(_this.properties.active - 1);
      });
      this.next.click(function() {
        return _this.set(_this.properties.active + 1);
      });
      return this.select.change(function() {
        var idx;
        idx = parseInt(_this.select.val()) - 1;
        console.log(idx, _this.properties.active);
        if (idx !== _this.properties.active) {
          return _this.set(idx);
        }
      });
    };

    paging.prototype.set = function(page) {
      return this.event('switch', {
        page: page
      });
    };

    return paging;

  })(window.Control);

  window.Controls.pathbox = (function(_super) {

    __extends(pathbox, _super);

    function pathbox() {
      return pathbox.__super__.constructor.apply(this, arguments);
    }

    pathbox.prototype.createDom = function() {
      var _this = this;
      this.dom = $("<div class=\"control container pathbox\">\n</div>");
      this.childContainer = this.dom;
      this.textbox = new Controls.textbox(this.ui, {
        value: this.properties.value
      });
      this.button = new Controls.button(this.ui, {
        style: 'mini',
        icon: this.properties.directory ? 'folder-close' : 'file',
        text: ''
      });
      this.append(this.textbox);
      this.append(this.button);
      return this.button.on_click = function() {
        return _this.event('start', {});
      };
    };

    pathbox.prototype.detectUpdates = function() {
      return this.textbox.detectUpdates();
    };

    return pathbox;

  })(window.Control);

}).call(this);
