//copyright 2007 assemblesoft. info@assemblesoft.com

function calendarInit()
{
	var els = getElementsByClassName( document, '*', 'date');
	removeChildren(els[0]);
	var dateSel = document.createElement('input');
	dateSel.type = 'text';
	dateSel.id = 'dateInput';
	dateSel.name = 'dateInput';
	dateSel.className += ' default';
	els[0].appendChild( dateSel);
	date = new calendarInput( dateSel);
}

Date.prototype.getRealYear = function()
{
	return (this.getYear() < 1000) ? this.getYear()+1900 : this.getYear()
}

function padout(number) { return (number < 10) ? '0' + number : number; }

var now = new Date();
// Diese Daten sind jetzt in travis.php definiert, weil sprachabhängig!
// var date_months		= ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',	'September', 'October',	'November',	'December'];
// var date_dayNames	= ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
var	date_dayCounts	= [	31,		   28,		  31,	   30,		31,	   30,	   31,	   31,		 30,		  31,		 30,		 31	];
var	date_years		= [ now.getFullYear()-10, now.getFullYear()-9, now.getFullYear()-8, now.getFullYear()-7, now.getFullYear()-6, now.getFullYear()-5, now.getFullYear()-4, now.getFullYear()-3, now.getFullYear()-2, now.getFullYear()-1, now.getFullYear() ];

function calendarInput(input)
{
	var inp = input;
	var setdate = new Date(0);
	var viewdate = new Date();
	var dropdown = null;
	var cal = null;
	var monthsel, yearsel;
	
	input.defaultValue='DD.MM.YYYY'; input.value='DD.MM.YYYY';
	input.className+=' default';	
	input.maxLength=10;
	input.onfocus = function() {
		if(inp.value == inp.defaultValue) {
			inp.value = '';
			inp.className=inp.className.replace(/ ?default\b/,'');
		}
		createDropdown();
	}
	input.onclick = function() {
		if(inp.value == inp.defaultValue) {
			inp.value = '';
			inp.className=inp.className.replace(/ ?default\b/,'');
		}
		createDropdown();
	}
	input.onblur = function() {
		setTimeout(function() {
			if(inp.value == '') {
				if (!inp.className.match(/ ?default\b/))
					inp.className += ' default';
				inp.value = inp.defaultValue;
			}
		}, 300);
	}
	input.onkeyup = function(evt) {
		parseDate();
		return true;
	}
	input.onkeydown = function(evt) {
		evt = evt || window.event;
		if(evt.keyCode == 9) {
			killDropdown();
			return true;
		}
	}
	
	function parseDate() {
		var parseDate = inp.value.split('.');
		if((parseDate[2] >= date_years[2]) && (parseDate[2] <= date_years[date_years.length-1]))
		{
			viewdate.setYear(parseDate[2]);
			if((parseDate[1] > 0) && (parseDate[1] < 13)) {
				viewdate.setMonth(parseDate[1]-1);
				if((parseDate[0] > 0) && (parseDate[0] < date_dayCounts[viewdate.getMonth()])) {
					setdate.setYear(parseDate[2]);
					setdate.setMonth(parseDate[1]-1);
					setdate.setDate(parseDate[1]);
				}
				else {setdate.setDate(0);}
			}
			else {setdate.setDate(0);}
		}
		else {setdate.setDate(0);}
		update();
	}
	
	function update() {
		if(dropdown == null) {
			return false;
		}
		monthsel.selectedIndex = viewdate.getMonth();
		for (i=0; i<date_years.length; i++) {
			if(viewdate.getRealYear() == date_years[i])
				yearsel.selectedIndex = i;
		}
		render();
	}
	
	function dayCell(text) {
		var	label =	text ||	' ';
		var	cell = document.createElement('td');
		cell.date = padout(text) + '.' + padout(viewdate.getMonth()+1) + '.' + viewdate.getRealYear();
		if (text){
			cell.onmouseover = function(){cell.className+=' hover'}
			cell.onmouseout = function(){cell.className=cell.className.replace(/ ?hover\b/,'')}
			cell.onclick = function(){
				inp.className=inp.className.replace(/ ?default\b/,'');
				inp.value=cell.date;
				setdate.setYear(viewdate.getRealYear());
				setdate.setMonth(viewdate.getMonth());
				setdate.setDate(text);
				killDropdown();
			}
			cell.title = cell.date;
		}
		cell.appendChild(document.createTextNode(label))
		var tmpdate = new Date(viewdate);
		tmpdate.setDate(text);
		if ((tmpdate.getDay() == 0) || (tmpdate.getDay() == 6)) {
			cell.className+= ' weekend';
		}
		if (now.getFullYear() == viewdate.getFullYear() && now.getMonth() == viewdate.getMonth() && now.getDate() == text) {
			cell.className+= ' today'
// Wegen Mehrsprachigkeit jetzt in travis.php vorbelegt
//			cell.title += ' (Today)';
			cell.title += date_Today;
		}
		if (setdate.getFullYear() == viewdate.getFullYear()	&& setdate.getMonth() == viewdate.getMonth() &&	setdate.getDate() == text) {
			cell.className+= ' selected'
			cell.title += ' (Selected)';
		}

		// Tage, fuer die Noise Events vorliegen, werden fett dargestellt
		// Muss man hier noch die Zeitzone des Flughafens in getTime benutzen?
		var tmpStartDay = new Date( tmpdate.getFullYear(), tmpdate.getMonth(), tmpdate.getDate(), 0, 0, 0);
		var tmpEndDay = new Date( tmpdate.getFullYear(), tmpdate.getMonth(), tmpdate.getDate(), 23, 59, 59);
		var tmpNumStartDay = tmpStartDay.getTime() / 1000.0;
		var tmpNumEndDay = tmpEndDay.getTime() / 1000.0;
		if ( startFirstNoiseEvent > 0 && endLastNoiseEvent > 0 && tmpNumEndDay >= startFirstNoiseEvent && tmpNumStartDay <= endLastNoiseEvent)
		{
			cell.className+= ' fett'
		}
		tmpStartDay=null; tmpEndDay=null; tmpNumStartDay=null; tmpNumEndDay=null; tmpdate=null; label=null;

		return cell
	}
	
	function render() {
		if (cal != null && cal.parentNode != null)
		{
			cal.parentNode.removeChild(cal);
		}
		
		cal = document.createElement('table');
		cal.cellSpacing=0;
		var row = document.createElement('tr');
		var head = document.createElement('thead');
		
		for(i=0; i<date_dayNames.length; i++) {
			day = document.createElement('th');
			day.appendChild(document.createTextNode(date_dayNames[i].substr(0,3)));
			row.appendChild(day);
		}
		head.appendChild(row);
		cal.appendChild(head);

		if(cal.getElementsByTagName('tbody').length > 0)
			cal.removeChild(cal.getElementsByTagName('tbody')[0]);
		body = document.createElement('tbody');
		cells = [];

		if (now.getDay() < 7){
			var	firstDayOfMonth	= new Date(viewdate);
			firstDayOfMonth.setDate(0);


			for	(var i=1; i<=firstDayOfMonth.getDay(); i++){
				cells.push(dayCell())
			}
		}
		var days = date_dayCounts[viewdate.getMonth()];

		// Feb has 29 days in leap years
		if ( viewdate.getMonth() == 1 && viewdate.getFullYear() % 4 == 0) { days++; }

		for	(var i=0; i<days; i++){
			cells.push(dayCell(i+1))
		}
	
		while(cells.length > 0){
			var	row	= document.createElement('tr');
			for	(var i=0; i<7; i++){
				var	cell = cells.length>0 ?	cells.shift() :	dayCell();
				row.appendChild(cell)
			}
			body.appendChild(row)
		}
		
		body.appendChild(row);
		cal.appendChild(body);
		dropdown.appendChild(cal);
	}
	
	
	function killDropdown() {
		if (dropdown != null) {
			if (dropdown.parentNode)
				dropdown.parentNode.removeChild(dropdown);
				
			dropdown = null;
			
			DetachEvent(document,'mousedown',killDropdown,false);
		}
	}
	
	function createDropdown()
	{
		killDropdown();
		
		AttachEvent( document,'mousedown', killDropdown, false);

		dropdown = document.createElement('div');
		dropdown.style.position = "absolute";
		dropdown.style.left = getXY(inp).x + inp.clientWidth + 11 + 'px';
		dropdown.style.top = getXY(inp).y + 20 + 'px';
		dropdown.style.zIndex = '100000';
		dropdown.className = "dropdown";

		monthsel = document.createElement('select');
		monthsel.className = 'month'
		for (i=0; i<date_months.length; i++)
		{
			month = document.createElement('option');
			month.appendChild(document.createTextNode(date_months[i]));
			month.value = i;
			monthsel.appendChild(month);
			if(i == viewdate.getMonth()) monthsel.selectedIndex = i;
		}
		monthsel.onchange = function()
		{
			viewdate.setMonth(this.value);
			render();
		}
		dropdown.appendChild(monthsel);
		
		yearsel = document.createElement('select');
		yearsel.className = 'year'
		for (i=0; i<date_years.length; i++)
		{
			year = document.createElement('option');
			year.appendChild(document.createTextNode(date_years[i]));
			year.value = date_years[i];
			yearsel.appendChild(year);
			if(date_years[i] == viewdate.getRealYear()) yearsel.selectedIndex = i;
		}
		yearsel.onchange = function()
		{
			viewdate.setYear(this.value);
			render();
		}
		dropdown.appendChild(yearsel);

		leftButton = document.createElement('button');
		leftButton.appendChild(document.createTextNode('Back'));
		leftButton .className = 'left';
		leftButton .onclick = function()
		{
			if ( viewdate.getMonth() < 1)
			{
				viewdate.setMonth(11);
				viewdate.setYear( viewdate.getRealYear() - 1);
			}
			else
			{
				viewdate.setMonth( viewdate.getMonth() - 1);
			}
			update();
		}
		leftButton .onmouseup = function()
		{
			if ( this.blur) this.blur();
		}
		dropdown.appendChild(leftButton );
		
		rightButton = document.createElement('button');
		rightButton .appendChild(document.createTextNode('Forward'));
		rightButton .className = 'right';
		rightButton .onclick = function()
		{
			if ( viewdate.getMonth() > 10)
			{
				// nur bis Ende des aktuellen Jahres !!!
				if ( now.getFullYear() > viewdate.getFullYear())
				{
					viewdate.setMonth(0);
					viewdate.setYear( viewdate.getRealYear() + 1);
				}
			}
			else
			{
				viewdate.setMonth( viewdate.getMonth() + 1);
			}
			update();
		}
		rightButton .onmouseup = function()
		{
			if ( this.blur) this.blur();
		}
		dropdown.appendChild(rightButton );
		
		closeButton = document.createElement('button');
		closeButton.appendChild(document.createTextNode('Close'));
		closeButton.className = 'close';
		closeButton.onclick = function()
		{
			killDropdown();
			return false;
		}
		dropdown.appendChild(closeButton);
		
		dropdown.onmousedown = function(evt)
		{
			evt = evt || window.event;
			if(evt.stopPropagation)
				evt.stopPropagation();
			else
				evt.cancelBubble = true;
		}
		
		render();
		
		document.body.appendChild( dropdown);		
//		inp.parentNode.appendChild(dropdown);
	}
}
