// Generated by CoffeeScript 1.10.0
(function() {
  var EventsCalendar;

  EventsCalendar = (function() {
    function EventsCalendar() {
      return;
    }

    EventsCalendar.prototype.enable = function() {
      this._enablePopup();
      $(window).resize((function(_this) {
        return function() {
          return _this._handleResize();
        };
      })(this));
      this._handleResize();
    };

    EventsCalendar.prototype._enablePopup = function() {
      if ($("#calendar-overlay").length === 0) {
        $("<div id=\"calendar-overlay\"></div>\n<div class=\"day-popup-outer\">\n  <div id=\"read-more-events\" class=\"calendar day-popup\">\n    <a class=\"close\" href=\"#\">×</a>\n    <div class=\"day-title\"></div>\n    <div class=\"days-events\"></div>\n  </div>\n</div>").appendTo("body");
      }
      $("#calendar-overlay, .day-popup-outer, .day-popup .close").click(function() {
        $("#calendar-overlay, .day-popup-outer").hide();
        return false;
      });
      return $(".day-popup").click(function(event) {
        return event.stopPropagation();
      });
    };

    EventsCalendar.prototype._handleResize = function() {
      if ($("tbody").hasClass("monthly-view")) {
        this._adjustDays();
      }
      return this._linkReadMore();
    };

    EventsCalendar.prototype._adjustDays = function() {
      var eventsHeight, height, width;
      width = $("tbody.monthly-view td.day .day-title").first().outerWidth();
      height = $("tbody.monthly-view td.day .day-title").first().outerHeight();
      eventsHeight = (width - height - 1) * 0.71;
      $("tbody.monthly-view .days-events").outerHeight(eventsHeight);
    };

    EventsCalendar.prototype._linkReadMore = function() {
      $(".days-events").each((function(_this) {
        return function(index, element) {
          var day;
          day = $(element).closest("td.day");
          day.find("a.read-more").remove();
          if (element.offsetHeight < element.scrollHeight || element.offsetWidth < element.scrollWidth) {
            _this._addReadMoreLink(day);
          }
        };
      })(this));
    };

    EventsCalendar.prototype._addReadMoreLink = function(day) {
      var link;
      link = $("<a>").attr('href', 'javascript:void 0').attr('title', "Show all of this day's events").addClass("read-more").text("+");
      link.click(function(ev) {
        var events, title, y;
        title = day.find(".day-title").clone();
        $("#read-more-events .day-title").replaceWith(title);
        events = day.find(".days-events").clone().height('auto');
        $("#read-more-events .days-events").replaceWith(events);
        y = Math.max(ev.pageY - 100, $(window).scrollTop());
        $(".day-popup-outer").css('top', y);
        $("#calendar-overlay, .day-popup-outer").show();
        return false;
      });
      day.append(link);
    };

    return EventsCalendar;

  })();

  $(function() {
    var calendar;
    calendar = new EventsCalendar();
    calendar.enable();
  });

}).call(this);
