// The place, observatory definitions and daylight savings functions

// Copyright Ole Nielsen 2002-2003, Peter Hayes 1999-2001

// Alterado por: alexandre guimarães - 2006

function place(name,latitude,ns,longitude,we,zone,dss,dse) {
  this.name      = name;
  this.latitude  = latitude;
  this.ns        = ns;
  this.longitude = longitude;
  this.we        = we;
  this.zone      = zone;
  this.dss       = dss;
  this.dse       = dse;
}

// A selection of places
// Please leave Greenwich in the first entry as the default
// The second entry is my home town, I suggest you change it to yours
// is you keep a copy for your personal use.
// This database is based on Peter Hayes' original database with several places added

var atlas = new Array(
  new place("Nepomuceno - MG","21:14:04",1,"45:14:14",0,180,"",""),
  new place("Rio de Janeiro","22:54:00",1,"43:16:00",0,180,"",""),
  new place("BR:Brasília","15:46:49",1,"47:55:48",0,180,"",""),
  new place("BR:Campo Grande","20:26:34",1,"54:51:38",0,240,"",""),
  new place("BR:Campo Verde","15:32:50",1,"55:10:10",0,240,"",""),
  new place("BR:Caxambu","21:58:48",1,"44:56:03",0,180,"",""),
  new place("BR:Cuiabá","15:35:47",1,"56:05:50",0,240,"",""),
  new place("BR:Guarapari","20:40:01",1,"40:29:52",0,180,"",""),
  new place("BR:Rio Pomba","21:16:31",1,"43:10:46",0,180,"",""),
  new place("BR:São Paulo","23:32:53",1,"46:38:11",0,180,"",""),
  new place("BR:Silverânia","21:09:34",1,"43:12:56",0,180,"",""),
  new place("BR:Teresópolis","22:26:01",1,"42:54:50",0,180,"",""),
  new place("UK:Greenwich","51:28:38",0,"00:00:00",0,0,"3:5:0","10:5:0")
);


function observatory(place,year,month,day,hr,min,sec) {
// The observatory object holds local date and time,
// timezone correction in minutes with daylight saving if applicable,
// latitude and longitude (west is positive)
	this.name = place.name;
	this.year = year;
	this.month = month;
	this.day = day;
	this.hours = hr;
	this.minutes = min;
	this.seconds = sec;
	this.tz = place.tz;
	this.dst = false;	// is it DST?
	this.latitude = place.latitude;
	this.longitude = place.longitude;
}

// The default observatory (Greenwich noon Jan 1 2000) 
// changed by user setting place and time from menu

var observer  = new observatory(atlas[0],2000,1,1,12,0,0);

// Site name returns name and latitude / longitude as a string
function sitename() {
  var sname=observer.name;
  var latd=Math.abs(observer.latitude)+0.00001;
  var latdi=Math.floor(latd);
  sname+=((latdi < 10) ? " 0" : " ") + latdi;
  latm=60*(latd-latdi); latmi=Math.floor(latm);
  sname+=((latmi < 10) ? ":0" : ":") + latmi;
//  lats=60*(latm-latmi); latsi=Math.floor(lats);
//  sname+=((latsi < 10) ? ":0" : ":") + latsi;
  sname+=((observer.latitude >= 0) ? " N, " : " S, ");
  var longd=Math.abs(observer.longitude)+0.00001;
  var longdi=Math.floor(longd);
  sname+=((longdi < 10) ? "0" : "") + longdi;
  longm=60*(longd-longdi); longmi=Math.floor(longm);
  sname+=((longmi < 10) ? ":0" : ":") + longmi;
//  longs=60*(longm-longmi); longsi=Math.floor(longs);
//  sname+=((longsi < 10) ? ":0" : ":") + longsi;
  sname+=((observer.longitude >= 0) ? " W" : " E");
  return sname;
}	// sitename()


function checkdst(obs) {
	// Check DST is an attempt to check daylight saving, its not perfect.
	// Returns 0 or -60 that is amount to remove to get to zone time.
	// this function is now only called when selecting a place from the dropdown list. No dst check when updating the time!
	// We only know daylight saving if in the atlas
	if ((selectedIndex < 0) || (selectedIndex >= atlas.length))
		return 0;
	var dss=atlas[selectedIndex].dss;
	var dse=atlas[selectedIndex].dse;
	var ns=atlas[selectedIndex].ns;
	if (dss.length==0) return 0;
	if (dse.length==0) return 0;
	// parse the daylight saving start & end dates
	var col1=dss.indexOf(":");
	var col2=dss.lastIndexOf(":");
	var col3=dss.length;
	var dssm=parseInt(dss.substring(0,col1),10);
	var dssw=parseInt(dss.substring(col1+1,col2),10);
	var dssd=parseInt(dss.substring(col2+1,col3),10);
	col1=dse.indexOf(":");
	col2=dse.lastIndexOf(":");
	col3=dse.length;
	var dsem=parseInt(dse.substring(0,col1),10);
	var dsew=parseInt(dse.substring(col1+1,col2),10);
	var dsed=parseInt(dse.substring(col2+1,col3),10);
	// Length of months
	// year,month,day and day of week
	var jdt=jd0(obs.year,obs.month,obs.day);
	var ymd=jdtocd(jdt);
	// first day of month - we need to know day of week
	var fymd=jdtocd(jdt-ymd[2]+1);
	// look for daylight saving / summertime changes
	// first the simple month checks
	// Test for the northern hemisphere
	if (ns==0) {
		if ((ymd[1]>dssm) && (ymd[1]<dsem)) return -60;
		if ((ymd[1]<dssm) || (ymd[1]>dsem)) return 0;
	} 
	else{
		// Southern hemisphere, New years day is summer.
		if ((ymd[1]>dssm) || (ymd[1]<dsem)) return -60;
		if ((ymd[1]<dssm) && (ymd[1]>dsem)) return 0;
	}
	// check if we are in month of change over
	if (ymd[1]==dssm) { // month of start of summer time
		// date of change over
		var ddd=dssd-fymd[3]+1;
		ddd=ddd+7*dssw;
		while (ddd>month_length[ymd[1]-1]) ddd-=7;
		if (ymd[2]<ddd) return 0;
		// assume its past the change time, its impossible
		// to know if the change has occured.
		return -60;
	} 
	if (ymd[1]==dsem) { // month of end of summer time
		// date of change over
		var ddd=dsed-fymd[3]+1;
		ddd=ddd+7*dsew;
		while (ddd>month_length[ymd[1]-1]) ddd-=7;
		if (ymd[2]<ddd) return -60;
		// see comment above for start time
		return 0;
	}
	return 0;
}	// checkdst()


function jd(obs) {
// The Julian date at observer time
	var j = jd0(obs.year,obs.month,obs.day);
	j+=(obs.hours+((obs.minutes+obs.tz)/60.0)+(obs.seconds/3600.0))/24;
	return j;
}	// jd()


function local_sidereal(obs) {
// sidereal time in hours for observer
	var res=g_sidereal(obs.year,obs.month,obs.day);
	res+=1.00273790935*(obs.hours+(obs.minutes+obs.tz+(obs.seconds/60.0))/60.0);
	res-=obs.longitude/15.0;
	while (res < 0) res+=24.0;
	while (res > 24) res-=24.0;
	return res;
}

