window.onload=function() {
	if(location.href.match(/\/it\//)) {
		days=['lu','ma','me','gi','ve','sa','do'];
		months=['gen','feb','mar','apr','mag','giu','lug','ago','set','ott','nov','dic'];
	}
	else {
		days=['Mo','Tu','We','Th','Fr','Sa','Su'];
		months=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
	}
	
	setupCalendar(document.getElementById('calendar'));
}

var days;
var months;
var today, realToday;

var isplus1900=(new Date(2000,1,1)).getYear()<1900; //some browsers want and give current year-1900, IE obviously not...

function setupCalendar(div) {
	//the days heading
	var line=document.createElement('div');
	line.className='dline';
	var caps=document.createElement('div'); //the rounds at the edges of the lines
	caps.className='lcap';
	line.appendChild(caps);
	var cell;
	for(i=0; i<days.length; ++i) {
		cell=document.createElement('div');
		cell.className='day';
		cell.appendChild(document.createTextNode(days[i]));
		line.appendChild(cell);
	}
	caps=document.createElement('div');
	caps.className='rcap';
	line.appendChild(caps);
	div.appendChild(line);
	
	today=new Date(); //the date object that changes when changing month and year
	realToday=new Date(); //today kept as a reference
	line=document.createElement('div');
	updateView(line);
	line.id='calview';
	div.appendChild(line);
	
	//month chooser
	cell=document.createElement('a');
	cell.href='#';
	cell.className='chgm';
	cell.onclick=monthMinus;
	div.appendChild(cell);
	cell=document.createElement('div');
	cell.id='curm';
	cell.appendChild(document.createTextNode(months[today.getMonth()]));
	div.appendChild(cell);
	cell=document.createElement('a');
	cell.href='#';
	cell.className='chgp';
	cell.onclick=monthPlus;
	div.appendChild(cell);
	
	//return to this month
	cell=document.createElement('a');
	cell.href='#';
	cell.className='ret';
	cell.onclick=goToday;
	div.appendChild(cell);
	
	//year chooser
	cell=document.createElement('a');
	cell.href='#';
	cell.className='chgm';
	cell.onclick=yearMinus;
	div.appendChild(cell);
	cell=document.createElement('div');
	cell.id='cury';
	cell.appendChild(document.createTextNode((isplus1900?1900:0)+today.getYear()));
	div.appendChild(cell);
	cell=document.createElement('a');
	cell.href='#';
	cell.className='chgp';
	cell.onclick=yearPlus;
	div.appendChild(cell);
}

//returns the last day of the selected month
function getMonthEnd(month,year) {
	var d;
	if(month==11)
		d=new Date((isplus1900?1900:0)+year+1,0,1);
	else
		d=new Date((isplus1900?1900:0)+year,month+1,1);
	d.setTime(d.getTime()-3600000);
	
	return d.getDate();
}

//called by the arrow that augments the months
function monthPlus() {
	if(today.getMonth()==11) {
		today.setMonth(0);
		yearPlus();
	}
	else
		today.setMonth(today.getMonth()+1);
	document.getElementById('curm').firstChild.nodeValue=months[today.getMonth()];
	updateView(document.getElementById('calview'));
	return false;
}

//called by the arrow that decrements the months
function monthMinus() {
	if(today.getMonth()==0) {
		today.setMonth(11);
		yearMinus();
	}
	else
		today.setMonth(today.getMonth()-1);
	document.getElementById('curm').firstChild.nodeValue=months[today.getMonth()];
	updateView(document.getElementById('calview'));
	return false;
}

//called by the arrow that augments the years
function yearPlus() {
	today.setYear(today.getYear()+1+(isplus1900 || today.getYear()<2000?1900:0));
	document.getElementById('cury').firstChild.nodeValue=(today.getYear()+(isplus1900 || today.getYear()<1900?1900:0));
	updateView(document.getElementById('calview'));
	return false;
}

//called by the arrow that decrements the years
function yearMinus() {
	today.setYear(today.getYear()-1+(isplus1900 || today.getYear()<2000?1900:0));
	document.getElementById('cury').firstChild.nodeValue=(today.getYear()+(isplus1900 || today.getYear()<1900?1900:0));
	updateView(document.getElementById('calview'));
	return false;
}

function goToday() {
	today.setMonth(realToday.getMonth());
	today.setYear(realToday.getYear()+(isplus1900?1900:0));
	document.getElementById('curm').firstChild.nodeValue=months[today.getMonth()];
	document.getElementById('cury').firstChild.nodeValue=(today.getYear()+(isplus1900?1900:0));
	updateView(document.getElementById('calview'));
	return false;
}

//creates the calendar
function updateView(div) {
	while(div.firstChild)
		div.removeChild(div.firstChild);
	
	var line, caps, cell;
	end=getMonthEnd(today.getMonth(),today.getYear());
	first=new Date(today.getYear()+(isplus1900?1900:0),today.getMonth(),1);
	num=first.getDay()-1;
	if(num==-1)
		num=6;
	num=-num+1; //leaves the initial cells empty
	for(i=1; i<=6; ++i) { //always makes 6 lines
		line=document.createElement('div');
		line.className='line';
		caps=document.createElement('div');
		caps.className='lcap';
		line.appendChild(caps);
		for(j=0; j<days.length; ++j, ++num) {
			cell=document.createElement('div');
			if(num==realToday.getDate() && today.getMonth()==realToday.getMonth() && today.getYear()==realToday.getYear()) {
				cell.className='cell today';
				//the red circle
				caps=document.createElement('div');
				caps.className='circle';
				caps.style.left=(22*j+7)+'px';
				line.appendChild(caps);
			}
			else
				cell.className='cell';
			//leaves the cell empty if the day is not between the range
			if(num>0 && num<=end)
				cell.appendChild(document.createTextNode(num));
			line.appendChild(cell);
		}
		caps=document.createElement('div');
		caps.className='rcap';
		line.appendChild(caps);
		div.appendChild(line);
	}
}