//developed by ODL www.odl.co.uk odl_itsolutions@odl.co.uk

//common code for the clocks on 'splash' and 'banner' pages

//if browser allows, some images
//set up global image frames used, then extra function to actually preload graphics cos relative url of image frames may change from page to page



function	preload_digits(rel_image_path)
{
	if(document.images)
	{
		digit_0.src=rel_image_path + "dige_00.gif";
		digit_1.src=rel_image_path + "dige_01.gif";
		digit_2.src=rel_image_path + "dige_02.gif";
		digit_3.src=rel_image_path + "dige_03.gif";
		digit_4.src=rel_image_path + "dige_04.gif";
		digit_5.src=rel_image_path + "dige_05.gif";
		digit_6.src=rel_image_path + "dige_06.gif";
		digit_7.src=rel_image_path + "dige_07.gif";
		digit_8.src=rel_image_path + "dige_08.gif";
		digit_9.src=rel_image_path + "dige_09.gif";

		digit_bleep_on.src=rel_image_path + "dot_middle.gif";
		digit_bleep_off.src=rel_image_path + "shim.gif";
	}
}





function	swap_image(image_obj,object_name)
{
	if(document.images)
	{
		document.images[image_obj].src=eval(object_name + ".src");
	}
}




//work out whether given Date() is in daylight savings (if so add an hour)
function	is_daylight_savings_time(sometime)
{
	var	month=sometime.getMonth();
	var	day=sometime.getDate();
	var	dow=sometime.getDay();
	var	next_sunday=((7 - dow) + day);

	var	is_summer_time=false;


	//depends on month - MARCH, OCTOBER, or somewhere in between
	if((month > 2) && (month < 9))
	{
		is_summer_time=true;
	}
	else if(month == 2)
	{
		//march

		//basically, if next sunday is next month, we're in summertime
		if(next_sunday > 31)
			is_summer_time=true;
	}
	else if(month == 9)
	{
		//october

		//basically, if next sunday is still this month, we're in summertime
		if(next_sunday <= 31)
			is_summer_time=true;
	}



	return(is_summer_time);
}



function	update_clock(right_now,clock_name_prefix)
{
	var	hours_t_val=0;
	var	hours_u_val=0;
	var	mins_t_val=0;
	var	mins_u_val=0;

	var	this_hour=right_now.getHours();
	var	this_minute=right_now.getMinutes();


	//now extract the _h/_t/_u versions (used for printing individual digits)...

	hours_t_val=Math.floor(this_hour / 10);
	hours_u_val=this_hour - (hours_t_val * 10);

	mins_t_val=Math.floor(this_minute / 10);
	mins_u_val=this_minute - (mins_t_val * 10);





	//swap out the images

	//bleeep!
	if(bleep_phase)
		swap_image(clock_name_prefix + 'blip1_img','digit_bleep_on');
	else
		swap_image(clock_name_prefix + 'blip1_img','digit_bleep_off');


	swap_image(clock_name_prefix + 'hours_t_img',"digit_" + hours_t_val);
	swap_image(clock_name_prefix + 'hours_u_img',"digit_" + hours_u_val);
	swap_image(clock_name_prefix + 'mins_t_img',"digit_" + mins_t_val);
	swap_image(clock_name_prefix + 'mins_u_img',"digit_" + mins_u_val);
}



//wake up every so often and set the clocks - also called when page starts
function	set_all_times()
{
	var	rightnow=new Date();
	var	rightnow_gmt_ms=rightnow.getTime() + (rightnow.getTimezoneOffset() * 60 * 1000);


	var	gmt_offset=rightnow.getTimezoneOffset();
	var	rightnow_ms=rightnow.getTime();


	//get timezone offsets if necessary
	//only do this once
	//note site will not update if left to run over the changeover between daylight savings and normal time

	if(! timezone_offsets_decided)
	{
		timezone_offset_sanfrancisco=-8;
		timezone_offset_london=0;
		//change this for joberg differences
		timezone_offset_johannesburg=1;


		//determine whether in daylight savings time
		if(is_daylight_savings_time(time_in_timezone(timezone_offset_sanfrancisco,rightnow_gmt_ms)))
			timezone_offset_sanfrancisco++;
		if(is_daylight_savings_time(time_in_timezone(timezone_offset_london,rightnow_gmt_ms)))
			timezone_offset_london++;
		if(is_daylight_savings_time(time_in_timezone(timezone_offset_johannesburg,rightnow_gmt_ms)))
			timezone_offset_johannesburg++;

		timezone_offsets_decided=true;
	}



	update_clock(time_in_timezone(timezone_offset_sanfrancisco,rightnow_gmt_ms),"sf_");
	update_clock(time_in_timezone(timezone_offset_london,rightnow_gmt_ms),"lndn_");
	update_clock(time_in_timezone(timezone_offset_johannesburg,rightnow_gmt_ms),"jburg_");



	//disable this

//	//toggle?
//	bleep_phase=(!bleep_phase);


	//repeat every so many seconds...
	setTimeout("set_all_times();",15000);
}


//initialise on loading - may be included in html in different directories so image path may change
function	startup_clocks(rel_image_path)
{
	preload_digits(rel_image_path);
	set_all_times();
}


function	time_in_timezone(hours_difference,rightnow_gmt_ms)
{
	var	rightnow_mod_ms=rightnow_gmt_ms + ((hours_difference * 60) * 60 * 1000);
	return(new Date(rightnow_mod_ms));
}

