function checkOverlap($event_date, $event_time, $uid)
{
    global $master_array, $overlap_array;
    if (!isset($event_date)) {
        return;
    }
    $event = $master_array[$event_date][$event_time][$uid];
    // Copy out the array - we replace this at the end.
    $ol_day_array = $overlap_array[$event_date];
    $drawTimes = drawEventTimes($event['event_start'], $event['event_end']);
    // For a given date,
    // 	- check to see if the event's already in a block, and if so, add it.
    //		- make sure the new block doesn't overlap another block, and if so, merge the blocks.
    // - check that there aren't any events we already passed that we should handle.
    //		- "flatten" the structure again, merging the blocks.
    // $overlap_array structure:
    //	array of ($event_dates)
    //		array of unique overlap blocks (no index) -
    // $overlap_block structure
    // 'blockStart'    - $start_time of block - earliest $start_time of the events in the block.
    //					 Shouldn't be any overlap w/ a different overlap block in that day (as if they overlap, they get merged).
    // 'blockEnd'      - $end_time of block - latest $end_time of the events in the block.
    // 'maxOverlaps'   - max number of overlaps for the whole block (highest 'count' in overlapRanges)
    // 'events'        - array of event "pointers" (no index) - each event in the block.
    //		'time' - $start_time of event in the block
    //		'key'  - $uid of event
    // 'overlapRanges' - array of time ranges + overlap counts (no index) - the specific overlap info.
    //					 Shouldn't be any overlap w/ the overlap ranges in a given overlap_block - if there is overlap, the block should be split.
    //		'count' - number of overlaps that time range (can be zero if that range has no overlaps).
    //		'start' - start_time for the overlap block.
    //		'end'	- end_time for the overlap block.
    $ol_day_array = $overlap_array[$event_date];
    // Track if $event has been merged in, so we don't re-add the details to 'event' or 'overlapRanges' multiple times.
    $already_merged_once = false;
    // First, check the existing overlap blocks, see if the event overlaps with any.
    if (isset($ol_day_array)) {
        foreach ($ol_day_array as $loop_block_key => $loop_ol_block) {
            // Should $event be in this $ol_block? If so, add it.
            if ($loop_ol_block['blockStart'] < $drawTimes['draw_end'] && $loop_ol_block['blockEnd'] > $drawTimes['draw_start']) {
                // ... unless it's already in the $ol_block
                if (!in_array(array('time' => $drawTimes['draw_start'], 'key' => $uid), $loop_ol_block['events'])) {
                    $loop_ol_block['events'][] = array('time' => $drawTimes['draw_start'], 'key' => $uid);
                    if ($loop_ol_block['blockStart'] > $drawTimes['draw_start']) {
                        $loop_ol_block['blockStart'] = $drawTimes['draw_start'];
                    }
                    if ($loop_ol_block['blockEnd'] < $drawTimes['draw_end']) {
                        $loop_ol_block['blockEnd'] = $drawTimes['draw_end'];
                    }
                    // Merge in the new overlap range
                    $loop_ol_block['overlapRanges'] = merge_range($loop_ol_block['overlapRanges'], $drawTimes['draw_start'], $drawTimes['draw_end']);
                    $loop_ol_block['maxOverlaps'] = find_max_overlap($loop_ol_block['overlapRanges']);
                    foreach ($loop_ol_block['events'] as $max_overlap_event) {
                        $master_array[$event_date][$max_overlap_event['time']][$max_overlap_event['key']]['event_overlap'] = $loop_ol_block['maxOverlaps'];
                    }
                    $ol_day_array[$loop_block_key] = $loop_ol_block;
                    $ol_day_array = flatten_ol_blocks($event_date, $ol_day_array, $loop_block_key);
                    $already_merged_once = true;
                    break;
                    // Handle repeat calls to checkOverlap - semi-bogus since the event shouldn't be created more than once, but this makes sure we don't get an invalid event_overlap.
                } else {
                    $master_array[$event_date][$event_time][$uid]['event_overlap'] = $loop_ol_block['maxOverlaps'];
                }
            }
        }
    }
    // Then, check all the events, make sure there isn't a new overlap that we need to create.
    foreach ($master_array[$event_date] as $time_key => $time) {
        // Skip all-day events for overlap purposes.
        if ($time_key != '-1') {
            foreach ($time as $loop_event_key => $loop_event) {
                // Make sure we haven't already dealt with the event, and we're not checking against ourself.
                if ($loop_event['event_overlap'] == 0 && $loop_event_key != $uid) {
                    $loopDrawTimes = drawEventTimes($loop_event['event_start'], $loop_event['event_end']);
                    if ($loopDrawTimes['draw_start'] < $drawTimes['draw_end'] && $loopDrawTimes['draw_end'] > $drawTimes['draw_start']) {
                        if ($loopDrawTimes['draw_start'] < $drawTimes['draw_start']) {
                            $block_start = $loopDrawTimes['draw_start'];
                        } else {
                            $block_start = $drawTimes['draw_start'];
                        }
                        if ($loopDrawTimes['draw_end'] > $drawTimes['draw_end']) {
                            $block_end = $loopDrawTimes['draw_end'];
                        } else {
                            $block_end = $drawTimes['draw_end'];
                        }
                        $events = array(array('time' => $loopDrawTimes['draw_start'], 'key' => $loop_event_key));
                        $overlap_ranges = array(array('count' => 0, 'start' => $loopDrawTimes['draw_start'], 'end' => $loopDrawTimes['draw_end']));
                        // Only add $event if we haven't already put it in a block
                        if (!$already_merged_once) {
                            $events[] = array('time' => $drawTimes['draw_start'], 'key' => $uid);
                            $overlap_ranges = merge_range($overlap_ranges, $drawTimes['draw_start'], $drawTimes['draw_end']);
                            $already_merged_once = true;
                        }
                        $ol_day_array[] = array('blockStart' => $block_start, 'blockEnd' => $block_end, 'maxOverlaps' => 1, 'events' => $events, 'overlapRanges' => $overlap_ranges);
                        foreach ($events as $max_overlap_event) {
                            $master_array[$event_date][$max_overlap_event['time']][$max_overlap_event['key']]['event_overlap'] = 1;
                        }
                        // Make sure we pass in the key of the newly added item above.
                        end($ol_day_array);
                        $last_day_key = key($ol_day_array);
                        $ol_day_array = flatten_ol_blocks($event_date, $ol_day_array, $last_day_key);
                    }
                }
            }
        }
    }
    $overlap_array[$event_date] = $ol_day_array;
    //for debugging the checkOverlap function
    //if ($event_date == '20050506') {
    //print 'Date: ' . $event_date . ' / Time: ' . $event_time . ' / Key: ' . $uid . "<br />\n";
    //print '<pre>';
    //print_r($master_array[$event_date]);
    //print_r($overlap_array[$event_date]);
    //print '</pre>';
    //}
}
function checkOverlap($event_date, $event_time, $uid)
{
    global $master_array, $overlap_array;
    if (!isset($event_date)) {
        return;
    }
    $event = $master_array[$event_date][$event_time][$uid];
    // Copy out the array - we replace this at the end.
    $ol_day_array = @$overlap_array[$event_date];
    $drawTimes = drawEventTimes($event['display_start'], $event['display_end'], $event['event_length'] >= 60 * 60 * 24);
    // Track if $event has been merged in, so we don't re-add the details to 'event' or 'overlapRanges' multiple times.
    $already_merged_once = false;
    // First, check the existing overlap blocks, see if the event overlaps with any.
    if (isset($ol_day_array)) {
        foreach ($ol_day_array as $loop_block_key => $loop_ol_block) {
            // Should $event be in this $ol_block? If so, add it.
            if ($loop_ol_block['blockStart'] < $drawTimes['draw_end'] && $loop_ol_block['blockEnd'] > $drawTimes['draw_start']) {
                // ... unless it's already in the $ol_block
                if (!in_array(array('time' => $drawTimes['draw_start'], 'key' => $uid), $loop_ol_block['events'])) {
                    $loop_ol_block['events'][] = array('time' => $drawTimes['draw_start'], 'key' => $uid);
                    if ($loop_ol_block['blockStart'] > $drawTimes['draw_start']) {
                        $loop_ol_block['blockStart'] = $drawTimes['draw_start'];
                    }
                    if ($loop_ol_block['blockEnd'] < $drawTimes['draw_end']) {
                        $loop_ol_block['blockEnd'] = $drawTimes['draw_end'];
                    }
                    // Merge in the new overlap range
                    $loop_ol_block['overlapRanges'] = merge_range($loop_ol_block['overlapRanges'], $drawTimes['draw_start'], $drawTimes['draw_end']);
                    $loop_ol_block['maxOverlaps'] = find_max_overlap($loop_ol_block['overlapRanges']);
                    foreach ($loop_ol_block['events'] as $max_overlap_event) {
                        $master_array[$event_date][$max_overlap_event['time']][$max_overlap_event['key']]['event_overlap'] = $loop_ol_block['maxOverlaps'];
                    }
                    $ol_day_array[$loop_block_key] = $loop_ol_block;
                    $ol_day_array = flatten_ol_blocks($event_date, $ol_day_array, $loop_block_key);
                    $already_merged_once = true;
                    break;
                    // Handle repeat calls to checkOverlap - semi-bogus since the event shouldn't be created more than once, but this makes sure we don't get an invalid event_overlap.
                } else {
                    $master_array[$event_date][$event_time][$uid]['event_overlap'] = $loop_ol_block['maxOverlaps'];
                }
            }
        }
    }
    // Then, check all the events, make sure there isn't a new overlap that we need to create.
    foreach ($master_array[$event_date] as $time_key => $time) {
        // Skip all-day events for overlap purposes.
        if ($time_key != '-1') {
            foreach ($time as $loop_event_key => $loop_event) {
                // Make sure we haven't already dealt with the event, and we're not checking against ourself.
                if ($loop_event['event_overlap'] == 0 && $loop_event_key != $uid) {
                    $loopDrawTimes = drawEventTimes($loop_event['display_start'], $loop_event['display_end'], $loop_event['event_length'] >= 60 * 60 * 24);
                    if ($loopDrawTimes['draw_start'] < $drawTimes['draw_end'] && $loopDrawTimes['draw_end'] > $drawTimes['draw_start']) {
                        if ($loopDrawTimes['draw_start'] < $drawTimes['draw_start']) {
                            $block_start = $loopDrawTimes['draw_start'];
                        } else {
                            $block_start = $drawTimes['draw_start'];
                        }
                        if ($loopDrawTimes['draw_end'] > $drawTimes['draw_end']) {
                            $block_end = $loopDrawTimes['draw_end'];
                        } else {
                            $block_end = $drawTimes['draw_end'];
                        }
                        $events = array(array('time' => $loopDrawTimes['draw_start'], 'key' => $loop_event_key));
                        $overlap_ranges = array(array('count' => 0, 'start' => $loopDrawTimes['draw_start'], 'end' => $loopDrawTimes['draw_end']));
                        // Only add $event if we haven't already put it in a block
                        if (!$already_merged_once) {
                            $events[] = array('time' => $drawTimes['draw_start'], 'key' => $uid);
                            $overlap_ranges = merge_range($overlap_ranges, $drawTimes['draw_start'], $drawTimes['draw_end']);
                            $already_merged_once = true;
                        }
                        $ol_day_array[] = array('blockStart' => $block_start, 'blockEnd' => $block_end, 'maxOverlaps' => 1, 'events' => $events, 'overlapRanges' => $overlap_ranges);
                        foreach ($events as $max_overlap_event) {
                            $master_array[$event_date][$max_overlap_event['time']][$max_overlap_event['key']]['event_overlap'] = 1;
                        }
                        // Make sure we pass in the key of the newly added item above.
                        end($ol_day_array);
                        $last_day_key = key($ol_day_array);
                        $ol_day_array = flatten_ol_blocks($event_date, $ol_day_array, $last_day_key);
                    }
                }
            }
        }
    }
    $overlap_array[$event_date] = $ol_day_array;
    //for debugging the checkOverlap function
    //if ($event_date == '20050506') {
    //print 'Date: ' . $event_date . ' / Time: ' . $event_time . ' / Key: ' . $uid . "<br />\n";
    //print '<pre>';
    //print_r($master_array[$event_date]);
    //print_r($overlap_array[$event_date]);
    //print '</pre>';
    //}
}