/**
 * Outputs an HTML list of readings for a reading plan
 *
 * @param array $args
 */
function bfox_plan_reading_list($args = array())
{
    $defaults = array('post_id' => 0, 'user_id' => 0, 'from_reading' => 0, 'to_reading' => -1, 'max_count' => 0, 'column_class' => 'reading-list-3c-h', 'date_format' => 'M j, Y');
    extract(wp_parse_args($args, $defaults));
    $reading_count = bfox_plan_reading_count($post_id);
    if (0 > $from_reading) {
        $from_reading += $reading_count;
    }
    // Adjust the max_count
    if (0 >= $max_count) {
        $max_count = $reading_count;
    }
    $max_count = min($max_count, $reading_count - $from_reading);
    // Adjust the to_reading
    if (0 > $to_reading) {
        $to_reading += $from_reading + $max_count;
    }
    if ($to_reading < $from_reading) {
        $to_reading = $from_reading + $max_count - 1;
    }
    // Adjust the count
    $count = $to_reading - $from_reading + 1;
    if ($count > $max_count) {
        $to_reading = $from_reading + $max_count - 1;
        $count = $max_count;
    }
    if ($count) {
        ?>
	<ol class="reading-list <?php 
        echo $column_class;
        ?>
" start="<?php 
        echo $from_reading + 1;
        ?>
">
	<?php 
        for ($reading_id = $from_reading; $reading_id <= $to_reading; $reading_id++) {
            ?>
		<li>
			<div class="reading-info">
			<?php 
            if (bfox_plan_is_scheduled($post_id)) {
                ?>
				<span class="reading-date"><?php 
                echo bfox_plan_reading_date($reading_id, $date_format, $post_id);
                ?>
</span>
			<?php 
            }
            ?>
				<span class="reading-ref"><?php 
            echo bfox_ref_link(bfox_plan_reading_ref_str($reading_id, $post_id, BibleMeta::name_short));
            ?>
</span>
				<span class="reading-note"><?php 
            echo bfox_plan_reading_note($reading_id, $post_id);
            ?>
</span>
			</div>
		</li>
	<?php 
        }
        ?>
	</ol>
<?php 
    }
    return $count;
}
function bfox_plan_reading_shortcode($atts)
{
    // [bible-reading plan="cbr" intersects="old"]
    extract(shortcode_atts(array('post_id' => 0, 'reading' => 'latest', 'intersects' => '', 'tool' => ''), $atts));
    if (empty($post_id)) {
        return;
    }
    if ($reading == 'latest') {
        $reading = bfox_plan_latest_reading($post_id);
    }
    $reading_id = intval($reading);
    if ($reading_id >= bfox_plan_reading_count($post_id)) {
        return;
    }
    $ref_str = bfox_plan_reading_ref_str($reading_id, $post_id);
    if (!empty($intersects)) {
        $ref = new BfoxRef($ref_str);
        $sub = new BibleGroupPassage('bible');
        $sub->sub_ref(new BibleGroupPassage($intersects));
        $ref->sub_ref($sub);
        $ref_str = $ref->get_string();
    }
    $link = bfox_ref_link($ref_str);
    $content = $link;
    if (!empty($tool)) {
        $passage = bfox_tool_shortcode(array('tool' => $tool, 'ref' => $ref_str));
        $content = "{$link} {$passage}";
    }
    return $content;
}
    return apply_filters('bfox_plan_update_frequency_rss', (int) bfox_plan_meta('per_day', $post_id));
}
function bfox_plan_last_build_date_rss($format = 'D, d M Y H:i:s +0000', $post_id = 0)
{
    $last_reading_date = bfox_plan_reading_gmdate(bfox_plan_latest_reading(), $format);
    $last_reading_time = strtotime($last_reading_date);
    $post = get_post($post_id);
    $last_save_date = mysql2date($format, $post->post_modified_gmt, false);
    $last_save_time = strtotime($last_save_date);
    if ($last_save_time > $last_reading_time) {
        return $last_save_date;
    } else {
        return $last_reading_date;
    }
}
function bfox_plan_ical_print_array($arr)
{
    foreach ($arr as $key => $value) {
        echo "{$key}:{$value}\n";
    }
}
header('Content-Type: text/calendar; charset=' . get_option('blog_charset'), true);
bfox_plan_ical_print_array(array('BEGIN' => 'VCALENDAR', 'PRODID' => '-//Epicfox//Biblefox for WordPress//EN', 'VERSION' => '2.0', 'CALSCALE' => 'GREGORIAN', 'METHOD' => 'PUBLISH', 'X-WR-CALNAME' => get_the_title_rss(), 'X-WR-TIMEZONE' => get_option('timezone_string'), 'X-WR-CALDESC' => get_the_excerpt()));
while (have_posts()) {
    the_post();
    for ($reading_id = bfox_plan_reading_count() - 1; $reading_id >= 0; $reading_id--) {
        $arr = array('BEGIN' => 'VEVENT', 'DTSTART;VALUE=DATE' => bfox_plan_reading_date($reading_id, 'Ymd'), 'DTEND;VALUE=DATE' => date('Ymd', strtotime(bfox_plan_reading_date($reading_id, 'Ymd') . ' + 1 day')), 'DTSTAMP' => bfox_plan_reading_gmdate($reading_id, 'Ymd\\THis\\Z'), 'UID' => bfox_plan_reading_guid($reading_id), 'URL' => esc_url(bfox_plan_reading_url($reading_id)), 'CREATED' => bfox_plan_reading_gmdate($reading_id, 'Ymd\\THis\\Z'), 'SUMMARY' => sprintf(__('#%d: %s', 'bfox'), $reading_id + 1, bfox_plan_reading_ref_str($reading_id, 0, BibleMeta::name_short)), 'LAST-MODIFIED' => bfox_plan_reading_gmdate($reading_id, 'Ymd\\THis\\Z'), 'LOCATION' => '', 'SEQUENCE' => 0, 'STATUS' => 'CONFIRMED', 'TRANSP' => 'OPAQUE', 'END' => 'VEVENT');
        bfox_plan_ical_print_array($arr);
    }
}
echo "END:VCALENDAR\n";