/**
 * vhsgg
 *
 * Created 02.12.2007 by Petra Arentzen - all rights reserved
 * @version		CVS: $Id$
 * @copyright Copyright (C) 2009 PEGU Consulting, P. Arentzen - all rights reserved
 *
 * @package		user_pgkufcon
 **/

function PG_Calendar() {

	this.setDayNames = function(a_day_names) {
		if (arguments.length != 7) {
			return;
		}
		this.day_names = new Array();
		this.day_names_short = new Array();
		for (var ii=0; ii < 7; ii++) {
			this.day_names.push(arguments[ii]);
		}

	}

	this.setMonthNames = function(a_month_names) {
		if (arguments.length != 12) {
			return;
		}
		this.month_names = new Array();
		for (var ii = 0; ii < 12; ii++) {
			this.month_names.push(arguments[ii]);
		}
	}

	this.setDateFormat = function(a_date_format) {
		this.date_format = a_date_format;
	}

	if (document.onmousedown != null) {
		this.old_onmousedown = document.onmousedown;
		document.onmousedown = function(e) {
			window.pg_calendar.save_mouse_pos(e);
			window.pg_calendar.old_onmousedown(e);
		}
	} else {
		document.onmousedown = function(e) {
			window.pg_calendar.save_mouse_pos(e);
		}
	}

	this.testBrowser = function() {
		//alert(navigator.appVersion);
		var ie_test = /MSIE\s(\d+\.\d+)/;
		if (ie_test.test(navigator.appVersion)) {
			this.is_ie = true;
			this.ver = RegExp.$1;
		} else {
			this.is_ie = false;
		}
	}


	this.createBox = function() {
		var box = document.createElement('div');
		box.className = 'pg-date-outerbox';
		box.style.height = "auto";
		box.style.width = "auto";
		//box.style.backgroundColor = "red";
		box.style.position = 'absolute';
		box.id = 'pg_calender_box';
		box.style.visibility = 'hidden';
		box.style.marginTop = '0';
		box.style.marginBottom = '0';
		box.style.marginLeft = '0';
		box.style.marginRight = '0';
		box.style.paddingTop = '2px';
		box.style.paddingBottom = '2px';
		box.style.paddingLeft = '2px';
		box.style.paddingRight = '2px';
		/*
		box.style.left = window.pg_mousePos.X;
		box.style.top =  window.pg_mousePos.Y;
		*/

		document.getElementsByTagName('body')[0].appendChild(box);
		box.onmouseout = window.pg_calendar.mouseout;
		box.onmouseover = window.pg_calendar.mouseover;

		return box;

	}

	this.getRealDay = function(a_date) {
		var retval = a_date.getDay()-1;
		if (retval == -1) retval = 6;
		return retval;

	}

	this.cloneDate = function() {
		var retval = new Date();
		retval.setYear(this.cur_date.getFullYear());
		retval.setMonth(this.cur_date.getMonth());
		retval.setDate(this.cur_date.getDate());
		return retval;
	}

	this.previousYear = function() {
		var tmp_date = this.cloneDate();

		var y = this.cur_date.getFullYear();
		y--;
		this.cur_date.setYear(y);
		this.rebuildCalendar();
	}

	this.nextYear = function() {
		var y = this.cur_date.getFullYear();
		y++;
		this.cur_date.setYear(y);
		this.rebuildCalendar();
	}

	this.previousMonth = function() {
		var m = this.cur_date.getMonth();
		m--;
		this.cur_date.setMonth(m);
		this.rebuildCalendar();
	}

	this.nextMonth = function() {
		var m = this.cur_date.getMonth();
		m++;
		this.cur_date.setMonth(m);
		this.rebuildCalendar();
	}

	this.rebuildCalendar = function() {
		var box = document.getElementById('pg_calender_box');

		if (box == undefined) {
			box = window.pg_calendar.createBox();
		}

		/*
		box.style.left = (window.pg_calendar.pg_mousePos.X-2)+"px";
		box.style.top =  (window.pg_calendar.pg_mousePos.Y-2)+"px";
		*/
		box.innerHTML = this.buildCalendar();

		box.style.visibility = 'visible';
		//window.pg_calendar.timer = window.setInterval("window.pg_calendar.hideBox()",5000);
	}


	this.buildCalendar = function() {
		var cal = '<table class="pg-date-table">';

		cal += '<tr><th colspan="7" class="pg-date-nav">';
		cal += '<table border="0" align="center" width="100%">';
		cal += '<tr>';

		cal += '<th width="1%"><a href="#" onclick="window.pg_calendar.previousYear(); return false">&lt;&lt;</a>&nbsp;&nbsp;';
		cal += '<a href="#" onclick="window.pg_calendar.previousMonth(); return false">&lt;</a></th>';
		cal += '<th>'+this.getMonthName()+' '+this.cur_date.getFullYear()+'</th>';
		cal += '<th width="1%"><a href="#" onclick="window.pg_calendar.nextMonth(); return false">&gt;</a>&nbsp;&nbsp;';
		cal += '<a href="#" onclick="window.pg_calendar.nextYear(); return false">&gt;&gt;</a></th>';


		cal += '</tr>';
		cal += '</table>';
		cal += '</th></tr>';


		for (var ii = 0; ii < 7; ii++) {
			cal += '<th class="pg-date-day-caption">'+this.getDayNameShort(ii)+'</th>';
		}

		cal += '</tr>';


		var day = new Date();
		day.setMonth(this.cur_date.getMonth());
		day.setYear(this.cur_date.getFullYear());
		day.setDate(1);

		var offset = this.getRealDay(day)*-1+1;

		var day_of_month = offset;
		var not_stop = 0;
		do {
			cal += '<tr>';
			for (var ii = 0; ii < 7; ii++) {
				var d = day.getDate();
				day.setDate(1);
				day.setMonth(this.cur_date.getMonth());
				day.setYear(this.cur_date.getFullYear());
				//alert(day_of_month);
				day.setDate(day_of_month);

				var day_html = day.getDate();
				if (day.getMonth() != this.cur_date.getMonth()) {
					var td_class="pg-out-of-month";
				} else {
					if (day.getDate() == this.cur_date.getDate()) {
						var td_class="pg-in-month-current";
					} else {
						var td_class="pg-in-month";
					}

					if (this.is_ie) {
						var hover_code = ' onmouseover="window.pg_calendar.calmouseover(this)" onmouseout="window.pg_calendar.calmouseout(this)"';
					} else {
						var hover_code = '';
					}

					day_html = '<a href="#"'+hover_code+' onclick="window.pg_calendar.selectDate(this,\''+this.getDateStr(day)+'\'); return false;" title="Doppelklick zum Übernehmen">'+day_html+'</a>';
				}

				if (this.getRealDay(day) >= 5) {
					td_class += ' pg-weekend';
				}
				cal += '<td class="'+td_class+'">'+day_html+'</td>';
				day_of_month++;
			}
			cal += '</tr>';

			not_stop++;
			if (not_stop > 7) {
				alert(not_stop);
				break;
			}

		} while ((day.getMonth() <= this.cur_date.getMonth()) && (day.getFullYear() <= this.cur_date.getFullYear()));


		cal += '<tr><th colspan="7" class="pg-date-close">';
		cal += '<a href="#" onclick="window.pg_calendar.hideBox()">'+this.cancel_label+'</a>';
		cal += '</th></tr>';

		cal += '</table>';

		return cal;
	}

	this.selectDate = function(obj,a_day_str) {
		if (this.sel_date == a_day_str) { // simulate double click
			this.setDate(a_day_str);
			if (window.pg_calendar.field_name != undefined) {
				document.forms[window.pg_calendar.form_name].elements[window.pg_calendar.field_name].value = a_day_str;
				window.pg_calendar.hideBox();
			}
		} else {
			if (this.selected_td != undefined) {
				this.selected_td.className = this.selected_td.oldClassName;
			}

			obj.parentNode.oldClassName = obj.parentNode.className;
			obj.parentNode.className = 'pg-in-month-selected';
			this.selected_td = obj.parentNode;
			this.sel_date = a_day_str;
		}

		//alert(a_day_str);
	}

	this.calmouseover = function(obj) {
		if (obj.parentNode.outClassName == undefined) {
			obj.parentNode.outClassName = obj.parentNode.className;
		}

		obj.parentNode.className = obj.parentNode.outClassName+'-pg-hover';


	}

	this.calmouseout = function(obj) {
		if (obj.parentNode.outClassName != undefined) {
			obj.parentNode.className = obj.parentNode.outClassName;
			obj.parentNode.outClassName = undefined;
		}

	}

	this.mouseout = function() {
		//window.pg_calendar.timer = window.setInterval("window.pg_calendar.hideBox()",1000);

	}

	this.mouseover = function() {
		if (window.pg_calendar.timer != undefined) {
			window.clearInterval(window.pg_calendar.timer);
			window.pg_calendar.timer = undefined;
		}
	}


	this.hideBox = function() {
		if (window.pg_calendar.timer != undefined) {
			window.clearInterval(window.pg_calendar.timer);
			window.pg_calendar.timer = undefined;
		}
		document.getElementById('pg_calender_box').style.visibility = 'hidden';

	}

	this.save_mouse_pos = function(evt) {
		if (!evt) {
			evt = window.event;
		}

		window.pg_calendar.pg_mousePos = new Object();
		window.pg_calendar.pg_mousePos.X = evt.clientX;
		window.pg_calendar.pg_mousePos.Y = evt.clientY;
	}

	this.show = function(form_name,field_name) {
		this.selected_td = undefined;
		this.sel_date = '';

		if ((document.forms[form_name] != undefined) && (document.forms[form_name].elements[field_name] != undefined)) {
			this.form_name = form_name;
			this.field_name = field_name;
			this.setDate(document.forms[form_name].elements[field_name].value);
		} else {
			this.form_name = undefined;
			this.field_name = undefined;
		}

		var box = document.getElementById('pg_calender_box');
		if (box == undefined) {
			box = window.pg_calendar.createBox();
		}

		box.style.left = (window.pg_calendar.pg_mousePos.X-2)+"px";
		box.style.top =  (window.pg_calendar.pg_mousePos.Y-2)+"px";
		box.innerHTML = this.buildCalendar();

		box.style.visibility = 'visible';
		//window.pg_calendar.timer = window.setInterval("window.pg_calendar.hideBox()",5000);
		box.style.zIndex = '9999';

	}

	this.setDate = function(a_new_date_str) {

		var a_date = new Array();
		var tmp_date = new Date();
		a_date['d'] = tmp_date.getDate();
		a_date['m'] = tmp_date.getMonth()+1;
		a_date['y'] = tmp_date.getYear();

		var a_array = a_new_date_str.split(/[^\d]+/);

		//alert(a_array);

		var a_fields = this.date_format.split(/[^dmy]+/i);

		if ((a_array.length == 3) && (a_fields.length == 3)) {
			for (var ii=0; ii < 3; ii++) {
				a_date[a_fields[ii].toLowerCase()] = a_array[ii]*1;
			}
		}

		var err = false;
		try {
			tmp_date.setDate(a_date['d']);
			tmp_date.setMonth(a_date['m']-1);

			if ((a_date['y'] > 50) && (a_date['y'] < 1000)) {
				var x = new String(a_date['y']);
				a_date['y'] = ("20"+(x.substr(x.substr.length-1,2)))*1;
			}

			if (a_date['y'] < 50) {
				a_date['y'] += 2000;
			}
			tmp_date.setYear(a_date['y']);
		} catch (e) {
			err = true;
		}

		if (err == false) {
			this.cur_date = tmp_date;
		}

		//alert(this.cur_date);
	}

	this.getMonthName = function() {
		return this.month_names[this.cur_date.getMonth()];
	}

	this.getDayName = function(a_d) {
		return this.day_names[a_d];
	}

	this.getDayNameShort = function(a_d) {
		var retval = this.day_names[a_d];
		retval = retval.substr(0,2);

		return retval;
	}

	this.getDateStr = function(a_date) {
		var retval = this.date_format;

		if (a_date == undefined) {
			a_date = this.cur_date;
		}

		var d = new String('00'+a_date.getDate());
		var m = new String('00'+(a_date.getMonth()+1));
		var y = new String('00'+a_date.getYear());
		var Y = new String('00'+a_date.getFullYear());

		d = d.substr(d.length-2,2);
		m = m.substr(m.length-2,2);
		y = y.substr(y.length-2,2);
		Y = Y.substr(Y.length-4,4);

		retval = retval.replace(/d/,d);
		retval = retval.replace(/m/,m);
		retval = retval.replace(/y/,y);
		retval = retval.replace(/Y/,Y);

		return retval;
	}

	window.pg_calendar = this;

	this.day_names = new Array();
	this.day_names_short = new Array();
	this.month_names = new Array();
	this.date_format = 'd.m.Y';

	this.pg_mousePos = new Object();
	this.pg_mousePos.X = 0;
	this.pg_mousePos.Y = 0;

	this.selected_td = undefined;


	this.setDayNames("Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag","Sonntag");
	this.setMonthNames(
		"Januar","Februar","März",
		"April","Mai","Juni",
		"Juli","August","September",
		"Oktober","November","Dezember"
	);

	this.testBrowser();

	this.cur_date = new Date();
	this.sel_date = '';

	this.cancel_label = 'Abbrechen';

	this.form_name = undefined;
	this.field_name = undefined;
}

new PG_Calendar();


/* DUMMY */
function setRowClass() {}


