﻿/**********************************************************************
*          Calendar JavaScript [DOM] v3.10 by Michael Loesler          *
 **********************************************************************/
// edited by Manuel Tremmel
	function CalendarJS() {
		this.now = new Date();
		this.dayname = ["Mo","Di","Mi","Do","Fr","Sa","So"];
		this.monthname = ["Januar","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember"];	
		this.tooltip = ["vorheriger Monat","nächster Monat","aktuelles Datum"];
		this.monthCell = document.createElement("th");
		this.tableHead = null;
		this.parEl = null;
		
		this.init = function( id, initDate, xmlTrips ) {
			this.now = initDate?initDate:new Date();
			this.xmlTrips = xmlTrips;
			this.date = this.now.getDate();
			this.month = this.mm = this.now.getMonth();
			this.year = this.yy = this.now.getFullYear();
			this.monthCell.colSpan = 5;
			this.monthCell.appendChild(document.createTextNode( this.monthname[this.mm]+" "+this.yy ));
			this.tableHead = this.createTableHead();
			this.parEl = document.getElementById( id );
			this.show();
			if (!initDate) this.checkDate();
		},
		this.xml2date = function( xml, field ) {
			return new Date(
				xml.getElementsByTagName(field)[0].getAttribute("year"),
				xml.getElementsByTagName(field)[0].getAttribute("month")-1,
				xml.getElementsByTagName(field)[0].getAttribute("day"));
		},
				
		this.checkDate = function() {
			var self = this;
			var today = new Date();
			if (this.date != today.getDate()) {
				this.date  = today.getDate();
				if (this.mm == this.month && this.yy == this.year)
					this.switchMonth("current");
				this.month = today.getMonth();
				if (this.mm == this.month && this.yy == this.year)
					this.switchMonth("current");
				this.year  = today.getFullYear();
				if (this.mm == this.month && this.yy == this.year)
					this.switchMonth("current");
 
			}
			window.setTimeout(function() { self.checkDate(); }, Math.abs(new Date(this.year, this.month, this.date, 24, 0, 0)-this.now));
		},
		
		this.removeElements = function( Obj ) {
			while( Obj.childNodes.length > 0)
				Obj.removeChild(Obj.childNodes[Obj.childNodes.length-1]);
			return Obj;
		},
			
		this.show = function() {
			this.parEl = this.removeElements( this.parEl );
			this.monthCell.firstChild.replaceData(0, this.monthCell.firstChild.nodeValue.length, this.monthname[this.mm]+" "+this.yy);
			var table = document.createElement("table");
			table.appendChild( this.createTableBody() );
			table.appendChild( this.tableHead );
			this.parEl.appendChild( table );
			
			var spacer = document.createElement("div");
			spacer.id = "spacer";
			spacer.className = "hiddeninfo";
			this.parEl.appendChild( spacer );
		},
				
		this.createTableHead = function() {
			var thead = document.createElement("thead");
			var tr = document.createElement("tr");
			var th = this.getCell( "th", "\u00AB", "last_month" )
			th.Instanz = this;
			th.onclick = function() { this.Instanz.switchMonth("prev"); };
			th.title = this.tooltip[0];
			try { th.style.cursor = "pointer"; } catch(e){ th.style.cursor = "hand"; }
			tr.appendChild( th );
			this.monthCell.Instanz = this;
			this.monthCell.onclick = function() { this.Instanz.switchMonth("current"); };
			this.monthCell.title = this.tooltip[2];
			try { this.monthCell.style.cursor = "pointer"; } catch(e){ this.monthCell.style.cursor = "hand"; }
			tr.appendChild( this.monthCell );			
			th = this.getCell( "th", "\u00BB", "next_month" )
			th.Instanz = this;
			th.onclick = function() { this.Instanz.switchMonth("next"); };
			th.title = this.tooltip[1];
			try { th.style.cursor = "pointer"; } catch(e){ th.style.cursor = "hand"; }
			tr.appendChild( th );
			thead.appendChild( tr );
			tr = document.createElement('tr');
			for (var i=0; i<this.dayname.length; i++)
				tr.appendChild( this.getCell("th", this.dayname[i], "weekday" ) );
			thead.appendChild( tr );
			return thead;
		},
		
		this.createTableBody = function() {
			var dayspermonth = [31,28,31,30,31,30,31,31,30,31,30,31];
			var sevendaysaweek = 0;
			var begin = new Date(this.yy, this.mm, 1);
			var firstday = begin.getDay()-1;
			if (firstday < 0)
				firstday = 6;
			if ((this.yy%4==0) && ((this.yy%100!=0) || (this.yy%400==0)))
				dayspermonth[1] = 29;
			var tbody = document.createElement("tbody");
			var tr = document.createElement('tr');
			if (firstday == 0) {
				for (var i=0; i<this.dayname.length; i++) {
					var prevMonth = (this.mm == 0)?11:this.mm-1;
					tr.appendChild( this.getCell( "td", dayspermonth[prevMonth]-6+i, "last_month" ) );
				}
				tbody.appendChild( tr );
				tr = document.createElement('tr');
			}
			
			for (var i=0; i<firstday; i++, sevendaysaweek++) {
				var prevMonth = (this.mm == 0)?11:this.mm-1;
				tr.appendChild( this.getCell( "td", dayspermonth[prevMonth]-firstday+i+1, "last_month" ) );
				
			}
			
			for (var i=1; i<=dayspermonth[this.mm]; i++, sevendaysaweek++){
				if (this.dayname.length == sevendaysaweek){
					tbody.appendChild( tr );
					tr = document.createElement('tr');
					sevendaysaweek = 0;
				}
				
				var td = null;
				var free = false;
				for (var j = 0; j < this.xmlTrips.length; j++) {
					if ((Date.parse(new Date(this.yy,this.mm,i))>=Date.parse(this.xml2date(this.xmlTrips[j],"start")))
						&& (Date.parse(new Date(this.yy,this.mm,i))<=Date.parse(this.xml2date(this.xmlTrips[j],"end"))))
						free = true;
				}
				
				var td = null;
				if (free && (sevendaysaweek == 5 || sevendaysaweek == 6))
					td = this.getCell( "td", i, "free weekend" );
				else if (free)
					td = this.getCell( "td", i, "free" );
				else if (sevendaysaweek == 5 || sevendaysaweek == 6)
					td = this.getCell( "td", i, "weekend" );
				else
					td = this.getCell( "td", i, null ); 

				td.setDate = this.setDate;
				td.dd = i;
				td.mm = this.mm;
				td.yy = this.yy;
				td.msg="";
				var currentDate = new Date(this.yy, this.mm, i);
				for (var j = 0; j < this.xmlTrips.length; j++) {
					var start = this.xml2date(this.xmlTrips[j],"start");
					var end = this.xml2date(this.xmlTrips[j],"end");
					if ((Date.parse(currentDate)>=Date.parse(start))
					&& (Date.parse(currentDate)<=Date.parse(end))) {
						start.setHours(12);
						end.setHours(12);
						td.msg = '<b>Törnbeginn:</b> '+start.toLocaleString()+'<br>\n'
							+'<b>Törnende:</b> '+end.toLocaleString()+'<br>\n'
							+'<b>Revier:</b> '+this.xmlTrips[j].getElementsByTagName("location")[0].firstChild.nodeValue+'<br>\n'
							+'<span class="available">Kojen verfügbar</span>';
					}
				}
				td.onclick = function(e) {
					this.setDate( new Date(this.yy, this.mm, this.dd), this.msg );
				};
				tr.appendChild( td );
			}
	
			var daysNextMonth = 1;
			for (var i=sevendaysaweek; i<this.dayname.length; i++) 
				tr.appendChild( this.getCell( "td", daysNextMonth++, "next_month"  ) );
				
			tbody.appendChild( tr );
			
			while (tbody.getElementsByTagName("tr").length<6) {
				tr = document.createElement('tr');
				for (var i=0; i<this.dayname.length; i++) 
					tr.appendChild( this.getCell( "td", daysNextMonth++, "next_month"  ) );
				tbody.appendChild( tr );
			}

			return tbody;
			
		},
		
		this.setDate = function(date, message) {
			// Weiterverarbeitung des geklickten Datums, Anzeige einer Meldung
			if (message!="") showMessage("spacer", message);
		},
		
		this.getCell = function(tag, str, cssClass) {
			var El = document.createElement( tag );
			El.appendChild(document.createTextNode( str ));
			if (cssClass != null)
				El.className = cssClass;
			return El;
		},
		
		this.switchMonth = function( s ){
			switch (s) {
				case "prev": 
					this.yy = (this.mm == 0)?this.yy-1:this.yy;
					this.mm = (this.mm == 0)?11:this.mm-1;
				break;
				
				case "next":
					this.yy = (this.mm == 11)?this.yy+1:this.yy;
					this.mm = (this.mm == 11)?0:this.mm+1;
				break;
				
				case "current":
					this.yy = this.year;
					this.mm = this.month;
				break;
			}
			this.show();
		}
	}
	
	var DOMContentLoaded = false;
	function addContentLoadListener (func) {
		if (document.addEventListener) {
			var DOMContentLoadFunction = function () {
				window.DOMContentLoaded = true;
				func();
			};
			document.addEventListener("DOMContentLoaded", DOMContentLoadFunction, false);
		}
		var oldfunc = (window.onload || new Function());
		window.onload = function () {
			if (!window.DOMContentLoaded) {
				oldfunc();
				func();
			}
		};
	}
	
function calendar(start,id) {
	addContentLoadListener( function() { 
			//new CalendarJS().init("calendar");
			// (C) 2010 Manuel Tremmel:
			if (window.XMLHttpRequest) {
				xhttp=new XMLHttpRequest();
			} else {
				xhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			xhttp.open("GET","/xml.php?plans",false);
			xhttp.send("");
			
			new CalendarJS().init(id, start, xhttp.responseXML.getElementsByTagName("trip"));
	} );
}

hideInterval = null;
function showMessage(id, message) {
	//alert(message);
	show(id);
	if (hideInterval!=null) window.clearInterval(hideInterval);
	hideInterval = window.setInterval(function() { hide(id); }, 9000);
	document.getElementById(id).innerHTML=message;		
}

