<?php
//http://www.google.com/ig/api?weather=opava,cz
//http://www.exorithm.com/algorithm/view/weather_forecast

function att($att,$type,$row=0)
{
global $xml;
switch ($type)
  {
  case 1 : return $xml->weather->current_conditions->{$att}['data'];
  case 2 : return $xml->weather->forecast_conditions->$row->{$att}['data'];
  default: return $xml->weather->forecast_information->{$att}['data'];
  }
}

function img($url,$width=35,$title)
{
//return '<img src="http://www.google.com/'.$url.'"/>';
$name   = substr($url,strrpos($url,'/')+1,-4);
$images = array(
'cloudy'         => 'cloudy',
'mostly_cloudy'  => 'partly_cloudy',
'mostly_sunny'   => 'partly_cloudy',
'chance_of_rain' => 'light_rain',
'chance_of_storm'=> 'tstorms',
'mist'           => 'light_rain',
'sunny'          => 'sun',
'chance_of_snow' => 'light_snow',
'flurries'       => 'light_snow'
);
$name = isset($images[$name]) ? $images[$name] : $name;
$name = $name.'.png';
$url_folder = $width==60 || $width==35 ? $width : 35;
return '<img src="http://ssl.gstatic.com/onebox/weather/'.$url_folder.'/'.$name.'" alt="'.$name.'" width="'.$width.'" title="'.$title.'">';
}



header('content-type: text/html; charset: utf-8');
$data  = file_get_contents('http://www.google.com/ig/api?weather=opava,cz&hl=cs');
$data  = iconv("iso-8859-2", "UTF-8", $data );
$xml   = simplexml_load_string($data);
$loc   = str_replace(',',', ',att('city',0));
$date  = att('forecast_date',0);
$t     = '&deg;C';
$html  = '
<html>
<head>
<title>Počasí</title>
<style type="text/css">
html, body {margin:0;padding:0;}
.f1, .f2 {border-collapse:collapse; border-spacing:0;}
.f1 caption {font-weight:bold;text-align:left;}
.f1 caption:first-letter {text-transform:uppercase;}
.f2 {color:#666;}
.f1 .temp {font-size:125%;}
.f2 .temp {font-size:85%;}
</style>
</head>
<body>
';

$html .= "
<table class=\"f1\" summary=\"Aktuální počasí pro ".$loc."\">
  <caption>".$loc."</caption>
  <tr>
    <td width=\"35\">".img(att('icon',1),35,att('condition',1))."</td>
    <td><div class=\"temp\">".att('temp_c',1).$t."</div></td>
  </tr>
  <tr>
    <td colspan=\"2\">
      <div class=\"wind\">".preg_replace('/\ss.*?rychlostí/',',',att('wind_condition',1))."</div>
      <div class=\"hum\">" .att('humidity',1)."</div>
    </td>
  </tr>
</table>";

$html .= "
<table class=\"f2\" summary=\"Předpověď počasí\">";
for ($i=0;$i<4;$i++)
  {
  $html .= "
  <tr>
    <td>".img(att('icon',2,$i),16,att('condition',2,$i))."</td>
    <td><div class=\"temp\">".att('high',2,$i).$t.', '.att('low',2,$i).$t."</div></td>
    <td>&nbsp;".att('day_of_week',2,$i)."</td>
  </tr>";
  }
$html .= "
</table>
</body>
</html>
";
echo $html;
?>
