text: // Generated by CoffeeScript 1.8.0
  var guid, init_i18n, s4,
    __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  
  Array.prototype.toDict = function(key) {
    return this.reduce((function(dict, obj) {
      if (obj[key] != null) {
        dict[obj[key]] = obj;
      }
      return dict;
    }), {});
  };
  
  s4 = function() {
    return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
  };
  
  guid = function() {
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
  };
  
  $.fn.serializeObject = function() {
    var a, o;
    o = {};
    a = this.serializeArray();
    $.each(a, function() {
      if (o[this.name] !== void 0) {
        if (!o[this.name].push) {
          o[this.name] = [o[this.name]];
        }
        return o[this.name].push(this.value || '');
      } else {
        return o[this.name] = this.value || '';
      }
    });
    return o;
  };
  
  init_i18n = function() {
    var locale;
    locale = $("body").data("lang");
    return $.ajax({
      url: "/static/js/camper-" + locale + ".json",
      type: "GET",
      dataType: "json",
      success: function(data) {
        var i18n, ntrans, trans;
        i18n = new Jed(data);
        trans = function(string, params) {
          return i18n.translate(string).fetch(params);
        };
        ntrans = function(string, plural_string, num, params) {
          return i18n.translate(string).ifPlural(num, plural_string).fetch(params);
        };
        Handlebars.registerHelper('trans', function(options) {
          var content;
          content = options.fn(this);
          return trans(content, options.hash);
        });
        Handlebars.registerHelper('_', function(string, options) {
          var content;
          content = string;
          return trans(content, options.hash);
        });
        Handlebars.registerHelper('ntrans', function(num, options) {
          var content, plural_content;
          content = options.fn(this);
          plural_content = options.inverse(this);
          return ntrans(content, plural_content, num, options.hash);
        });
        Handlebars.registerHelper('n_', function(num, string, plural_string, options) {
          return ntrans(string, plural_string, num, options.hash);
        });
        return $("#newsessions").sessionboard();
      },
      error: function() {
        return alert("Could not load translations, please try again later");
      }
    });
  };
  
  (function($, window, document) {
    var Plugin, defaults, pluginName;
    pluginName = "sessionboard";
    defaults = {
      foo: "bar"
    };
    Plugin = (function() {
      function Plugin(element, options) {
        this.element = element;
        this.del_session = __bind(this.del_session, this);
        this.update_session = __bind(this.update_session, this);
        this.add_session_modal = __bind(this.add_session_modal, this);
        this.del_timeslot = __bind(this.del_timeslot, this);
        this.add_timeslot = __bind(this.add_timeslot, this);
        this.add_timeslot_modal = __bind(this.add_timeslot_modal, this);
        this.edit_room = __bind(this.edit_room, this);
        this.edit_room_modal = __bind(this.edit_room_modal, this);
        this.del_room = __bind(this.del_room, this);
        this.add_room = __bind(this.add_room, this);
        this.add_room_modal = __bind(this.add_room_modal, this);
        this.delete_all_sessions = __bind(this.delete_all_sessions, this);
        this.data = {};
        this.options = $.extend({}, defaults, options);
        this._defaults = defaults;
        this._name = pluginName;
        this.init();
      }
  
      Plugin.prototype.init = function() {
        this.loadState();
        return $('#delete-all-sessions').click((function(_this) {
          return function() {
            return _this.delete_all_sessions();
          };
        })(this));
      };
  
      Plugin.prototype.update = function() {
        this.saveState();
        return this.render();
      };
  
      Plugin.prototype.loadState = function() {
        return $.ajax({
          url: "sessionboard/data",
          dataType: 'json',
          cache: false,
          success: (function(_this) {
            return function(data) {
              _this.data = data;
              return _this.render();
            };
          })(this),
          error: (function(_this) {
            return function(xhr, status, err) {
              return console.error("url", status, err.toString());
            };
          })(this)
        });
      };
  
      Plugin.prototype.saveState = function() {
        var data;
        data = {
          rooms: this.data.rooms,
          timeslots: this.data.timeslots,
          sessions: this.data.sessions
        };
        return $.ajax({
          url: "sessionboard/data",
          data: JSON.stringify(this.data),
          contentType: 'application/json',
          type: 'POST',
          success: (function(_this) {
            return function(data) {};
          })(this),
          error: (function(_this) {
            return function(data) {
              alert("an error occurred saving the data");
              return _this.loadState();
            };
          })(this)
        });
      };
  
      Plugin.prototype.get_session_id = function(slot, room) {
  
        /*
        generate a session if from slot and room
         */
        return room.id + "@" + slot.time;
      };
  
      Plugin.prototype.generate_sessiontable = function() {
  
        /*
        generates the session table for rendering.
        
        It basically is a list of lists for each column and row
         */
        var room, row, sid, slot, table, _i, _j, _len, _len1, _ref, _ref1;
        table = [];
        _ref = this.data.timeslots;
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          slot = _ref[_i];
          row = {
            time: slot.time,
            blocked: slot.blocked,
            block_reason: slot.reason,
            slots: []
          };
          _ref1 = this.data.rooms;
          for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
            room = _ref1[_j];
            sid = this.get_session_id(slot, room);
            if (this.data.sessions[sid]) {
              row.slots.push(this.data.sessions[sid]);
            } else {
              row.slots.push({
                _id: sid
              });
            }
          }
          table.push(row);
        }
        return table;
      };
  
      Plugin.prototype.render = function() {
        var html;
        html = JST["sessiontest"]({
          data: this.data,
          sessions: this.generate_sessiontable(),
          colwidth: 90 / (this.data.rooms.length + 1)
        });
        $("#newsessions").html(html);
        return this.init_handlers();
      };
  
      Plugin.prototype.delete_all_sessions = function() {
        this.data.sessions = {};
        return this.update();
      };
  
      Plugin.prototype.add_room_modal = function() {
        var html;
        html = JST["room-modal"]({
          add_room: true
        });
        $("#modals").html(html);
        $('#add-room-modal').modal('show');
        $('#room-form-name').focus();
        $("#add-room-form").submit(this.add_room);
        return false;
      };
  
      Plugin.prototype.add_room = function(event) {
        var room;
        event.preventDefault();
        room = $("#add-room-form").serializeObject();
        room.id = guid();
        this.data.rooms.push(room);
        this.update();
        $('#add-room-modal').modal('hide');
        return false;
      };
  
      Plugin.prototype.del_room = function(event) {
  
        /*
        delete a room after asking for confirmation
         */
        var idx;
        if (confirm($('body').data("i18n-areyousure"))) {
          idx = $(event.currentTarget).data("index");
          this.data.rooms.splice(idx, 1);
          return this.update();
        }
      };
  
      Plugin.prototype.edit_room_modal = function(event) {
        var html, idx, room;
        idx = $(event.currentTarget).data("index");
        room = this.data.rooms[idx];
        html = JST["room-modal"]({
          room: room,
          room_idx: idx,
          add_room: false
        });
        $("#modals").html(html);
        $('#add-room-modal').modal('show');
        $('#room-form-name').focus();
        $("#add-room-form").submit(this.edit_room);
        return false;
      };
  
      Plugin.prototype.edit_room = function(event) {
        var room, room_idx;
        event.preventDefault();
        room = $("#add-room-form").serializeObject();
        room_idx = room['room_idx'];
        if (!room_idx) {
          console.error("room index was missing");
          return alert("Error");
        }
        room = JSON.parse(JSON.stringify(room));
        if (room.room_idx) {
          delete room.room_idx;
        }
        this.data.rooms[room_idx] = room;
        this.update();
        $('#add-room-modal').modal('hide');
        return false;
      };
  
      Plugin.prototype.set_next_time = function() {
  
        /*
        computes the next possible time for the timeslot modal
         */
        var hour, l, last_time, parts;
        l = this.data.timeslots.length;
        if (l) {
          last_time = this.data.timeslots[l - 1].time;
          try {
            parts = last_time.split(":");
            hour = parseInt(parts[0]) + 1;
            return $("#timepicker").timepicker('setTime', hour + ':' + parts[1]);
          } catch (_error) {
  
          }
        } else {
          $("#timepicker").timepicker('option', 'minTime', '00:00');
          return $("#timepicker").timepicker('setTime', '09:00');
        }
      };
  
      Plugin.prototype.add_timeslot_modal = function(event) {
  
        /*
        show the timeslot modal and set the next available time
         */
        var html;
        html = JST["timeslot-modal"]();
        $("#modals").html(html);
        $("#timepicker").timepicker({
          timeFormat: "G:i",
          show24: true
        });
        this.set_next_time();
        $('#add-timeslot-modal').modal('show');
        $('#timepicker').focus();
        $('#add-timeslot-form').parsley();
        $("#add-timeslot-form").submit(this.add_timeslot);
        return false;
      };
  
      Plugin.prototype.add_timeslot = function(event) {
  
        /*
        add a new timeslot to the list of timeslots
         */
        var parts, timeslot;
        event.preventDefault();
        timeslot = $("#add-timeslot-form").serializeObject();
        parts = timeslot.time.split(":");
        if (parts[0].length === 1) {
          timeslot.time = "0" + timeslot.time;
        }
        this.data.timeslots.push(timeslot);
        this.data.timeslots = _.sortBy(this.data.timeslots, 'time');
        this.update();
        $('#add-timeslot-modal').modal('hide');
      };
  
      Plugin.prototype.del_timeslot = function(event) {
  
        /*
        delete a timeslot after asking for confirmation
         */
        var idx;
        if (confirm($('body').data("i18n-areyousure"))) {
          idx = $(event.currentTarget).data("index");
          this.data.timeslots.splice(idx, 1);
          return this.update();
        }
      };
  
      Plugin.prototype.add_session_modal = function(event) {
        var html, moderators, payload, proposals, sid;
        sid = $(event.currentTarget).closest(".sessionslot").data("id");
        payload = this.data.sessions[sid];
        if (!payload) {
          payload = {};
        }
        payload.session_idx = sid;
        html = JST["session-modal"](payload);
        $("#modals").html(html);
        moderators = new Bloodhound({
          datumTokenizer: Bloodhound.tokenizers.whitespace,
          queryTokenizer: Bloodhound.tokenizers.whitespace,
          local: this.data.participants.map(function(p) {
            return p.name;
          })
        });
        $("#moderator").tagsinput({
          tagClass: 'btn btn-info btn-xs',
          typeaheadjs: {
            source: moderators.ttAdapter()
          }
        });
        proposals = new Bloodhound({
          datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
          queryTokenizer: Bloodhound.tokenizers.whitespace,
          local: this.data.proposals
        });
        $('#ac-title').typeahead(null, {
          name: 'proposals',
          display: 'value',
          templates: {
            suggestion: Handlebars.compile('<div>{{label}}</div>')
          },
          source: proposals.ttAdapter()
        }).bind("typeahead:select", (function(_this) {
          return function(obj, datum, name) {
            var user, user_id, _i, _len, _ref, _results;
            $("#session-description").text(datum.description);
            $("#ac-title").text(datum.value);
            user_id = datum.user_id;
            _ref = _this.data.participants;
            _results = [];
            for (_i = 0, _len = _ref.length; _i < _len; _i++) {
              user = _ref[_i];
              if (user._id === user_id) {
                $('#moderator').tagsinput('removeAll');
                _results.push($('#moderator').tagsinput('add', user.name));
              } else {
                _results.push(void 0);
              }
            }
            return _results;
          };
        })(this));
        $('#edit-session-modal').modal('show');
        $('#ac-title').focus();
        return $("#edit-session-form").submit(this.update_session);
      };
  
      Plugin.prototype.update_session = function(event) {
  
        /*
        actually add the session to the data structure
         */
        var fd, session;
        event.preventDefault();
        fd = $("#edit-session-form").serializeObject();
        if (!fd.session_idx) {
          alert("An error occurred, please reload the page");
        }
        session = {
          sid: guid(),
          slug: null,
          _id: fd.session_idx,
          title: fd.title,
          description: fd.description,
          moderator: fd.moderator
        };
        this.data.sessions[fd.session_idx] = session;
        this.update();
        $('#edit-session-modal').modal('hide');
        return false;
      };
  
      Plugin.prototype.del_session = function(event) {
  
        /*
        delete a session after asking for confirmation
         */
        var elem_id, idx;
        if (confirm($('body').data("i18n-areyousure"))) {
          idx = $(event.currentTarget).closest(".sessionslot").data("id");
          elem_id = $(event.currentTarget).closest(".sessionslot").attr("id");
          delete this.data.sessions[idx];
          this.saveState();
          $("#" + elem_id).css({
            background: "#f00"
          }).fadeOut(400, this.render);
        }
        return false;
      };
  
      Plugin.prototype.init_handlers = function() {
        var room_dict, that;
        that = this;
        try {
          room_dict = this.data.rooms.toDict("id");
        } catch (_error) {
          room_dict = {};
        }
        $("#roomcontainment").sortable({
          axis: 'x',
          helper: "clone",
          items: "th",
          placeholder: "sortable-placeholder",
          containment: 'parent',
          cancel: ".not-sortable",
          opacity: 0.5,
          update: (function(_this) {
            return function(event, ui) {
              var new_rooms;
              new_rooms = [];
              $("#newsessions #roomcontainment .sorted").each(function() {
                var id;
                id = $(this).data("id");
                return new_rooms.push(room_dict[id]);
              });
              _this.data.rooms = new_rooms;
              return _this.update();
            };
          })(this)
        });
        $(".sessionslot.enabled").draggable({
          revert: true,
          snap: ".sessionslot.enabled",
          zIndex: 10000
        }).droppable({
          hoverClass: "btn btn-info",
          drop: (function(_this) {
            return function(event, ui) {
              var dest_idx, old_element, src_idx;
              src_idx = ui.draggable.data("id");
              dest_idx = $(event.target).data("id");
              old_element = _this.data.sessions[dest_idx];
              _this.data.sessions[dest_idx] = _this.data.sessions[src_idx];
              _this.data.sessions[dest_idx]._id = dest_idx;
              if (old_element) {
                _this.data.sessions[src_idx] = old_element;
                old_element._id = src_idx;
              } else {
                delete _this.data.sessions[src_idx];
              }
              return _this.update();
            };
          })(this)
        });
        $("#add-room-modal-button").click(this.add_room_modal);
        $(".del-room-button").click(this.del_room);
        $(".edit-room-modal-button").click(this.edit_room_modal);
        $("#add-timeslot-modal-button").click(this.add_timeslot_modal);
        $(".del-timeslot-button").click(this.del_timeslot);
        $(".edit-session-button").click(this.add_session_modal);
        return $(".del-session-button").click(this.del_session);
      };
  
      return Plugin;
  
    })();
    return $.fn[pluginName] = function(options) {
      return this.each(function() {
        if (!$.data(this, "plugin_" + pluginName)) {
          return $.data(this, "plugin_" + pluginName, new Plugin(this, options));
        }
      });
    };
  })(jQuery, window, document);
  
  $(function() {
    init_i18n();
    return $('.dropdown-toggle').dropdown();
  });
  