<?php
/********************
* Fuzzy Thermometer *
* Ver 0.3           *
* By Ben Beltran    *
*********************/

/******************************
* CHANGELOG					  *
*******************************
* 0.1 -> Initial Release      *
* 0.2 -> Config options       *
* 0.3 -> Day and Night        *
*******************************/
			

/*Weather.com configuration
********************************/
$citycode = 'MXCA0026';				//Zip Code (US) or City code according to weather.com
$partnerid = '1140062701'; 			//Partner ID
$license = '5b323d77586070aa'; 		//License number

/*Color Configuration
********************************/
$nightbackground = '#222';				//main colors.
$daybackground = '#fff';

$daymaincolor = '#666';
$nightmaincolor = '#e1e1e1;';

$taglinecolor = '#999';

$coldcolor = '#78d6fd';				//Colors for each temperature.
$coolcolor = '#1b95fb';
$temperatecolor = '#999';
$warmcolor = '#ff9226';
$hotcolor = '#ff1818';

/*Text Configuration
*******************************/
$maintext = 'Ju&aacute;rez est&aacute;:';
$title = '&iquest;C&oacute;mo est&aacute; el clima en Ju&aacute;rez?';

$coldstring = 'frio.';				//string for each temperature.
$coolstring = 'fresco.';
$temperatestring = 'templado.';
$warmstring = 'c&aacute;lido.';
$hotstring = 'caliente.';

$coldtagline = '(abrigate bien.)';	//tagline for each temperature.
$cooltagline = '(perfecto para un saco.)';
$temperatetagline = '(ni fu ni fa.)';
$warmtagline = '(como para estar afuera.)';
$hottagline = '(hidr&aacute;tate bien.)';

/*Temperature Configuration
******************************/
$coldlimit = 15;					//How many degrees? (celsius)
$coollimit = 20;
$temperatelimit = 25;
$warmlimit = 30;



/******************************
* DANGER! DANGER!			  *
*******************************
* Don't edit below this part  *
* unless you know what you're *
* doing.                      *
*******************************
* It's not that hard really.  *
* but it's better if we leave *
* it like it is. Unless you   *
* like customizing the shit   *
* out of stuff.               *
*******************************/
			
				
				$ch = curl_init('http://xoap.weather.com/weather/local/'.$citycode.'?cc=*&link=xoap&prod=xoap&par='.$partnerid.'&key='.$license.''); //Initialize curl.

				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);		//More curl config
				curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);

				$data = curl_exec($ch);	//this is your xml stuff
				curl_close($ch);

				$xml = new SimpleXmlElement($data, LIBXML_NOCDATA);	//now let's make an XmlElement
				$feelslike = $xml->cc->flik;						//This is the "Feel's Like temp"
				$sunset = date('G.i',strtotime($xml->loc->suns));
				$sunrise = date('G.i',strtotime($xml->loc->sunr));
				$now = date('G.i',time()-28800);
				$feelslike = ($feelslike -32)*5/9;					//Convert fahrenheit to celsius.
				
				//now let's get the sponsored links. Stupid EULA.
				$sponsors = '<a href="'.$xml->lnks->link[0]->l.'">'.$xml->lnks->link[0]->t.'</a> &middot; ';
				$sponsors .= '<a href="'.$xml->lnks->link[1]->l.'">'.$xml->lnks->link[1]->t.'</a> &middot; ';
				$sponsors .= '<a href="'.$xml->lnks->link[2]->l.'">'.$xml->lnks->link[2]->t.'</a> &middot; ';	
				$sponsors .= '<a href="'.$xml->lnks->link[3]->l.'">'.$xml->lnks->link[3]->t.'</a>';		
				
				
				if(isset($_GET['temp'])){
					$feelslike = $_GET['temp'];		//This overrides the weather.com temperature for debugging.
				}
				if(isset($_GET['now'])){
					$now = $_GET['now'];		//This overrides the weather.com temperature for debugging.
				}


echo '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
	"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title><?php echo $title; ?></title>
		<style type="text/css">
			*{
				padding: 0;
				margin: 0;
			}
			
			body{
			<?php
				if($now > $sunrise && $now < $sunset){
					echo 'background-color:'.$daybackground.';';
				}else{
					echo 'background-color:'.$nightbackground.';';
				}
			?>
				font-family:Helvetica,Arial,sans-serif;
				<?php
				if($now > $sunrise && $now < $sunset){
					echo 'color:'.$daymaincolor.';';
				}else{
					echo 'color:'.$nightmaincolor.';';
				}
			?>
			}
			
			#container{
				margin: auto;
				width: 400px;
				text-align:center;
				margin-top:200px;
			}
			
			#container p{
				font-size:4em;
				padding-left: 24px;
			}
			
			#container .rec{
				font-style: italic;
				font-size: .6em;
				letter-spacing: .1em;
				color: <?php echo $taglinecolor; ?>;
			}
			
			#container .cold{
				color: <?php echo $coldcolor?>;
			}
			#container .cool{
				color: <?php echo $coolcolor?>;
			}
			#container .temperate{
				color: <?php echo $temperatecolor?>;
			}
			#container .warm{
				color: <?php echo $warmcolor?>;
			}
			#container .hot{
				color: <?php echo $hotcolor?>;
			}
			
			#footer{
				color: #999;
				font-size: .6em;
				bottom: 0;
				left: 0;
				position: absolute;
				padding: 10px;
			}
			
			img{ border: 0; }
			a{ color: #1b95fb; }
		</style>
	</head>
	<body>
		<div id="container">
		<span><?php echo $maintext; ?></span><br/>
<?php
				
/******************************
* DANGER! DANGER!			  *
*******************************
* I guess the following part  *
* is not that dangerous, but  *
* make sure you know what     *
* you're doing.               *
*******************************/
				
if($feelslike < $coldlimit){ echo '<p class="cold">'.$coldstring.'</p><span class="rec">'.$coldtagline.'</span>'; }
else if($feelslike < $coollimit){ echo '<p class="cool">'.$coolstring.'</p><span class="rec">'.$cooltagline.'</span>'; }
else if($feelslike < $temperatelimit){ echo '<p class="temperate">'.$temperatestring.'</p><span class="rec">'.$temperatetagline.'</span>'; }
else if($feelslike < $warmlimit){ echo '<p class="warm">'.$warmstring.'</p><span class="rec">'.$warmtagline.'</span>'; }
else{ echo '<p class="hot">'.$hotstring.'</p><span class="rec">'.$hottagline.'</span>'; }
?>
		</div>
<!-- <div id="footer">
Powered by: <a href="http://weather.com"><img src="TWClogo_31px.png" alt="The Weather Channel" /></a> Sponsored by: <?php echo $sponsors; ?>
</div> -->
	</body>
</html>