<?php
    
    
// Start a session so that users can select to use their own XML config.
    
    
session_start();
    
    
// Check if the user has opted to reset to the default XML config, if so unset the session variable.
    // Then reload the page.
    
    
if(isset($_GET['reset'])) {
        unset(
$_SESSION['xml']);
        
header('Location: index.php'); exit();
    }
    
    
// Check if the user has just submitted an XML config url to be used, if so set it as the session variable.
    // Then reload the page.
    
    
if(isset($_POST['url'])) {
        
$_SESSION['xml'] = $_POST['url'];
        
header('Location: index.php'); exit();
    }
    
    
// Include the getConfig and file_get_contents functions used for data retrieval.
    
    
include('includes/functions.php');
    
    
// Retrieve the XML config file as an object. Then set 2 variables based on content within it.
    
    
$config getConfig();
    
$title $config->title;
    
$refresh $config->refresh;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title><?=$title?></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="style.css" type="text/css" />
    
    <!-- The stylesheets below are all components of MultiBox (See Apendix) -->
    
    <link href="multibox.css" rel="stylesheet" type="text/css" />
    <!--[if lte IE 6]><link rel="stylesheet" href="ie6.css" type="text/css" media="all" /><![endif]-->

    <!-- The scripts below are all components of MultiBox (See Apendix) -->
    
    <script type="text/javascript" src="js/mootools.js"></script>
    <script type="text/javascript" src="js/overlay.js"></script>
    <script type="text/javascript" src="js/multibox.js"></script>
    
    <!-- AJAX Script to retrieve data from the weather stations XML files. -->
    
    <script type="text/javascript" src="js/ajax.js"></script>
</head>
<body>

<div id="wrapper">

    <h1><?=$title?></h1>
    <form action="index.php" method="post">
        <div id="xmlform">
            <a href="kml.php?view=true" class="link">View KML</a>
            <a href="kml.php" class="link">Download KML</a>
            <a href="wdlconfig.xml" class="link">View Config XML</a>
            <a href="source/" class="link">View Source</a>
            <a href="?reset=true" class="link">Reset to Default</a>
            
            <!-- The following line controls the Alternate XML Config input textbox.
                If the session is active, that value fills the textbox, else a welcoming message is displayed.
                If the user has recently submitted an INVALID XML Config, an error message is displayed.
                If the welcome or error message is displayed, when the user clicks into the textbox, the textbox is emptied, ready for URL input -->
            
            <input type="text" name="url" value="<? if(isset($_SESSION['xml'])) echo $_SESSION['xml']; else echo 'Enter Alternative Config XML URL Here.'?>" class="url" onfocus="if(this.value=='Enter Alternative Config XML URL Here.' || this.value=='Error: Invalid Config XML Submitted.')this.value=''" onblur="if(this.value=='')this.value='Enter Alternative Config XML URL Here.'" />
            <input type="submit" value="Go" />
        </div>
    </form>
        <?
            
// The following 2 vars are used only for itteration, their purpose is to give following div's unique ID's so that AJAX can change the contents of each div individually.
            
            
$i 0$s 0;
            
            
// This loop goes through each weather station listed in the Config XML.
            
            
foreach ($config->weather->station as $station) {
                echo 
'
    <div class="station_wrapper">
        <div class="station">
            
            <!-- This span is used to contain the time AJAX last updated the page with new weather station XML data. -->
            
            <span id="timestamp'
.$s.'" class="timestamp">
            </span>
            <h2>
                <a href="'
.$station->home.'">'.$station->title.'</a>
            </h2>
            
            <!-- This div by default is empty. AJAX is used to fill this div with weather statistics retrieved from the individual weather stations by PHP. -->
            
            <div id="station'
.$s.'" class="stats">
            </div>
            <div class="text_right">
                
                <!-- This div contains links to the other components of this assignment, weather station names are passed as the GET variable \'q\' -->
                
                <a href="bbc.php?q='
.urlencode($station->title).'" rel="width:700,height:400" id="mb'.$i.'" class="mb" title="'.$station->title.'"><img src="images/bbc.png" alt="Search for articles on BBC News relevant to '.$station->title.'" /></a>
                <div class="multiBoxDesc mb'
.$i.'">BBC News RSS Feed</div>';
                
$i++;
                echo 
'
                <a href="flickr.php?q='
.urlencode($station->title).'" rel="width:700,height:400" id="mb'.$i.'" class="mb" title="'.$station->title.'"><img src="images/flickr.png" alt="Search for photos on Flickr relevant to '.$station->title.'" /></a>
                <div class="multiBoxDesc mb'
.$i.'">Flickr RSS Feed</div>
            </div>
        </div>
    </div>
                    '
;
                
$i++; $s++;
            }
            echo 
'
    
    <!-- This JavaScript runs the ajax function found in ajax.js for each of the weather stations found in the Config XML.
        The 2 parameters passed are the XML url of the weather station and an itterator value, so AJAX knows where to insert the statistics retrieved via PHP on the page. -->
    
    <script type="text/javascript">
        <!--
        '
;
            
$i 0;
            foreach (
$config->weather->station as $station) {
                echo 
'ajax(\''.$station->base.'\', \''.$i.'\', \''.$refresh.'\');
        '
;
            
$i++;
            }
            
            echo 
'-->
    </script>'
;
        
?>
        
    <div class="map_wrapper">
        <div>
            
            <!-- This JavaScript performs a document.write on output echo'd by PHP.
                If the default config XML is in use, echo the Google Maps iFrame onto the apge
                If a user submitted config XML is in use, echo a link to Google Maps querying KML based on their weather stations data.
                This is done because Google Maps does not openly support automatic embedding of new maps, probably to encourage users to visit their site. -->
            
            <script type="text/javascript">
                <!--
                    document.write('<?
                        
if(!isset($_SESSION['xml']) || substr($_SESSION['xml'], 05)=='Error') {
                            echo 
'<iframe src="http://www.google.co.uk/maps?f=q&amp;hl=en&amp;geocode=&amp;q=http:%2F%2Fstevelacey.net%2Fsamples%2Fweather%2Fkml.php&amp;ie=UTF8&amp;ll=52.1185,-119.583&amp;spn=27.743,83.074&amp;output=embed&amp;s=AARTsJoXNKQwaKYLvcOskMOCtnp6RFd5uQ" class="map"></iframe>';
                        } else {
                            echo
'<a href="http://www.google.co.uk/maps?q=http://stevelacey.net/samples/weather/kml.php?xml='.$_SESSION['xml'].'" target="_blank">Click Here to Google Map the KML File Generated from Your Config XML</a>';
                        }
                                    
?>');
                -->
            </script>
        </div>
    </div>

    <!-- This JavaScript is a component of MultiBox (See Apendix) -->

    <script type="text/javascript">
        var box = {};
        window.addEvent('domready', function(){
            box = new MultiBox('mb', {descClassName: 'multiBoxDesc', useOverlay: true});
        });
    </script>
    
</div>

</body>
</html>