function loadCalendar () {
	var cal = new Calendar(1, null);		
	//cal.flatCallback = dateChanged;
	cal.getDateStatus = dateStatusFunc;
	Calendar.dayClickCallback = dayClick;
	Calendar.dayMouseOverCallback = dayMouseOver;
	Calendar.dayMouseOutCallback = dayMouseOut;
	Calendar.setNextSpecialDate = setNextSpecialDate; 
	
	cal.weekNumbers = false;
	cal.create(document.getElementById('calendar'));
	
	cal.show();
	
	if(typeof sIFR.replace == "function"){
		callSifr();	
	}
}

function callSifr() {
	sIFR.replace(hoefler, {
	    selector: 'span.todayDate'
	    ,css: [
	      '.sIFR-root { color: #ffffff; background-color: #828081; text-align: center; font-weight: bold; font-size: 16px ;}'
	    ],thickness: '167',sharpness: '-17',wmode: 'opaque',offsetLeft: offset,tuneWidth: '8',preventWrap: true
	});	
	
	var className = document.getElementById('nav_now').className;
	if (className.indexOf('sIFR-replaced') != -1) {
		className = className.replace(/sIFR-replaced/, '');
		document.getElementById('nav_now').className = className;
	}
	sIFR.replace(hoefler, {
	    selector: '.nav_now'
	    ,css: [
	      '.sIFR-root { color: #666666; background-color: #ffffff; text-align: center; font-weight: bold; font-size: 14px;}'
	    ],thickness: '167',sharpness: '-17',wmode: 'opaque',offsetLeft: offset,tuneWidth: '8',preventWrap: true
	});
}

function setNextSpecialDate(TY, TM, TD) {
	TM += 1;
	
	var maxTY = 2020;
	var maxTM = 12;
	var maxTD = 31;
	
	var initY = TY;
	var initM = TM;
	var initD = TD;
	
	var selectM = null;
	var selectD = null;
	
	for (var y = initY; y <= maxTY; y++) {
		for (var m = initM; m <= maxTM; m++) {
			for (var d = initD; d <= maxTD; d++) {
				selectM = m;
				if ((selectM / 10) < 1) {
					selectM = 0 + '' + selectM;
				}
				selectD = d;
				if ((selectD / 10) < 1) {
					selectD = 0 + '' + selectD;
				}
				var nsd = SPECIAL_DAYS[y + '' + selectM + '' + selectD];
				if (nsd) {
					Calendar.terminText = nsd;
					Calendar.terminDate = selectD + " | " + selectM + " | " + y;
					Calendar.nextSpecialDateWasSet = true;
					return true;
				}
			}
			initD = 1;
		}
		initM = 1;
	}
}

function dateIsSpecial(y, m, d, now, cell) {
	m = m + 1;
	var mShow = m;
	if ((m / 10) < 1) {
		m = "0" + m;
	}
	
	var dShow = d;
	if ((d / 10) < 1) {
		d = "0" + d;
	}
	
	var special = y + "" + m + "" + d
	var m = SPECIAL_DAYS[special];
	
	if (cell.id) {
		cell.id = '';
	}
	
	if (!m) {
		return false;
	} else {
		if (special > now && Calendar.nextSpecialDateWasSet === false) {
			Calendar.terminText = m;
			Calendar.terminDate = dShow + " | " + mShow + " | " + y;
			Calendar.nextSpecialDateWasSet = true;			
		}
		cell.id = special;
		
		var div = Calendar.createElement('div', cell);
		div.className = 'actionLayerEraseLineContainer'
		
		var div2 = Calendar.createElement('div', div);
		div2.className = 'actionLayerEraseLine';
		
		return "specialDay";
	}
};

function dayClick(calendar) {
	if (calendar.dateClicked) {
		var y = calendar.date.getFullYear();
		var m = calendar.date.getMonth();     // integer, 0..11
		var d = calendar.date.getDate();      // integer, 1..31
		m += 1;
		
		if ((m / 10) < 1) {
			m = "0" + m;
		}
		if ((d / 10) < 1) {
			d = "0" + d;
		}
		
		var special = y + 1000 + "" + m + "" + d		
		var m = SPECIAL_DAYS[special];
		if (m) {
			window.location = m;	
		}
	}
};

function dateStatusFunc(date, y, m, d, cell) {
	if (typeof(cell) == 'undefined') {
		return false;
	}
	
	var now = new Date();

	var ny = now.getFullYear();
	
	var nm = now.getMonth();
	nm = nm + 1;
	if ((nm / 10) < 1) {
		nm = "0" + nm;
	}
		
	var nd = now.getDate();
	if ((nd / 10) < 1) {
		nd = "0" + nd;
	}
	
	now = ny + "" + nm + "" + nd; 
	
	return dateIsSpecial(y, m, d, now, cell);
};

function dayMouseOver(el) {
	if (el.id && SPECIAL_DAYS[el.id]) {
		showCalendarActionLayer('Calendar', el);
		el.className += " specialDayHilite";
		
		var actionLayerEraseLine = el.childNodes[1].childNodes[0];
		actionLayerEraseLine.style.visibility = 'visible';
	}
}

function dayMouseOut(el) {
	if (el.id && SPECIAL_DAYS[el.id]) {
		hideCalendarActionLayer('Calendar');
		el.className = el.className.replace("specialDayHilite", "");
		
		var actionLayerEraseLine = el.childNodes[1].childNodes[0];
		actionLayerEraseLine.style.visibility = 'hidden';
	}
}

function getPosition(element, stop) {
	var elem = element, tagname = "", x = 0, y = 0;

	while ((typeof(elem) == "object") && (typeof(elem.tagName) != "undefined")) {
		if (elem.parentNode.id == stop) {
			elem = 0;
			break;
		}
		y += elem.offsetTop;     /* Offset des jeweiligen Elements addieren */
		x += elem.offsetLeft;    /* Offset des jeweiligen Elements addieren */
		tagname = elem.tagName.toUpperCase(); /* tag-Name ermitteln, Grossbuchstaben */

		if (tagname == "BODY") {
			elem=0;
		}

		if (typeof(elem) == "object") {
			if (typeof(elem.offsetParent )== "object") {
				elem = elem.offsetParent;
			}
		}
	}

	position = new Object();
	position.x = x;
	position.y = y;
	
	return position;
}

function showCalendarActionLayer(itemID, el) {
	if (document.getElementById) {
		actionLayer = document.getElementById('actionLayer' + itemID);
		if (actionLayer) {
			actionLayer.innerHTML = SPECIAL_DAYS[el.id];
			
			var position = getPosition(el, 'calendar');
			actionLayer.style.top = position.y - parseInt(actionLayer.offsetHeight);
			//actionLayer.style.left = position.x;
			actionLayer.style.visibility = 'visible';
		}
	}
}

function hideCalendarActionLayer(itemID) {
  	if (document.getElementById) {
		actionLayer = document.getElementById('actionLayer' + itemID);
		if (actionLayer) {
			actionLayer.style.visibility = 'hidden';
		}
	}
}
