<?php

$status = nyss_session_get_status();
$current_channel = variable_get('nyss_videosettings_main_livestream_channel', 'nysenate');
?>
<p><?php 
print $status['message'];
?>
</p>
<?php 
if ($status['display_video']) {
    ?>
  <?php 
    if (!nyss_internalusers_is_internal()) {
        ?>
    <?php 
        if (nyss_videosettings_livestream_working()) {
            ?>
      <center>
        <div id="livestream_block">
          <script src="http://static.mogulus.com/scripts/playerv2.js?channel=<?php 
            print $current_channel;
            ?>
&amp;layout=playerEmbedDefault&amp;backgroundColor=0xffffff&amp;backgroundAlpha=1&amp;backgroundGradientStrength=0&amp;chromeColor=0x000000&amp;headerBarGlossEnabled=true&amp;controlBarGlossEnabled=true&amp;chatInputGlossEnabled=false&amp;uiWhite=true&amp;uiAlpha=0.5&amp;uiSelectedAlpha=1&amp;dropShadowEnabled=true&amp;dropShadowHorizontalDistance=10&amp;dropShadowVerticalDistance=10&amp;paddingLeft=10&amp;paddingRight=10&amp;paddingTop=10&amp;paddingBottom=10&amp;cornerRadius=3&amp;backToDirectoryURL=null&amp;bannerURL=null&amp;bannerText=null&amp;bannerWidth=320&amp;bannerHeight=50&amp;showViewers=true&amp;embedEnabled=true&amp;chatEnabled=false&amp;onDemandEnabled=true&amp;programGuideEnabled=false&amp;fullScreenEnabled=true&amp;reportAbuseEnabled=false&amp;gridEnabled=false&amp;initialIsOn=false&amp;initialIsMute=false&amp;initialVolume=10&amp;contentId=null&amp;initThumbUrl=null&amp;playeraspectwidth=4&amp;playeraspectheight=3&amp;mogulusLogoEnabled=false&amp;width=550&amp;height=430&amp;wmode=window" type="text/javascript"></script>
        </div>
      </center>
    <?php 
        }
        ?>
    <p>Having trouble? Try viewing the video at <a href="http://www.livestream.com/<?php 
        print $current_channel;
function nyss_session_get_status()
{
    $result = _nyss_session_get_xml("http://63.118.56.3/wtsstatus.xml");
    $status = array();
    // Parse out values from the retrieved XML
    $status['state'] = (string) $result->channel->item->wtsstate;
    $status['text'] = (string) $result->channel->item->wtstext;
    $status['start_day'] = (string) $result->channel->item->startstringday;
    $status['start_time'] = (string) $result->channel->item->startstringtime;
    $status['majority_conference_meeting'] = (string) $result->channel->item->wtsmajconfmeeting;
    $status['last_convened'] = (string) $result->channel->item->wtslastconvened;
    $status['last_adjourned'] = (string) $result->channel->item->wtslastadjourned;
    // Calculate a boolean value
    // This flag also gets set individually below, so this code arguably isn't necessary.
    // Belts and braces. Belts and braces.
    $status['display_video'] = in_array(trim(strtolower($status['state'])), array("standby to convene", "standby to reconvene", "in session", "senate and assembly convened in joint session", "convened in extraordinary session"));
    // Construct the message and headline, and decide whether to display video.
    switch (trim(strtolower($status['state']))) {
        // Senate is meeting, so show video
        case 'standby to convene':
            $status['message'] = 'The Senate is standing by to convene. ';
            $status['message'] .= $status['text'];
            $status['headline'] = 'Senate Status';
            $status['display_video'] = TRUE;
            break;
        case 'standby to reconvene':
            $status['message'] = 'The Senate is standing by to reconvene. ';
            $status['message'] .= $status['text'];
            $status['headline'] = 'Senate Status';
            $status['display_video'] = TRUE;
            break;
        case 'in session':
            $status['message'] = 'The Senate is in Session. ';
            $status['message'] .= $status['text'];
            $status['headline'] = 'Senate Status. ';
            $status['display_video'] = TRUE;
            break;
        case 'senate and assembly convened in joint session':
            $status['message'] = 'The Senate and Assembly has convened in a Joint Session. ';
            $status['message'] .= $status['text'];
            $status['headline'] = 'Senate Status';
            $status['display_video'] = TRUE;
            break;
        case 'convened in extraordinary session':
            $status['message'] = 'The Senate is Convened in Extraordinary Session. ';
            $status['message'] .= $status['text'];
            $status['headline'] = 'Senate Status';
            $status['display_video'] = TRUE;
            break;
            // Senate is not currently meeting, so point people to video archives
        // Senate is not currently meeting, so point people to video archives
        case 'adjourned to [date, time]':
            $status['message'] = 'Adjourned until ' . $status['start_time'] . ', ' . $status['start_day'] . '. ';
            $status['message'] .= _nyss_session_archives_text();
            $status['message'] .= $status['text'];
            $status['headline'] = 'When is Session?';
            $status['display_video'] = FALSE;
            break;
        case 'adjourned to the call of the temporary president':
            $status['message'] = 'Adjourned to the call of the Temporary President. ';
            $status['message'] .= _nyss_session_archives_text();
            $status['message'] .= $status['text'];
            $status['headline'] = 'When is Session?';
            $status['display_video'] = FALSE;
            break;
        case 'temporarily at ease':
        case 'at ease':
            $status['message'] = 'The Senate is Temporarily at Ease. ';
            $status['message'] .= _nyss_session_archives_text();
            $status['message'] .= $status['text'];
            $status['headline'] = 'Senate Status';
            $status['display_video'] = FALSE;
            break;
        case 'in recess until [time]':
            $status['message'] = 'The Senate is in recess until ' . $status['start_time'] . '. ';
            $status['message'] .= _nyss_session_archives_text();
            $status['message'] .= $status['text'];
            $status['headline'] = 'When is Session?';
            $status['display_video'] = FALSE;
            break;
        case 'in recess':
            $status['message'] = 'The Senate is in recess. ';
            $status['message'] .= _nyss_session_archives_text();
            $status['message'] .= $status['text'];
            $status['headline'] = 'When is Session?';
            $status['display_video'] = FALSE;
            break;
            // Free form text doesn't meet any of the above categories, so don't show video
            // OR point to the video archives.
        // Free form text doesn't meet any of the above categories, so don't show video
        // OR point to the video archives.
        case 'free form text box':
            $status['message'] = $status['text'];
            $status['headline'] = 'Senate Status';
            $status['display_video'] = FALSE;
            break;
        default:
            // belts and braces logic to make sure the temporary president case gets matched correctly
            if (substr($status['state'], 0, 12) == 'Adjourned to' && preg_match('/temporary president/i', $status['state'], $matches)) {
                $status['message'] = 'The Senate is adjourned to the call of the Temporary President. ';
                $status['message'] .= _nyss_session_archives_text();
                $status['message'] .= $status['text'];
                $status['headline'] = 'When is Session?';
                $status['display_video'] = FALSE;
            } else {
                if (substr($status['state'], 0, 12) == 'Adjourned to' && trim($status['start_day']) != '' && trim($status['start_time']) != '') {
                    $status['message'] = 'Adjourned until ' . $status['start_time'] . ', ' . $status['start_day'] . '. ';
                    $status['message'] .= _nyss_session_archives_text();
                    $status['message'] .= $status['text'];
                    $status['headline'] = 'When is Session?';
                    $status['display_video'] = FALSE;
                } else {
                    if (substr($status['state'], 0, 15) == 'In Recess until' && $status['start_time'] != '') {
                        $status['message'] = 'The Senate is in recess until ' . $status['start_time'] . '. ';
                        $status['message'] .= _nyss_session_archives_text();
                        $status['message'] .= $status['text'];
                        $status['headline'] = 'When is Session?';
                        $status['display_video'] = FALSE;
                    } else {
                        if ($status['state'] != '') {
                            $status['message'] = $status['state'] . ' ';
                            $status['message'] .= $status['text'];
                            $status['headline'] = 'Senate Status';
                            $status['display_video'] = FALSE;
                        } else {
                            if ($status['text']) {
                                $status['message'] = $status['text'];
                                $status['headline'] = 'Senate Status';
                                $status['display_video'] = FALSE;
                            } else {
                                $status['message'] = 'We are currently experiencing technical difficulties. Please stand by. ';
                                $status['headline'] = 'Senate Status';
                                $status['display_video'] = FALSE;
                            }
                        }
                    }
                }
            }
            break;
    }
    $last_char = substr(trim($status['message']), -1, 1);
    // add a period in case the journal clerk's office appends a message and forgets to punctuate.
    if (!in_array($last_char, array('.', '?', '!'))) {
        $status['message'] = trim($status['message']) . '. ';
    }
    $status['internal'] = nyss_internalusers_is_internal();
    $status['ip'] = ip_address();
    return $status;
}