function station_convert_time($time = array())
{
    if (empty($time)) {
        return false;
    }
    $now = strtotime(current_time("mysql"));
    $curDate = date('Y-m-d', $now);
    $tomDate = date('Y-m-d', $now + 86400);
    //get the date for tomorrow
    //convert to 24 hour time
    $time = station_convert_schedule_to_24hour($time);
    //get a timestamp for the schedule start and end
    $time['start_timestamp'] = strtotime($curDate . ' ' . $time['start_hour'] . ':' . $time['start_min']);
    if ($time['start_meridian'] == 'pm' && $time['end_meridian'] == 'am') {
        //check for shows that run overnight into the next morning
        $time['end_timestamp'] = strtotime($tomDate . ' ' . $time['end_hour'] . ':' . $time['end_min']);
    } else {
        $time['end_timestamp'] = strtotime($curDate . ' ' . $time['end_hour'] . ':' . $time['end_min']);
    }
    //a show can't end before it begins... if it does, it ends the following day.
    if ($time['end_timestamp'] <= $time['start_timestamp']) {
        $time['end_timestamp'] = $time['end_timestamp'] + 86400;
    }
    return $time;
}
function station_shortcode_coming_up($atts)
{
    extract(shortcode_atts(array('title' => '', 'display_djs' => 0, 'show_avatar' => 0, 'show_link' => 0, 'limit' => 1, 'time' => '12', 'show_sched' => 1), $atts));
    //find out which DJ(s) are coming up today
    $djs = dj_get_next($limit);
    //print_r($djs);
    $now = strtotime(current_time("mysql"));
    $curDate = date('Y-m-d', $now);
    $dj_str = '';
    $dj_str .= '<div class="on-air-embedded dj-coming-up-embedded">';
    if ($title != '') {
        $dj_str .= '<h3>' . $title . '</h3>';
    }
    $dj_str .= '<ul class="on-air-list">';
    //echo the show/dj coming up
    if (isset($djs['all']) && count($djs['all']) > 0) {
        foreach ($djs['all'] as $showtime => $dj) {
            if (is_array($dj) && $dj['type'] == 'override') {
                echo '<li class="on-air-dj">';
                if ($show_avatar) {
                    if (has_post_thumbnail($dj['post_id'])) {
                        $dj_str .= '<span class="on-air-dj-avatar">' . get_the_post_thumbnail($dj['post_id'], 'thumbnail') . '</span>';
                    }
                }
                echo $dj['title'];
                if ($show_sched) {
                    if ($time == 12) {
                        $dj_str .= '<span class="on-air-dj-sched">' . $dj['sched']['start_hour'] . ':' . $dj['sched']['start_min'] . ' ' . $dj['sched']['start_meridian'] . '-' . $dj['sched']['end_hour'] . ':' . $dj['sched']['end_min'] . ' ' . $dj['sched']['end_meridian'] . '</span><br />';
                    } else {
                        $dj['sched'] = station_convert_schedule_to_24hour($dj['sched']);
                        $dj_str .= '<span class="on-air-dj-sched">' . $dj['sched']['start_hour'] . ':' . $dj['sched']['start_min'] . ' ' . '-' . $dj['sched']['end_hour'] . ':' . $dj['sched']['end_min'] . '</span><br />';
                    }
                }
                echo '</li>';
            } else {
                $dj_str .= '<li class="on-air-dj">';
                if ($show_avatar) {
                    $dj_str .= '<span class="on-air-dj-avatar">' . get_the_post_thumbnail($dj->ID, 'thumbnail') . '</span>';
                }
                $dj_str .= '<span class="on-air-dj-title">';
                if ($show_link) {
                    $dj_str .= '<a href="';
                    $dj_str .= get_permalink($dj->ID);
                    $dj_str .= '">';
                    $dj_str .= $dj->post_title . '</a>';
                } else {
                    $dj_str .= $dj->post_title;
                }
                $dj_str .= '</span>';
                if ($display_djs) {
                    $names = get_post_meta($dj->ID, 'show_user_list', true);
                    $count = 0;
                    if ($names) {
                        $dj_str .= '<div class="on-air-dj-names">With ';
                        foreach ($names as $name) {
                            $count++;
                            $user_info = get_userdata($name);
                            $dj_str .= $user_info->display_name;
                            if ($count == 1 && count($names) == 2 || count($names) > 2 && $count == count($names) - 1) {
                                $dj_str .= ' and ';
                            } elseif ($count < count($names) && count($names) > 2) {
                                $dj_str .= ', ';
                            } else {
                                //do nothing
                            }
                        }
                        $dj_str .= '</div>';
                    }
                }
                $dj_str .= '<span class="radio-clear"></span>';
                if ($show_sched) {
                    $showtimes = explode("|", $showtime);
                    if ($time == 12) {
                        $dj_str .= '<span class="on-air-dj-sched"><span class="on-air-dj-sched-day">' . __(date('l', $showtimes[0]), 'radio-station') . ', </span>' . date('g:i a', $showtimes[0]) . '-' . date('g:i a', $showtimes[1]) . '</span><br />';
                    } else {
                        $dj_str .= '<span class="on-air-dj-sched"><span class="on-air-dj-sched-day">' . __(date('l', $showtimes[0]), 'radio-station') . ', </span>' . date('H:i', $showtimes[0]) . '-' . date('H:i', $showtimes[1]) . '</span><br />';
                    }
                }
                $dj_str .= '</li>';
            }
        }
    } else {
        $dj_str .= '<li class="on-air-dj default-dj">' . __('None Upcoming', 'radio-station') . '</li>';
    }
    $dj_str .= '</ul>';
    $dj_str .= '</div>';
    return $dj_str;
}
    function widget($args, $instance)
    {
        extract($args, EXTR_SKIP);
        echo $before_widget;
        $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
        $display_djs = $instance['display_djs'];
        $djavatar = $instance['djavatar'];
        $link = $instance['link'];
        $default = empty($instance['default']) ? '' : $instance['default'];
        $time = empty($instance['time']) ? '' : $instance['time'];
        $show_sched = $instance['show_sched'];
        $show_playlist = $instance['show_playlist'];
        $show_all_sched = isset($instance['show_all_sched']) ? $instance['show_all_sched'] : false;
        //keep the default settings for people updating from 1.6.2 or earlier
        $show_desc = isset($instance['show_desc']) ? $instance['show_desc'] : false;
        //keep the default settings for people updating from 2.0.12 or earlier
        //fetch the current DJs
        $djs = dj_get_current();
        $playlist = myplaylist_get_now_playing();
        ?>
		<div class="widget">
			<?php 
        if (!empty($title)) {
            echo $before_title . $title . $after_title;
        } else {
            echo $before_title . $after_title;
        }
        ?>
			
			<ul class="on-air-list">
				<?php 
        //find out which DJ/show is currently scheduled to be on-air and display them
        if ($djs['type'] == 'override') {
            //print_r($djs);
            echo '<li class="on-air-dj">';
            if ($djavatar) {
                if (has_post_thumbnail($djs['all'][0]['post_id'])) {
                    echo '<span class="on-air-dj-avatar">' . get_the_post_thumbnail($djs['all'][0]['post_id'], 'thumbnail') . '</span>';
                }
            }
            echo $djs['all'][0]['title'];
            //display the override's schedule if requested
            if ($show_sched) {
                if ($time == 12) {
                    $start_hour = $djs['all'][0]['sched']['start_hour'];
                    if (substr($djs['all'][0]['sched']['start_hour'], 0, 1) === '0') {
                        $start_hour = substr($djs['all'][0]['sched']['start_hour'], 1);
                    }
                    $end_hour = $djs['all'][0]['sched']['end_hour'];
                    if (substr($djs['all'][0]['sched']['end_hour'], 0, 1) === '0') {
                        $end_hour = substr($djs['all'][0]['sched']['end_hour'], 1);
                    }
                    echo ' <span class="on-air-dj-sched">' . $start_hour . ':' . $djs['all'][0]['sched']['start_min'] . ' ' . $djs['all'][0]['sched']['start_meridian'] . '-' . $end_hour . ':' . $djs['all'][0]['sched']['end_min'] . ' ' . $djs['all'][0]['sched']['end_meridian'] . '</span><br />';
                } else {
                    $djs['all'][0]['sched'] = station_convert_schedule_to_24hour($djs['all'][0]['sched']);
                    echo ' <span class="on-air-dj-sched">' . $djs['all'][0]['sched']['start_hour'] . ':' . $djs['all'][0]['sched']['start_min'] . ' ' . '-' . $djs['all'][0]['sched']['end_hour'] . ':' . $djs['all'][0]['sched']['end_min'] . '</span><br />';
                }
                echo '</li>';
            }
        } else {
            if (isset($djs['all']) && count($djs['all']) > 0) {
                foreach ($djs['all'] as $dj) {
                    $scheds = get_post_meta($dj->ID, 'show_sched', true);
                    echo '<li class="on-air-dj">';
                    if ($djavatar) {
                        echo '<span class="on-air-dj-avatar">' . get_the_post_thumbnail($dj->ID, 'thumbnail') . '</span>';
                    }
                    if ($link) {
                        echo '<a href="';
                        echo get_permalink($dj->ID);
                        echo '">';
                        echo $dj->post_title . '</a>';
                    } else {
                        echo $dj->post_title;
                    }
                    if ($display_djs) {
                        $names = get_post_meta($dj->ID, 'show_user_list', true);
                        $count = 0;
                        if ($names) {
                            echo '<div class="on-air-dj-names">With ';
                            foreach ($names as $name) {
                                $count++;
                                $user_info = get_userdata($name);
                                echo $user_info->display_name;
                                if ($count == 1 && count($names) == 2 || count($names) > 2 && $count == count($names) - 1) {
                                    echo ' and ';
                                } elseif ($count < count($names) && count($names) > 2) {
                                    echo ', ';
                                } else {
                                    //do nothing
                                }
                            }
                            echo '</div>';
                        }
                    }
                    if ($show_desc) {
                        $desc_string = station_shorten_string(strip_tags($dj->post_content), 20);
                        echo '<span class="on-air-show-desc">' . $desc_string . '</span>';
                    }
                    if ($show_playlist) {
                        echo '<span class="on-air-dj-playlist"><a href="' . $playlist['playlist_permalink'] . '">' . __('View Playlist', 'radio-station') . '</a></span>';
                    }
                    echo '<span class="radio-clear"></span>';
                    if ($show_sched) {
                        if (!$show_all_sched) {
                            //if we only want the schedule that's relevant now to display...
                            $current_sched = station_current_schedule($scheds);
                            if ($current_sched) {
                                if ($time == 12) {
                                    echo '<span class="on-air-dj-sched">' . __($current_sched['day'], 'radio-station') . ', ' . $current_sched['start_hour'] . ':' . $current_sched['start_min'] . ' ' . $current_sched['start_meridian'] . '-' . $current_sched['end_hour'] . ':' . $current_sched['end_min'] . ' ' . $current_sched['end_meridian'] . '</span><br />';
                                } else {
                                    $current_sched = station_convert_schedule_to_24hour($current_sched);
                                    echo '<span class="on-air-dj-sched">' . __($current_sched['day'], 'radio-station') . ', ' . $current_sched['start_hour'] . ':' . $current_sched['start_min'] . ' ' . '-' . $current_sched['end_hour'] . ':' . $current_sched['end_min'] . '</span><br />';
                                }
                            }
                        } else {
                            foreach ($scheds as $sched) {
                                if ($time == 12) {
                                    echo '<span class="on-air-dj-sched">' . __($sched['day'], 'radio-station') . ', ' . $sched['start_hour'] . ':' . $sched['start_min'] . ' ' . $sched['start_meridian'] . '-' . $sched['end_hour'] . ':' . $sched['end_min'] . ' ' . $sched['end_meridian'] . '</span><br />';
                                } else {
                                    $sched = station_convert_schedule_to_24hour($sched);
                                    echo '<span class="on-air-dj-sched">' . __($sched['day'], 'radio-station') . ', ' . $sched['start_hour'] . ':' . $sched['start_min'] . ' ' . '-' . $sched['end_hour'] . ':' . $sched['end_min'] . '</span><br />';
                                }
                            }
                        }
                    }
                    echo '</li>';
                }
            } else {
                echo '<li class="on-air-dj default-dj">' . $default . '</li>';
            }
        }
        ?>
			</ul>
		</div>
		<?php 
        echo $after_widget;
    }