/*
 * 
 * $bbc.co.uk	:: BBC Weather
 * $title		:: Map Presenter SlideShow Engine
 * $version		:: 2.1
 * $author		:: BBC Weather Team
 * $date		:: 01.04.05
 * $notes 		:: Main slide show engine.
 * $updates		:: April 2006  
 * 
*/

// Browser detection.
/**********************************************************************************************/
var NETFOUR = document.layers? true: false;
var DHTML = ( NETFOUR || document.all || (parseInt(navigator.appVersion) >= 5)) ? true : false;
var DOM = (document.getElementById) ? true : false;

// Cross browser object finder.
/**********************************************************************************************/

// int object type
var Obj;

function getElement(nameOfObject)
{
	Obj = null;

	if (DOM) Obj = findDOMObject(nameOfObject)
	else if (DHTML && document.all) Obj = document.all[nameOfObject]
	else if (NETFOUR) findLayer(window,nameOfObject);

	if (!Obj || ( NETFOUR && Obj == window ) ) Obj = "Object not found"

	return Obj;
}

function findDOMObject(nameOfObject) 
{
	for (var i = 0; i < document.images.length; i++) {
		if (document.images[i].name==nameOfObject) return document.images[i]
	}
	return document.getElementById(nameOfObject);
}

function findLayer(node,nameOfObject) 
{
	if ( node.name == nameOfObject ) Obj = node;
	
	for ( var counter = 0; counter < node.document.images.length; counter++ ) {
		if (node.document.images[counter].name==nameOfObject) Obj = node.document.images[counter];
	}
	
	for ( var i = 0; i < node.document.layers.length; i++ ) {
		findLayer(node.document.layers[i],nameOfObject);
	}
}



/**********************************************************************************************
 *  Cross browser ".innerText"
 */
function writeText( what, where )
{
	var i = 0;

	if( NETFOUR ) 							// Netscape4
	{
		where.document.open();
		where.document.write( what );
		where.document.close();
	}
	else if( document.getElementById && document.createTextNode )	// IE 4, 5 6?, Moz, Opera7
	{
		what = document.createTextNode(what);

		var len = where.childNodes.length;
		for( i = 0; i < len; i++ )
		{
			ig = where.removeChild(where.childNodes[0]);
		}

		where.appendChild( what );
	}
	else if( where.innerText )
	{
		where.innerText = what;
	}
}

// x-browser style attribute setter
function setStyle( where, which, what )
{
	// where = where is the style to be set?
	// which = which property is to be set?
	// what  = what is the value of the property?

	if( typeof where.style == 'undefined' || where.style == 'undefined') {
		eval( 'where.'+ which +' = '+ what );
	} else  {
		eval( 'where.style.'+ which +' = '+ what );
	}
}


/**********************************************************************************************/
// SetInterval and ClearInterval Compatibility Script
// Copyright 1999 InsideDHTML.com, LLC. All rights reserved
// See www.insideDHTML.com for more information.

var aTracking = new Array()

function runInterval(sIndex) 
{
	if ( typeof aTracking[sIndex] != 'undefined' ) 
	{
		var args = aTracking[sIndex].arguments
		// Call function and pass in any extra argument
		var callargs="aTracking[sIndex].code(";

		for(var i=0;i < args.length;++i) 
		{
		  callargs=callargs+"args["+i+"]";
		  if(i < args.length-1) 
		  	callargs+=",";
		}

		callargs=callargs+")";
		eval(callargs);

		// Start up timer for next iteration
		if( aTracking[sIndex] )
			aTracking[sIndex].timerID = setTimeout("runInterval(" + sIndex + ")",aTracking[sIndex].interval)
	}
	
}

function newSetInterval(func,interval) 
{
	var fCall = func;
	var nextIdx = aTracking.length;
	if (typeof func!="function") 
		var fCall= new Function(func);

	aTracking[nextIdx] = new Object;
	aTracking[nextIdx].interval = interval;
	aTracking[nextIdx].code = fCall;
	aTracking[nextIdx].arguments = new Array();

	for (var i=2;i < arguments.length;i++) 
		aTracking[nextIdx].arguments[aTracking[nextIdx].arguments.length] = arguments[i];

	aTracking[nextIdx].timerID = setTimeout("runInterval(" + nextIdx + ")",interval);
	return nextIdx;
}

function newClearInterval(idx) 
{
	if (aTracking[idx]) {
		clearTimeout( aTracking[idx].timerID );
		aTracking[idx] = null;
	}
}

// Functions and data structures for dates, days and time.
/**********************************************************************************************/
var EN = 0;
var CY = 1;
var DAYS = new Array(	
			new Array('SUNDAY','SUL'),	 // day name in english, day name in welsh
			new Array('MONDAY','LLUN'),
			new Array('TUESDAY','MAWRTH'),
			new Array('WEDNESDAY','MERCHER'),
			new Array('THURSDAY','IAU'),
			new Array('FRIDAY','GWENER'),
			new Array('SATURDAY','SADWRN')
		);

/* returns numeric day of the week, 0 = sunday, 1 = monday etc.
 *  ### NOTE ### Javascript's Date object's getMonth returns zero-based month index, whilst DayOfWeek expects a one-based index
 */
function DayOfWeek(day,month,year) {
//alert( 	"day: "+ day 	+"\nmonth: "+ month 	+"\nyear: "+ year );
	
	if( year < 1000 )
	{
		var x = year.toString().substr( year.toString().length-2, 2 );
		year = '200' + x;
	}

    var a = Math.floor((14 - month)/12);
    var y = year - a;
    var m = month + 12*a - 2;
    var d = (day + y + Math.floor(y/4) - Math.floor(y/100) +
             Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
    return d;
}

// returns a string representing date&time of the form yymmddhh, out of the supplied date. used to make image filenames
// params: when - a date
// returns: strShortDate - string, the date in format described above
function getShortDateTime( when )
{
	var strShortDate = '';
	if( getShortDateTime.arguments.length == 0 )
		when = new Date(g_now);

	strShortDate += when.getFullYear().toString().substr(when.getFullYear().toString().length-2, 2);
	strShortDate += (when.getMonth()+1) < 10? '0' + (when.getMonth()+1): (when.getMonth()+1).toString();
	strShortDate += when.getDate() < 10? '0' + when.getDate(): when.getDate();
	strShortDate += when.getHours() < 10? '0' + when.getHours(): when.getHours();
	return strShortDate;
}


function getShorterDateTime( when )
{
	var strShorterDate = '';
	if( getShorterDateTime.arguments.length == 0 )
		when = new Date(g_now);

	strShorterDate += when.getFullYear().toString().substr(when.getFullYear().toString().length-2, 2);
	strShorterDate += (when.getMonth()+1) < 10? '0' + (when.getMonth()+1): (when.getMonth()+1).toString();
	strShorterDate += when.getDate() < 10? '0' + when.getDate(): when.getDate();
	return strShorterDate;
}

