/*
 * 
 * $bbc.co.uk	:: BBC Weather
 * $title	:: Map Presenter Europe Weather Products
 * $version	:: 2.1
 * $author	:: BBC Weather Centre
 * $date	:: 01.04.05
 * $updates`	:: 01.05.05 
 * 		:: Various updates and code amends, mainly to the 'update times' to reflect any updated product scope.
 * 		:: Problem with users who have different local timezone offsets. 
 * 		:: Amended the scripts date object and timezone to work with a fixed UTC (GMT) parsed date instead of using the users local machine settings.
 * 		:: Added BST offset to the timezone to be reflected with each UK product hour caption when required.
 * 		:: Updated comments globally and added new ones where helpful for development. 
 *		
 *		:: 03.08.05 - brendan gibson
 *		:: The images were not being populated between 11 and 12.  Changed selector for cloud/rain
 *		:: below to handle this case.
 *
 *		:: 10.08.05 - brendan gibson
 *		:: The images were not being shown after 21.  Changed selector for cloud/rain
 *		:: below to handle this case.
 * 
*/

// 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 FILETYPE = '.jpg'; 				// chart file format.
var IMAGEPATH = '/weather/charts/uk/'; 	// chart file path.
// local UK constants
var FIRST = 0; 	 				// ('0') case statment to use. 
var LAST = 1; 					// ('1') case statment to use. 
var PRIOR = 2;	 				// ('2') case statment to use. 
var FOLLOWING = 3; 				// ('3') case statment to use.
/******************************************************************/

// 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

// function
function createSlidesAndCaptions ( productName, region )
{
	var len;				// length of arrays or how many slides in show.
	var diffBack;				// time difference in hours between slides in the past.
	var diffFrwd;				// time difference in hours between slides in the future.
	var diffFrwdStep2;			// time difference in hours between slides in the future (extended).
	var diff;				// int diff.
	var offset = 0;				// where in the array the first viewed slide sits.
	var nowMarker;				// essentially a copy of "offset" but used in a different context, see below.
	var now = new Date(g_now);		// local time.
	var startTime = new Date(g_now);	// when does array start.
	var productTime = new Date(g_now);	// a floating time object, used to get time at incrememnts past startTime

	var mark = true;
	var jump;

	// 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 uk national and uk regional product timings.
	if( g_productTerritory == 'uknational' || g_productTerritory == 'ukregional' )
	{

//		now.setHours(22);
		// 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 AND FORECAST PRODUCTS.

		// these products have been set to not show the hour that we are presently within.

			diffBack = 1; // more on this later.
			diffFrwd = 3; // more on this later.
			diffFrwdStep2 = 12; // better if we moved all these to an array

			// when hourly service has been resored please delete everything marked from this script.

			// BG: Made a change in the next selection
			// changed an 11 to a 12 as times between 11 and 12
			// were not matching and therefore not getting a len
			// BG: Made a second change for times after 21
			// open delete
			// every 3 hours chop one chart off the end of the show.
			var nowhours = now.getHours();
			jump = 6;

			if ( nowhours >= 6 && nowhours < 9 ) { 
			  len = 23;
			  jump = 5;
			} else if ( nowhours >= 9 && nowhours < 12 ) { 
			  len = 23;
			  jump = 6;
			} else if ( nowhours >= 12 && nowhours < 15 ) {
			  len = 22;
			  jump = 6;
			} else if ( nowhours >= 15 && nowhours < 18 ) {
			  len = 21;
			  jump = 6;
			} else if ( nowhours >= 18 && nowhours < 21 ) {
			  len = 20;
			  jump = 6;
			} else if ( nowhours >= 21 ) {
			  len = 19;
			  jump = 7;
			} else if ( nowhours >= 0 && nowhours < 3 ) {
  			  len = 18;
			  jump = 7;
			} else if ( nowhours >= 3 && nowhours < 6 ) {
			  len = 18;
			  jump = 7;	
			}
			// close delete
			len += jump;

			startTime.setTime( now.getTime() - (12*ONEHOUR), 0, 0 ); // start time.
			offset = PRIOR; // set initial chart offset.

		break; // close case statement.

		// open case statement.
		// set productName case statement for product timings only.
		
		case 'visiblesatellite':
			diff = 1;
			if ( now.getHours() > 0 && now.getHours() < 9 ) { // if it's between midnight and 9 am
				startTime.setTime( now.getTime() - (24*ONEHOUR) ); // show yesterday's chart
				startTime.setHours( 8 );								
				len = 9;
			} else { 
				startTime.setHours( 8 );
				if ( now.getHours() <= 16 ) { // if it's earlier than 4pm
					len = (now.getHours() - startTime.getHours() ); 
				} else {
					len = 9;
				}
			}
			//alert('Len:'+len);
			

		offset = LAST; // LAST set initial chart offset.

		break; // close case statement.

		// open case statement.
		// set productName case statements for forecast products only.
		case 'temperature':

		// CONSTRUCT FORECAST PRODUCTS.

			diff = 6;
			jump = 6;
			// uknational specific timings.
			if (g_productTerritory == 'uknational' ) {
				if ( now.getHours() > 20 ) {
					startTime.setTime( now.getTime() + (24*ONEHOUR) ); // make day "startTime" tomorrow.
					startTime.setHours( 0 ); // that's 0000 tomorrow.
					len = 19;
				} else if ( now.getHours() > 18  ) {
					startTime.setTime( now.getTime() + (24*ONEHOUR) ); // make day "startTime" tomorrow.
					startTime.setHours( 0 ); // that's 0000 tomorrow.
					len = 17;
				} else if ( now.getHours() > 12  ) {
					startTime.setHours( 18 ); // fixed time to 1800 GMT.
					len = 18;
				} else if ( now.getHours() > 8 ) {
					startTime.setHours( 12 ); // fixed time to 1200 GMT.
					len = 19;
				} else if ( now.getHours() > 6 && now.getHours() < 8 ) {
					startTime.setHours( 12 ); // fixed time to 1200 GMT.
					len = 17;
				} else {
					startTime.setHours( 6 ); // fixed time to 0600 GMT.
					len = 18;
				}
			}
			// ukregional specific timings.
			else if ( g_productTerritory == 'ukregional' ) {
				if ( now.getHours() > 21 ) {
					startTime.setTime( now.getTime() + (24*ONEHOUR) ); // make day "startTime" tomorrow.
					startTime.setHours( 0 ); // that's 0000 tomorrow.
					len = 5;
					jump = 6;
				} else if ( now.getHours() > 20 ) {
					startTime.setTime( now.getTime() + (24*ONEHOUR) ); // make day "startTime" tomorrow.
					startTime.setHours( 0 ); // that's 0000 tomorrow.
					len = 5;
					jump = 5;
				} else if ( now.getHours() > 18  ) {
					startTime.setTime( now.getTime() + (24*ONEHOUR) ); // make day "startTime" tomorrow.
					startTime.setHours( 0 ); // that's 0000 tomorrow.
					len = 3;
					jump = 6;
				} else if ( now.getHours() > 12  ) {
					startTime.setHours( 18 ); // fixed time to 1800 GMT.
					len = 4;
					jump = 6;
				} else if ( now.getHours() > 8 ) {
					startTime.setHours( 12 ); // fixed time to 1200 GMT.
					len = 5;
					jump = 6;
				} else if ( now.getHours() > 6 && now.getHours() < 8 ) {
					startTime.setHours( 12 ); // fixed time to 1200 GMT.
					len = 3;
					jump = 6;
				} else {
					startTime.setHours( 6 ); // fixed time to 0600 GMT.
					len = 4;
					jump = 6;
				}

			len += jump; // for extra 3 day forecast (24 hours)
			}

		offset = FIRST; // set initial chart offset.

		break; // close case statement.

		// open case statement.
		// set productName case statements for forecast products only.
		case 'wind':
			diff = 6;

			if ( now.getHours() > 21 ) {
				startTime.setTime( now.getTime() + (24*ONEHOUR) ); // make day "startTime" tomorrow.
				startTime.setHours( 0 ); // that's 0000 tomorrow.
				len = 5;
				jump = 6;
			} else if ( now.getHours() > 20 ) {
				startTime.setTime( now.getTime() + (24*ONEHOUR) ); // make day "startTime" tomorrow.
				startTime.setHours( 0 ); // that's 0000 tomorrow.
				len = 5;
				jump = 5;
			} else if ( now.getHours() > 18  ) {
				startTime.setTime( now.getTime() + (24*ONEHOUR) ); // make day "startTime" tomorrow.
				startTime.setHours( 0 ); // that's 0000 tomorrow.
				len = 3;
				jump = 6;
			} else if ( now.getHours() > 12  ) {
				startTime.setHours( 18 ); // fixed time to 1800 GMT.
				len = 4;
				jump = 6;
			} else if ( now.getHours() > 8 ) {
				startTime.setHours( 12 ); // fixed time to 1200 GMT.
				len = 5;
				jump = 6;
			} else if ( now.getHours() > 6 && now.getHours() < 8 ) {
				startTime.setHours( 12 ); // fixed time to 1200 GMT.
				len = 3;
				jump = 6;
			} else {
				startTime.setHours( 6 ); // fixed time to 0600 GMT.
				len = 4;
				jump = 6;
			}

		len += jump; // (for 3 extra days info)

		offset = FIRST; // set initial chart offset.

		break; // close case statement.

		// open case statement.
		// set productName case statements for forecast products only.
		case 'pressure':	
		case 'summary':
			diff = 24;
			if ( now.getHours() > 20 ) {
				startTime.setTime( now.getTime() + (24*ONEHOUR) ); // make day "startTime" tomorrow.
				startTime.setHours( 12 ); // fixed time to 1200 GMT.
				len = 5;
			} else if ( now.getHours() > 8 ) {
				startTime.setHours( 12 ); // fixed time to 1200 GMT.
				len = 5;
			} else {
				startTime.setHours( 12 ); // fixed time to 1200 GMT.
				len = 4;
			}
		
		len = 5;
		offset = FIRST; // set initial chart offset.

		break; // close case statement.

		// open case statement.
		case 'seatemperature':
			diff = 24;
			if( g_now.getHours() > 8 ) {
				startTime.setTime( g_now.getTime() ); // make day "startTime" today.
				startTime.setHours(12);	// fixed time to 1200 GMT.
			} else {
				startTime.setTime( g_now.getTime() - (24*ONEHOUR) ); // make "startTime" tomorrow.
				startTime.setHours(12);	// fixed time to 1200 GMT.
			}
			startTime.setHours( 12,0,0 ); // fixed time to 1200 GMT.
			len = 5; 
			}
	}
// close uk national and uk regional product timings.

// open product chart and caption construction.

		// populate array of chart names and chart captions.
		for( var slideItr=0; slideItr<len; slideItr++ )
		{
			// chart path and chart name string.
			var strImgFileName = IMAGEPATH;
			strImgFileName += region +'_';
			strImgFileName += productName +'_';
			//int captions array.
			g_caps[slideItr] = new Array();

			// alert(g_caps);

			// slideItr[n] = len[n];
			// each seperate chart has a difference between it and the next chart. 
			// this statement determines what difference has been set above and then apply's it,
			if ( typeof diff != 'undefined') { // has diff been set?
				// if using just one hours incrementer go ahead and use it 
				if(productName == "wind" || (productName == "temperature" && g_productTerritory == 'ukregional')) {
					if(slideItr == (len - jump)) {
						if(productTime.getHours() < 12) {
							diff = 12 - productTime.getHours();
						}
						else {
							diff = 24 - productTime.getHours();
						}
//						diff = 12 - productTime.getHours();
					}
					else if(slideItr > (len - jump)) {
						diff = 12;						
					}
				}
				if(slideItr != 0) {
					startTime.setTime(startTime.getTime() + (diff*ONEHOUR));
				}
				productTime.setTime( startTime.getTime() );
			} else { 

				// else, get what difference has been set for the product and then use it.
				if ( (startTime.getTime() + (diffBack*ONEHOUR)) < now.getTime() ) {
					if(slideItr != 0) {
						startTime.setTime(startTime.getTime() + (diffBack*ONEHOUR));
					}
					productTime.setTime( startTime.getTime() );
				} else { // fired if still no product difference has been declared. 
				  if(mark) {
  				    offset = slideItr - 1;
				    mark = false;
				  }
				// if diffBack and diffFrwd are used we want to prevent the present chart to be shown, hence the -1. 
				if( !nowMarker )
 					nowMarker = slideItr - 1;
					// show diffBack and diffFrwd product charts from now time in GMT

					if(slideItr == (len - jump)) {
						if(productTime.getHours() < 12) {
							diffFrwd = 12 - productTime.getHours();
						}
						else {
							diffFrwd = 24 - productTime.getHours();
						}
						productTime.setTime( productTime.getTime() + (diffFrwd*ONEHOUR) );
					}
					else if(slideItr > (len - jump)) {
						diffFrwd = diffFrwdStep2;
						productTime.setTime( productTime.getTime() + (diffFrwd*ONEHOUR) );
					}
					else {


					// when hourly service has been resored please delete everything marked from this script.
					// also reset diffFrwd back to equal 1.

					// open delete
					var bridgeDiff = 0;
					if ( now.getHours() >= 0 && now.getHours() < 1 ) { 
					bridgeDiff = 0;
					//alert("0000");
					} else if ( now.getHours() >= 1 && now.getHours() < 2 ) { 
					bridgeDiff = -1;
					//alert("0100");	
					} else if ( now.getHours() >= 2 && now.getHours() < 3 ) {
					bridgeDiff = -2;
					//alert("0200");
					} else if ( now.getHours() >= 3 && now.getHours() < 4 ) {
					bridgeDiff = 0;
					//alert("0300");
					} else if ( now.getHours() >= 4 && now.getHours() < 5 ) {
					bridgeDiff = -1;
					//alert("0400");
					} else if ( now.getHours() >= 5 && now.getHours() < 6 ) {
					bridgeDiff = -2;
					//alert("0500");
					} else if ( now.getHours() >= 6 && now.getHours() < 7 ) {
					bridgeDiff = 0;
					//alert("0600");
					} else if ( now.getHours() >= 7 && now.getHours() < 8 ) { 
					bridgeDiff = -1;
					//alert("0700");
					} else if ( now.getHours() >= 8 && now.getHours() < 9 ) { 
					bridgeDiff = -2;
					//alert("0800");
					} else if ( now.getHours() >= 9 && now.getHours() < 10 ) { 
					bridgeDiff = 0;
					//alert("0900");
					} else if ( now.getHours() >= 10 && now.getHours() < 11 ) {
					bridgeDiff = -1;
					//alert("1000");	
					} else if ( now.getHours() >= 11 && now.getHours() < 12 ) {
					bridgeDiff = -2;
					//alert("1100");	
					} else if ( now.getHours() >= 12 && now.getHours() < 13 ) {
					bridgeDiff = 0;
					//alert("1200");
					} else if ( now.getHours() >= 13 && now.getHours() < 14 ) {
					bridgeDiff = -1;
					//alert("1300");
					} else if ( now.getHours() >= 14 && now.getHours() < 15 ) {
					bridgeDiff = -2;	
					//alert("1400");
					} else if ( now.getHours() >= 15 && now.getHours() < 16 ) {
					bridgeDiff = 0;	
					//alert("1500");
					} else if ( now.getHours() >= 16 && now.getHours() < 17 ) {
					bridgeDiff = -1;	
					//alert("1600");
					} else if ( now.getHours() >= 17 && now.getHours() < 18 ) {
					bridgeDiff = -2;
					//alert("1700");	
					} else if ( now.getHours() >= 18 && now.getHours() < 19 ) {
					bridgeDiff = 0;
					//alert("1800");
					} else if ( now.getHours() >= 19 && now.getHours() < 20 ) {
					bridgeDiff = -1;	
					//alert("1900");	
					} else if ( now.getHours() >= 20 && now.getHours() < 21 ) {
					bridgeDiff = -2;	
					//alert("2000");
					} else if ( now.getHours() >= 21 && now.getHours() < 22 ) {
					bridgeDiff = 0;
					//alert("2100");	
					} else if ( now.getHours() >= 22 && now.getHours() < 23 ) {
					bridgeDiff = -1;	
					//alert("2200");
					} else if ( now.getHours() >= 23 && now.getHours() < 0 ) {
					bridgeDiff = -2;	
					//alert("2300");
					}

					
					productTime.setTime( now.getTime() + ( (slideItr - nowMarker)*diffFrwd*ONEHOUR) );
					productTime.setHours( productTime.getHours() + bridgeDiff );
					}
	

					// close delete
				}
			}

			// 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 ) {
				// product charts array.
				g_imgs[slideItr] = new Image();
				g_imgs[slideItr].src = strImgFileName + FILETYPE;
			} else {
				// product charts array.
				g_imgs[slideItr] = strImgFileName + FILETYPE;
			}

			// 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 forecast products only.
		case 'summary':
		case 'seatemperature':
		case 'pressure':
			// push the day string and hour string into the g_caps array. 
			g_caps[slideItr][HOUR] = ''; // empty
			g_caps[slideItr][DAY]  = DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][g_lang];

		break; // close case statement.

		// open case statement
		// default product construct statement.
		default: 
/*			if((productName == "wind" || (productName == "temperature" && g_productTerritory == 'ukregional')) && slideItr > 3) {
				g_caps[slideItr][HOUR] = ''; // empty
				g_caps[slideItr][DAY]  = DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][g_lang];
				break;
			} */

			// Set forecast to be Day and Night
			if((productName == "wind" || productName == "cloud" || productName == "cloudrain" || productName == "rain" || (productName == "temperature" && g_productTerritory == 'ukregional')) && slideItr >= (len - jump)) {
				g_caps[slideItr][HOUR] = ''; // empty
				g_caps[slideItr][DAY]  = DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][g_lang];
				if(productTime.getHours() >= 0 && productTime.getHours() < 6) {
					g_caps[slideItr][HOUR] = 'NIGHT'; // empty
					// Fix since 00 or 1am Monday is considered Sunday Night still
					var offsetTime = new Date(g_now);
					offsetTime.setTime(productTime.getTime());
					offsetTime.setTime(productTime.getTime()-(ONEHOUR*2));
					g_caps[slideItr][DAY]  = DAYS[DayOfWeek(offsetTime.getDate(), offsetTime.getMonth()+1, offsetTime.getFullYear() )][g_lang];
				}

				break;
			}
			

			// determine whether its AM : PM 
			var modifier = ':00';
			// set where we are in relation to each looped product.
			var theHour = productTime.getHours();

 			// alert("GMT " + theHour);

			// if the UK timezone is BST add the additional hour to all hour captions where required.  
			if ( g_ukbst != 'false' && theHour == 0 ) { //
				// alert(theHour + modifier);
				theHour = '1';
				modifier = ':00';
				// alert(theHour + modifier);
			} else if ( g_ukbst != 'false' && theHour == 12 && modifier == ':00' ) { //
				// alert(theHour + modifier);
				theHour = '13';
				modifier = ':00';
				// alert(theHour + modifier);
			} else if ( g_ukbst != 'false' )  {
				 theHour = theHour + HOUR;
			}

			// alert("BST " + theHour);

			// 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
				g_caps[slideItr][DAY] = DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][g_lang];
				g_caps[slideItr][HOUR] = theHour + modifier;
			} else if ( productTime.getHours() > 0 ) { // hours between 0000 and 1000 GMT
				g_caps[slideItr][DAY] = DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][g_lang];
				g_caps[slideItr][HOUR] = '0'+theHour +':00';
			} else if ( productTime.getHours() == 10 ) { // 1000 GMT
				g_caps[slideItr][DAY] = DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][g_lang];
				g_caps[slideItr][HOUR] = '10:00';						
			} else if ( productTime.getHours() == 0 ) { // hour dead on 0000 GMT
				g_caps[slideItr][DAY] = DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][g_lang];
				g_caps[slideItr][HOUR] = '00:00';
			}
			// same as above but clear up any bad timings for 1200 and 0000 GMT to allow for BST timings.
			if ( g_ukbst != 'false' && productTime.getHours() == 0 ) { // clear up any BST times for 0000 GMT
				g_caps[slideItr][DAY] = DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][g_lang];
				g_caps[slideItr][HOUR] = '01:00';
			} else if ( g_ukbst != 'false' && productTime.getHours() == 23 ) { // clear up any BST times for 2300 GMT
				g_caps[slideItr][DAY] = DAYS[DayOfWeek(productTime.getDate()+1, productTime.getMonth()+1, productTime.getFullYear() )][g_lang];
				g_caps[slideItr][HOUR] = '00:00';
			} else if ( g_ukbst != 'false' && productTime.getHours() == 11 ) { // clear up any BST times for 1100 GMT
				g_caps[slideItr][DAY] = DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][g_lang];
				g_caps[slideItr][HOUR] = '12:00';
				
			} else if ( g_ukbst != 'false' && productTime.getHours() == 9 ) { // clear up any BST times for 0900 GMT
				g_caps[slideItr][DAY] = DAYS[DayOfWeek(productTime.getDate(), productTime.getMonth()+1, productTime.getFullYear() )][g_lang];
				g_caps[slideItr][HOUR] = '10:00';
			} 

		break; // close case statement.

		}
	}
// close product chart and caption construction.


	// switch statement to works out increment requirements
	switch( offset )
	{
	// open case statement.
	case 0:	// or FIRST
		offset = 0; // set increment offset.
	break; // close case statement.

	// open case statement.
	case 1:	// or LAST
		offset = len-1; // set increment offset.
	break; // close case statement.

	// open case statement.
	case 2:	// or PRIOR
		for ( offset=0; offset<len; offset++ )
		{
			if ( diff ) {
				startTime.setTime(startTime.getTime() + (diff*ONEHOUR));
				productTime.setTime( startTime.getTime()); // set increment offset.
//				productTime.setTime( startTime.getTime() + offset*diff*ONEHOUR ); // set increment offset.
			}
			else {
				startTime.setTime(startTime.getTime() + (diffBack*ONEHOUR));
				productTime.setTime( startTime.getTime()); // set increment offset.
//				productTime.setTime( startTime.getTime() + offset*diffBack*ONEHOUR ); // set increment offset.
			}
			if ( productTime.getTime() > now.getTime() )
			{
//				offset-=2;
				break;
			}
		}

	break; // close case statement.

	// open case statement.
	case 3:	// or FOLLOWING
		for ( offset=0; offset<len; offset++ )
		{
			if ( diff ) {
//				productTime.setTime( startTime.getTime() + offset*diff*ONEHOUR ); // set increment offset.
				startTime.setTime(startTime.getTime() + (diff*ONEHOUR));
				productTime.setTime( startTime.getTime()); // set increment offset.
			}
			else {
				startTime.setTime(startTime.getTime() + (diffBack*ONEHOUR));
				productTime.setTime( startTime.getTime()); // set increment offset.
//				productTime.setTime( startTime.getTime() + offset*diffBack*ONEHOUR ); // set increment offset.
			}
			if ( productTime.getTime() < now.getTime() )
				continue;
			else
				break; // close case statement.
		}
	break; // close case statement.

	}

	// 
	// used for testing purposes only. 
	//var ig=''; // int array group 
	//for(var i=0;i<g_imgs.length;i++)
	//alert(g_imgs[i]+'\n');
	//	ig+=g_imgs[i]+'\n';
	//for(i=0;i<g_caps.length;i++)
	//alert(g_caps[i]+'\n');
	//	ig+=g_caps[i]+'\n';
	//
	//alert("ig" + ig);
	//

	return offset;
}

// bounce
