//////////////// CLOCKS ////////////////////////////////

////////////// DAYLIGHT SAVING ADJUSTMENTS /////////////////
//check for daylight saving periods
var dstBegin = new Array();
var dstEnd = new Array();

//London - starts at 1am on the last sunday in March and ends at 2am on the last sunday in October.
dstBegin["london"] = getLastSundayInMonth(2,1);
dstEnd["london"] = getLastSundayInMonth(9,1);

//New York - starts at 2am on the 2nd sunday in March and ends at 2am on the 1st sunday in November
dstBegin["newyork"] = getSecondSundayInMonth(2,2);
dstEnd["newyork"] = getFirstSundayInMonth(10,1);

//Yokyo - no daylight saving in operation

//Delhi - no daylight saving in operation

////////////// END DAYLIGHT SAVING ADJUSTMENTS /////////////

function setTime() {

	// LONDON //
	var london=calcTime('London', 0);
	var newyork=calcTime('NewYork', -5);
	var tokyo=calcTime('Tokyo', 9);
	var delhi=calcTime('Delhi', 5.5);
	clocks=london+"--"+newyork+"--"+tokyo+"--"+delhi;


	clocks="<span style='font-size: 80%;text-align: right;'>&nbsp;<br><b>London</b>: "+london+" &nbsp; <b>New York</b>: "+newyork+" &nbsp; <b>Delhi</b>: "+delhi+" &nbsp; <b>Tokyo</b>: "+tokyo+"</span>";
	insertText(clocks);
}

function calcTime(city, offset) {
    // create Date object for current city
    d = new Date();
    
	//account for daylight saving time
	tzOffset = city.tzOffset;
	if (city.dstBegin && city.dstEnd) {
		if (d > city.dstBegin && d <= city.dstEnd) tzOffset ++;
	}


    // convert to msec
    // add local timezone offset 
    // get UTC time in msec
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    
    // create new Date object for different city using timezone offset
    nd = new Date(utc + (3600000*offset));
    
    // return time as a string
    // return city + ": " + nd.toLocaleString();

	// get the hh:mm:ss
	temp_hour=nd.getHours();
	if (city == "London") {
//		alert("Found London");
		if ((nd > dstBegin["london"]) && (nd < dstEnd["london"])) {
//		alert("running this bit with:"+nd);
		temp_hour++;
		}
	}


	var hour=temp_hour % 24;
	var hour=padNumber(hour, 2);
	var mins=padNumber(nd.getMinutes(),2);
	var secs=padNumber(nd.getSeconds(),2);
	return hour+":"+mins+":"+secs;
}

function updateTime() {
	setTime();
}







	

function padNumber(number,width) {
  //pad a number with zeros, up to the specified width;
  var s = '' + number;
  while (s.length < width) s = '0' + s;
  return s;
}


function insertText(content){
	//if IE 4+
	if (document.all)
		dcontent1.innerHTML=content;
	//else if NS 4
	else if (document.layers){
		document.ns4dcontent1.document.ns4dcontent21.document.write(content);
		document.ns4dcontent1.document.ns4dcontent21.document.close();
	}
	//else if NS 6 (supports new DOM)
	else if (document.getElementById){
		rng = document.createRange();
		el = document.getElementById("dcontent1");
		rng.setStartBefore(el);
		htmlFrag = rng.createContextualFragment(content);
		while (el.hasChildNodes())
		el.removeChild(el.lastChild);
		el.appendChild(htmlFrag);
	}else {
		alert('Unfortunately, your browser does not support "layers" in JavaScript.  This function is available in Internet Explorer or Netscape versions 4 or above.');
	}
}


var months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

function getFirstSundayInMonth(month,time) {
  //get the date of the 1st sunday in the given month
  var d = new Date();
  d.setMonth(month);
  d.setDate(1);
  var day = d.getDay();
  while (day > 0) {
    d.setDate(d.getDate()+1);
    day = d.getDay();
  }
  d.setHours(time);
  d.setMinutes(0);
  d.setSeconds(0);
  return d;
}

function getSecondSundayInMonth(month,time) {
  //get the date of the 2nd sunday in the given month
  var d = new Date();
  d.setMonth(month);
  d.setDate(8);
  var day = d.getDay();
  while (day > 0) {
    d.setDate(d.getDate()+1);
    day = d.getDay();
  }
  d.setHours(time);
  d.setMinutes(0);
  d.setSeconds(0);
  return d;
}

function getLastSundayInMonth(month,time) {
  //get the date of the last sunday in the given month
  var d = new Date();
  d.setMonth(month);
  var match = new Date();
  match.setMonth(month);
  match.setDate(1);
  while (d.getMonth() == month) {
    if (d.getDay() == 0) match.setDate(d.getDate());
    d.setDate(d.getDate()+1);
  }
  match.setHours(time);
  match.setMinutes(0);
  match.setSeconds(0);
  return match;
}
