If your usage of it requires different things then perhaps we could abstract a more general case together.
Here is a more cut and paste friendly version of the changes.
I have cut from comment to comment to allow you to see some overlapping lines on each end, for ease in finding the end position.
/*
* If overlapping is allowed, check for overlapping events and format
* for greater readability
*/
_adjustOverlappingEvents : function($weekDay) {
var self = this;
if(self.options.allowCalEventOverlap) {
var groups = self._groupOverlappingEventElements($weekDay);
$.each(groups, function(){
var groupAmount = this.length;
var curGroup = this;
// do we want events to be displayed as overlapping
if (self.options.overlapEventsSeparate){
var newWidth = 100/groupAmount;
var newLeftAdd = newWidth;
} else {
// TODO what happens when the group has more than 10 elements
var newWidth = (100 - (groupAmount*10));
var newLeftAdd = (100 - newWidth) / (groupAmount-1);
}
$.each(this, function(i){
if (!$(this).hasClass('weekcalendar_background')) {
var newLeft = (i * newLeftAdd);
// bring mouseovered event to the front
if(!self.options.overlapEventsSeparate){
$(this).bind("mouseover.z-index",function(){
var $elem = $(this);
$.each(curGroup, function() {
$(this).css({"z-index": "1"});
});
$elem.css({"z-index": "3"});
});
}
$(this).css({width: newWidth+"%", left: newLeft+"%", right: 0});
});
});
}
},
/*
* Find groups of overlapping events
*/
_groupOverlappingEventElements : function($weekDay) {
var self = this;
var sortedEvents = $events.sort(function(a, b){
return $(a).data("calEvent").start.getTime() - $(b).data("calEvent").start.getTime();
});
var $lastEvent;
var groups = [];
var currentGroup = [];
var $curEvent;
var currentGroupStartTime = 0;
var currentGroupEndTime = 0;
var lastGroupEndTime = 0;
$.each(sortedEvents, function(){
$curEvent = $(this);
currentGroupStartTime = $curEvent.data("calEvent").start.getTime();
currentGroupEndTime = $curEvent.data("calEvent").end.getTime();
$curEvent.css({width: "100%", left: "0%", right: "", "z-index": "1"});
$curEvent.unbind("mouseover.z-index");
// if current start time is lower than current endtime time than either this event is earlier than the group, or already within the group
if ($curEvent.data("calEvent").start.getTime() < lastGroupEndTime) {
return;
}
// loop through all current weekday events to check if they belong to the same group
$.each(sortedEvents, function() {
// check for same element and possibility to even be in the same group note: somehow ($curEvent == $(this) doens't work
if ($curEvent.data("calEvent").id == $(this).data("calEvent").id ||
currentGroupStartTime > $(this).data("calEvent").start.getTime()+1 ||
currentGroupEndTime < $(this).data("calEvent").start.getTime()+1) {
return;
}
//set new endtime of the group
if ($(this).data("calEvent").end.getTime() > currentGroupEndTime) {
currentGroupEndTime = $(this).data("calEvent").end.getTime();
}
// ain't we adding the same element
if ($.inArray($(this), currentGroup) == -1) {
currentGroup.push($(this));
}
// ain't we adding the same element
if ($.inArray($curEvent, currentGroup) == -1) {
currentGroup.push($curEvent);
}
});
if(currentGroup.length) {
currentGroup.sort(function(a,b){
if ($(a).data("calEvent").start.getTime() > $(b).data("calEvent").start.getTime()) {
return 1;
} else if ($(a).data("calEvent").start.getTime() < $(b).data("calEvent").start.getTime()) {
return -1;
} else {
return ($(a).data("calEvent").end.getTime() - $(a).data("calEvent").start.getTime())
- ($(b).data("calEvent").end.getTime() - $(b).data("calEvent").start.getTime());
}
});
groups.push(currentGroup);
currentGroup = [];
lastGroupEndTime = currentGroupEndTime;
}
});
return groups;
},
/*
* Check if two groups containt the same events. It assumes the groups are sorted NOTE: might be obsolete
*/