<?php

#include_once ("/var/www/includes/sanitize.inc.php");
include_once ("/var/www/includes/sanitize.inc.php");
#$wfo="otx";

#Process and sanitize incoming variables
#Set target size default
$size=1;
#Set image number default to first WFO active image
$img=1;
$output="";
$iname="";
$full="";

#Process and sanitize incoming variables
$wfo=isset($_REQUEST[wfo])     ? sanitize($_REQUEST[wfo], PARANOID,3,3) :$wfo;
$size=isset($_REQUEST[size])     ? sanitize($_REQUEST[size], FLOAT) :$size;
$full=isset($_REQUEST[full])     ? sanitize($_REQUEST[full], PARANOID,3,3) :$full;
$img=isset($_REQUEST[img])     ? sanitize($_REQUEST[img], INT) :$img;
$output=isset($_REQUEST[output])     ? sanitize($_REQUEST[output], PARANOID,3,3) :$output;
$iname=isset($_REQUEST[iname])     ? sanitize($_REQUEST[iname], STRICT,1,20) :$iname;
$iname=preg_replace("/\.png/","",$iname);

#Check to see if wfo exists
#Do we have a valid wfo? If not then error out to 
include_once('/var/www/html/FXC/wxstory.inc.php');
$gotSite = array_search(strtoupper($wfo),$arrSites);

#echo "IN main!!!!\n";
#echo "Variables - wfo:$wfo, size:$size,img=$img,output=$output,iname=$iname\n";
#print_r($arrSites);
#exit;


if($gotSite === false){
#echo "gotSite is false for: $wfo\n";
#print_r($arrSites);
#exit;
#	$wfo = "wrh";
#	include_once "/var/www/includes/nwscwi-header.php";
#	echo "<p>There is an issue with this page. Please check with the Webmaster 1.</p>\n";
#	include_once "/var/www/includes/nwscwi-footer.php";
	header('Location: /FXC/');
	exit;
}


$wfo_caps = strtoupper($wfo);

if (file_exists("/var/www/includes/$wfo/nwscwi.conf")){

	include_once("/var/www/includes/$wfo/nwscwi.conf");
	#echo "version=$UseWeatherStory3";
	#exit;
}else{
	#include_once "/var/www/includes/nwscwi-header.php";
	echo "There is an issue with this page. Please check with the administrator.\n";
	exit;
	#include_once "/var/www/includes/nwscwi-footer.php";
	header('Location: /FXC/');
	exit;
}

#echo "usewxs3=$UseWeatherStory3\n";
#exit;


if($UseWeatherStory3 == "no") {
#echo "2going into $UseWeatherStory3\n";
#exit;
	ws2();
	exit;
}elseif ($UseWeatherStory3 == "yes" ){
#echo "3going into $UseWeatherStory3\n";
#exit;
	ws3();
	exit;
}else{

	#include_once "/var/www/includes/nwscwi-header.php";
	#echo "<H1> There is an issue with this page. Please check with the administrator\n";
	#include_once "/var/www/includes/nwscwi-footer.php";
	header('Location: /FXC/');
	exit;
}

################################################################################################3
#Begin main code for version 2 or 3

function ws3() {

global $wfo;
global $facebook_username, $youtube_username, $twitter_username;
global $size;
global $img;
global $output;
global $iname;
global $full;
global $arrSites;

#Take care of clickable large image first with hotspots

if($full == "yes"){

#holderexample
    #<a  class=\"hotspot-container\"
    #    href=\"http://dev.wrh.noaa.gov/FXC/wsnew/make_img.php?wfo=$wfo&iname=$iname&size=1\">
    #    <image src=\"http://dev.wrh.noaa.gov/FXC/wsnew/make_img.php?wfo=$wfo&iname=$iname&size=1\">
    #</a>
     #background: url(http://www.wrh.noaa.gov/images/youtube32x32.jpg) no-repeat;
$arrSitesState = array('BOI'=>'Boise,ID','BYZ'=>'Billings,MT','EKA'=>'Eureka,CA','FGZ'=>'Flagstaff,AZ','GGW'=>'Glasgow,MT','HNX'=>'Hanford,CA','LKN'=>'Elko,NV','LOX'=>'LosAngeles,CA','MFR'=>'Medford,OR','MSO'=>'Missoula,MT','MTR'=>'Monterey,CA','OTX'=>'Spokane,WA','PDT'=>'Pendleton,OR','PIH'=>'Pocatello,ID','PQR'=>'Portland,OR','PSR'=>'Phoenix,AZ','REV'=>'Reno,NV','SEW'=>'Seattle,WA','SGX'=>'SanDiego,CA','SLC'=>'SaltLakeCity,UT','STO'=>'Sacramento,CA','TFX'=>'GreatFalls,MT','TWC'=>'Tucson,AZ','VEF'=>'LasVegas,NV','ZLC'=>'SaltLakeCWSU,UT','ZOA'=>'OaklandCWSU,CA','ZLA'=>'LosAngelesCWSU,CA','ZSE'=>'SeattleCWSU,WA');

$wfouc = strtoupper($wfo);
#list($url_name,$state_id)= split(",",$arrSitesState[$wfouc]);
list($url_name,$state_id)= preg_split("/,/",$arrSitesState[$wfouc]);
#$wfo_title = $wfo_title.", ".$state_id;
#echo "wfo=$wfo , urlname=$url_name , stateid=$state_id\n";
#exit;

echo"
<!DOCTYPE html>
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<style>

.hotspot-container {
  width: 960px; height: 827px;
  position: relative;
  background: url(/FXC/make_img.php?wfo=$wfo&iname=$iname&size=1) no-repeat;
  border: 1px solid black;
}
  .hotspot-container .hotspotFacebook {
    width: 45px; height: 49px;
    position: absolute;
    top:  726px; /* Top  coord of hotspot (relative to parent, bottom: 20px is also valid) */
    left: 771px; /* Left coord of hotspot (right: 20px is also valid); */
  }

  .hotspot-container .hotspotFacebook:hover {
     opacity: 0.1;
  }

  .hotspot-container .hotspotTwitter {
    width: 45px; height: 49px;
    position: absolute;
    top:  726px; /* Top  coord of hotspot (relative to parent, bottom: 20px is also valid) */
    left: 825px; /* Left coord of hotspot (right: 20px is also valid); */
}
  .hotspot-container .hotspotYoutube {
    width: 60px; height: 49px;
    position: absolute;
    top:  726px; /* Top  coord of hotspot (relative to parent, bottom: 20px is also valid) */
    left: 882px; /* Left coord of hotspot (right: 20px is also valid); */
  }

  .hotspot-container .hotspoturl {
    width: 335px; height: 35px;
    position: absolute;
    top:  790px; /* Top  coord of hotspot (relative to parent, bottom: 20px is also valid) */
    left: 615px; /* Left coord of hotspot (right: 20px is also valid); */
  }
  .hotspot-container .hotspotnameurl {
    width: 275px; height: 65px;
    position: absolute;
    top:  726px; /* Top  coord of hotspot (relative to parent, bottom: 20px is also valid) */
    left: 210px; /* Left coord of hotspot (right: 20px is also valid); */
  }

  .hotspot-container .hotspoturlnoaa {
    width: 90px; height: 105px;
    position: absolute;
    top:  726px; /* Top  coord of hotspot (relative to parent, bottom: 20px is also valid) */
    left: 2px; /* Left coord of hotspot (right: 20px is also valid); */
  }
  .hotspot-container .hotspoturlnws {
    width: 90px; height: 105px;
    position: absolute;
    top:  726px; /* Top  coord of hotspot (relative to parent, bottom: 20px is also valid) */
    left: 100px; /* Left coord of hotspot (right: 20px is also valid); */
  }

</style>

</head>

<body>

<div class=\"hotspot-container\">
    <a href=\"/FXC/make_img.php?wfo=$wfo&iname=$iname&size=1\">
	<image src=\"/FXC/make_img.php?wfo=$wfo&iname=$iname&size=1\" alt=\"Weather Story image depiciting important weather events\">
    </a>
    <a  class=\"hotspotFacebook\"
        href=\"http://www.wrh.noaa.gov/wfoexit.php?wfo=$wfo&url=http://www.facebook.com/$facebook_username\">
    </a>
    <a  class=\"hotspotTwitter\"
        href=\"http://www.wrh.noaa.gov/wfoexit.php?wfo=$wfo&url=https://www.twitter.com/$youtube_username\">
    </a>
    <a  class=\"hotspotYoutube\"
        href=\"http://www.wrh.noaa.gov/wfoexit.php?wfo=$wfo&url=http://www.youtube.com/$twitter_username\">
    </a>
    <a  class=\"hotspoturl\"
        href=\"http://weather.gov/$url_name\">
    </a>
    <a  class=\"hotspotnameurl\"
        href=\"http://weather.gov/$url_name\">
    </a>
    <a  class=\"hotspoturlnoaa\"
        href=\"http://noaa.gov\">
    </a>
    <a  class=\"hotspoturlnws\"
        href=\"http://weather.gov/\">
    </a>
</div>

</body>
</html>
\n";

exit;
}


include_once "/var/www/includes/nwscwi-header.php";

echo"
<!-- // CONTENT STARTS HERE -->

<!-- // LOCAL CONTENT STARTS HERE -->
		<link type=\"text/css\" href=\"js/jquery/css/custom-theme2/jquery-ui-1.8.16.custom.css\" rel=\"stylesheet\" />		
		<script type=\"text/javascript\" src=\"js/jquery/js/jquery-1.6.2.min.js\"></script>
		<script type=\"text/javascript\" src=\"js/jquery/js/jquery-ui-1.8.15.custom.min.js\"></script>
		
		<script type=\"text/javascript\">
		
		
			var siteid = '$wfo';
			var newsStyle = \"\";
			var htmlBig = Array();
			var defaultGcast = \"3\";
			var gcastNumber = Array();
			var gcastAndOrder = Array();
	
			$(function(){			
				/* GCAST MODULE */
				gcastJqueryTabsInit()
				/* GCAST MODULE */
			});
			
		
			function gcastJqueryTabsInit(){			
				refreshInterval = 5000;
				//xmlFilename = \"/images/fxc/\"+siteid+\"/graphicast/graphicast.xml\";
				//xmlFilename = \"/images/fxc/\"+siteid+\"/ws_meta.xml\";
				xmlFilename = \"/images/fxc/\"+siteid+\"/WeatherStory.xml\";
				//xmlFilename = \"/images/fxc/\"+siteid+\"/WeatherStoryNIDS.xml\";
				//xmlFilename = \"ws_meta.xml\";
				parseGraphicastXML(xmlFilename);

				//setTimeout ( \"refreshGraphicasts(xmlFilename)\", refreshInterval );
				
				$( \"#gcast-dialog-modal\" ).dialog({
					autoOpen: false,
					height: 750,
					width: 860,
					modal: true,
					title: '',
					position: 'top'
				});
				
			}



			
			var jqxhr;

			function parseGraphicastXML(xmlFilename){
				//$.get(\"data/graphicastSample.xml?rand=\"+Math.floor(Math.random()*10000),{},function(xml){
				jqxhr = $.get(xmlFilename+\"?rand=\"+Math.floor(Math.random()*10000),function(xml){	
					$('#tabsGraphicast').tabs();	
					
					//Some manipulation to work with NIDS xml
					//var wxvars = new Array();
		
					var patt = new RegExp(\"/\");

					//Have to strip the path away if necessary from the image names using this function
					function basename(path) {
						//console.log('got path '+path);	
						//return path.replace( '/.*//g', \"\" );
						return path.replace( /.*\//, '' );
					}


					//Looping through each graphicast node
					$('graphicast',xml).each(function(i){
						var order = $(this).find('order').text();
						var description = $(this).find('description').text();
						var title = $(this).find('title').text();
						var WebURL = $(this).find('WebURL').text();
						var SmallImaget = $(this).find('SmallImage').text();

						//Test small image for path and remove if necessary
						if(patt.test(SmallImaget)){
							var SmallImage = basename(SmallImaget)
							//console.log('found directory'+SmallImage); 
						}else { 
							var SmallImage = SmallImaget
						}

						var FullImaget = $(this).find('FullImage').text();

						//Test large image for path and remove if necessary
						if(patt.test(FullImaget)){
							var FullImage = basename(FullImaget)
							//console.log('found directory'+FullImage); 
						}else { 
							var FullImage = FullImaget
						}

						var startTime = $(this).find('StartTime').text();
						var endTime = $(this).find('EndTime').text();
						var radar = $(this).find('radar').text();
						var ImageLoop = $(this).find('ImageLoop').text();
						var descriptionShort;
						var d = new Date();

						//wxvars[i] = title+' '+SmallImage+' '+startTime+' '+endTime;
						//console.log(wxvars[i]);

						gcastNumber = WebURL.split(\"=\");
						//console.log(gcastNumber[2]);

						gcastAndOrder[gcastNumber[2]] = order;
						

							descriptionShort = description.substring(0,349)+'... <span class=\"more\">More</span>';

							htmlPopUp = '<h1>'+title+'</h1><img width=\"820\" src=\"'+FullImage+'\"/><br />'+description;

							if(radar == 0){
							//html = '<a href=\"/FXC/make_img.php?wfo=$wfo&iname='+FullImage+'\"><img src=\"/FXC/make_img.php?wfo=$wfo&iname='+SmallImage+'&size=.68\"/></a><br/><span style=\"font-size:175%;\">'+description+\"</span>\";
							html = '<a href=\"/FXC/wxstory.php?wfo=$wfo&full=yes&iname='+FullImage+'\"><img src=\"/FXC/make_img.php?wfo=$wfo&timeout=no&iname='+SmallImage+'&size=.68\" border=\"1\"/></a><br/><span style=\"font-size:175%;\">'+description+\"</span>\";
							}
							else{
								html = '<a href=\"'+WebURL+'\"><img width=\"520px\" src=\"'+ImageLoop+'\" /></a><br/><span style=\"font-size:175%;\">'+description+\"</span>\";
							}
							

						if(isGraphicastValid(startTime,endTime)){
							addATab(order,html,title,htmlPopUp)
							$( '#thumbs' ).append('<a href=\"javascript:void\" onClick=\"$( \'#tabsGraphicast\' ).tabs( \'select\' , \'tabsGraphicast-'+order+'\')\"><span style=\"font-size:175%;\">'+title+'</span><img width=\"225px\" src=\"/FXC/make_img.php?wfo=$wfo&timeout=no&iname='+SmallImage+'&size=.22\" border=\"1\"/></a><br/><br/>');
						}
						else{/* do nothing */ }


						if($( '#tabsGraphicast' ).tabs( \"length\") == 0){
							//$('#tabsGraphicast').css(\"display\",\"none\");
							$('#box4').css(\"display\",\"none\");
						}
						else{
							$('#tabsGraphicast').css(\"display\",\"block\");
							$('#box4').css(\"display\",\"block\");
						}
					});
				});



				jqxhr.complete(function() {
							//console.log(gcastAndOrder[defaultGcast]);
							$( '#tabsGraphicast' ).tabs( \"select\" , \"tabsGraphicast-\"+ gcastAndOrder[defaultGcast]);
							if($( '#tabsGraphicast' ).tabs( \"length\") == 0){
								$('#tabsGraphicast').html('<span style=\"font-size:175%;\">No Weather Stories are active at this time.</span>');
							}
							//$('#tabsGraphicast').tabs();	
							//parseGraphicastXML(xmlFilename);
				});
		        
				jqxhr.error(function() {
							//console.log('file does not exist (line 40)');
							$('#tabsGraphicast').css(\"display\",\"none\");
							$('#box4').css(\"display\",\"none\");
				});
	

			}

			function addATab(number,html,title,htmlPopUp){
				htmlBig[number] = htmlPopUp;
				$( '#tabsGraphicast' ).append('<div style=\"display:none\" id=\"tabsGraphicast-'+number+'\">'+html+'</div>');
				//$( '#tabsGraphicast' ).append('<div onClick=infoForPopup(htmlBig['+number+']); style=\"display:none\" id=\"tabsGraphicast-'+number+'\">'+html+'</div>');
				$( '#tabsGraphicast' ).tabs( \"add\" , '#tabsGraphicast-'+number , '<span style=\"font-size:120%;\">'+title+'</span>',[number]);
				$('#tabsGraphicast-'+number).css(\"display\",\"block\");


			}

			function isGraphicastValid(startTime,endTime){
				var d = new Date(); 
				var currentEpochLocalTime = (d.getTime()-d.getMilliseconds())/1000;
				//var localOffset = d.getTimezoneOffset() * 60000;
				//console.log(localOffset);
				var localOffset = 0;
				var currentEpochUTC = currentEpochLocalTime + localOffset;
				//console.log(startTime + ' ' + currentEpochUTC +' '+  endTime);
				
				if(currentEpochUTC > startTime && currentEpochUTC < endTime && startTime!=endTime){
					return true;
				}
				else{
					return false;
				}
			}


			function refreshGraphicasts(xmlFilename){
				$('#tabsGraphicast').css(\"height\",\"\");
				$('#tabsGraphicast').css(\"height\",$('#tabsGraphicast').height());
						for (i=0;i<=$( '#tabsGraphicast' ).tabs( \"length\")+2;i++){
							$( '#tabsGraphicast' ).tabs( \"remove\" , '#tabsGraphicast-'+i);
						}
						gcastJqueryTabsInit();

			}
			
			
			
			function infoForPopup (html) {

				document.getElementById('gcastInfo').innerHTML = html;
				
			        $( \"#gcast-dialog-modal\" ).dialog( \"open\");
			    

			}
		</script>	
		
		
		<!--  GRAPHICAST MODULE -->
	<style type=\"text/css\">
		#tabsGraphicast{ margin: 10px;left: 0px;float:left; font: 82.5% \"Trebuchet MS\", sans-serif;}
                #tabsGraphicast a:link{ color: blue;}
                #tabsGraphicast a:visited{ color:#003399;text-decoration:none;}
                #tabsGraphicast a:hover{color:red;text-decoration:underline;}
		#thumbs{ margin: 10px;left: 30px;font: 82.5% \"Trebuchet MS\", sans-serif;}
	</style>	
	<!-- Tabs Graphicast-->
	<table>
\n";

echo "<tr><td bgcolor='#000099' align='center' width='500'>
<i><b><font color='#FFFF00' size='+2' face='Arial, Helvetica, sans-serif'>Weather Story</font></b></i><br>
<i><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'>Click image below for larger version</font></i>
\n";
echo "</td>\n";
echo "<td align='left'>\n";
include_once("/var/www/html/FXC/nav.php");
echo "</td>\n";

echo "</tr>\n";
echo "
		<tr><td valign=\"top\">
		<div id=\"tabsGraphicast\" style=\"width:920px\">
			<ul>
			</ul>
		</div>
		</td><td valign=\"top\">
		<div id=\"thumbs\" style=\"width:225px\">
		</div>
		</td></tr></table>
		<div id=\"gcast-dialog-modal\" title=\"\">
			<div id=\"gcastInfo\">x</div>

		</div>
		<!--  GRAPHICAST MODULE -->



<table width='600'>
<tr>
<td align='left'><a href=\"http://www.wrh.noaa.gov/FXC/wx_story_disc.php\"><b><i><font size='2' face='Arial, Helvetica, sans-serif'>Product Description Document</font></b></i></a></td>
<td><a href='http://www.wrh.noaa.gov/wrh/wr_metars/$wfo.rss'><img style='border: 2px solid ; width: 36px; height: 14px;' src='xml.gif' alt='RSS Feed' title='RSS Feed' border='0' /><b> Weather Story RSS Feed</b></a></td>
<td><a href='http://www.wrh.noaa.gov/data/nwsweatherstory.kml'><img style='border: 2px solid ; width: 36px; height: 14px;' src='kml.gif' alt='KML Feed' title='KML Feed' border='0' /><b> Weather Story KML Feed</b></a></td>
</tr>
</table>
\n";

include_once "/var/www/includes/nwscwi-footer.php";
#end ws3
}

function ws2(){

global $wfo;
global $size;
global $img;
global $output;
global $iname;
global $arrSites;
global $data_dir;
global $iname;
global $arrSites;
global $data_dir;
global $age_secs;
global $age_special;
global $data_root;
global $img_names;

#echo "IN WS2!!!!\n";
#echo "Variables - wfo:$wfo, size:$size,img=$img,output=$output,iname=$iname,data_root=$data_root,data_dir=$data_dir\n"; 
#print_r($arrSites);
#exit;

#  Purpose: to make sure there are no cache on entering the page, and
#  the "refresh" button on IE would appear to 'clear cache' before displaying the page.
# NO CACHE!!
#         header("Pragma: no-cache");
#         header("Cache-Control: no-cache, must-revalidate");
#         header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
#         header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
#----------------------------------------------------------------------------------------------
#  set CACHE to be every 2 minutes: 2x60 = 120 eseconds   or  5min=5x60=300
$seconds_to_cache = 300;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: max-age=$seconds_to_cache");
#----------------------------------------------------------------------------------------------
/*
Description	wxstory.php displays grapics generated by FSL's FXC package
		Has image age check default to 8 hours but is configurable per
		WFO

Author:		Todd Carter - NWS Spokane, WA

Location:	/export/vhosts/www/html/FXC

Inputs:         5 images available
                        noActive_Screen_wr.png- default WR not available image
                        noActive_Screen.png - WFO not available image
                        Active_Screen1.png - Default WFO active image
                        Active_Screen(2,3).png - Additional images (optional)

		Active_Screen(1,2,3)-description.txt - accompanies each image
			and is updated by WFO

URL input: 	wfo= Any WR WFO
		size= .1 to 1
		img= 0 through 3 - 1 is default 'active' image 
		iname= Any additional name with only alphanumeric characters and restricted
                       to 20 characters. This will be wrapped in the NWS header and footer.

Output:		wxstory.php calls make_img.php which generates dynamic image output.
		
Changes		2/6/2007 - Version 1.0
		11/26/2007 - 2.0 Adapted to suit WRHQ
		06/18/2008 - 3.0 File expiration times for all files now investigated. 
		12/09/2008 - 3.2 Additional images are now clickable thumb images below
				the primary image. Primary image is clickable to larger
				image.

*/ 


#Request for xml don't check for wfo first
if($output == "xml"){ 
#include_once('/var/www/html/FXC/wxstory.inc.php');
#echo "IN WS2 xml!!!!\n";
#echo "Variables - wfo:$wfo, size:$size,img=$img,output=$output,iname=$iname,data_root=$data_root,data_dir=$data_dir\n"; 
#print_r($arrSites);
#exit;
xml($arrSites);
exit;
}
#Request for xml don't check for wfo first
if($output == "kml"){ 
#include_once('/var/www/html/FXC/wxstory.inc.php');
kml($arrSites);
exit;
}

#Do we have a valid wfo? If not then error out to 
#include_once('/var/www/html/FXC/wxstory.inc.php');

#echo "checking wfo: $wfo\n";
#print_r($arrSites);
#exit;

$gotSite = array_search(strtoupper($wfo),$arrSites);

if($gotSite === false){ 
	#$wfo = "wrh";
	#default_error($wfo);
	header ("Location: /FXC/");
	exit;
}


#Handle iname (extra images) issuance with wrapped header and footer.
if($iname){
	$inameL=$iname."L";
	$fname = "$data_root/$wfo/$data_dir/$iname.png";

	if(check_file($wfo,$fname,$age_secs,$age_special)){

	include "/var/www/includes/nwscwi-header.php";
	echo "<table width='654'>\n";
        echo "<tr><td><a href=\"make_img.php?wfo=$wfo&iname=$inameL&size=$size&force=no\"><img src=\"make_img.php?wfo=$wfo&iname=$iname\" alt=\"Weather Story (click the image for a higher resolution image).\" title=\"Weather Story (click the image for a higher resolution image).\"></a><br></td></tr>\n";
	echo "<tr><td><b>\n";
	echo fxc_text($iname);
	echo "</b></td></tr>\n";
	echo "</table>\n";
	include "/var/www/includes/nwscwi-footer.php";
	exit;
	}else{
#echo "Fname=$fname  ...passed!!!<br>\n";
#echo "Fname=$fname  ...failed!!!<br>\n";
		default_error($wfo);
		exit;
	}
}

#testing
#Test variable checks
#echo "<br>From gcast.php
#wfo=$wfo
#size=$size
#img=$img
#age=$age_secs[1];
#age=$age_special[$wfo];
#<br>\n";
#exit;
#Test


#Must have current image #1 (Active_Screen1.png) or no go
#echo "Checking image 1 first!!!<br>\n";
if ($img == 1){

	$fname = "$data_root/$wfo/$data_dir/$img_names[1]";
#echo "Fname=$fname<br>\n";
#exit;
	if(check_file($wfo,$fname,$age_secs,$age_special)){
#echo "Fname=$fname  ...passed!!!<br>\n";
#exit;
	}else{
#echo "Fname=$fname  ...failed!!!<br>\n";
#kexit;
		default_error($wfo);
	}
}else{


	#Determine if graphic exists sinc we have valid wfo
	$fname = "$data_root/$wfo/$data_dir/$img_names[$img]";

	#Check any image request for existence and default age 
	if (check_file($wfo,$fname,$age_secs,$age_special)){
		}else{
		default_error($wfo);
	}
}

#Start the web page
include "/var/www/includes/nwscwi-header.php";
echo "<link rel='alternate' type='application/rss+xml' title='NOAA/NWS Weather Story Feed' href='http://www.wrh.noaa.gov/FXC/wxstory.php?output=xml'>";

echo"
        <style type=\"text/css\">
        <!--
        .xxsmall  { font-size: 8pt;  font-family: Arial, Helvetica, San Serif}
        .xsmall  { font-size: 9pt;  font-family: Arial, Helvetica, San Serif}
        .small   { font-size: 10pt; font-family: Arial, Helvetica, San Serif}
        .large   { font-size: 12pt; font-family: Arial, Helvetica, San Serif}
        -->
        </style>
\n";


echo "<table width='654'>
<tr><td bgcolor='#000099' align='center' width='500'>
<i><b><font color='#FFFF00' size='+2' face='Arial, Helvetica, sans-serif'>Weather Story</font></b></i>
\n";
echo "</td>\n";
echo "<td align='left'>\n";
include_once("nav.php");
echo "</td>\n";

echo "</tr>\n";


#It passes the age test show image 
#for large image link
$imgL=$img_names[$img];
$imgL=preg_replace("/\.png/","L",$imgL);
        echo "<tr><td colspan='2'><a href=\"make_img.php?wfo=$wfo&iname=$imgL&size=$size&force=no\"><img src=\"make_img.php?wfo=$wfo&img=$img&size=$size\" alt=\"Weather Story (click the image for a higher resolution image).\" title=\"Weather Story (click the image for a higher resolution image).\"></a></b><br></td></tr>\n";
	#echo"<tr><td><img src='make_img.php?wfo=$wfo&img=$img&size=$size' border='1' alt='Weather Story Image'></img></td></tr>\n";


#Process Text file if available

echo"<tr><td class='small' colspan='2'><b>\n";
$tfile = str_replace(".png","",$img_names[$img]);
echo fxc_text($tfile);
echo "</b><hr width=\"654\" align=\"left\"></hr></td></tr></table>\n";


#Additional links to other images based on current image displayed
$img_key="";
$image_list=array(1,2,3);
#Get the number of the primary image
$img_key = array_search($img,$image_list);
#Remove primary image number from array
unset($image_list[$img_key]);

#Count what's left
$count = count($image_list);

$handled = 0;
		#echo "Have count $count $img_key<br>\n";	

#Loop through the remaining images and display in order
if ($img_key == 0 or $img_key == 1 or $img_key == 2){
	
	foreach($image_list as $imgs){
		#echo "Have $imgs left $count<br>\n";	

			#if($imgs != $image_list[count($array) - 1])
		$fname = "$data_root/$wfo/$data_dir/$img_names[$imgs]";

       			#Check image to make link
		if(check_file($wfo,$fname,$age_secs,$age_special)){
			$handled++;

			if($handled == 1){
			#start the table and first row/cell
			echo "<table width='654'>\n";
		
               		echo"<tr><td class='xsmall' width=\"327\" valign=\"top\"><b>\n";
                	echo "<a href=\"wxstory.php?wfo=$wfo&img=$imgs&size=1\">Weather Story Image #$imgs</a>&nbsp;&nbsp;<font class='xxsmall'>(click for larger image)</font><br>\n";
               		echo "<a href=\"wxstory.php?wfo=$wfo&img=$imgs\"><img src=\"make_img.php?wfo=$wfo&img=$imgs&size=.49\" alt=\"Weather Story #$imgs (click for larger image)\"></a></b><br>\n";

			$tfile = str_replace(".png","",$img_names[$imgs]);
			#Get text
			echo fxc_text($tfile);
			
               		echo "</td>\n";
			#end of first cell. go on to check for second image.
			continue;
			}

			#Handling second image 
			if($handled == 2){

				#start second  image handle tags
                		echo"<td class='xsmall' width=\"327\" valign=\"top\"><b>\n";
                		echo "<a href=\"wxstory.php?wfo=$wfo&img=$imgs&size=1\">Weather Story Image #$imgs</a>&nbsp;&nbsp;<font class='xxsmall'>(click for larger image)</font><br>\n";
               			echo "<a href=\"wxstory.php?wfo=$wfo&img=$imgs\"><img src=\"make_img.php?wfo=$wfo&img=$imgs&size=.49\" alt=\"Weather Story #$imgs (click for larger image)\"></a></b><br>\n";
				$tfile = str_replace(".png","",$img_names[$imgs]);
				echo fxc_text($tfile);

				#Finish after second image
               			echo "</td></tr>\n";
				echo "</table>\n";
			}else{
				#finish for one extra image only
                			echo "<td width=\"327\"></td></tr>\n";
					echo "</table>\n";


			}

		}

	}
}

#echo "<HEAD>
#<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">
#<META HTTP-EQUIV=\"Expires\" CONTENT=\"-1\">
#</HEAD>\n";


#Make the link to disclaimer
echo "<table width='600'>
<tr>
<td align='left' valign='top'><a href=\"wx_story_disc.php\"><b><i><font size='2' face='Arial, Helvetica, sans-serif'>Product Description</font></b></i></a>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>
<td align='left' valign='top'><a href='http://www.wrh.noaa.gov/wrh/wr_metars/$wfo.rss'><img style='border: 2px solid ; width: 36px; height: 14px;' src='xml.gif' alt='RSS Feed' title='RSS Feed' border='0' /><b> Weather Story RSS Feed</b></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>
<td align='left' valign='top'><a href='http://www.wrh.noaa.gov/data/nwsweatherstory.kml'><img style='border: 2px solid ; width: 36px; height: 14px;' src='kml.gif' alt='KML Feed' title='KML Feed' border='0' /><b> Weather Story KML Feed</b></a></td>
</tr>
</table>
\n";

include "/var/www/includes/nwscwi-footer.php";
exit;
#end ws2
}

#For ws2 to access only
function default_error($wfo) {
#echo "$wfo = Entering default_error!!!!<br>\n";
        global $wfo;
#include_once "/var/www/includes/global-config.php";
        include_once "/var/www/includes/nwscwi-header.php";

        echo "<table width='654'>
        <tr><td bgcolor='#000099' align='center' width='500'>
<i><b><font color='#FFFF00' size='+2' face='Arial, Helvetica, sans-serif'>Weather Story</font></b></i>
\n";
        echo "</td>\n";
        echo "<td align='left'>\n";
        include_once("nav.php");
        echo "</td>\n";
        echo "</tr>\n";
   #     echo"<tr><td colspan='2'><img src='make_img.php' border='1' alt='No Weather Story has been issued today.'></img>\n";
	echo "<tr><td><br><b>No Weather Story has been issued today.</b> </td></tr></table> \n";
        include_once "/var/www/includes/nwscwi-footer.php";
        exit;
}

exit;
?>

