// Written by James Thiele
// jet@eskimo.com
// http://www.eskimo.com/~jet/javascript/moonphase

function showMoonDyn(a,lng) {
var height = 1
var size = a/2+1
var lg = a*1.102
var hlg=-lg/2
var i
var lang1  = (lng == "it") ? "Luna Piena<br>tra circa " : "Next Full Moon<br>in about "
var lang2  = (lng == "it") ? " giorn" : " day"
var lang3  = (lng == "it") ? "i" : "s"
var lang4  = (lng == "it") ? "o" : ""
var currentDate  = new Date()

// Convert it to GMT
	currentDate.setTime(currentDate.getTime() + (currentDate.getTimezoneOffset()*60000))

// Compute length of lunar period -- source: World Almanac
var lunarPeriod  = 29*(24*3600*1000) + 12*(3600*1000) + 44.05*(60*1000)

// NOTE: months, hours, and minutes are 0 based
// var blueMoonDate = new Date(96, 1, 3, 16, 15, 0)
// Make a Date object for time of recent new moon
var blueMoonDate = new Date(Date.UTC(2002, 7, 8, 18, 14) - (lunarPeriod/2))
// 2002, 7, 8, 18, 14
 
// Get Date (GMT) for recent full moon

var moonPhaseTime = (currentDate.getTime() - blueMoonDate.getTime()) % lunarPeriod
// alert("Moon phase in days = "+moonPhaseTime/(24*3600*1000))

// Compute various percentages of lunar cycle
var percentRaw = (moonPhaseTime / lunarPeriod)
	// alert("% = "+percentRaw)
var percent    = Math.round(100*percentRaw) / 100
	// alert("% = "+percent)
var percentBy2 = Math.round(200*percentRaw)

var left  = (percentRaw >= 0.5) ? "images/singleblack.gif" : "images/singlewhite.gif"
var right = (percentRaw >= 0.5) ? "images/singlewhite.gif" : "images/singleblack.gif"

document.write("<div align=center style='position:relative; padding:2px; height:"+(lg+15)+"px'>",
"<div style='position:relative; float:left; margin-left:auto'>",
"<div id='moontable2' style='position: absolute; top: 50%; left: 50%; background-color: transparent; overflow: hidden; display: block; width:"+lg+"px; height:"+lg+"px; margin:-15 0 0 "+hlg+"'>",
"<img src='images/full_moon.png' width="+lg+" height="+lg+"><div id='moonimg' align=center style='position: absolute; left: 4.45%; top: 4.45%; z-index:+200'>");

	if (percentBy2 > 100) {
		percentBy2 = percentBy2 - 100;
		}

	for (i = -(size-1); i < size; ++i) {
		var wid=2*parseFloat(Math.sqrt((size*size)-(i*i)));
		if (percentBy2 != 100)
			document.write ("<img src="+left +" height=1 width="+(wid*((100-percentBy2)/100))+">");
		if (percentBy2 != 0)
			document.write ("<img src="+right+" height=1 width="+(wid*((percentBy2)/100))+">");
		document.write ("<br>");
	} // for

document.write(	"</div></div><div style='position:relative; top:"+(lg+5)+"px; width:auto'>",
			lang1,
			Math.round((lunarPeriod-moonPhaseTime)/(24*3600*1000)),
			lang2,
			Math.round((lunarPeriod-moonPhaseTime)/(24*3600*1000)) != 1 ? lang3 : lang4,
			"</div></div>");
document.write("<br></div>");

}
