<?php
function display_schedule($s) {
    global $_REQUEST;
    $yes = true;
    $wwi = array();
    $ddi = array();
    $ss = explode(",", $s);
    $p = count($ss);
    if($p > 0) {
        if($p == 2) {
            $d = trim($ss[0]);
            $w = trim($ss[1]);
            $w = str_replace( array("weekends", "also", "at", "weekend", "w"), "", strtolower($w) );
            $w = trim($w);
        }
        elseif($p == 1) {
            $tmp = strtolower(trim($ss[0]));
            if(strpos($tmp, "w") === false) {
                $d = trim($tmp);
            }
            else {
                $w = str_replace( array("weekends also at", "weekend", "w"), "", $tmp );
                $w = trim($w);
            }
        }
    }
    else {
        $yes = false;
    }

    if(isset($_REQUEST['debug_movie'])) {
        echo '<pre>', var_export($ss, true), '</pre>';
        echo '<pre>', var_export($d, true), '</pre>';
        echo '<pre>', var_export($w, true), '</pre>';
    }


    if($yes) {
        $all_h = array();
        $w_all = array(
            "d" => "",
            "a"	=> "",
            "s"	=> ""
        );

        $d_all = array(
            "st"	=> "",
            "nd"	=> "",
            "rd"	=> ""
        );
        if(isset($d)) {
            $dd = explode(" ", $d);
            $ddi = array();
            foreach ($dd as $i=>$h) {
                $ih = intval(str_replace(":", "", $h));
                $ddi[$ih] = $h;
            }

            ksort($ddi);
            $dcnt = 1;
            $h = "";
            $d_all['st'] = $h;
            $d_all['nd'] = $h;
            $d_all['rd'] = $h;
            $d_all['4th'] = $h;
            $d_all['5th'] = $h;
            foreach ($ddi as $ki=>$h) {
                //if($ki > 700 && $ki<=1730) $d_all['st'] = $h;
                //if($ki > 1730 && $ki<=2100) $d_all['nd'] = $h;
                //if($ki > 2100 && $ki<=2359) $d_all['rd'] = $h;

                if($dcnt == 1) $d_all['st'] = $h;
                if($dcnt == 2) $d_all['nd'] = $h;
                if($dcnt == 3) $d_all['rd'] = $h;
                if($dcnt == 4) $d_all['4th'] = $h;
                if($dcnt == 5) $d_all['5th'] = $h;

                $dcnt ++;
            }
            //print_r($dd);
        }

        if(isset($w)) {
            $ww = explode(" ", $w);
            $wwi = array();
            foreach ($ww as $i=>$h) {
                if(!in_array($h, $ddi)) {
                    $ih = intval(str_replace(":", "", $h));
                    $wwi[$ih] = $h;
                }
            }

            ksort($wwi);
            $wcnt = 1;
            //echo '<pre>', var_export($wwi, true), '</pre>';
            foreach ($wwi as $ki=>$h) {
                if($ki < 700) $w_all['s'] .= ($w_all['s'] == '') ? $h : ', ' . $h;
                if($ki > 700 && $ki<1200) $w_all['d'] .= ($w_all['d'] == '') ? $h : ', ' . $h;
                if($ki >= 1200 && $ki<=1600) $w_all['a'] .= ($w_all['a'] == '') ? $h : ', ' . $h;
                if($ki > 1600 && $ki<=2359) $w_all['s'] .= ($w_all['s'] == '') ? $h : ', ' . $h;

                /*
                if($wcnt == 1) $w_all['d'] = $h;
                if($wcnt == 2) $w_all['a'] = $h;
                if($wcnt == 3) $w_all['s'] = $h;
                */
                $wcnt ++;
            }



        }

        $hidden_xs = 'hidden-xs';
        foreach($w_all as $k=>$v) {
            $w_all[$k] = ltrim(rtrim(trim($v), ','), ',');
            if($w_all[$k] == "") {
                $w_all[$k] = "&nbsp;";
            }
            else {
                $hidden_xs = '';
            }
        }

        ?>
        <div class="row">
            <div class="col-sm-5 col-xs-10 sched-wd">
                <table class="table sched-days">
                    <thead>
                    <tr>
                        <th colspan="5" class="text-center text-primary cine-th">Daily Schedule</th>
                    </tr>
                    <tr>
                        <th class="text-center cine-th">1<sup>st</sup></th>
                        <th class="text-center cine-th">2<sup>nd</sup></th>
                        <th class="text-center cine-th">3<sup>rd</sup></th>
                        <th class="text-center cine-th">4<sup>th</sup></th>
                        <th class="text-center cine-th">5<sup>th</sup></th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td class="text-center cine-val"><?php echo $d_all['st'];?></td>
                        <td class="text-center cine-val"><?php echo $d_all['nd'];?></td>
                        <td class="text-center cine-val"><?php echo $d_all['rd'];?></td>
                        <td class="text-center cine-val"><?php echo $d_all['4th'];?></td>
                        <td class="text-center cine-val"><?php echo $d_all['5th'];?></td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div class="col-sm-5 col-xs-10 sched-we <?=$hidden_xs;?>">
                <table class="table sched-days">
                    <thead>
                    <tr>
                        <th colspan="3" class="text-center text-primary cine-th">Weekend Extra Shows</th>
                    </tr>
                    <tr>
                        <th class="text-center cine-th">Morning</th>
                        <th class="text-center cine-th">Midday</th>
                        <th class="text-center cine-th">Evening</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td class="text-center cine-val"><?php echo $w_all['d'];?></td>
                        <td class="text-center cine-val"><?php echo $w_all['a'];?></td>
                        <td class="text-center cine-val"><?php echo $w_all['s'];?></td>
                    </tr>
                    </tbody>
                </table>
            </div>

        </div>

        <?php
        if(isset($_REQUEST['debug_movie'])) {
            echo '<pre>', print_r($d_all), '</pre>';
            echo '<pre>', print_r($w_all), '</pre>';
        }
    }
}