// Original sunchart.js by FourOhFour from http://skigod.us
// Modified 5/6/09 by Jim McMurry - jmcmurry@mwt.net - jcweather.us
//  

var $image =  97;
var $max = 150;   // Maximum cycles of the moonorbit image ... approx each second
var $cycles = 0;

function motion() {
if (windowFocused) {		
	if ($image == 1) {
		$image = 96;		
	} else {
		$image = $image - 1;		
	}                                 // was xplanet/
	update_graph("moon_orbit", "http://xplanet.jcweather.us/moonorbit2/" + $image + ".jpg"+"?"+new Date());   // need id="moon_orbit" in html
	$cycles ++;
	if ($cycles < $max) {
		if ($image == 1) {
			setTimeout(motion, 3000 );    // Pause a bit on most current image
		} else {
			setTimeout(motion, 1000 );
		}
	}
} else {
	setTimeout(motion, 5000 );
}
}

function setup_sunchart() {
    var img = document.createElement('img');
    
    img.setAttribute('src', sunpath + "arrow_dn.png");
    img.setAttribute('width', 16);
    img.setAttribute('height', 7);
    img.id = 'sunarr';
	
    var sunchart = document.getElementById('sunchart');
	if (sunchart) { 
		sunchart.appendChild(img);
		update_sunchart();
	}  
	update_sun_text();
	motion();
}

function update_sunchart() {
if (windowFocused) {			
    var now = (new Date()).getTime();               // The time on the viewer's browser
	
    var midn = new Date();
    midn.setUTCHours(-timeOffset);                  // Set midn to the time that midnight happens at the host site
    midn.setUTCMinutes(0);
    midn.setUTCSeconds(0);
	
	if (timeOffset > 0) {
		if( (now - midn.getTime()) / 3600000 > 24) {	// midn was the previous midnight, so check for 24 hrs later
			midn.setDate(midn.getDate() + 1);           // Move to the next day
		}
	} else {                                            // But have to have this old one for mine to work
		if(midn.getTime() > now) {
			midn.setDate(midn.getDate() - 1);
		}		
	}
	
	var perc = ((now - midn.getTime()) / 86400000 * 388) - 3;   // Here's where you can add or subtract a few pixels if the pointer is a little off
    document.getElementById('sunarr').style.marginLeft = Math.round(perc) + 'px';
    
    setTimeout(update_sunchart, updtPtr );
} else {
	setTimeout(update_sunchart, updtPtr );
}
	
	
}

function update_sun_text() {

	var currentTime = new Date();
	var hrs = currentTime.getUTCHours() + timeOffset;
	if (hrs < 0) {
		hrs = hrs + 24;
	}	
	if (hrs > 23) {
		hrs = hrs - 24;
	}
	var mins = currentTime.getUTCMinutes();
	nowtime = hrs * 60 + mins;
	
	if (hrs < 10) {
		hrs = "0" + hrs;
	}	
	if (mins < 10) {
		mins = "0" + mins;
	}
	var nowtext = hrs + ":" + mins;

	if (midnight  < 720 && nowtime == midnight ) {	
		 update_text("ajaxastro", nowtext + ' - Solar Midnight !');
		 update_text("ajaxtimetogo", min2hours(astrostart - nowtime) + ' Until Astronomical Twilight (' + min2hours(astrostart ) + ')');
	} else if (astroend < 720 && nowtime  < astroend ) {	
		 update_text("ajaxastro", nowtext + ' - Astronomical Twilight'); 
		 if (nowtime < midnight) {
			 update_text("ajaxtimetogo", min2hours(midnight  - nowtime ) + ' Until Solar Midnight (' + min2hours(midnight ) + ')'); 
		 } else {
			 update_text("ajaxtimetogo", min2hours(nautstart  - nowtime ) + ' Until Nautical Twilight (' + min2hours(nautstart ) + ')'); 
		 }
	} else if (nautend < 720 && nowtime  < nautend ) {	
		 update_text("ajaxastro", nowtext + ' - Nautical Twilight');
		 update_text("ajaxtimetogo", min2hours(nautstart - nowtime) + ' Until Civil Twilight (' + min2hours(civilstart ) + ')'); 
	} else if (civilend < 720 && nowtime  < civilend ) {	
		 update_text("ajaxastro", nowtext + ' - Civil Twilight');
		 update_text("ajaxtimetogo", min2hours(sunrise - nowtime) + ' Until Sunrise (' + min2hours(sunrise ) + ')'); 
	} else if (midnight  < 720 && nowtime  < midnight ) {	  // This is when transit + 12 hrs is after midnight
		 update_text("ajaxastro", nowtext + ' - Night Time');
		 update_text("ajaxtimetogo", min2hours(midnight - nowtime) + ' Until Solar Midnight (' + min2hours(midnight ) + ')'); 
	} else if (nowtime  < astrostart ) {	
		 update_text("ajaxastro", nowtext + ' - Night Time');
		 update_text("ajaxtimetogo", min2hours(astrostart - nowtime) + ' Until Astronomical Twilight (' + min2hours(astrostart ) + ')'); 
	} else if (nowtime  == astrostart ) {	
		 update_text("ajaxastro", nowtext + ' - Astronomical Twilight !');
		 update_text("ajaxtimetogo", min2hours(nautstart - nowtime) + ' Until Nautical Twilight (' + min2hours(nautstart ) + ')'); 
	} else if (nowtime  < nautstart ) {	
		 update_text("ajaxastro", nowtext + ' - Astronomical Twilight');
		 update_text("ajaxtimetogo", min2hours(nautstart - nowtime) + ' Until Nautical Twilight (' + min2hours(nautstart ) + ')'); 
	} else if (nowtime  == nautstart ) {	
		 update_text("ajaxastro", nowtext + ' - Nautical Twilight !');
		 update_text("ajaxtimetogo", min2hours(civilstart - nowtime) + ' Until Civil Twilight (' + min2hours(civilstart ) + ')'); 
	} else if (nowtime  < civilstart ) {	
		 update_text("ajaxastro", nowtext + ' - Nautical Twilight');
		 update_text("ajaxtimetogo", min2hours(civilstart - nowtime) + ' Until Civil Twilight (' + min2hours(civilstart ) + ')'); 
	} else if (nowtime  == civilstart ) {	
		 update_text("ajaxastro", nowtext + ' - Civil Twilight');
		 update_text("ajaxtimetogo", min2hours(sunrise - nowtime) + ' Until Sunrise (' + min2hours(sunrise ) + ')'); 
	} else if (nowtime  < sunrise ) {	
		 update_text("ajaxastro", nowtext + ' - Civil Twilight');
		 update_text("ajaxtimetogo", min2hours(sunrise - nowtime) + ' Until Sunrise (' + min2hours(sunrise ) + ')'); 
	} else if (nowtime  == sunrise ) {	
		 update_text("ajaxastro", nowtext + ' - Sunrise !');
		 update_text("ajaxtimetogo", min2hours(midday - nowtime) + ' Until Solar Noon (' + min2hours(midday ) + ')'); 
	} else if (nowtime  < midday ) {	
		 update_text("ajaxastro", nowtext + ' - Morning');      
		 update_text("ajaxtimetogo", min2hours(midday  - nowtime ) + ' Until Solar Noon (' + min2hours(midday ) + ')'); 
	} else if (nowtime  == midday ) {	
		 update_text("ajaxastro", nowtext + ' - Solar Noon !');
		 update_text("ajaxtimetogo", min2hours(sunset - nowtime) + ' Until Sunset (' + min2hours(sunset ) + ')');
	} else if (nowtime  < sunset ) {	
		 update_text("ajaxastro", nowtext + ' - Afternoon');      
		 update_text("ajaxtimetogo", min2hours(sunset - nowtime) + ' Until Sunset (' + min2hours(sunset ) + ')'); 
	} else if (nowtime  == sunset ) {	
		 update_text("ajaxastro", nowtext + ' - Sunset !');
		 update_text("ajaxtimetogo", min2hours(civilend - nowtime) + ' Until Nautical Twilight (' + min2hours(civilend ) + ')');
	} else if (nowtime  < civilend || civilend < 720) {	
		 update_text("ajaxastro", nowtext + ' - Civil Twilight');
		 update_text("ajaxtimetogo", min2hours(civilend - nowtime) + ' Until Nautical Twilight (' + min2hours(civilend ) + ')'); 
	} else if (nowtime  == civilend ) {	
		 update_text("ajaxastro", nowtext + ' - Nautical Twilight !');
		 update_text("ajaxtimetogo", min2hours(nautend - nowtime) + ' Until Astronomical Twilight (' + min2hours(nautend ) + ')'); 
	} else if (nowtime  < nautend || nautend < 720) {	
		 update_text("ajaxastro", nowtext + ' - Nautical Twilight');
		 update_text("ajaxtimetogo", min2hours(nautend - nowtime) + ' Until Astronomical Twilight (' + min2hours(nautend ) + ')'); 
	} else if (nowtime  == nautend ) {	
		 update_text("ajaxastro", nowtext + ' - Astronomical Twilight !');
		 update_text("ajaxtimetogo", min2hours(midnight - nowtime) + ' Until Solar Midnight (' + min2hours(midnight ) + ')'); 
	} else if (nowtime < astroend || astroend < 720) {	
		 update_text("ajaxastro", nowtext + ' - Astronomical Twilight'); 
		 if (midnight < 720) {
			update_text("ajaxtimetogo", min2hours(midnight+1440 - nowtime ) + ' Until Solar Midnight (' + min2hours(midnight ) + ')'); 
		 } else {
			update_text("ajaxtimetogo", min2hours(midnight  - nowtime ) + ' Until Solar Midnight (' + min2hours(midnight ) + ')'); 
		 }
	} else if (midnight  < 720 && nowtime  > astroend ) {	  // This is when transit + 12 hrs is after midnight
		 update_text("ajaxastro", nowtext + ' - Night Time');
		 update_text("ajaxtimetogo", min2hours(midnight+1440 - nowtime) + ' Until Solar Midnight (' + min2hours(midnight ) + ')'); 
	} else if (nowtime  == midnight ) {	                 // This is for when transit + 12 is prior to midnight
		 update_text("ajaxastro", nowtext + ' - Solar Midnight !');
		 update_text("ajaxtimetogo", min2hours(astrostart+1440 - nowtime) + ' Until Astronomical Twilight (' + min2hours(astrostart ) + ')');
	} else {
		 update_text("ajaxastro", nowtext + ' - Night Time'); 
		 update_text("ajaxtimetogo", min2hours(astrostart+1440 - nowtime) + ' Until Astronomical Twilight (' + min2hours(astrostart ) + ')');
	}
	update_graph("sunpathgraph","sunpath.php"+"?"+new Date());   // This updates a graph on my page.  Won't hurt if it stays here (or comment it out)
	update_graph("sunmoongraph","sunmoon.php"+"?"+new Date());   // This updates a graph on my page.  Won't hurt if it stays here (or comment it out)
	update_graph("jozef","jozef.php"+"?"+new Date());   // This updates a graph on my page.  Won't hurt if it stays here (or comment it out)
	
	setTimeout(update_sun_text, updtText );
}

function update_text(name, value ) {               

		var element = document.getElementById(name);
		if (! element ) { return; }
		var lastobs = element.getAttribute("lastobs");
		element.setAttribute("lastobs",value);
		if (unescape(value) != unescape(lastobs)) {
		  element.innerHTML =  value;
		  
		}
}

function update_graph(name, value ) {
		var element = document.getElementById(name);
		if (! element ) { return; }  
		element.src = value;		
}

function min2hours(minutes) {
	hours = minutes/60;
	hours = Math.floor(hours);
	minutes = minutes - (hours * 60); 
	if (minutes < 10){
		minutes = "0" + minutes;
	}
	if (hours == 24) {
		hours = "00";	
	}	
	if (hours < 10){
		hours = "0" + hours;
	}
	return hours + ":" + minutes;
}


setup_sunchart();
