function openCalendar(callback, year, month) {
	var	d=new Date()
			,data={
				callback:callback
				,year:(year?year:d.getFullYear())
				,month:(month?month-1:d.getMonth())
				,win:window.open(
					'about:blank',
					'_blank',
					'width=200,height=200,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,toolbar=no'
				)
				,months:new Array('Januar', 'Februar', 'M&auml;rz', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember')
				,weekdays:new Array('So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa')
				,day_offset:1 /* !!! 0<=day_offset<=6 !!! */
			}
	;
	writeCalendar(data);
}

function resizeCalendar(data) {
	if( navigator.appName.match(/(netscape|microsoft)/i) ) {
		var win=data.win
				,body=win.document.getElementsByTagName('body')[0]
				,fS
		;
		switch(RegExp.$1.toLowerCase()) {
		case 'netscape':
			if( parseInt(navigator.appVersion)>=5 )
				fS=Math.round(Math.min(win.innerWidth, win.innerHeight)/16);
			break;
		case 'microsoft':
			if( parseInt(navigator.appVersion)>=4 )
				fS=Math.round(Math.min(win.document.body.offsetWidth, win.document.body.offsetHeight)/16);
			break;
		}
		if(body && fS && (fS=''+fS+'px', body.style.fontSize!=fS) ) body.style.fontSize=fS;
	}
}

function rewriteCalendar(data, mode, val) {
	switch(mode) {
	case 0:
		data.month+=val;
		data.year+=Math.floor(data.month/12);
		data.month=(data.month<0?12:0)+data.month%12;
		break;
	case 1:
		data.year=val;
		break;
	case 2:
		data.month=val;
		break;
	}
	writeCalendar(data);
}

function writeCalendar(data) {
	var	doc=data.win.document
			,callback=data.callback
			,year=data.year
			,month=data.month
			,date=new Date(year, month, 1)
			,day_offset=data.day_offset
			,now=new Date()
			,weekdays=data.weekdays
			,months=data.months
	;
	doc.open();
	data.win.data=data;

	// html header
	doc.writeln('<html>'
							+'<head>'
							+'<link rel="stylesheet" type="text/css" href="/scripts/module/calendar.css">'
							+'</head>'
	);

	// body, form, table
	doc.writeln('<body onLoad="window.setInterval(\'window.opener.resizeCalendar(data)\', 500)">'
							+'<table cellspacing="1" cellpadding="0" border="0">'
							+'<form>'
	);

	// year pager
	var year_select='<select onChange="window.opener.rewriteCalendar(data, 1, parseInt(options[options.selectedIndex].text))">'
	for(var y=year-5; y<=year+5; y++) year_select+='<option '+(y==year?' selected':'')+'>'+y+'</option>';
	year_select+='</select>';
	doc.writeln(
		'<tr>'
		+'<th><a href="javascript:window.opener.rewriteCalendar(data, 0, -12)">&lt;</a></th>'
		+'<th colspan="5">' +year_select+ '</th>'
		+'<th><a href="javascript:window.opener.rewriteCalendar(data, 0, 12)">&gt;</a></th>'
		+'</tr>'
	);

	// month pager
	var month_select='<select onChange="window.opener.rewriteCalendar(data, 2, options.selectedIndex)">';
	for(var m=0; m<months.length; m++) month_select+='<option '+(m==month?' selected':'')+'>'+months[m]+'</option>';
	month_select+='</select>';
	doc.writeln(
		'<tr>'
		+'<th><a href="javascript:window.opener.rewriteCalendar(data, 0, -1)">&lt;</a></th>'
		+'<th colspan="5">' +month_select+ '</th>'
		+'<th><a href="javascript:window.opener.rewriteCalendar(data, 0, 1)">&gt;</a></th>'
		+'</tr>'
	);

	// week days
	doc.write('<tr>');
	for(var i=0; i<7; i++) doc.write('<th>'+weekdays[(i+day_offset)%7]+'</th>');
	doc.writeln('</tr>');

	// days of month
	doc.write('<tr>');
	date.setDate( date.getDate() + day_offset - date.getDay() );
	if( date.getMonth()==month ) date.setDate( date.getDate() - 7 );
	for(var i=0; i<42; date.setDate(date.getDate()+1), i++) {
		if(date.getMonth()==month) {
			doc.writeln(
				'<td'+(now.getFullYear()==year&&now.getMonth()==month&&now.getDate()==date.getDate()?' class="today"':'')+'>'
				+'<a'+(date.getDay()==0?' class="sunday"':'')+' href="javascript:window.opener[data.callback](data.win, '+year+', '+(1+month)+', '+date.getDate()+')">' +date.getDate()+ '</a>'
				+'</td>'
			);
		} else {
			doc.writeln('<td style="padding:1px">&nbsp;</td>');
		}
		if(i%7==6) doc.writeln('</tr>');
	}

	// end of table, form and body
	doc.writeln('</form>'
							+'</table>'
							+'</body>'
	);

	// end of html
	doc.writeln('</html>');
	doc.close();
	doc.title='Kalender';
	resizeCalendar(data);
}

