/**
 * Sponsor Functions
 */
function mf_sponsor_carousel($category_name)
{
    // Get all of the sponsor from the links
    $sponsors = get_bookmarks(array('orderby' => 'rating', 'category_name' => $category_name));
    // Split them into chucks of two
    $sponsors = array_chunk($sponsors, 2, true);
    // Get the output started.
    $output = '<div id="myCarousel" class="carousel slide">';
    $output .= '<div class="carousel-inner">';
    // Loop through each block of sponsors
    foreach ($sponsors as $idx => $sponsor) {
        if ($idx == 0) {
            $output .= '<div class="item active">';
        } else {
            $output .= '<div class="item">';
        }
        $output .= '<div class="row-fluid">';
        // Loop through the individual sponsors
        foreach ($sponsor as $spon) {
            $output .= '<div class="span6"><div class="thumb"><a href="' . esc_url($spon->link_url) . '"><img src="' . wpcom_vip_get_resized_remote_image_url($spon->link_image, 125, 105) . '" alt="' . esc_attr($spon->link_name) . '"></a></div></div>';
        }
        $output .= '</div></div>';
    }
    $output .= '</div></div>';
    return $output;
}
/**
 * Generate the schedule for the schedule page. Based on a shortcode. Pass in the ID, and get the schedule for both days.
 */
function mf_schedule($atts)
{
    extract(shortcode_atts(array(), $atts));
    $output = '';
    $location = isset($atts['location']) ? sanitize_text_field($atts['location']) : '';
    $faire = isset($atts['faire']) ? sanitize_text_field($atts['faire']) : '';
    if (!empty($location)) {
        $term = wpcom_vip_get_term_by('id', $location, 'location');
        $url = get_term_link($term);
        if (!is_wp_error($url)) {
            $output .= '<a href="' . esc_url(home_url('/stage-schedule/?location=' . $term->slug)) . '" class="pull-right" style="position:relative; top:7px;"><img src="' . get_stylesheet_directory_uri() . '/images/print-ico.png" alt="' . __('Print this schedule', 'make-mini-mf') . '" /></a>';
            $output .= '<h2><a href="' . esc_url($url) . '">' . esc_html($term->name) . '</a></h2>';
            $output .= mf_stage_description($term);
        }
    }
    $query = wp_cache_get($location . '_saturday_schedule');
    if (!isset($term->slug)) {
        return;
    }
    if ($query == false) {
        $args = array('location' => sanitize_title($term->slug), 'post_type' => 'event-items', 'orderby' => 'meta_value', 'meta_key' => 'mfei_start', 'order' => 'asc', 'posts_per_page' => '30', 'faire' => $faire, 'meta_query' => array(array('key' => 'mfei_day', 'value' => 'Saturday')));
        $query = new WP_Query($args);
        wp_cache_set($location . '_saturday_schedule', $query, '', 300);
    }
    if ($query->found_posts >= 1) {
        $output .= '<table class="table table-striped table-bordered table-schedule">';
        if ($faire == 'world-maker-faire-new-york-2013') {
            $output .= '<thead><tr><th colspan="2">September 21st, 2013</th></tr></thead>';
        }
        while ($query->have_posts()) {
            $query->the_post();
            $meta = get_post_meta(get_the_ID());
            $sched_post = get_post($meta['mfei_record'][0]);
            $json = json_decode(mf_convert_newlines(str_replace("\\'", "'", $sched_post->post_content)));
            $day = $meta['mfei_day'][0] ? $meta['mfei_day'][0] : '';
            $start = $meta['mfei_start'][0] ? $meta['mfei_start'][0] : '';
            $stop = $meta['mfei_stop'][0] ? $meta['mfei_stop'][0] : '';
            $output .= '<tr>';
            $output .= '<td width="150" style="max-width:150px;">';
            $output .= '<h5>' . esc_html($day) . '</h5>';
            $output .= '<p>' . esc_html($start) . ' &mdash; ' . esc_html($stop) . '</p>';
            if (isset($json->presenter_photo) or isset($json->project_photo) or isset($json->presentation_photo) or isset($json->performer_photo) or has_post_thumbnail(get_the_ID())) {
                if (get_the_post_thumbnail()) {
                    $output .= '<div class="pull-left thumbnail"><a href="';
                    $output .= get_permalink($sched_post) . '">';
                    $output .= get_the_post_thumbnail(get_the_ID(), 'schedule-thumb');
                    $output .= '</a></div>';
                } elseif (isset($json->presenter_photo[0]) && !is_array($json->presenter_photo[0]) && strlen($json->presenter_photo[0]) > 5) {
                    $output .= '<div class="pull-left thumbnail"><a href="';
                    $output .= get_permalink($sched_post) . '">';
                    $output .= '<img src="' . wpcom_vip_get_resized_remote_image_url($json->presenter_photo[0], 140, 140) . '" alt="' . esc_attr(get_the_title($sched_post->ID)) . '" />';
                    $output .= '</a></div>';
                } else {
                    $output .= '<div class="pull-left thumbnail"><a href="';
                    $output .= get_permalink($sched_post) . '">';
                    $output .= '<img src="' . wpcom_vip_get_resized_remote_image_url(mf_get_the_maker_image($json), 140, 140) . '" alt="' . esc_attr(get_the_title($sched_post->ID)) . '" />';
                    $output .= '</a></div>';
                }
            }
            $output .= '</td>';
            $output .= '<td>';
            $output .= '<h3><a href="' . get_permalink($sched_post) . '">' . get_the_title($sched_post->ID) . '</a></h3>';
            if (!empty($json->presenter_name)) {
                $names = $json->presenter_name;
                $names_output = '';
                foreach ($names as $name) {
                    $names_output .= ', ' . $name;
                }
                $output .= '<h4>' . substr($names_output, 2) . '</h4>';
            }
            if (!empty($json->short_description)) {
                $output .= Markdown(stripslashes(wp_filter_post_kses(mf_convert_newlines($json->short_description, "\n"))));
            } elseif (!empty($json->public_description)) {
                $output .= Markdown(stripslashes(wp_filter_post_kses(mf_convert_newlines($json->public_description, "\n"))));
            }
            if (!empty($meta['mfei_coverage'][0])) {
                $output .= '<p><a href="' . esc_url($meta['mfei_coverage'][0]) . '" class="btn btn-mini btn-primary">Watch Video</a></p>';
            }
            // $output .= '<ul class="unstyled">';
            // $terms = get_the_terms( $sched_post->ID, array( 'category', 'post_tag' ) );
            // if (!empty($terms)) {
            // 	$output .= '<li>Topics: ';
            // 	$the_terms = '';
            // 	foreach ($terms as $idx => $term) {
            // 		$the_terms .= ', <a href="' . esc_url( get_term_link( $term ) ) . '">' . $term->name . '</a>';
            // 	}
            // 	$output .= substr( $the_terms, 2 );
            // 	$output .= '</li>';
            // }
            // $output .= '</ul>';
            $output .= '</td>';
            $output .= '</tr>';
        }
        $output .= '</table>';
    }
    wp_reset_postdata();
    // Roll the schedule for Sunday.
    if ($query->found_posts >= 1) {
        $query = wp_cache_get($location . '_sunday_schedule');
        if ($query == false) {
            $args = array('location' => sanitize_title($term->slug), 'post_type' => 'event-items', 'orderby' => 'meta_value', 'meta_key' => 'mfei_start', 'order' => 'asc', 'posts_per_page' => '30', 'faire' => $faire, 'meta_query' => array(array('key' => 'mfei_day', 'value' => 'Sunday')));
            $query = new WP_Query($args);
            wp_cache_set($location . '_sunday_schedule', $query, '', 300);
        }
        $output .= '<table class="table table-striped table-bordered table-schedule">';
        if ($faire == 'world-maker-faire-new-york-2013') {
            $output .= '<thead><tr><th colspan="2">September 22nd, 2013</th></tr></thead>';
        }
        while ($query->have_posts()) {
            $query->the_post();
            $meta = get_post_meta(get_the_ID());
            $sched_post = get_post($meta['mfei_record'][0]);
            $json = json_decode(str_replace("\\'", "'", $sched_post->post_content));
            $day = $meta['mfei_day'][0] ? $meta['mfei_day'][0] : '';
            $start = $meta['mfei_start'][0] ? $meta['mfei_start'][0] : '';
            $stop = $meta['mfei_stop'][0] ? $meta['mfei_stop'][0] : '';
            $output .= '<tr>';
            $output .= '<td width="150">';
            $output .= '<h5>' . esc_html($day) . '</h5>';
            $output .= '<p>' . esc_html($start) . ' &mdash; ' . esc_html($stop) . '</p>';
            if (isset($json->presenter_photo) or isset($json->project_photo) or isset($json->presentation_photo) or isset($json->performer_photo) or has_post_thumbnail(get_the_ID())) {
                if (get_the_post_thumbnail()) {
                    $output .= '<div class="pull-left thumbnail"><a href="';
                    $output .= get_permalink($sched_post) . '">';
                    $output .= get_the_post_thumbnail(get_the_ID(), 'schedule-thumb');
                    $output .= '</a></div>';
                } elseif (isset($json->presenter_photo[0]) && !is_array($json->presenter_photo[0]) && strlen($json->presenter_photo[0]) > 5) {
                    $output .= '<div class="pull-left thumbnail"><a href="';
                    $output .= get_permalink($sched_post) . '">';
                    $output .= '<img src="' . wpcom_vip_get_resized_remote_image_url($json->presenter_photo[0], 140, 140) . '" alt="' . esc_attr(get_the_title($sched_post->ID)) . '" />';
                    $output .= '</a></div>';
                } else {
                    $output .= '<div class="pull-left thumbnail"><a href="';
                    $output .= get_permalink($sched_post) . '">';
                    $output .= '<img src="' . wpcom_vip_get_resized_remote_image_url(mf_get_the_maker_image($json), 140, 140) . '" alt="' . esc_attr(get_the_title($sched_post->ID)) . '" />';
                    $output .= '</a></div>';
                }
            }
            $output .= '</td>';
            $output .= '<td>';
            $output .= '<h3><a href="' . get_permalink($sched_post) . '">' . get_the_title($sched_post->ID) . '</a></h3>';
            if (!empty($json->presenter_name)) {
                $names = $json->presenter_name;
                $names_output = '';
                foreach ($names as $name) {
                    $names_output .= ', ' . $name;
                }
                $output .= '<h4>' . substr($names_output, 2) . '</h4>';
            }
            if (!empty($json->short_description)) {
                $output .= Markdown(stripslashes(wp_filter_post_kses(mf_convert_newlines($json->short_description, "\n"))));
            } elseif (!empty($json->public_description)) {
                $output .= Markdown(stripslashes(wp_filter_post_kses(mf_convert_newlines($json->public_description, "\n"))));
            }
            if (!empty($meta['mfei_coverage'][0])) {
                $output .= '<p><a href="' . esc_url($meta['mfei_coverage'][0]) . '" class="btn btn-mini btn-primary">Watch Video</a></p>';
            }
            // $output .= '<ul class="unstyled">';
            // $terms = get_the_terms( $sched_post->ID, array( 'category', 'post_tag' ) );
            // if (!empty($terms)) {
            // 	$output .= '<li>Topics: ';
            // 	$the_terms = '';
            // 	foreach ($terms as $idx => $term) {
            // 		$the_terms .= ', <a href="' . get_term_link( $term ) . '">' . $term->name . '</a>';
            // 	}
            // 	$output .= substr( $the_terms, 2 );
            // 	$output .= '</li>';
            // }
            // $output .= '</ul>';
            $output .= '</td>';
            $output .= '</tr>';
        }
        $output .= '</table>';
    }
    wp_reset_postdata();
    return $output;
}