var calendar = {

	selectDay: function (objId, strId, arrDate)	{

		var strDay = ( arrDate.day < 10 ) ? '0' + arrDate.day : arrDate.day;
		var strMonth = ( arrDate.month.numb < 10 ) ? '0' + arrDate.month.numb : arrDate.month.numb;
		
		var strDate = arrDate.year + '.' + strMonth + '.' + strDay;
		
		var objP = document.getElementById('div' + strId + 'View').getElementsByTagName('p')[0];
		objP.innerHTML = strDate + ' ';
		objP.innerHTML += '<a href="javascript:;" onclick="user.element.edit(\'' + strId + '\');">' + gt.gettext("Bearbeiten") + '</a>';
	
		var arrTdList = document.getElementById('dtm' + strId).getElementsByTagName('td');
		var intTdListCount = arrTdList.length;
		
		for(var intCount = 0; intCount < intTdListCount; intCount++)	{
			arrTdList[intCount].style.border = '';
		}

		document.getElementById(strId).value = strDate;
		objId.style.border = objId.style.border == '' ? '1px solid black' : '';
	
	}, // end selectDay
	get: function (arrTimeList, strId)	{

		// add todays date if no date has been passed -> which means only zeros were in the database for the date
		if (isNaN(arrTimeList.month.numb) || isNaN(arrTimeList.year) || isNaN(arrTimeList.day)) {
			var now_is_now = new Date();
			
			arrTimeList.day = now_is_now.getDay();
			arrTimeList.month.numb = now_is_now.getMonth();
			arrTimeList.year = now_is_now.getFullYear();
		}
		
		var arrWeekdayList = new Array(
				gt.gettext("Mo"), 
				gt.gettext("Di"), 
				gt.gettext("Mi"), 
				gt.gettext("Do"), 
				gt.gettext("Fr"), 
				gt.gettext("Sa"), 
				gt.gettext("So"));
				
		var arrMonthList = new Array(
				gt.gettext("Januar"), 
				gt.gettext("Februar"), 
				gt.gettext("M&auml;rz"), 
				gt.gettext("April"), 
				gt.gettext("Mai"), 
				gt.gettext("Juni"), 
				gt.gettext("Juli"), 
				gt.gettext("August"), 
				gt.gettext("September"), 
				gt.gettext("Oktober"), 
				gt.gettext("November"),  
				gt.gettext("Dezember"));
		var intMonth = arrTimeList.month.numb;
		if(typeof intMonth == 'string')	{
			var intMonth = intMonth.replace(/^(0+)/g, '');
		}	
		var strMonth = arrMonthList[intMonth];
		var dtmCurrent = new Date(arrTimeList.year, arrTimeList.month.numb, 1);
		var strDayOfWeek = dtmCurrent.getDay();
		
		if(strDayOfWeek == 0)	{
			strDayOfWeek = 6;
		}
		var strColspan = 7 - strDayOfWeek + 1;
		var strOutput;
		
		strOutput = '<table>';
		strOutput += '<colgroup>';
		for(var intCount = 0; intCount < 7; intCount++)	{
			strOutput += '<col width="30" />';
		}
		strOutput += '</colgroup>';
		strOutput += '<tr>';
		

		var strPreMonth = arrTimeList.month.numb - 1;
		if(strPreMonth == -1)	{
			strPreYear = arrTimeList.year - 1;
			strPreMonth = 11;
		}
		else	{
			strPreYear = arrTimeList.year;
			strPreMonth = arrTimeList.month.numb - 1;
		}
		strOutput += '<td align="center"><a href="javascript:;" onclick="calendar.get( { month: { numb:' + strPreMonth + ' }, year:' + strPreYear + ', day:' + arrTimeList.day + '}, \'' + strId + '\' )">&lt;</a></td>';
		strOutput += '<td align="center" colspan="5">' + strMonth + '&nbsp;' + arrTimeList.year + '</td>';
		
		var strNextMonth = arrTimeList.month.numb + 1;
		if(strNextMonth == 12)	{
			strNextYear = arrTimeList.year + 1;
			strNextMonth = 0;
		}
		else	{
			var strNextYear = arrTimeList.year;
			strNextMonth = arrTimeList.month.numb + 1;
		}
		strOutput += '<td align="center"><a href="javascript:;" onclick="calendar.get( { month: { numb:' + strNextMonth + ' }, year:' + strNextYear + ', day:' + arrTimeList.day + '}, \'' + strId + '\' )">&gt;</a></td>';
		strOutput += '</tr>';
		strOutput += '<tr>';
		for(var intCount = 0; intCount < 7; intCount++)	{
			strOutput += '<td align="center"><b>' + arrWeekdayList[intCount] + '</b></td>';
		}
		strOutput += '</tr>';
		for(intCount = 1; intCount < 32; intCount++)	{
			
			if(intCount == 1)	{
				strOutput += '<tr>';
				if(strDayOfWeek != 1)	{
					var colspan = strDayOfWeek - 1;
					if(colspan < 0) colspan = 6;
					strOutput += '<td colspan="' + colspan + '">&nbsp;</td>';
				}
			}
			
			if(arrTimeList.day == intCount)	{
				strOutput += '<td align="center" onclick="calendar.selectDay(this, \'' + strId + '\', { month: { numb:' + strNextMonth+ ' }, year:' + arrTimeList.year + ', day: ' + intCount + ' });" style="border:1px solid black;">' + intCount + '</td>';
			}
			else	{
				strOutput += '<td align="center" class="calendar_day" onclick="calendar.selectDay(this, \'' + strId + '\', { month: { numb:' + strNextMonth + ' }, year:' + arrTimeList.year + ', day: ' + intCount + ' });">' + intCount + '</td>';
			}
			
			
			if(strColspan == intCount) strOutput += "</tr><tr>";
			if(strColspan+7 == intCount) strOutput += "</tr><tr>";
			if(strColspan+14 == intCount) strOutput += "</tr><tr>";
			if(strColspan+21 == intCount) strOutput += "</tr><tr>";
			if(strColspan+28 == intCount) strOutput += "</tr><tr>";
			 
		}
		
		strOutput += '</tr>'
		strOutput += '</table>';
		document.getElementById('dtm' + strId).innerHTML = strOutput;
		
	}, // end get
	projectroom : {
		toggle : function (cal_div_id) {
			$("#" + cal_div_id).slideToggle('slow');
		},
		
		
		/**
		 * 
		 * @param {DOM-Obj} objId - TD-Element of the month table of a day
		 * @param {String} strId 
		 * @param {} arrDate
		 */
		selectDay: function (objId, strId, arrDate)	{
			if (jQuery('#dtm' + strId).length > 0) {
				var strDay = ( arrDate.day < 10 ) ? '0' + arrDate.day : arrDate.day;
				
				var strMonth = ( arrDate.month.numb < 10 ) ? '0' + arrDate.month.numb : arrDate.month.numb;
				
				var strDate = strDay + '.' + strMonth + '.' + arrDate.year;
				var objP = document.getElementById('div' + strId + 'View').getElementsByTagName('p')[0];
							
				if(objP){
					objP.innerHTML = strDate + ' ';
					objP.innerHTML += '<a href="javascript:;" onclick="calendar.projectroom.toggle(\'dtm' + strId + '\');">' + gt.gettext("Ändern") + '</a>';
				}
			
				var arrTdList = document.getElementById('dtm' + strId).getElementsByTagName('td');
				var intTdListCount = arrTdList.length;
				
				for(var intCount = 0; intCount < intTdListCount; intCount++)	{
					arrTdList[intCount].style.border = '';
				}
		
				document.getElementById(strId).value = strDate;
				
				if (objId) { // objId was passed as valid DOM element
					objId.style.border = objId.style.border == '' ? '1px solid black' : '';
					$("#dtm" + strId).slideToggle('slow');
				}
				else { // when no TD day elemen was passed, access this element with its id but it has to built before
					// this go like this
					var tdId = strId + arrDate.month.numb + arrDate.year + arrDate.day;
					// do a border around it
					jQuery('#' + tdId).css('border', '1px solid black');
				}
			}
			
		
		}, // end selectDay	
		get: function (arrTimeList, strId)	{
	
			// add todays date if no date has been passed -> which means only zeros were in the database for the date
			if (isNaN(arrTimeList.month.numb) || isNaN(arrTimeList.year) || isNaN(arrTimeList.day)) {
				var now_is_now = new Date();
				
				arrTimeList.day = now_is_now.getDay();
				arrTimeList.month.numb = now_is_now.getMonth();
				arrTimeList.year = now_is_now.getFullYear();
			}
			
			
			
			var arrWeekdayList = new Array(
					gt.gettext("Mo"), 
					gt.gettext("Di"), 
					gt.gettext("Mi"), 
					gt.gettext("Do"), 
					gt.gettext("Fr"), 
					gt.gettext("Sa"), 
					gt.gettext("So"));
					
			var arrMonthList = new Array(
					gt.gettext("Januar"), 
					gt.gettext("Februar"), 
					gt.gettext("M&auml;rz"), 
					gt.gettext("April"), 
					gt.gettext("Mai"), 
					gt.gettext("Juni"), 
					gt.gettext("Juli"), 
					gt.gettext("August"), 
					gt.gettext("September"), 
					gt.gettext("Oktober"), 
					gt.gettext("November"),  
					gt.gettext("Dezember"));

			var intMonth = arrTimeList.month.numb;

			if(typeof intMonth == 'string')	{
				var intMonth = intMonth.replace(/^(0+)/g, '');
			}	
			
			var strMonth = arrMonthList[intMonth-1];
			var dtmCurrent = new Date(arrTimeList.year, arrTimeList.month.numb, 1);
			var strDayOfWeek = dtmCurrent.getDay();
			
			if(strDayOfWeek == 0)	{
				strDayOfWeek = 6;
			}
			var strColspan = 7 - strDayOfWeek + 1;
			var strOutput;
			
			strOutput = '<table>';
			strOutput += '<colgroup>';
			for(var intCount = 0; intCount < 7; intCount++)	{
				strOutput += '<col width="30" />';
			}
			strOutput += '</colgroup>';
			strOutput += '<tr>';
			
	
			var strPreMonth = arrTimeList.month.numb - 1;
			if(strPreMonth <= 0)	{
				strPreYear = arrTimeList.year - 1;
				strPreMonth = 12;
			}
			else	{
				strPreYear = arrTimeList.year;
				strPreMonth = arrTimeList.month.numb - 1;
			}
			strOutput += '<td align="center"><a href="javascript:;" onclick="calendar.projectroom.get( { month: { numb:' + strPreMonth + ' }, year:' + strPreYear + ', day:' + arrTimeList.day + '}, \'' + strId + '\' )">&lt;</a></td>';
			strOutput += '<td align="center" colspan="5">' + strMonth + '&nbsp;' + arrTimeList.year + '</td>';
			
			
			
			var strNextMonth = arrTimeList.month.numb + 1;

			if(strNextMonth >= 12)	{
				strNextYear = arrTimeList.year + 1;
				strNextMonth = 1;
			}
			else	{
				var strNextYear = arrTimeList.year;
				strNextMonth = arrTimeList.month.numb + 1;
			}
			strOutput += '<td align="center"><a href="javascript:;" onclick="calendar.projectroom.get( { month: { numb:' + strNextMonth + ' }, year:' + strNextYear + ', day:' + arrTimeList.day + '}, \'' + strId + '\' )">&gt;</a></td>';
			strOutput += '</tr>';
			strOutput += '<tr>';
			for(var intCount = 0; intCount < 7; intCount++)	{
				strOutput += '<td align="center"><b>' + arrWeekdayList[intCount] + '</b></td>';
			}
			
			strOutput += '</tr>';
			for(intCount = 1; intCount < 32; intCount++)	{
				
				if(intCount == 1)	{
					strOutput += '<tr>';
					if(strDayOfWeek != 1)	{
						var colspan = strDayOfWeek - 1;
						if(colspan < 0) colspan = 6;
						strOutput += '<td colspan="' + colspan + '">&nbsp;</td>';
					}
				}
				
				if(arrTimeList.day == intCount)	{
					strOutput += '<td id="'+ strId + intMonth + arrTimeList.year + intCount + '" align="center" onclick="calendar.projectroom.selectDay(this, \'' + strId + '\', { month: { numb:' + intMonth + ' }, year:' + arrTimeList.year + ', day: ' + intCount + ' });" style="border:1px solid black;">' + intCount + '</td>';
				}
				else	{
					strOutput += '<td id="'+ strId + intMonth + arrTimeList.year + intCount + '" align="center" class="calendar_day" onclick="calendar.projectroom.selectDay(this, \'' + strId + '\', { month: { numb:' + intMonth + ' }, year:' + arrTimeList.year + ', day: ' + intCount + ' });">' + intCount + '</td>';
				}
				
				
				if(strColspan == intCount) strOutput += "</tr><tr>";
				if(strColspan+7 == intCount) strOutput += "</tr><tr>";
				if(strColspan+14 == intCount) strOutput += "</tr><tr>";
				if(strColspan+21 == intCount) strOutput += "</tr><tr>";
				if(strColspan+28 == intCount) strOutput += "</tr><tr>";
				 
			}
			
			strOutput += '</tr>'
			strOutput += '</table>';
			if (jQuery('#dtm' + strId).length > 0) {
				document.getElementById('dtm' + strId).innerHTML = strOutput;
			}
			
		} // end get
	
	}

};
