
/* object calendar */
function calendar(id, nameofcalobj, thisnode, return_value, show_time, show_events){
	thiscalObj = this;
	this.node = thisnode;
	this.return_value = return_value;
	this.show_time = show_time;
	this.show_events = show_events;
	this.nameofcalobj = nameofcalobj;
	this.id = id;
	this.answerofallquestions = 42;
	this.today_month = "1";
	this.today_year = "2012";
	this.time_hour = "06";
	this.time_minute = "56";
	this.month = "1";
	this.year = "2012";
	this.indicated = 0;

	this.strtime = this.time_hour+":"+this.time_minute+":00";
	this.strunix = Date.UTC(1970, 1, 1, parseInt(this.time_hour, 10), parseInt(this.time_minute, 10), 0)/1000;

	this.monthnames = new Array();
	this.monthnames[0] = "Januar";
	this.monthnames[1] = "Februar";
	this.monthnames[2] = "März";
	this.monthnames[3] = "April";
	this.monthnames[4] = "Mai";
	this.monthnames[5] = "Juni";
	this.monthnames[6] = "Juli";
	this.monthnames[7] = "August";
	this.monthnames[8] = "September";
	this.monthnames[9] = "Oktober";
	this.monthnames[10] = "November";
	this.monthnames[11] = "Dezember";


}

calendar.prototype={
	display:function(){
		this.calc_header();
		this.calc_body();
		this.calc_footer();
		document.getElementById(this.id).innerHTML = this.output;
		if(this.show_events == 1){
			this.add_events_to_calendar();
		}
	},

	return_body:function(){
		this.output = "";
		this.calc_body();
		return this.output;
	},

	show_div:function(){
		if(this.indicated == 1){
			return;
		}

		this.close_button = 1;
		this.calc_header();
		this.calc_body();
		this.calc_footer();
		
		this.pos = new Array();
		this.pos = getpos(this.node);
		this.posX = this.pos[0];
		this.posY = this.pos[1];

		this.div = document.createElement("div");
		this.div.style.backgroundColor = "#FFFFFF";
		this.div.style.border = "1px solid black";
		this.div.style.position = "absolute";
		this.div.style.top = parseInt(this.posX)+"px";
		this.div.style.left = (parseInt(this.posY)+parseInt(this.node.offsetWidth))+"px";
		this.div.id = this.id;
		this.div.innerHTML = this.output;
		this.div.style.zIndex = "99999999";

		document.body.appendChild(this.div);
		//document.onclick = function(){ eval(thiscalObj.nameofcalobj+".remove_div();"); }

		this.indicated = 1;

		if(this.show_events == 1){
			this.add_events_to_calendar();
		}
	},

	remove_div:function(){
		if(this.div){
			document.body.removeChild(this.div);
			this.indicated = 0;
		}
	},

	calc_header:function(){
		if(this.close_button && this.close_button == 1){
			this.close_button_html = "<input type='button' class='button' onClick=\""+this.nameofcalobj+".remove_div();\" value='x'>";
		}else{
			this.close_button_html = "&nbsp;";
		}
		this.output = "<table class='calendar_table'><tr>";
		this.output += "<tr class='calendar_row'><td colspan=6><input type='button' class='button' onClick=\""+this.nameofcalobj+".today();\" value='Heute'></td><td>" + this.close_button_html + "</td></tr>";
		
		this.output += "<tr class='calendar_row'><td colspan=7><table cellspacing='0' cellpadding='0' width='100%' height='28px'><tr class='calendar_row'><td><input type='button' class='button' style='width:18px;' onClick=\""+this.nameofcalobj+".monthback();\" value='<<'>&nbsp;<font>"+this.monthnames[this.month]+"</font>&nbsp;<input type='button' class='button' style='width:18px;' onClick=\""+this.nameofcalobj+".monthforward();\" value='>>'></td>";

		this.output += "<td><input type='button' class='button' style='width:18px;' onClick=\""+this.nameofcalobj+".yearback();\" value='<<'>&nbsp;<font>"+this.year+"</font>&nbsp;<input type='button' class='button' style='width:18px;' onClick=\""+this.nameofcalobj+".yearforward();\" value='>>'></td></tr></table></td></tr>";
		
		if(this.show_time == 1){
			this.output += "<tr class='calendar_row'><td colspan=7>";
			this.output += "<table cellspacing='0' cellpadding='0' border='0' height='28px'><tr>";
			this.output += "<td rowspan='2'><font>&nbsp;Uhrzeit:&nbsp;</font></td>";
			this.output += "<td><button type='button' style='width:28px; height:12px; border: 1px dotted black;' onClick=\""+this.nameofcalobj+".inchour();\"><img src='img/icons/arrow_up.gif'></button></td>";
			this.output += "<td rowspan='2'>&nbsp;<input type='text' disabled='disabled' maxlength='8' id='calendar_time' style='width:55px; border: 1px dotted black;' value='"+this.strtime+"'>&nbsp;</td>";
			this.output += "<td><button type='button' style='width:28px; height:12px; border: 1px dotted black;' onClick=\""+this.nameofcalobj+".incminute();\"><img src='img/icons/arrow_up.gif'></button></td>";
			this.output += "</tr><tr>";
			this.output += "<td><button type='button' style='width:28px; height:12px; border: 1px dotted black;' onClick=\""+this.nameofcalobj+".dechour();\"><img src='img/icons/arrow_down.gif'></button></td>";
			this.output += "<td><button type='button' style='width:28px; height:12px; border: 1px dotted black;' onClick=\""+this.nameofcalobj+".decminute();\"><img src='img/icons/arrow_down.gif'></button></td>";
			this.output += "</tr></table></td><tt>";
		}

		this.output += "<tr class='calendar_row'><td class='calendar_column'>Mo</td><td class='calendar_column'>Di</td><td class='calendar_column'>Mi</td><td class='calendar_column'>Do</td><td class='calendar_column'>Fr</td><td class='calendar_column'>Sa</td>	<td class='calendar_column'>So</td></tr>";

		this.output += "</table>";

		this.output += "<div id='"+this.id+"_calendar' class='calendar'>";
	},

	calc_body:function(){
		this.output += "<table class='calendar_table'>";
		this.firstdayinmonth = new Date(this.year,this.month,1);
		this.output += "<tr class='calendar_row'>";
		if(this.firstdayinmonth.getDay() != 0){	this.until = this.firstdayinmonth.getDay(); }
		else{ this.until = 7; }
		
		for(this.i=1;this.i<this.until;this.i++){
			this.output += "<td class='calendar_column_last_month'>&nbsp;</td>";
		}
		this.a = 0;
		this.i--;
		//this.counter = this.i;
		for(this.i;this.a<31;this.i++){
			this.a++;
			if(String(this.a).length == 1){ this.strday = "0"+String(this.a); }else{ this.strday = String(this.a); }
			if(String(parseInt(this.month, 10)+1).length == 1){ this.strmonth = "0"+String(parseInt(this.month, 10)+1); }else{ this.strmonth = String(parseInt(this.month, 10)+1); }

			this.day = new Date(this.year,this.month,this.a);
			if (this.day.getMonth() != this.month){break;}
			if (this.i==7||this.i==14||this.i==21||this.i==28||this.i==35){this.output += "</tr><tr class='calendar_row'>";}

			if(this.return_value != undefined){
				if(this.return_value == "hrdate"){
					this.fielddate = this.strday+"."+this.strmonth+"."+this.year+" - ";
				}else if(this.return_value == "mysqldate"){
					this.fielddate = this.year+"-"+this.strmonth+"-"+this.strday+" ";
				}else if(this.return_value == "unixdate"){
					//this.fielddate = Date.UTC(parseInt(this.year, 10), parseInt(this.month, 10), parseInt(this.a, 10), parseInt(this.time_hour, 10), parseInt(this.time_minute, 10), 0)/1000;
					this.fielddate = Date.UTC(parseInt(this.year, 10), parseInt(this.month, 10)-1, parseInt(this.a, 10), 0, 0, 0)/1000;
				}
				this.output += "<td class='calendar_column_this_month' onClick=\"";
				if(this.return_value == "hrdate"){
					this.output += this.nameofcalobj + ".node.value = '"+this.fielddate+"' + " + this.nameofcalobj + ".strtime" + "; "+this.nameofcalobj+".remove_div();" + "\"";
				}if(this.return_value == "mysqldate"){
					this.output += this.nameofcalobj + ".node.value = '"+this.fielddate+"' + " + this.nameofcalobj + ".strtime" + "; "+this.nameofcalobj+".remove_div();" + "\"";
				}else if(this.return_value == "unixdate"){
					this.output += this.nameofcalobj + ".node.value = "+this.fielddate+" + " + this.nameofcalobj + ".strunix" + "; "+this.nameofcalobj+".remove_div();" + "\"";
				}
				this.output += "onmouseover=\"this.className = 'calendar_column_this_month_active';\" onmouseout=\"this.className = 'calendar_column_this_month';\"><font class='calendar_noevent' id='calendar_" + this.strday + "'>"+this.strday+"</font></td>";
			}else{
				this.output += "<td class='calendar_column_this_month'><font class='calendar_noevent' id='calendar_" + this.strday + "'>"+this.strday+"</font></td>";
			}
		}
		for(this.i;this.i<this.answerofallquestions;this.i++){
			if (this.i==28||this.i==35||this.i==42){this.output += "</tr><tr class='calendar_row'>";}
			this.output += "<td class='calendar_column_last_month'>&nbsp;</td>";
		}
		this.output += "</tr></table>";
	},

	calc_footer:function(){
		this.output += "</div>";
		this.output += "<div id='"+this.id+"_calendar_footer' class='calendar_footer_div'></div>";
	},

	today:function(){
		this.month = this.today_month;
		this.year = this.today_year;
		var fadeobj = new fade(this.return_body(), this.id+"_calendar");
		fadeobj.onfinish(this.nameofcalobj+".display();")
		fadeobj.out_in();
	},

	monthback:function(){
		if(this.month == 0){
			this.month = 11;
			this.year--;
		}else{
			this.month--;
		}
		var scrollobj = new scroll("up", document.getElementById(this.id+"_calendar").innerHTML, this.return_body(), this.id+"_calendar");
		scrollobj.calc_pix_to_scroll();
		scrollobj.onfinish(this.nameofcalobj+".display();");
		scrollobj.start();
	},

	monthforward:function(){
		if(this.month == 11){
			this.month = 0;
			this.year++;
		}else{
			this.month++;
		}
		var scrollobj = new scroll("down", document.getElementById(this.id+"_calendar").innerHTML, this.return_body(), this.id+"_calendar");
		scrollobj.calc_pix_to_scroll();
		scrollobj.onfinish(this.nameofcalobj+".display();");
		scrollobj.start();
	},

	yearback:function(){
		this.year--;
		var scrollobj = new scroll("left", document.getElementById(this.id+"_calendar").innerHTML, this.return_body(), this.id+"_calendar");
		scrollobj.calc_pix_to_scroll();
		scrollobj.onfinish(this.nameofcalobj+".display();");
		scrollobj.start();
	},

	yearforward:function(){
		this.year++;
		var scrollobj = new scroll("right", document.getElementById(this.id+"_calendar").innerHTML, this.return_body(), this.id+"_calendar");
		scrollobj.calc_pix_to_scroll();
		scrollobj.onfinish(this.nameofcalobj+".display();");
		scrollobj.start();
	},

	updatetimefield:function(){
		this.strtime = this.time_hour+":"+this.time_minute+":00";
		if(this.return_value == "unixdate"){
			this.strunix = Date.UTC(1970, 1, 1, parseInt(this.time_hour, 10), parseInt(this.time_minute, 10), 0)/1000;
		}
		document.getElementById("calendar_time").value = this.strtime;
	},

	incminute:function(){
		if(this.time_minute == "59"){ this.time_minute = "00"; this.inchour(); }else{
			this.time_minute = String(parseInt(this.time_minute, 10)+1);
			if(String(this.time_minute).length == 1){ this.time_minute = "0"+String(this.time_minute); }
		}
		this.updatetimefield();
	},

	decminute:function(){
		if(this.time_minute == "00"){ this.time_minute = "59"; this.dechour(); }else{
			this.time_minute = String(parseInt(this.time_minute, 10)-1);
			if(String(this.time_minute).length == 1){ this.time_minute = "0"+String(this.time_minute); }
		}
		this.updatetimefield();
	},

	inchour:function(){
		if(this.time_hour == "23"){ this.time_hour = "00"; }else{
			this.time_hour = String(parseInt(this.time_hour, 10)+1);
			if(String(this.time_hour).length == 1){ this.time_hour = "0"+String(this.time_hour); }
		}
		this.updatetimefield();
	},

	dechour:function(){
		if(this.time_hour == "00"){ this.time_hour = "23"; }else{
			//alert("plain: "+this.time_hour+" parseint, 10: "+parseInt(this.time_hour, 10)+" string.parseint-1 "+String(parseInt(this.time_hour, 10)-1));
			this.time_hour = String(parseInt(this.time_hour, 10)-1);
			if(String(this.time_hour).length == 1){ this.time_hour = "0"+String(this.time_hour); }
		}
		this.updatetimefield();
	},



	add_events_to_calendar:function(){
		document.getElementById(this.id+"_calendar_footer").innerHTML = "<img src='img/icons/searching_animation3.gif' style='width:20px; height:20px;'>&nbsp;<font>lade Events...</font>";
		document.getElementById(this.id+"_calendar_footer").style.visibility = "visible";

		this.events_url = "func/xmlevents.php?month=" + (parseInt(this.month, 10)+1) + "&year=" + this.year;
		this.events_xml = getxml(this.events_url);

		try{ //Internet Explorer 
			this.xmldoc=new ActiveXObject("Microsoft.XMLDOM"); 
			this.xmldoc.async="false"; 
			this.xmldoc.loadXML(this.events_xml); 
		}catch(e){
			try{ //Firefox, Mozilla, Opera, etc.
				parser=new DOMParser();
				this.xmldoc=parser.parseFromString(this.events_xml,"text/xml");
			}catch(e){
				alert(e.message);
				return;
			}
		}

		this.events_arr = new Array();
		for(this.i=0;this.i<this.xmldoc.getElementsByTagName("event").length;this.i++){
			this.event = this.xmldoc.getElementsByTagName("event")[this.i];
			this.events_arr[this.i] = new Array();
			this.events_arr[this.i]["id"] = this.event.getElementsByTagName("id")[0].childNodes[0].nodeValue;
			this.events_arr[this.i]["name"] = this.event.getElementsByTagName("name")[0].childNodes[0].nodeValue;
			this.events_arr[this.i]["evt_start_timestamp"] = this.event.getElementsByTagName("evt_start_timestamp")[0].childNodes[0].nodeValue;
			this.events_arr[this.i]["evt_end_timestamp"] = this.event.getElementsByTagName("evt_end_timestamp")[0].childNodes[0].nodeValue;
			this.events_arr[this.i]["evt_days"]= new Array();
			for(this.a=0;this.a<this.event.getElementsByTagName("evt_day").length;this.a++){
				this.event_day = this.event.getElementsByTagName("evt_day")[this.a];
				this.events_arr[this.i]["evt_days"][this.a] = this.event_day.childNodes[0].nodeValue;
			}
			this.events_arr[this.i]["desc"] = this.event.getElementsByTagName("desc")[0].childNodes[0].nodeValue;
		}
		//alert(this.events_arr[0]["evt_days"][1]);

		for(this.i=0;this.i<this.events_arr.length;this.i++){
			for(this.a=0;this.a<this.events_arr[this.i]["evt_days"].length;this.a++){



				this.o = this.events_arr[this.i]["evt_days"][this.a];
				document.getElementById("calendar_"+this.o).className = "calendar_event";
				document.getElementById("calendar_"+this.o).name = this.events_arr[this.i]["id"] + "|" + this.events_arr[this.i]["name"];
				document.getElementById("calendar_"+this.o).onclick = function () {
					this.k = this.name.split("|");
					ajaxFunction("func/events_show_item.php?id="+this.k[0], "main", "fade", 1);
				}

				this.cont = this.events_arr[this.i]["name"];
				document.getElementById("calendar_"+this.o).onmouseover = function () {
						this.tt = document.createElement("div");
						this.tt.className = "calendar_tooltip_div";
						this.tt.style.top = (getY(this)+24)+"px";
						this.tt.style.left = getX(this)+"px";
						this.tt.id = "tt";
						this.k = this.name.split("|");
						this.tt.innerHTML = "<font class='calendar_tooltip_text'>" + this.k[1] + "</font>";
						document.body.appendChild(this.tt);
						setopacity("tt", 8);
				}

				document.getElementById("calendar_"+this.o).onmouseout = function () {
					document.body.removeChild(this.tt);
				}
			}
		}
		document.getElementById(this.id+"_calendar_footer").style.visibility = "hidden";
	}
}

