//alert("calendar.js");
var calendars = [];
var method;
function bind(object, method) {
	return function () {
		return method.apply(object,arguments);
	}
}
function Calendar( date, containerID, callback, until ){
	var index = calendars.length;
	var monatsName = _s('monatsNamen');
	var m = date.getMonth() + 1;
	var y = date.getFullYear();
	initDate(this,y,m,date.getDate());

	this.publish = function () {
		if (callback)
			callback( this.selectedDate );
	}
	
	this.SelectDate = function( y, m, d ) {
		initDate(this,y,m,d);
		this.showSelectedDate();
		this.publish();
	}

	this.showSelectedDate = function () {
		div = document.getElementById(containerID);
		div.innerHTML = '<div class="selectedDate" ' +
							'onclick="calendars['+index+'].showCal()">' + 
							this.selectedDate.localDate() + 
						"</div>";
	}

	this.prevMonth = function() {
		if ( m == 1 ) {
			m  = 12;
			y -= 1;
		}
		else
			m -= 1;
		this.buildCal();
	}

	this.nextMonth = function() {
		if ( m == 12 ) {
			m  = 1;
			y += 1;
		}
		else
			m += 1;
		this.buildCal();
	}

	this.buildCal = function (){
		var dim=[31,0,31,30,31,30,31,31,30,31,30,31];
		
		var oD = new Date(y, m-1, 1);
		oD.od  = oD.getDay() + 1;
		
		var scanforselected = ( y == this.selectedDate.getFullYear() && 
							    m == this.selectedDate.getMonth() + 1 ) 
							 		? this.selectedDate.getDate() 
							 		: 0;
		
		dim[1] = (
					(
						( oD.getFullYear() % 100 != 0 ) && 
						( oD.getFullYear() % 4 == 0 )
					) ||
					( oD.getFullYear() % 400 == 0)
				 ) 		? 29 
					 	: 28;
		var t='<table class="calendarTable" cellpadding="0" cellspacing="0">' +
					'<tr>' +
						'<td class="vorMonat" ' +
							'onclick="calendars['+index+'].previous()">&lt;</td>' +
						'<td colspan="5" class="calendarMonat" ' +
							'onclick="calendars['+index+'].showDate()">' +
							monatsName[m-1] + ' - ' + y +
						'</td>' +
						'<td class="nachMonat" ' +
							'onclick="calendars['+index+'].next()">&gt;</td>' +
					'</tr>' +
					'<tr>';
		for ( s = 0; s < 7; s++ )
			t += 		'<td class="calendarWochentag">' + 
							"SMDMDFS".substr(s,1) + 
						'</td>';
		t +=		'</tr><tr>';
		for( i = 1; i <= 42; i++ ) {
			var x = ( ( i - oD.od >= 0 ) && ( i - oD.od < dim[m-1] ) ) 
					? i - oD.od + 1 
					: '&nbsp;';
			var selectedDay = ( x == '&nbsp;' ) ? "" : "calendarTag";
			var click = ( x != '&nbsp;' ) 
				?'onclick="calendars['+index+'].select('+ y +','+ m +','+ x +')"'
				:'onclick="calendars['+index+'].showCal()"';
			if ( x == scanforselected )
				selectedDay += ' selected'
			t +=		'<td class="' + selectedDay + '" '+click+'>' +
							x +
						'</td>';
			if ( ( ( i ) % 7 == 0 ) && ( i < 36 ) )
				t += '</tr><tr>';
		}
		div = document.getElementById(containerID);
		div.innerHTML = t + '</tr>' + '</table>';
	}
	function initDate(that,y,m,d) {
		delete that.selectedDate;
		that.selectedDate = until 
			? new Date(y,m-1,d,23,59,59) 
			: new Date(y,m-1,d, 0, 0, 0);
	}
	calendars[index] = {
		showDate: bind(this, this.showSelectedDate),
		showCal: bind(this, this.buildCal),
		previous: bind(this, this.prevMonth),
		next: bind(this, this.nextMonth),
		select: bind(this, this.SelectDate)
	}
}

