function calendar_renderer(calendar)
{
	this.table;
	this.thead;
	this.tbody;
	
	this.calendar = calendar;
	
	// this.anreisetag;
	this.anreisetage;
	this.zimmerbelegung;
	this.zimmerbelegung_liste;
	
	this.render = function(items)	{
		
		// var aT = document.getElementById('value_anreisetag');
		// this.anreisetag = (aT ? parseInt(aT.value) : 0);
		var aT = document.getElementById('value_anreisetage');
		if(aT) {
			if(aT.value=="0" || aT.value=="") var aTListe = [];
			else var aTListe = aT.value.split(",");
		}
		// alert(aTListe.length);
		// this.anreisetag = (aT ? parseInt(aT.value) : 0);
		this.anreisetage = (is_array(aTListe) ? aTListe : []);

		this.zimmerbelegung = (typeof zimmerbelegung != "undefined" ? true : false);
		this.zimmerbelegung_liste = (this.zimmerbelegung ? zimmerbelegung_liste : []);
		// alert(getType(this.zimmerbelegung_liste));
		
		this.table = document.createElement('table');
		this.table.setAttribute('cellpadding', 0, 0);
		this.table.setAttribute('cellspacing', 0, 0);
		
		this.renderThead();
		
		this.renderTbody(items);
		
		if (this.calendar.htmlElement) {
			this.calendar.frame.replaceChild(this.table, this.calendar.htmlElement);
		} else {
			this.calendar.frame.appendChild(this.table);
		}
		
		return this.table;
	}
	
	this.renderThead = function()
	{
		var col, row, item, title;
		
		var from = new Date(this.calendar.config.range.from['year'], this.calendar.config.range.from['month'], this.calendar.config.range.from['day']);
		var to = new Date(this.calendar.config.range.to['year'], this.calendar.config.range.to['month'], this.calendar.config.range.to['day']);
		
		var self = this;
		
		
		
		var prevmonth = function()
		{
			self.calendar.prev();
		}

		var nextmonth = function()
		{
			self.calendar.next();
		}
		
		this.thead = document.createElement('thead');
		this.table.appendChild(this.thead);
		
		row = document.createElement('tr');
		this.thead.appendChild(row);
		
		
		
		row.className = 'navigate';
		
		col = document.createElement('td');
		row.appendChild(col);
		
		col.className = 'prevmonth';
		
		if (this.calendar.calc.firstdate(this.calendar.year, this.calendar.month).getTime() > from.getTime()) {
			item = document.createElement('a');
			col.appendChild(item);
			
			title = this.calendar.lang.date.months[this.calendar.calc.prevmonth(this.calendar.year, this.calendar.month).getMonth()] + ' ' + 
					this.calendar.calc.prevmonth(this.calendar.year, this.calendar.month).getFullYear();
			
			item.className = 'prev'
			
			item.setAttribute('href', 'javascript:void(0)', 0);
			item.setAttribute('title', title, 0);
			
			elementAddEventListener('mouseup', item, prevmonth, false);
			elementAddEventListener('click', item, prevmonth, false);
		}
		
		col = document.createElement('td');
		row.appendChild(col);
		
		col.className = 'thismonth';
		col.setAttribute('colspan', 5, 0);
		col.appendChild(document.createTextNode(this.calendar.lang.date.months[this.calendar.month] + ' ' + this.calendar.year));

		col = document.createElement('td');
		row.appendChild(col);

		col.className = 'nextmonth';
		
		if (this.calendar.calc.nextmonth(this.calendar.year, this.calendar.month).getTime() < to.getTime()) {
			item = document.createElement('a');
			col.appendChild(item);
			
			title = this.calendar.lang.date.months[this.calendar.calc.nextmonth(this.calendar.year, this.calendar.month).getMonth()] + ' ' + 
					this.calendar.calc.nextmonth(this.calendar.year, this.calendar.month).getFullYear();
			
			item.className = 'next'
			
			item.setAttribute('href', 'javascript:void(0)', 0);
			item.setAttribute('title', title, 0);
			
			elementAddEventListener('mouseup', item, nextmonth, false);
			elementAddEventListener('click', item, nextmonth, false);
		}
		
		/* weekdays */
		row = document.createElement('tr');
		this.thead.appendChild(row);
		
		row.className = 'weekdays';
		
		for (x in this.calendar.lang.date.days) {
			col = document.createElement('td');
			row.appendChild(col);
			
			item = document.createElement('div');
			col.appendChild(item);
			
			item.setAttribute('title', this.calendar.lang.date.days[x].fullname, 0);
			
			item.appendChild(document.createTextNode(this.calendar.lang.date.days[x].shortname));
		}
	}
	
	this.renderTbody = function(items)
	{
		var classes;
		var elements;
		var self = this;
		var item;
		var onclickhandler = function(item) {
			this.onclick = function(event) {
				self.calendar.setValue(item.getFullYear(), item.getMonth(), item.getDate(), event);
			}
		}
		
		this.tbody = document.createElement('tbody');
		this.table.appendChild(this.tbody);

		for (i = 0; i < items.length; i++) {
			classes = new Array();

			if (parseInt(i / 7) === i / 7) {
				row = document.createElement('tr');
				this.tbody.appendChild(row);
			}

			col = document.createElement('td');
			row.appendChild(col);

			if (this.calendar.calc.firstdate(this.calendar.year, this.calendar.month).getTime() > items[i].getTime()) {
				classes.push('prevmonth');
			} else if (this.calendar.calc.lastdate(this.calendar.year, this.calendar.month).getTime() < items[i].getTime()) {
				classes.push('nextmonth');
			} else {
				classes.push('thismonth');
			}

			if (this.calendar.curdate.getTime() == items[i].getTime()) {
				classes.push('thisdate');
			} else if (this.calendar.curdate.getTime() > items[i].getTime()) {
				classes.push('prevdate');
			}

			if (items[i].getDay() == 6 || items[i].getDay() == 0) {
				classes.push('weekend');
			}

			if (this.calendar.curdate.getTime() == items[i].getTime() && this.calendar.config.disallowthisdate) {
				classes.push('disallowthisdate');
			}
			
			elements = new Array();

			for (k = 0; k < classes.length; k++) {
				elements[k] = document.createElement('div');

				elements[k].className = classes[k];
				if (elements[k-1] != undefined) {
					elements[k-1].appendChild(elements[k]);
				}
			}

			if (this.calendar.curdate.getTime() > items[i].getTime() || (this.calendar.curdate.getTime() == items[i].getTime() && this.calendar.config.disallowthisdate)) {
				item = document.createElement('p');

			// nur Anreise belegen
			} else if (this.calendar.frame.id=='cal_date_anreise' && this.zimmerbelegung && in_array(Time2Date(items[i].getTime()), this.zimmerbelegung_liste)) {
				item = document.createElement('p');
				item.className = 'belegt';	
				// alert(Time2Date(items[i].getTime()));
				

			} else {
				item = document.createElement('a');
				item.setAttribute('href', 'javascript:void(0)');
				elementAddEventListener('click', item, new onclickhandler(items[i]).onclick, false);
				// Anreisetag OK
				// if(this.calendar.frame.id=='cal_date_anreise' && this.anreisetag > 0 && (items[i].getDay() == this.anreisetag || (items[i].getDay() == 0 && this.anreisetag == 7))) {
				if(this.calendar.frame.id=='cal_date_anreise' && this.anreisetage.length > 0 && ((items[i].getDay() != 0 && in_array(items[i].getDay(), this.anreisetage)) || (items[i].getDay() == 0 && in_array(7,this.anreisetage)))) {
					item.className = 'anreise_ok';				
				}
			}
			
			title = this.calendar.lang.date.days[items[i].getDay()].fullname + ', der ' + 
					items[i].getDate() + '. ' + 
					this.calendar.lang.date.months[items[i].getMonth()] + ' ' + 
					items[i].getFullYear();
					
			if (this.calendar.curdate.getTime() == items[i].getTime()) {
				title = 'Heute - ' + title;
			} else if(item.className == 'belegt') {
				title = 'Belegt - ' + title;	
			} else if(item.className == 'anreise_ok') {
				title = 'Anreisetag - ' + title;	
			}
			
			item.setAttribute('id', this.calendar.frame.id + '-' + items[i].getFullYear() + '-' + items[i].getMonth() + '-' + items[i].getDate());
			item.setAttribute('title', title, 0);

			item.appendChild(document.createTextNode(items[i].getDate()));

			elements[elements.length-1].appendChild(item);
			col.appendChild(elements[0]);
		}
	}
}