<?php
############################################################################
# European Weather Network v. 2016.4 (Feb 2016)
############################################################################
#
# Author:	Henkka <nordicweather@gmail.com.net>
#
# Copyright:	(c) 2008-2016 Copyright nordicweather.net.
#
############################################################################
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
############################################################################
#
# This work is licensed under the 
# Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License. 
# To view a copy of this license, visit 
# http://creativecommons.org/licenses/by-nc-nd/3.0/.
#
############################################################################

date_default_timezone_set("UTC");
$tkey=hash('sha256', 'euweather'.date("dmYH"));
date_default_timezone_set($tzabb);
$tzdiff = date("Z")*1000;
$frcdata[frc]=15;

$times = frccurl("https://a.maps.nordicweather.net/maptime_e.php?tz=".$tzabb."&tk=".$tkey."&du=".time());

$query=$_SERVER["QUERY_STRING"];
$q=explode("|",$query);
if(preg_match("|\||",$query)){
  $lang=$q[0];
  $geoid=$q[1];
  $args='?lang='.$lang.'&geoid='.$geoid;
}else{
  if(isset($_GET[lang])){$lang = $_GET[lang];}
  if(isset($_GET[p])){$p = '&p='.$_GET[p];} // Leuven fix
  $args='?lang='.$lang;
  $argb='&geoid=';
  $baseurl='https://'.$_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"].$args.$p.$argb;

  if($_COOKIE[ewndef]){
    $dcookie=json_decode($_COOKIE[ewndef],true);
    if(!isset($_GET[geoid])&&!isset($_GET[lat])&&$dcookie[co]){
      $geoid=$dcookie[id];
    }
  }

  if($_GET[lat] && is_numeric($_GET[lat])){$centerlat=$_GET[lat];}
  if($_GET[lon] && is_numeric($_GET[lon])){$centerlon=$_GET[lon];}
  if($_GET[geoid]){$geoid=$_GET[geoid];}
}

if($geoid){$args.='&geoid='.$geoid.$p;}
else{$args.='&lat='.$centerlat.'&lon='.$centerlon.$p;}
$ewnargs = '&wsp='.$wunit.'&datestyle='.$datestyle.'&v='.$ewnversion.'&tk='.$tkey.'&all=1';

$frcdata = frccurl("https://euweather.eu/backend/frc/data_ew_front3.php".$args.$ewnargs);
$frcdata = json_decode($frcdata,true);

foreach ($days as $key => $value) {
  $da[]=utf8_encode(html2utf8($value));
}
$day_array = json_encode($da);
foreach ($months as $key => $value) {
  $mo[]=utf8_encode($value);
}
$mo_array = json_encode($mo);

###########################################################

$ewnhead='
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://static.nordicweather.net/css/leaflet-0.7.3.min.css" rel="stylesheet"/>
<link href="https://static.nordicweather.net/css/bootstrap_ewn.css?'.$ewnversion .'" rel="stylesheet"/>
<link rel="stylesheet" href="'.$path_to_css.'ewn.css?'.$ewnversion .'" />
<style>
.frcnav {list-style:none;float:left;margin: -12px -40px !important;}
.frcnav li {float:left;display:block;}
.frcnav li span {font-size:15px;color: #6699cc;font-weight:700;-webkit-transition: all 0.2s ease-in-out;-moz-transition: all 0.2s ease-in-out;-o-transition: all 0.2s ease-in-out;-ms-transition: all 0.2s ease-in-out;transition: all 0.2s ease-in-out;display: inline-block;padding: 10px;line-height: 45px;}
.frcnav li span:hover{color: #BDBDBD;text-shadow: 0 0 10px #336699;text-shadow: 0 0 10px rgba(51,102,153,0.8);-webkit-transition: color 0.1s linear;-moz-transition: color 0.1s linear;text-decoration:none;cursor:pointer}
.frcnav li span.selected {color:#BDBDBD;}
.frcnavbar {border-radius: 2px;background: #00456E;height:40px;padding-left:5px;}
</style>
';

###########################################################

if($jqueryload ){
  $ewnfooter.='
  <script src="https://static.nordicweather.net/jq/jquery-1.10.2.min.js"></script>';
}
$ewnfooter.='
  <script src="https://static.nordicweather.net/jq/jq_plugins.min.js?'.$ewnversion .'"></script>';
if($bootstrapload ){
  $ewnfooter.='
  <script src="https://static.nordicweather.net/jq/bootstrap-3.3.5.min.js"></script>';
}
$ewnfooter.='
  <script src="https://static.nordicweather.net/jq/bootstrap_plugins.min.js?'.$ewnversion .'"></script>
  <script src="https://static.nordicweather.net/jq/leaflet-0.7.3.min.js"></script>
  <script src="https://static.nordicweather.net/jq/leaflet_plugins.min.js?'.$ewnversion .'"></script>
  <script src="https://static.nordicweather.net/jq/highcharts-custom-4.2.0.js"></script>
<script>
';
$pcou=$frcdata[cou];
$scand=array("NO","DK","EE");
if($pcou=="FI"||$pcou=="AX"||$pcou=="SE"){
  $pco="FI";
}elseif(in_array($pcou,$scand)){
  $pco="scand";
}elseif($pcou=="NL"||$pcou=="BE"){
  $pco="NL";
}elseif($pcou=="DE"){
  $pco="DE";
}elseif($pcou=="GB"){
  $pco="UK";
}else{
  $pco="eur";
}
$ewnfooter.='   
  var lat='.$frcdata[lat].',lon='.$frcdata[lon].',geoid="'.$frcdata[geoid].'",dname="'.$frcdata[name].'",co="'.$frcdata[cou].'",pco="'.$pco.'",tk="'.$tkey.'",lang="'.$lang.'",p="'.$p.'",defmap="'.$defmap.'";
  var days = '.$day_array.',months = '.$mo_array.';
  var fdaynames = ['.html2utf8($daynames).'],rgr=1;
  var mapconf = '.$times.';
  var strtxt="'.ucfirst(STRIKES).'",temptxt="'.html2utf8(TEMP).'",barotxt="'.BARO.'",prectxt="'.html2utf8(PRECIP).'",windtxt="'.WIND.'",dewtxt="'.DEWP.'",snowtxt="'.html2utf8(SNOB).'",rangetxt="'.strtolower(RANGE).'",feeltxt="'.FEELS.'";
  var txtqa="'.VILLAGES.'",txtqb="'.ISLANDS.'",txtqc="'.LAKES.'",txtqd="'.AIRPORTS.'",txtqe="'.GOLFCOURSES.'",txtqf="'.SKICENTERS.'",txtqg="'.BEACHES.'",txtqh="'.ZOO.'",txtqi="'.PARKS.'",txtqv="'.MISC.'";
  var hourgraph='.json_encode($frcdata[graph1]).';
  var statgraph='.json_encode($frcdata[graph2]).';
  var wspdtip='.json_encode($frcdata[wspdtip]).';
  var lang="'.$lang.'",nofavotxt="'.NOFAVORITES.'",ewnfavo=[],ewndef=[],favotable,wiset="ms";
  var req = "'.$_SERVER["REQUEST_URI"].'";
</script>
<script src="'.$path_to_js.'ewn_frc.js?'.$ewnversion .'"></script>
';

###########################################################

$flagbox="<div class='popoverhead'><i id='flagclose' class='fa fa-times' style='top: -14px;right: -12px;'></i>".SELECTCOUNTRY."</div>".get_flags();

$settingsbox = "
<script>
var setboxa = \"<i id='gearclose' class='fa fa-times'></i><div class='popoverhead'>".FAVORITELOCATIONS."</div>\";
var setboxb = \"<button class='btn btn-default' id='gpsbtn' style='display:none;vertical-align: middle;margin-top:10px'>\"
+ \"<i class='fa fa-bullseye' style='color:#FFA500;font-size:22px;top:2px;position:relative;margin-right:2px;'></i>\"
+ \"&nbsp;&nbsp;".LOCATEME."</button>\"
+ \"<div class='popoverhead'>".SELECTWUNIT."</div>\"
+ \"<input type='radio' class='wunitbox' name='wunit' id='ms'/>&nbsp;<label for='ms'>m/s</label>&nbsp;&nbsp;\"
+ \"<input type='radio' class='wunitbox' name='wunit' id='kmh'/>&nbsp;<label for='kmh'>km/h</label>&nbsp;&nbsp;\"
+ \"<input type='radio' class='wunitbox' name='wunit' id='mph'/>&nbsp;<label for='mph'>mph</label>&nbsp;&nbsp;\"
+ \"<input type='radio' class='wunitbox' name='wunit' id='kts'/>&nbsp;<label for='kts'>kts</label>\";
</script>
";

$frcfront='
<span style="display:none" id="dummy"></span>
'.$settingsbox.'
<div>
  <div class="frcfront_half">
    <div style="min-height:750px">
    <div class="tabbs" style="position:relative;width:calc(100% - 38px);float:right;margin-bottom:15px;">
      <div class="nordui-table-header" style="padding:4px">
        '.FORECAST.'
      </div>
      <div id="frc6h">'.utf8_decode($frcdata[frc6h]).'</div>
    </div>
    <div id="frctabs" style="width:40px;position:relative">
      <div id="frcPanels">
        <div class="frcPanel selected" id="collapseTab1"><div class="frcpnum">'.DAY.' 1 - 3</div></div>
        <div class="frcPanel" style="margin-top:4px" id="collapseTab2"><div class="frcpnum">'.DAY.' 4 - 6</div></div>
        <div class="frcPanel" style="margin-top:4px" id="collapseTab3"><div class="frcpnum">'.DAY.' 7 - 9</div></div>
      </div>
    </div>
    </div>
  </div>

  <div class="frcfront_half_last" style="min-width:350px;">
    <div class="curfront_half_last">'.$adtorni.'
    </div>
    <div class="curfront_half">
      <table class="nordui-table" style="width:100%">
        <thead><tr class="nordui-table-header"><td colspan="4">'.CLOSEOBS.'</td></tr></thead>
        <tbody id="currcondtable">'.$stuff.'</tbody>
      </table>
      <div class="tabbs" style="position:relative;margin-top:12px;">
        <div class="nordui-table-header" style="padding:4px;margin-bottom:5px">
          '.LOCATIONINFO.'
        </div>
        '.AREA.': '.$frcdata[area].'<br/>';
        if(strlen($frcdata[postcode])>2){
          $frcfront.=POSTCODE.': '.$frcdata[postcode].'<br/>';
        }
        $frcfront.=LATI.': '.sprintf("%01.3f",$frcdata[lat]).'&deg;N<br/>
        '.LONGI.': '.sprintf("%01.3f",$frcdata[lon]).'&deg;E<br/>';
        if($frcdata[alt]<>0){
          $frcfront.=ALTITUDE.': '.$frcdata[alt].' m<br/>';
        }
        $frcfront.='
      </div>
    </div>
  </div>
  <div style="clear:both"></div>
</div>
';

$legendbox="<div>
<table class='letle'>
  <tr><td class='letlea' style='background-color:rgb(245,41,209)'></td><td class='letleb'>".LEVEL." 3</td></tr>
  <tr><td class='letlea' style='background-color:rgb(191,0,0)'></td><td class='letleb'>".LEVEL." 2</td></tr>
  <tr><td class='letlea' style='background-color:rgb(255,112,0)'></td><td class='letleb'>".LEVEL." 1</td></tr>
  <tr><td><img src='https://euweather.eu/img/lightning.png'/></td><td class='letleb'>".THUNDER."</td></tr>
  <tr><td><img src='https://euweather.eu/img/warrow.png'/></td><td class='letleb'>".WIND." &gt; ".fixwsp(17,true,true)."</td></tr>
  <tr><td><img src='https://euweather.eu/img/snowflake_24.png'/></td><td class='letleb'>".SNOW."</td></tr>
  <!--tr><td class='letlea' style='background-color:rgb(17,2,134)'></td><td class='letleb'>".PRECIP." &gt; 10 mm</td></tr-->
</table>
</div>";

###########################################################
# PUT TOGETHER BODY

$nfrcbody = '
<div class="form-group" style="position: relative;">
  <div class="input-group input-group">
    <span class="input-group-addon">
      <i id="searchflag" data-co="'.$pcou.'" class="knob knob-'.strtoupper($pcou).'" style="position:relative;top:2px;" data-container="body" data-toggle="popover" data-placement="bottom" data-html="true" data-content="'.$flagbox.'"></i>
    </span>
    <div class="icon-addon addon">
      <input type="text" placeholder="'.SEARCHLOC.'..." class="form-control" id="city">
    </div>
    <span class="input-group-btn">
      <button class="btn btn-default" type="button">Go!</button>
    </span>
  </div>
</div>

<span style="float:right;top: -5px;position: relative;">
  <button class="btn btn-default" id="favoaddbtn" style="vertical-align: middle;" data-container="body" data-toggle="popover" data-placement="top" data-html="true" data-content="<b>'.ADDEDTOFAVO.'</b>">
  <i class="fa fa-star" style="color:#FFA500;font-size:18px;top:2px;position:relative;margin-right:2px;"></i>
    <span class="col800">'.SAVEASFAVORITE.'</span>
  </button>
</span>

<h1>'.FORECASTFOR.' '.$frcdata[name].'</h1>

<div id="hgraph" class="frcgraph" style="padding-bottom:12px;background:#F2F2F2">
  <div id="hourgraph" style="min-width: 200px;margin:0 auto;position:relative;"></div>
</div>
<div id="graphtabs" style="margin-bottom:20px;position:relative">
  <div id="graphPanels">
    <span class="graphPanel selected" id="tempPanel">'.TEMP.'<span class="col800">/'.PRECIP.'/'.BARO.'</span></span>
    <span class="graphPanel" id="baroPanel">'.THUNDER.'<span class="col800">/'.DEWP.'</span></span>';
    if($frcdata[snowind]==1){
      $nfrcbody.='<span class="graphPanel" id="snowPanel">'.SNOWDEPTH.'</span>';
    }
  $nfrcbody.='</div>
</div>

<div class="frcnavbar">
  <div id="bigear">
    <i class="favobtn fa fa-gear menuicon" style="font-size:28px;" data-container="body" data-toggle="popover" data-placement="left" data-html="true" data-content="dummy"></i>
  </div>
  <ul class="frcnav">
    <li><span class="frcnavs selected" id="frc0">'.SHORTFRC.'</span></li>
    <li><span class="frcnavs" id="frc1">72h '.DETAILEDTABLE.'</span></li>
    <li><span class="frcnavs" id="frc2">15 '.DAYTABLE.'</span></li>
    <!--li><span class="frcnavs" id="frc3">'.GRAPH.'</span></li-->
    <li class="col650"><span class="frcnavs" id="frc4">'.SEVERE.'</span></li>
    <li class="col650"><span class="frcnavs" id="frc6">'.SOLARWINDENERGY.'</span></li>
    <li class="col650"><span class="frcnavs" id="frc5">'.COMPACCURACY.'</span></li>
  </ul>
</div>
        
<div style="margin:20px 0 0">
  <div class="frctables" id="frc0table" style="display:inline-block;width:100%">'.$frcfront.'</div>
  <div class="frctables" id="frc1table" style="display:none">'.$frcdata[frc1h].'</div>
  <div class="frctables" id="frc2table" style="display:none">'.$frcdata[frc3h].'</div>
  <div class="frctables" id="frc3table" style="display:none;overflow:hidden">
    
  </div>
  <div class="frctables" id="frc4table" style="display:none">'.$frcdata[extra].'</div>
  <div class="frctables" id="frc5table" style="display:none">'.$frcdata[comps].'</div>
  <div class="frctables" id="frc6table" style="display:none">'.$frcdata[solar].'</div>
</div>

<div id="adblock">'.$adblock.'</div>

<h1>'.WARNANDSEVERE.'</h1>';

if(strlen($frcdata[warn])>150){
  $nfrcbody.='
  <div style="margin-top:15px;" class="malarmtablewrap">
    <table style="width:100%">'.utf8_decode($frcdata[warn]).'</table>
    <div><small>Warnings by <a href="http://www.meteoalarm.eu" target="_new">MeteoAlarm</a>.</small></div>
  </div>';
}else{
  $nfrcbody.='
  <div style="margin-top:15px;" class="malarmtablenowarn">
    <i class="fa fa-info-circle" style="color:#00B400;font-size:24px;top:2px;position:relative;margin-right:2px;"></i>
    '.NOWARNS.'
  </div>';
}

$nfrcbody.='
<div id="mapfrontwrap" style="margin-bottom:10px;">
  <div class="mapfront_half_last tabbs">
    <div class="nordui-table-header" style="padding:4px">'.RADARSTRIKES.'</div>
    <div id="frontmap2" style="height:344px"></div>
  </div>
  <div class="mapfront_half tabbs">
    <div class="nordui-table-header" style="padding:4px">
      <i class="fa fa-info-circle" id="sevinfo" style="color:#BDBDBD;text-shadow:none;font-size:22px;position:relative;float:right;top:2px;right:6px;cursor:pointer"
         data-container="body" data-toggle="popover" data-placement="left" data-html="true" data-content="'.$legendbox.'"></i>
      '.SEVEREFRCTODAY.'
    </div>
    <div id="frontmap1" style="height:344px"></div>
  </div>
  <div style=""></div>
</div>

<div id="mapfrontwrap" style="margin-bottom:20px;">
  <div class="mapfront_half_last tabbs">
    <div class="nordui-table-header" style="padding:4px">'.LOCATIONS.'</div>
    <div id="frontmap4" style="height:344px"></div>
  </div>
  <div class="mapfront_half tabbs">
    <div class="nordui-table-header" style="padding:4px">';
    if(strlen($frcmapurl)>4){
      $nfrcbody.='<span style="float:right;"><a href="'.$frcmapurl.$args.'"><i class="fa fa-share" id="sevinfo" style="color:#BDBDBD;text-shadow:none;font-size:22px;position:relative;top:2px;right:6px;cursor:pointer"
         data-container="body" data-toggle="tooltip" data-placement="top" title="'.MOREMAPS.'"></i></a></span>';
    }
    $nfrcbody.=FRCSTMAPS.'
    </div>
    <div id="frontmap3" style="height:344px"></div>
  </div>
  <div style="clear:both"></div>
</div>
';

###########################################################

function html2utf8($name) {
  if(mb_detect_encoding($name, 'UTF-8', true)){$name=utf8_decode($name);}
  $n=str_replace("&auml;","ä",$name);
  $n=str_replace("&aring;","å",$n);
  $n=str_replace("&aelig;","æ",$n);
  $n=str_replace("&ouml;","ö",$n);
  $n=str_replace("&oslash;","ø",$n);
  return $n;
}

function fixwsp($num,$ext,$round){
  global $wunit;
  if($wunit=="kmh"){$num = sprintf("%01.1f",$num*3.6);$wu="km/h";}
  elseif($wunit=="mph"){$num = sprintf("%01.1f",$num*2.23693629);$wu="mph";}
  elseif($wunit=="kts"){$num = sprintf("%01.1f",$num*1.94384449);$wu="kts";}
  else{$wu="m/s";}
  if($round){$num=round($num);}
  if($ext){return $num.' '.$wu;}
  else{return $num;}
}

function get_flags(){
$countrys = array(
"AL" => defcountries("Albania"),
"AD" => defcountries("Andorra"),
"AT" => defcountries("Austria"),
"AX" => defcountries("&Aring;land"),
"BY" => defcountries("Belarus"),
"BE" => defcountries("Belgium"),
"BA" => defcountries("Bosnia"),
"BG" => defcountries("Bulgaria"),
"HR" => defcountries("Croatia"),
"CY" => defcountries("Cyprus"),
"CZ" => defcountries("Czech"),
"DK" => defcountries("Denmark"),
"EE" => defcountries("Estonia"),
"FO" => defcountries("Faroe"),
"FI" => defcountries("Finland"),
"FR" => defcountries("France"),
"DE" => defcountries("Germany"),
"GI" => defcountries("Gibraltar"),
"GR" => defcountries("Greece"),
"GL" => defcountries("Greenland"),
"HU" => defcountries("Hungary"),
"IS" => defcountries("Iceland"),
"IE" => defcountries("Ireland"),
"IT" => defcountries("Italy"),
"RS" => defcountries("Kosovo"), 
"LV" => defcountries("Latvia"),
"LI" => defcountries("Liechtenstein"),
"LT" => defcountries("Lithuania"),
"LU" => defcountries("Luxembourg"),
"MK" => defcountries("Macedonia"),
"MT" => defcountries("Malta"),
"MD" => defcountries("Moldova"),
"MC" => defcountries("Monaco"),
"ME" => defcountries("Montenegro"),
"NL" => defcountries("Netherlands"),
"NO" => defcountries("Norway"),
"PL" => defcountries("Poland"),
"PT" => defcountries("Portugal"),
"RO" => defcountries("Romania"),
"RU" => defcountries("Russia"),
"SM" => defcountries("San Marino"),
"RS" => defcountries("Serbia"),
"SK" => defcountries("Slovakia"),
"SI" => defcountries("Slovenia"),
"ES" => defcountries("Spain"),
"SE" => defcountries("Sweden"),
"CH" => defcountries("Switzerland"),
"TR" => defcountries("Turkey"),
"UA" => defcountries("Ukraine"),
"GB" => defcountries("UK"),
"VA" => defcountries("Vatican")
);
$flags='<table style=\'width:450px;position: relative;top: -10px;\'><tr><td style=\'width:33%\'>';
foreach ($countrys as $key => $value) {
  $flags.='<div class=\'popoverflags\' data-country=\''.strtolower($key).'\'><span class=\'knob knob-'.$key.' popoverflag\'></span>'.$value.'</div>';
  $i++;
  if($i==17||$i==34){$flags.='</td><td>';}
}
$flags.='</td></tr></table>';
  return $flags;
}
?>