function slyDateSelector(inputField) {
	
	this.inputField = inputField;
	this.inputField.style.color = 'black';
	this.inputField.disabled = 'disabled';
	this.calendarIcon = document.createElement('img');
	this.calendarIcon.src = 'res/scripts/slycms/images/calendar.png';
	this.calendarIcon.style.position = 'relative';
	this.calendarIcon.style.top = '3px';
	this.calendarIcon.style.left = '3px';
	this.calendarIcon.style.cursor = 'pointer';
    var _this = this;
    this.calendarIcon.onclick = function() {
    	if (_this.dateSelectorPopup) {
    		if (_this.dateSelectorPopup.isHidden()) _this.dateSelectorPopup.show();
    		else _this.dateSelectorPopup.hide();
    	} else {
    		_this.dateSelectorPopup = new slyDateSelectorPopup(document.getElementById(_this.inputField.id));
    		_this.dateSelectorPopup.show();
    	}
    };
    inputField.parentNode.appendChild(this.calendarIcon);
}

function slyDateSelectorPopup(inputField) {
  if (!inputField) alert('You must specify an input field for the DateSelectorPopup!');
  if (!inputField.id) alert('You must specify an "id" attribute for the DateSelectorPopup\'s input field!');
  this.inputField = inputField;
  this.weekdays = Array('Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So')
  this.months = Array('Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember');
  this.selectedYear = (new Date()).getFullYear();
  this.selectedMonth = (new Date()).getMonth() + 1;
  this.selectedDay = (new Date()).getDate();
  var element = inputField;
  this.left = 0;
  this.top = inputField.offsetHeight + 26;
  while (element != null) {
	if (element.tagName != 'TD') this.top += element.offsetTop;
	this.left += element.offsetLeft;
    element = element.offsetParent;
  }
}
slyDateSelectorPopup.prototype.show = function() {
  if (this.isVisible()) return;
  
  var _this = this;
  
  var div = document.createElement('div');
  div.id = this.inputField.id + '_dateSelectorPopup';
  div.style.position = 'absolute';
  div.style.zIndex = '110000';
  div.style.left = this.left + 'px';
  div.style.top = this.top + 'px';
  div.style.backgroundColor = 'white';
  div.style.border = '1px solid #888888';
  
  var table = document.createElement('table');
  table.cellSpacing = 0;
  table.cellPadding = 0;
  var tbody = document.createElement('tbody');
  var trow = document.createElement('tr');
  var tcell = document.createElement('td');
  tcell.className = 'slyDateSelector_Header';
  tcell.colSpan = 7;
  
  var prevMonthLink = document.createElement('a');
  prevMonthLink.className = 'slyDateSelector_PrevMonth';
  prevMonthLink.appendChild(document.createTextNode('<<'));
  prevMonthLink.onclick = function() {
	  if (_this.selectedMonth > 1) _this.selectedMonth--;
	  else {
		  _this.selectedYear--;
		  _this.selectedMonth = 12;
	  }
	  _this.hide();
	  _this.show();
	  return false;
  };
  tcell.appendChild(prevMonthLink);
  tcell.appendChild(document.createTextNode(this.months[this.selectedMonth - 1] + ' ' + this.selectedYear));
  var nextMonthLink = document.createElement('a');
  nextMonthLink.className = 'slyDateSelector_NextMonth';
  nextMonthLink.appendChild(document.createTextNode('>>'));
  nextMonthLink.onclick = function() {
	  if (_this.selectedMonth < 12) _this.selectedMonth++;
	  else {
		  _this.selectedYear++;
		  _this.selectedMonth = 1;
	  }
	  _this.hide();
	  _this.show();
	  return false;
  };
  tcell.appendChild(nextMonthLink);
  trow.appendChild(tcell);
  tbody.appendChild(trow);
  
  //Weekdays
  trow = document.createElement('tr');
  for(var i = 0; i < 7; i++) {
	  tcell = document.createElement('td');
	  tcell.className = 'slyDateSelector_Weekday';
	  tcell.appendChild(document.createTextNode(this.weekdays[i]));
	  trow.appendChild(tcell);
  }
  tbody.appendChild(trow);
  
  //weekday: MO (0) - SO (6)
  var d = new Date(this.selectedYear, this.selectedMonth - 1, 1);
  var daysInMonth = 32 - new Date(this.selectedYear, this.selectedMonth - 1, 32).getDate();
  var weekday = d.getDay() - 1;
  if (weekday == -1) weekday = 6;
  var z = 0;
  while(z < daysInMonth) {
	  trow = document.createElement('tr');
	  for(var i = 0; i < 7; i++) {
		  if (z == 0 && i == weekday) z = 1;
		  else if (z != 0) z++;
		  tcell = document.createElement('td');
		  tcell.className = 'slyDateSelector_Day';
		  if (this.selectedYear == (new Date()).getFullYear() && this.selectedMonth == (new Date()).getMonth() + 1 && (new Date()).getDate() == z) tcell.className = 'slyDateSelector_Today';
		  tcell.align = 'center';
		  if (z > 0 && z <= daysInMonth) {
			  var dayLink = document.createElement('a');
			  dayLink.appendChild(document.createTextNode(z));
			  dayLink.onclick = function() {
				  var val = this.innerHTML;
				  _this.inputField.value = (val.length == 1 ? '0' + val : val) + '.' + (String(_this.selectedMonth).length == 1 ? '0' + _this.selectedMonth : _this.selectedMonth) + '.' + _this.selectedYear;
				  _this.hide();
				  return false;
			  }
			  tcell.appendChild(dayLink);
		  }
		  else tcell.innerHTML = '&nbsp;';
		  trow.appendChild(tcell);
	  }
	  tbody.appendChild(trow);
  }
  
  table.appendChild(tbody);
  div.appendChild(table);
  
  document.body.appendChild(div);
};
slyDateSelectorPopup.prototype.hide = function() {
	if (this.isHidden()) return;
	document.body.removeChild(document.getElementById(this.inputField.id + '_dateSelectorPopup'));
};
slyDateSelectorPopup.prototype.isHidden = function() {
	if (document.getElementById(this.inputField.id + '_dateSelectorPopup')) return false;
	else return true;
};
slyDateSelectorPopup.prototype.isVisible = function() {
	if (document.getElementById(this.inputField.id + '_dateSelectorPopup')) return true;
	else return false;
};


