/**
 * PopupCalendar
 * v0.4.0 2007-06-06
 * 
 * @param {object} element receiving data value
 * @param {object} element which triggers calendar
 * @param {string} type of event which triggers calendar
 * @param {string} locale for date format {date_de|date_us|date_iso}
 * @param {string} name of months separated by spaces
 * @return {object} div element
 */
function PopupCalendar(elem, trigger, evt, locale, months, allDates) {
	if(!elem || !trigger)	{ return false; }
	this.locale		= locale || "date_de";
	this.months		= (months || "Januar Februar M\u00E4rz April Mai Juni Juli August September Oktober November Dezember").split(" ");
	this.htmlElem	= elem;
	this.trigger	= trigger;
	this.allDates 	= allDates;
	
	var oThis = this;

	addEvent(trigger, evt || "click",
		function(e) {
			e = e || window.event;
			oThis.handleDates();
			oThis.fillCalendar();
			oThis.show();
			e.cancelBubble = true;
		}
	);
	addEvent(document, "click", function() 
	{
		oThis.hide();
	} );

	this.createCalendar();
	this.setEnabled();
	return this.layer;
}

PopupCalendar.prototype = {

	createCalendar: function() {
		var d, t, r, oThis = this;

		this.layer = "div".domSetAttrib("class", "popupCalendar").domCreateElement();
		this.layer.style.display = "none";

		t =
			"table".domCreateElement("tbody".domCreateElement(
					"tr".domSetAttrib("class","nav").domCreateElement(
						["td", "td", "td"].domCreateElement())));

		d = this.locale == "date_de" ? "M D M D F S S".split(" ") : "S M T W T F S".split(" ");

		t.firstChild.appendChild("tr".domCreateElement(d.domWrapWithTag("th")));
		r = t.rows[0];
		r.cells[0].appendChild(document.createTextNode("\u00AB"));
		r.cells[2].appendChild(document.createTextNode("\u00BB"));
		r.cells[1].colSpan = 5;
		r.cells[1].className = "dragBar";

		this.layer.appendChild(t);
		document.body.appendChild(this.layer);

		addEvent(t, "click",
			function(e) {
				e = e || window.event;
				targetElem	= e.target || e.srcElement;
				/* AS 03032008 Nur g�ltiges Datum zulassen sonst bleibt der Dialog offen */
				if(targetElem.className.split(" ")[0] == "dateCell") {
					oThis.day = targetElem.firstChild.nodeValue;
					if( oThis.insertDate() )
						oThis.hide();
					else
						oThis.fillCalendar();	
				}
				e.cancelBubble = true;
			}
		);
		addEvent(r.cells[0], "click",
			function(e) {
				e = e || window.event;
				if(--oThis.month < 0) { oThis.month = 11; oThis.year--; }
					oThis.fillCalendar();
					e.cancelBubble = true;
				}
		);
		addEvent(r.cells[2], "click",
			function(e) {
				e = e || window.event;
				if(++oThis.month > 11) { oThis.month = 0; oThis.year++; }
				oThis.fillCalendar();
				e.cancelBubble = true;
			}
		);
	},

	getPosition: function () {
//var s1 = document.getElementSize(this.layer);
		var s1 = [0, 100];
		var s2 = document.getElementSize(this.trigger);
		var o = document.getElementOffset(this.trigger);
		document.setElementPosition(this.layer, [o[0], o[1]-s1[1]+s2[1]]);
	},

	setEnabled: function() {
		this.enabled = !this.htmlElem.disabled;
		if(!this.enabled) { this.hide(); }
	},

	show: function() {
		if(this.enabled) {
			this.getPosition();
			this.layer.style.display = "block";
		}
	},

	hide: function() {
		this.layer.style.display = "none";
	},

	handleDates: function() {
		var elemD, now = new Date();

		this.day	= this.currDay	= now.getDate();
		this.month	= this.currMon	= now.getMonth();
		this.year	= this.currYear	= now.getFullYear();
		this.elemDay = this.elemMon = this.elemYear = 0;
       if (isNaN(this.htmlElem.value.toDateTime(this.locale, true)) == 1) {
           // Nichts tun - Eingabefeld ist offensichtlich nicht mit einem Datum befüllt
       }
       else if(elemD = this.htmlElem.value.toDateTime(this.locale, true)) {
			this.day	= this.elemDay	= elemD.getDate();
			this.month	= this.elemMon	= elemD.getMonth();
			this.year	= this.elemYear	= elemD.getFullYear();
		}
	},

	fillCalendar: function() {
		var d = new Date(this.year, this.month, 1);
		var startTag = d.getDay(), endTag = d.getDaysOfMonth();
		var startCol = this.locale == "date_de" ? ((startTag == 0) ? 6 : startTag-1) : startTag;
		var m, n, r, i, c, trail = [];
	
		trail.fill("" ,startCol);

		for(i = this.layer.firstChild.rows.length-1; i > 1; i--) {
			this.layer.firstChild.firstChild.removeChild(this.layer.firstChild.rows[i]);
		}

		m = this.layer.firstChild.rows[0].cells[1];
		n = document.createTextNode(this.months[this.month]+" "+this.year);

		if(m.childNodes.length > 0) { m.replaceChild(n, m.firstChild); }
		else { m.appendChild(n); }

		r = "tr".domCreateElement(trail.domWrapWithTag("td"));

		for(i = 1; i <= endTag; i++) {
			if(this.month == this.currMon && this.year == this.currYear && i == this.currDay) {
				c = "td".domSetAttrib("class", "dateCell today").domCreateElement(i);
			}
			else if(this.month == this.elemMon && this.year == this.elemYear && i == this.elemDay) {
				c = "td".domSetAttrib("class", "dateCell marked").domCreateElement(i);
			}
			else {
				c = "td".domSetAttrib("class", "dateCell").domCreateElement(i);
			}
			/* AS 03032008: Tage die vor dem heutigen Datum liegen werden ausgegraut */
			if(this.month == this.currMon && this.year == this.currYear && i < this.currDay  && !this.allDates) {
				c = "td".domSetAttrib("class", "dateCell darken").domCreateElement(i);
			}
			
			if(((this.month < this.currMon && this.year <= this.currYear) || this.year < this.currYear) && !this.allDates) {
				c = "td".domSetAttrib("class", "dateCell darken").domCreateElement(i);
			}
			
			if(startCol++ % 7 == 0) {
				this.layer.firstChild.firstChild.appendChild(r);
				r = "tr".domCreateElement();
			}
			r.appendChild(c);
		}

		trail = [];
		trail.fill("td", 7 - (startCol % 7 != 0 ? startCol % 7 : 7));
		for(i = 0; i < trail.length; i++) {
			r.appendChild(trail[i].domCreateElement());
		}
		this.layer.firstChild.firstChild.appendChild(r);
	},

	insertDate: function() {
		//customized for vvt/ivb

		var d = this.day.toString().lpad(2, "0");
		var m = (this.month+1).toString().lpad(2, "0");
		var y = this.year.toString();

		/* AS 03032008 bereits vergangene Tage sollen nicht klickbar sein... */
		if( d < this.currDay && m == (this.currMon + 1) && y <= this.currYear  && !this.allDates) {
			return false;
		}
				
		if( (( m < (this.currMon + 1) && y <= this.currYear) || y < this.currYear ) && !this.allDates ) {
			return false;
		}
			

		switch (this.locale) {
			case "date_de":
				this.htmlElem.value = d+"."+m+"."+y; break;
			case "date_us":
				this.htmlElem.value = m+"/"+d+"/"+y; break;
			case "date_iso":
				this.htmlElem.value = y+"-"+m+"-"+d; break;
		}
		
		/* AS 03032008 Focus wird in das Textfeld gesetzt, damit eine onBlur �berpr�fung greift */
		this.htmlElem.focus();
		this.htmlElem.select();
		return true;
	}
}