<?php
$optres['css'][] = $module_css_path . 'amodules/weather_advanced/css/gen_forecast.css';
?>
<div class="weather_advanced_container">
    <?php
    if($module_change_scale == 1 || $module_show_print_link == 1) {
        ?>
        <div class="weather_tools noprint">
                    <?php
                    if($module_change_scale == 1) {
                        if($c_to_f == "c") {
                            $scale_name = "Fahrenheit (&deg;F)";
                            $scale_unit = "f";
                        }
                        if($c_to_f == "f") {
                            $scale_name = "Celsius (&deg;C)";
                            $scale_unit = "c";
                        }

                        $weather_link = $_SERVER['REQUEST_URI'];
                        if(($qmark = strpos($weather_link, "?")) === false) {
                            $weather_link .= "?show_scale=" . $scale_unit;
                        }
                        else {
                            $qs = substr($weather_link, $qmark, strlen($weather_link)-1);
                            if($qs == "?") {
                                $qs .= "show_scale=" . $scale_unit;
                            }
                            else {
                                $qp = str_replace("?", "", $qs);
                                $qp = explode("&", $qp);
                                if(is_array($qp) && count($qp)>0) {
                                    $new_qs = "?";
                                    foreach ($qp as $i=>$par) {
                                        if(strpos($par, "=") !== false) {
                                            $pars = explode("=", $par);
                                            if($pars[0] == "show_scale") {
                                                $new_qs .= "&show_scale=$scale_unit";
                                            }
                                            else {
                                                $new_qs .= "&$par";
                                            }
                                        }
                                        else {
                                            $new_qs .= "&$par";
                                        }
                                    }
                                    $qs = $new_qs;
                                }
                                else {
                                    $qs .= "&show_scale=" . $scale_unit;
                                }
                            }
                            $new_weather_link = substr($weather_link, 0, $qmark);
                            $weather_link = $new_weather_link . $qs;
                        }
                        $weather_link = str_replace("?&", "?", $weather_link);
                        ?>
                        <a class="wa-change-scale" href="<?php echo $weather_link;?>"><?php echo tx("$scale_name");?></a>
                        <?php
                    }

                    if($module_show_print_link == 1) {
                        if(!isset($print_screen)) {
                            ?>
                            <a class="wa-print hidden-xs" href="?print=1&module_page=weather_advanced&show_scale=<?php echo $c_to_f;?>&lang=en&action=print&ccmi=<?php echo $current_catdom_module_id;?>" target="_blank"><i class="fa fa-print fa-fw"></i></a>
                            <?php
                        }
                        else {
                            ?>
                            <a class="wa-print hidden-xs" href="?print=1&module_page=weather_advanced&show_scale=<?php echo $c_to_f;?>&lang=en&action=print&ccmi=<?php echo $current_catdom_module_id;?>" target="_blank"><i class="fa fa-print fa-fw"></i></a>
                            <?php
                        }
                    }

                    ?>
        </div>
        <?php
    }
    if(is_array($weather_current) && count($weather_current)>0) {
        foreach ($weather_current as $k=>$city) {
            $loc_fr = "";



            $fin_wnd_dir = mb_strtoupper($city['wind_dir']);
            $wnd_dir = str_split(mb_strtolower($city['wind_dir']));
            if(is_array($wnd_dir) && count($wnd_dir)>0) {
                $co_card = array("n"=> tx("NORTH"), "e"=> tx("EAST"), "s"=> tx("SOUTH"), "w"=> tx("WEST"));
                $fin_wnd_dir = "";
                foreach ($wnd_dir as $dir_letter) {
                    if(array_key_exists($dir_letter, $co_card)) {
                        $fin_wnd_dir .= mb_substr ( tx($co_card[$dir_letter]), 0, 1);
                    }
                }

                $fin_wnd_dir = mb_strtoupper($fin_wnd_dir, 'UTF-8');
            }

            //echo '<pre>', var_export($city, true), '</pre>';

            ?>



            <div class="weather_container_adv">
                <div class="wa-box">
                    <div class="wa-header">
                        <?php
                        $loc_target = "_self";
                        if(is_array($cities_list_link_title) && isset($cities_list_link_title[$city['location']]) && trim($cities_list_link_title[$city['location']]) != "") {
                            //echo '<h2>' . tx(stripslashes($cities_list_link_title[$city['location']])) . '</h2>';

                            if(isset($cities_list_friendly_url[$city['location']]) && trim($cities_list_friendly_url[$city['location']]) != "") {
                                $external_locs = false;
                                $loc_fr = (trim($cities_list_friendly_url[$city['location']]));
                                if(strpos($loc_fr, 'cyprus-weather.com') === false) {
                                    $loc_fr .= ".html";
                                }
                                else {
                                    $external_locs = true;
                                    $loc_target = "_blank";
                                }

                                if(!$external_locs) {
                                    echo '<h2 class="pull-left">';
                                    echo '<a href="', $loc_fr, '" target="_self">', tx($cities_list_link_title[$city['location']]), '</a>';
                                    echo '</h2>';
                                }
                                else {
                                    echo '<h2 class="pull-left">';
                                    echo tx($cities_list_link_title[$city['location']]);
                                    echo '</h2>';
                                }

                            }
                            else {
                                echo '<h2>' . tx(stripslashes($cities_list_link_title[$city['location']])) . '</h2>';
                            }

                        }
                        else {
                            echo '<h2>' . tx(stripslashes($city['ws_city_name'])) . '</h2>';
                        }

                        ?>

                        <?php
                        if($loc_fr != "") {
                            ?>
                            <span class="weather_forecast_link pull-right noprint">
                                <a href="<?php echo $loc_fr;?>" target="<?=$loc_target;?>"><?php echo tx("15 Days Forecast " . $city['ws_city_name'] . "");?></a>
                                <i class="fa fa-angle-double-right fa-lg text-danger"></i>
                            </span>
                            <?php
                        }
                        ?>

                    </div>
                    <div class="wa-body container-fluid">
                        <div class="row wa-row">
                            <div class="col-sm-2 col-xs-5 no-padding">
                                <div class="wa-c1">
                                    <span class="wa-current-text n-hidden-xs"><?php echo tx("CURRENT");?></span>

                                    <div class="wa-current-t"><span><?php echo convert_temp($city['temp'], $c_to_f) . '&deg; ' . show_scale($city['temp_units'], $c_to_f);?></span></div>
                                </div>
                            </div>
                            <div class="col-sm-2 col-xs-5 no-padding">
                                <div class="wa-c2">
                                    <span class="wa-current-phrase n-hidden-xs"><?php echo mb_strtoupper ( tx($city['phrase']), 'UTF-8');?></span>
                                    <div class="m_icon"><img src="<?php echo $path;?>images/weather_icons/<?php echo $city['icon'];?>.png" alt="<?php echo $city['phrase'];?>" title="<?php echo $city['phrase'];?>" border="0"/></div>
                                </div>
                            </div>
                            <div class="col-sm-6 col-xs-10 wa-current-specs no-padding">
                                <div class="wa-c3">
                                    <div class="row">
                                        <div class="col-sm-5 no-padding">
                                            <div class="col-xs-10">
                                                <span class="para_name pull-left"><?php echo tx("Visibility");?>:</span>
                                                <span class="para_value pull-right"><?php echo $city['vis'] . " " . $city['vis_unit'];?></span>
                                            </div>
                                        </div>
                                        <div class="col-sm-5 no-padding">
                                            <div class="col-xs-10">
                                                <span class="para_name pull-left"><?php echo tx("Wind speed");?>:</span>
                                                <span class="para_value pull-right"><?php echo $city['windspeed'] . " " . $city['windspeed_unit'];?></span>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="row">
                                        <div class="col-sm-5 no-padding">
                                            <div class="col-xs-10">
                                                <span class="para_name pull-left"><?php echo tx("Pressure");?>:</span>
                                                <span class="para_value pull-right"><?php echo $city['pres'] . " " . $city['pres_unit'];?></span>
                                            </div>
                                        </div>
                                        <div class="col-sm-5 no-padding">
                                            <div class="col-xs-10">
                                                <span class="para_name pull-left"><?php echo tx("Wind direction");?>:</span>
                                                <span class="para_value pull-right"><?php echo $fin_wnd_dir ;?></span>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-sm-5 no-padding">
                                            <div class="col-xs-10">
                                                <span class="para_name pull-left"><?php echo tx("Humidity");?>:</span>
                                                <span class="para_value pull-right"><?php echo $city['rhumid'] . " " . $city['rhumid_unit'];?></span>
                                            </div>
                                        </div>
                                        <div class="col-sm-5 no-padding">
                                            <div class="col-xs-10">
                                                <span class="para_name pull-left"><?php echo tx("Wind Chill");?>:</span>
                                                <span class="para_value pull-right"><?php echo convert_temp($city['wndchl'], $c_to_f) . "&deg; " . show_scale($city['wndchl_unit'], $c_to_f);?></span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Days-->
                        <div class="row wa-days">
                            <?php
                            if(isset($weather_fiveday[$city['ws_city_id']])) {
                                $tmp = array();
                                $tmp = $weather_fiveday[$city['ws_city_id']];
                                asort($tmp);
                                $weather_fiveday[$city['ws_city_id']] = $tmp;


                                // Put the particular forecast data per day
                                $cnt = 1;
                                foreach ($weather_fiveday[$city['ws_city_id']] as $id=>$fd) {
                                    if ($cnt <= 5) {
                                        $ext_cls = "";
                                        ?>
                                        <div class="col-sm-2 col-xs-10 wa-day-container">
                                            <div class="row wa-day">
                                                <div class="col-sm-10 col-xs-10 no-padding">
                                                    <span class="wa-day-name"><?= tx($fd['day_name']); ?></span>
                                                </div>

                                                <div class="m_icon col-sm-10 col-xs-3 no-padding">
                                                    <img
                                                        src="<?php echo $path; ?>images/weather_icons/<?php echo $fd['icon']; ?>.png" alt="<?php echo tx($fd['phrase']); ?>"
                                                        title="<?php echo tx($fd['phrase']); ?>"/>
                                                </div>
                                                <div class="col-sm-10 col-xs-3 no-padding">
                                                    <div class="wa-t-range">
                                                        <b class="wa-t-high">
                                                            <span class="sr-only"><?php echo tx("High"); ?>:</span>
                                                            <?php echo convert_temp($fd['high'], $c_to_f) . '&deg;' . show_scale($fd['high_unit'], $c_to_f); ?>
                                                        </b>
                                                        <b class="wa-t-low">
                                                            <span class="sr-only"><?php echo tx("Low"); ?>:</span>
                                                            <?php echo convert_temp($fd['low'], $c_to_f) . '&deg;' . show_scale($fd['low_unit'], $c_to_f); ?>
                                                        </b>
                                                    </div>
                                                </div>
                                                <div class="col-sm-10 col-xs-4 no-padding wa-phrase-container">
                                                    <span class="wa-phrase"><?= tx($fd['phrase']); ?></span>
                                                </div>
                                            </div>
                                        </div>
                                        <?php
                                    }
                                    $cnt++;
                                }
                            }
                            ?>
                        </div>
                    </div>
                </div>
            </div>
            <?php
        }
    }
    ?>

</div>