/*
	JScalendar 0.2.2 by Johannes Meyer (jm@meyer-kaufmann.de) 2005-02-01
	Based on
	JScalender 0.2  by Christopher Glaab (chris.glaab@gmail.com) 2005-08-17 
*/

/*
absLeft: HTML-Node -> integer
effect: none
Returns the x position of a given element
*/
function absLeft(el) {
	return (el.offsetParent) ? el.offsetLeft + absLeft(el.offsetParent) : el.offsetLeft;
}

/*
absTop: HTML-Node -> integer
effect: none
Returns the y position of a given element
*/
function absTop(el) {
	return (el.offsetParent) ? el.offsetTop+absTop(el.offsetParent) : el.offsetTop;
}

var img;
var x = 0;
var y = 0;
var monthNames = new Array
	(
		'Januar',
		'Februar',
		'M&auml;rz',
		'April',
		'Mai',
		'Juni',
		'Juli',
		'August',
		'September',
		'Oktober',
		'November',
		'Dezember'
	);
var weekdayNames = new Array
	(
		'Mo',
		'Di',
		'Mi',
		'Do',
		'Fr',
		'Sa',
		'So'
	);
var target_form;
var target_field;

/*
setCalDate: string -> bool
effect: sets the value of a form element to the given string. And hides the calendar layer.
Returns false.
This function will be called after a calendar Date was selected
*/
function setCalDate(day) {

	if (day < 10)
		day = '0' + day;
	r = false;
	document.getElementById('webcam').src = 'http://217.160.55.72/webcams/' + img + '_d' + day;
	return hideCal();

}

/*
showCal: int int string string HTML-Node -> bool
effect: shows the calendar layer with the given month of given year at position of given HTML-Node
Returns false
This function will be called when the calendar icon is clicked
*/
function showCal(el, aimg) {
	
	img = aimg;

	getCal();
	var cal = document.getElementById('cal');
	var overlay = document.getElementById('overlay');

	x=absLeft(el)+5;
	y=absTop(el)+5;
	cal.style.left = x + 'px';
	cal.style.top = y + 'px';
	cal.style.display = "block";

	/* this is an fix, because some browsers will not show layers above a dropwown */
	overlay.style.width = cal.offsetWidth;
	overlay.style.height = cal.offsetHeight;
	overlay.style.top = cal.style.top;
	overlay.style.left = cal.style.left;
	overlay.style.zIndex = cal.style.zIndex - 1;
	if(!window.netscape)
		overlay.style.display = "block";

	return false;
}

/*
hideCal: -> boolean (false)
consumes nothing; returnes false.
effect: hides the calendar layer
*/
function hideCal() {

	var cal = document.getElementById('cal');
	var overlay = document.getElementById('overlay');
	
	cal.style.display = "none";
	if(!window.netscape)
		overlay.style.display = "none";
		
	return false;

}

/*
getCal: int int -> boolean
consumes month and year; returnes false
effect: sets content of calendar layer
*/
function getCal(month, year) {

	var day = 1;
	var html;
	var firstDay = new Date();
	var today = new Date();
	var lastmonth = new Date();
	var stwab = new Date();
	var stiftskirche = new Date();
	var active = false;
	lastmonth.setTime(eval(lastmonth.getTime()-(1000*60*60*24*31)));
	stwab.setTime(1181685600000);
	stiftskirche.setTime(1188511200000);

	// get actual date
	var actDay = firstDay.getDate();
	var actMonth = firstDay.getMonth();
	var actYear = firstDay.getYear();

	// get acual date if none date given
	if (isNaN(month))
		month = actMonth + 1;
	if (isNaN(year))
		year = actYear;

	if (year < 999)
		year += 1900;
	if (actYear < 999)
		actYear += 1900;

	if (month == 13) {
		month = 1;
		year++;
	} else if (month == 0) {
		month = 12;
		year--;
	}

	firstDay.setMonth(month-1);
	firstDay.setYear(year);
	firstDay.setDate(1);

	html = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
	html += "<tr><td align=\"center\"><!--<a href=\"#\" onCLick=\"return getCal(" + month + ", " + eval(firstDay.getYear()-1) + ");\" class=\"cal\">&laquo;</a>-->&nbsp;</td><td colspan=\"5\" class=\"calYear\">" + year + "</td><td align=\"center\"><!--<a href=\"#\" onCLick=\" return getCal(" + month + ", " + eval(firstDay.getYear()+1) + ");\" class=\"cal\">&raquo;</a>-->&nbsp;</td></tr>";
	html += "<tr><td align=\"center\"><a href=\"#\" onCLick=\"return getCal(" + eval(month-1) + ", " + firstDay.getYear() + ");\" class=\"cal\">&laquo;</a></td><td colspan=\"5\" class=\"calMonth\">" + monthNames[firstDay.getMonth()] + "</td><td align=\"center\"><a href=\"#\" onCLick=\"return getCal(" + eval(month+1) + ", " + firstDay.getYear() + ");\" class=\"cal\">&raquo;</a></td></tr>";
	html += "<tr><td colspan=\"7\" style=\"border-bottom: 1px solid #999999;\"><img src=\"blank.gif\" width=\"1\" height=\"5\" border=\"0\" alt=\"\"><br></td></tr>";
	html += "<tr><td class=\"calDayWD\">" + weekdayNames[0] + "</td><td class=\"calDayWD\">" + weekdayNames[1] + "</td><td class=\"calDayWD\">" + weekdayNames[2] + "</td><td class=\"calDayWD\">" + weekdayNames[3] + "</td><td class=\"calDayWD\">" + weekdayNames[4] + "</td><td class=\"calDayWE\">" + weekdayNames[5] + "</td><td class=\"calDayWE\">" + weekdayNames[6] + "</td></tr>";

	for (var i = 0; i < 6; i++) {

		html += "<tr>";
		for (var c = 1; c < 8; c++) {

			d = (c == 7) ? 0 : c;

			if ((day != 1 || firstDay.getDay() == d) && firstDay.getDate() == day) {

				thisday = new Date (year, month-1, day);
				active = (thisday.getTime() < today.getTime() && thisday.getTime() > lastmonth.getTime());
				if (img == 'webcamstwab' && thisday.getTime() <= stwab.getTime())
					active = false;

				if (img == 'webcamstiftskirche' && thisday.getTime() <= stiftskirche.getTime())
					active = false;

//					alert(thisday.getTime() + '>=' + stwab.getTime() + '|' + active + '|' + (thisday.getTime() <= stwab.getTime())); 

				var cssClass = (day == actDay && firstDay.getMonth() == actMonth && year == actYear) ? "calActDay" : "calDay";
				thisDay = (day < 10) ? '0' + day : day;
				thisMonth = (month < 10) ? '0' + month : month;
				thisDate = year + "-" + thisMonth + "-" + thisDay;
				html += "<td align=\"center\" class=\"" + cssClass + "\"";
				if (active)
					html += " onClick=\"return setCalDate('" + day + "');\" onMouseOver=\"this.className = 'calDayOn';\" onMouseOut=\"this.className = '" + cssClass + "';\" style=\"cursor: pointer;\"";
				else
					html += " style=\" cursor: default;\"";
				html += ">";
				if (active)
					html += "<a href=\"#\" class=\"" + cssClass + "\">";
				html += day;
				if (active)
					html += "</a>";
				html += "</td>";
				day++;
				firstDay.setDate(day);

			} else {

				html += "<td class=\"calDayEmpty\">&nbsp;</td>";

			}

		}

		html += "</tr>";

	}
	html += "<tr><td colspan=\"7\" style=\"border-top: 1px solid #999999;\"><img src=\"blank.gif\" width=\"1\" height=\"5\" border=\"0\" alt=\"\"><br></td></tr>";
	html += "<tr><td align=\"center\" colspan=\"7\">[ <a href=\"#\" onClick=\"return hideCal();\" class=\"cal\">schliessen</a> ]</td></tr>\n";
	html += "</table>";
	document.getElementById('cal').innerHTML = html;
	return false;

}