/*
 * 
 * $bbc.co.uk	:: BBC Weather
 * $title	:: Map Presenter Europe Weather Products
 * $version	:: 2.1
 * $author	:: BBC Weather
 * $date	:: 01.04.05
 * $updates	:: 01.05.05 - BBC Weather
 * $revision	:: 2.3
 * $notes	:: Various updates and code amends, mainly to the 'update times' to reflect any updated product scope.
 * 		:: Separated world and europe products up due to differences in conditional statements for each products.
 * 		:: Amended Europe products requiring regional TIMEOFFSET and TIMEDIFFERENCE to correct products scope where required. 
 * 		:: Amended World and Europe products for pressure to now show an hour caption.
 * 		:: Amended the scripts date object and timezone to work with a fixed UTC (GMT) parsed date instead of using the users local machine settings.
 * 		:: Amended code for world chart preloader to act if set to false in the regions configuration file.
 * 		:: Updated comments globally and added new ones where helpful for development purposes. 
 * 		:: Updated North Europe rain and cloud and rain to show just forecast based charts.
 *      :: Removed the North Europe 'difference'; all products will work the same way now
*/

// declare constants.
/******************************************************************/
var DAY = 0; 					// base day. 
var HOUR = 1; 					// what is an hour worth?
var ONEHOUR = 3600000; 				// number of miliseconds in an hour.
var IMAGEPATH = '/weather/charts/world/'; 	// chart file path.
var FILETYPE = '.jpg'; 				// chart file format.
/******************************************************************/

// g_lobals: 
// creates an array of chart paths + chart names and an array of captions for the slideshow.
var g_imgs = new Array(); // filled out by createSlidesAndCaptions with chart path + chart name strings.
var g_caps = new Array(); // filled out by createSlidesAndCaptions with day&time captions.
// params: 
// productName - string, input name of the product.
// region - string, the name of the location for which the slideshow is being created.
// returns: 
// offset - the offset through the slide-show array of the first slide to show (this is passed as one of the 
// parameters to the slideshow constructor

// int function
function createSlidesAndCaptions ( productName, region )
{
	// create date variables
	var startTime = new Date(g_now); 	// when does array start?
	var productTime = new Date(g_now); 	// floating time object used to get the time at incrememnts past startTime.
	var offset = 0;				// set initial chart offset

	// get GMT time and set it to the time now and the start of the hour.
	// g_now.setHours( g_now.getHours(), 0, 0 )
	startTime.setTime( g_now.getTime() );

	// alert("now" + '\n' + now);
	// alert("startTime" + '\n' + startTime);
	// alert("productTime" + '\n' + productTime);

	// open europe products.
	if ( g_productTerritory == 'europe' )
	{
		// switch statement to works out which charts to include in the slide show, and when they 
		// refer to (the caption text)
		switch( productName ) 
		{
		// open case statement.
		// set productName case statements for combined observed and forecast products.
		case 'rain':
		case 'cloud':
		case 'cloudrain':
		// CONSTRUCT COMBINED OBSERVED PRODUCTS.	

			// declare temporary arrays for forecast and observed product charts and captions.
			var forecastImgs = new Array();
			var forecastCaps = new Array();
			var observedImgs = new Array();
			var observedCaps = new Array();

			// get time GMT and minus 12 hours.
			startTime.setTime( g_now.getTime() - (12*ONEHOUR) );

			// loop through observed product charts and caption arrays with a maximum of 12 passes.			
			for ( var itr=0; itr<12; itr++ )
			{
				// chart path and chart name string.
				var strImgFileName = IMAGEPATH;
				strImgFileName += region +'_';
				strImgFileName += productName +'_';
				//int captions array.
				observedCaps[itr] = new Array();

				// for each 'itr' array position (starting at 0) construct a numeric string that if applicable increments 
				// at 1 hour intervals ahead from the master GMT time which has been set to 12 hours behind the present time.
				productTime.setTime( startTime.getTime() + (itr*ONEHOUR) );

				// alert(startTime);

				// push the shorthand of the date and newly amended time on to strImgFileName to make the file 
				// name complete.
				strImgFileName += getShortDateTime( productTime );

				// preload charts where required.
				if ( g_bPreload ) {
					observedImgs[itr] = new Image();
					//int product charts array.
					observedImgs[itr].src = strImgFileName + FILETYPE;
				} else {
					//int product charts array.
					observedImgs[itr] = strImgFileName + FILETYPE;
				}

				// alert(getShortDateTime( productTime ));

				// set regional timezone TIMEDIFFERENCE for the products hour and day caption only.
				productTime.setTime( productTime.getTime() + (TIMEDIFFERENCE*ONEHOUR) );

				// determine whether its AM : PM 
				var modifier = ':00';
				// set where we are in relation to each looped product.
				var theHour = productTime.getHours();

				// push the day string and hour string into the observedCaps array. 
				// set chart scope.
				if ( productTime.getHours() >= 10 ) { // hours between 1000 and 'rolling hour' GMT
					observedCaps[itr][DAY] = DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][0];
					observedCaps[itr][HOUR] = theHour + modifier;
				} else if ( productTime.getHours() > 0 ) { // hours between 0000 and 1000 GMT
					observedCaps[itr][DAY] = DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][0];
					observedCaps[itr][HOUR] = '0'+theHour +':00';
				} else if ( productTime.getHours() == 0 ) { // hour dead on 0000 GMT
					observedCaps[itr][DAY] = DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][0];
					observedCaps[itr][HOUR] = '00:00';
				}

				// alert(observedCaps[itr]);

			}

			// CONSTRUCT COMBINED FORECAST PRODUCTS.

			var len; // charts in array from master GMT
			// cut array length for products which have validation time outs and are no longer needed in scope.
			if ( g_now.getHours() > 12 && g_now.getHours() < 21 ) { 
			// between 1200 GMT and 2100 GMT.
				startTime.setTime( g_now.getTime() + (24*ONEHOUR) );
				len = 4;
				// alert("Others Forecast length at 4");
			} else { 
				startTime.setTime( g_now.getTime() );
				len = 5;
			} 

			// fix product time to 1200 GMT.
			startTime.setHours(12);
			// update master GMT time to show fixed time of 1200 GMT.
			productTime.setTime( startTime.getTime() );

			// loop through forecast product charts and caption arrays.
			for ( var itr=0; itr<len; itr++ )
			{
				// chart path and chart name string.
				var strImgFileName = IMAGEPATH;
				strImgFileName += region +'_';
				strImgFileName += productName +'_';
				//int captions array.
				forecastCaps[itr] = new Array();

				// for each 'itr' array position (starting at 0) construct a numeric string that if applicable looks 
				// 24 hours ahead of the master GMT time.
				productTime.setTime( startTime.getTime() + (itr*24*ONEHOUR)  );

				if ( !( region == 'weur' ) ) {
					// update master GMT time, possibly by arounding it up or down depending on the regions TIMEOFFSET.
					productTime.setTime( productTime.getTime() + (TIMEOFFSET*ONEHOUR) );	
				}

				// push the shorthand of the date and newly amended time on to strImgFileName to make the file 
				// name complete.
				strImgFileName += getShortDateTime( productTime );	

				// alert(startTime);

				// preload charts where required.
				if ( g_bPreload ) { 
					forecastImgs[itr] = new Image();
					//int product charts array.
					forecastImgs[itr].src = strImgFileName + FILETYPE;
				} else { 
					//int product charts array.
					forecastImgs[itr] = strImgFileName + FILETYPE;
				}

				// alert(getShortDateTime( productTime ));

				// push the day string and hour string into the g_caps array. 
				forecastCaps[itr][HOUR] =  ''; // empty.
				forecastCaps[itr][DAY] =  DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][0];

			// alert(forecastCaps[itr]);

			}

			// join both the observed and forecast product chart arrays together and push on to g_imgs.
			g_imgs = observedImgs.concat( forecastImgs );
			// join both the observed and forecast product caption arrays together and push on to g_imgs.
			g_caps = observedCaps.concat( forecastCaps );

			// alert ("This product date" + '\n' + getShortDateTime( productTime ) ); 
			// alert ("This product caption" + '\n' + g_caps);

			offset = observedCaps.length-1; // set initial chart offset to show earliest forecast chart.

		break; // close case statement.


		// open case statement.
		// set productName case statements for forecast products only.
		case 'pressure':
		case 'wind':
		case 'temperature':
		
		// CONSTRUCT FORECAST PRODUCTS.

			var len; // charts in array from master GMT
			// cut array length for products which have validation time outs and are no longer needed in scope.
			if ( g_now.getHours() > 12 && g_now.getHours() < 21 ) { 
			// between 1200 GMT and 2100 GMT.
				startTime.setTime( g_now.getTime() + (24*ONEHOUR) );
				len = 4;
				// alert("Others Forecast length at 4");
			} else { 
				startTime.setTime( g_now.getTime() );
				len = 5;
			} 

			// fix product time to 1200 GMT.
			startTime.setHours(12);
			// update master GMT time to show fixed time of 1200 GMT.
			productTime.setTime( startTime.getTime() );
			
			// loop through forecast product charts and caption arrays.			
			for ( var itr=0; itr<len; itr++ )
			{
				// chart path and chart name string.
				var strImgFileName = IMAGEPATH;
				//int captions array.
				g_caps[itr] = new Array();
				
				// chart name = region_productName_
				strImgFileName += region +'_';
				strImgFileName += productName +'_';

				// for each 'itr' array position (starting at 0) construct a numeric string that if applicable looks 
				// 24 hours ahead of the master GMT time.
				productTime.setTime( startTime.getTime() + (itr*24*ONEHOUR) );

				// alert(startTime);

				// set regional timezone TIMEOFFSET to the hour and day caption for products other than pressure.
				if ( !( productName == 'pressure' ) ) {
					productTime.setTime( productTime.getTime() + (TIMEOFFSET*ONEHOUR) );

				}

				// push the shorthand of the date and newly amended time on to strImgFileName to make the file 
				// name complete.
				strImgFileName += getShortDateTime( productTime );	

				// alert(startTime);

				// preload charts where required.
				if( g_bPreload ) {
					g_imgs[itr] = new Image();
					//int product charts array.
					g_imgs[itr].src = strImgFileName + FILETYPE;
				} else {
					//int product charts array.
					g_imgs[itr] = strImgFileName + FILETYPE;
				}

				// alert (getShortDateTime( productTime ) ); 

				// display the hour for pressure products only to a fixed GMT time.
				if ( !( productName == 'wind' || productName == 'temperature' ) ) {

				// determine whether its AM : PM 
				var modifier = ':00';
				// set where we are in relation to each looped product.
				var theHour = productTime.getHours();

				// display day and hour for pressure
					g_caps[itr][HOUR] = theHour + modifier;
					g_caps[itr][DAY] = DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][0];
	
				} else {
				// display day only for other products
					g_caps[itr][HOUR] =  ''; // empty.
					g_caps[itr][DAY] =  DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][0];	
				}

			// alert (getShortDateTime( productTime ) );
			// alert (g_caps);

			}

		offset = 0;	// set initial chart offset to show earliest forecast chart.
		break; // close case statement.
	
		// open case statement.
		// set productName case statements for forecast products only.
		case 'summary':

		// CONSTRUCT FORECAST PRODUCTS.

			var len; // charts in array from master GMT
			// cut array length for products which have validation time outs and are no longer needed in scope.
			if ( g_now.getHours() > 12 && g_now.getHours() < 21 ) { 
			// between 1200 GMT and 2100 GMT.
				startTime.setTime( g_now.getTime() + (24*ONEHOUR) );
				len = 4;
				// alert("Others Forecast length at 4");
			} else { 
				startTime.setTime( g_now.getTime() );
				len = 5;
			} 

			// fix product time to 1200 GMT.
			startTime.setHours(12);
			// update master GMT time to show fixed time of 1200 GMT.
			productTime.setTime( startTime.getTime() );

			// loop through forecast product charts and caption arrays.			
			for ( var itr=0; itr<len; itr++ )
			{
				// chart path and chart name string.
				var strImgFileName = IMAGEPATH;
				//int captions array.
				g_caps[itr] = new Array();

				// chart name = region_productName_
				strImgFileName += region +'_';
				strImgFileName += productName +'_';

				// for each 'itr' array position (starting at 0) construct a numeric string that if applicable looks 
				// 24 hours ahead of the master GMT time.
				productTime.setTime( startTime.getTime() + (itr*24*ONEHOUR) );
				// push the shorthand of the date and newly amended time on to strImgFileName to make the file 
				// name complete.
				strImgFileName += getShortDateTime( productTime );	

				// alert(startTime);

				// preload charts where required.
				if( g_bPreload ) {
					g_imgs[itr] = new Image();
					//int product charts array.
					g_imgs[itr].src = strImgFileName + FILETYPE;
				} else {
					//int product charts array.
					g_imgs[itr] = strImgFileName + FILETYPE;
				}

				// alert(getShortDateTime( productTime ));

				// TIMEDIFFERENCE offset should apply to North Europe and West Europe captions only.
				if ( productName == 'weur' || productName == 'neur' && !( productName == 'eeur' ) ) {
					// set regional timezone difference for the products hour and day caption only.
					productTime.setTime( productTime.getTime() + (TIMEDIFFERENCE*ONEHOUR) );
				} else {
					// skip setting any region TIMEDIFFERENCE at all.
				}

				// alert(getShortDateTime( productTime ) );

				// push the day string and hour string into the g_caps array. 
				g_caps[itr][HOUR] =  ''; // empty.
				g_caps[itr][DAY] =  DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][0];
	
			// alert (getShortDateTime( productTime ) ); 
			// alert (g_caps); 
			
			}
		offset = 0; // set initial chart offset to show earliest forecast chart.
		break; // close case statement.

		// open case statement.
		// set productName case statements for forecast products only.
		case 'seatemperature':
			
			// chart path and chart name string.
			var strImgFileName = IMAGEPATH;
			//int captions array.
			g_caps[0] = new Array();

			// set chart scope.
			if ( g_now.getHours() >= 8 ) {
				// hours between 0800 and 0000 GMT
				productTime.setTime( g_now.getTime() );
				productTime.setHours(12); // today at 1200 GMT.
			} else {
				// hours between 0000 and 0800 GMT
				productTime.setTime( g_now.getTime() - (24*ONEHOUR) );
				productTime.setHours(12); // yesterday at 1200 GMT.
			}

			// chart name = region_productName_
			strImgFileName += region +'_';
			strImgFileName += productName +'_';

			// update master GMT time, possibly by arounding it up or down depending on the regions TIMEOFFSET.
			productTime.setTime( productTime.getTime() + ( TIMEOFFSET*ONEHOUR ) );

			// alert(startTime);

			// push the shorthand of the date and base time of 1200 GMT on to strImgFileName to make the file 
			// name complete.
			strImgFileName += getShortDateTime( productTime ); 

			// preload charts where required.
			if ( g_bPreload ) {
				g_imgs[0] = new Image();
				//int product charts array.
				g_imgs[0].src = strImgFileName + FILETYPE;
			} else {
				//int product charts array.
				g_imgs[0] = strImgFileName + FILETYPE;
			}

			// alert (getShortDateTime( productTime ) ); 
			// alert (g_caps); 

			// push the day string and hour string into the g_caps array. 
			g_caps[0][HOUR] =  ''; // empty.
			g_caps[0][DAY] =  DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][0];

		break; // close case statement.
		}
	}
// close europe products.

	return offset;
}

// bounce !
