/**
 * calendar widget
 * @version 0.5.4 2008-05-01
 * @author Gregor Kofler
 * 
 * @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
 */
if(!vxJS) { throw new Error("widget.PopupCalendar: vxJS core missing."); }

vxJS.widget.Calendar = function(elem, trigger, evt, locale, months) { 
	if(!elem || !trigger)	{ throw new Error("PopupCalendar: Missing element or trigger."); }
	this.locale		= locale || "date_iso";
/*	this.months		= (months || "Jan Feb Mar Apr May June July Aug Sept Oct Nov Dec").split(" ");*/
	this.months		= (months || "Jan Feb M\u00E4rz Apr Mai Juni Juli Aug Sept Okt Nov Dez").split(" "); 
	this.htmlElem	= elem;
	this.trigger	= trigger;

	var oThis = this;

	vxJS.event.addListener(trigger, evt || "click",
		function(e) {
			if(!oThis.htmlElem.disabled) {
				oThis.handleDates();
				oThis.fillCalendar();
				oThis.setPos();
				oThis.__show(oThis.layer);
			}
			e.cancelBubble = true;
		}
	);
	vxJS.event.addListener(document, "click", function() {oThis.__hide(oThis.layer);} );

	this.createCalendar();
	this.setPos();
}

vxJS.widget.Calendar.prototype = {

	createCalendar: function() {
		var d, t, r, oThis = this;

		this.layer = "div".setAttr("class", "popupCalendar").createElement();
		this.layer.style.display = "none";

		t =
			"table".createElement("tbody".createElement(
					"tr".setAttr("class","nav").createElement(
						["td", "td", "td", "td", "td"].createElement())));

		d = this.locale == "date_de" ? "MDMDFSS".split("") : "SMTWTFS".split("");

		t.firstChild.appendChild("tr".createElement(d.domWrapWithTag("th")));
		r = t.rows[0];
		r.cells[0].appendChild(document.createTextNode("\u00AB"));
		r.cells[1].appendChild(document.createTextNode("-"));
		r.cells[3].appendChild(document.createTextNode("+"));
		r.cells[4].appendChild(document.createTextNode("\u00BB"));
		r.cells[2].colSpan = 3;
		r.cells[2].className = "dragBar";

		this.layer.appendChild(t);
		document.body.appendChild(this.layer);

		vxJS.event.addListener(t, "click",
			function(e) {
				var te	= e.target || e.srcElement;
				if(te.className.split(" ")[0] == "dateCell") {
					oThis.day = te.firstChild.nodeValue;
					oThis.insertDate();
					oThis.__hide(oThis.layer);
				}
				e.cancelBubble = true;
			}
		);
		vxJS.event.addListener(r.cells[1], "click",
			function(e) {
				if(--oThis.month < 0) { oThis.month = 11; oThis.year--; }
				oThis.fillCalendar();
				e.cancelBubble = true;
			}
		);
		vxJS.event.addListener(r.cells[3], "click",
			function(e) {
				if(++oThis.month > 11) { oThis.month = 0; oThis.year++; }
				oThis.fillCalendar();
				e.cancelBubble = true;
			}
		);
		vxJS.event.addListener(r.cells[0], "click",
			function(e) {
				oThis.year--;
				oThis.fillCalendar();
				e.cancelBubble = true;
			}
		);
		vxJS.event.addListener(r.cells[4], "click",
			function(e) {
				oThis.year++;
				oThis.fillCalendar();
				e.cancelBubble = true;
			}
		);
		
	},

	setPos: function () {
		vxJS.dom.setElementPosition(this.layer, vxJS.dom.getElementOffset(this.trigger));
	},

	__show: function(elem) {
		vxJS.dom.showElement(elem);
	},

	__hide: function(elem) {
		vxJS.dom.hideElement(elem);
	},

	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(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 = this.layer.firstChild.rows[0].cells[2];
		var n = document.createTextNode(this.months[this.month]+" "+this.year);
		var 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]);
		}

		if(m.childNodes.length > 0) { m.replaceChild(n, m.firstChild); }
		else { m.appendChild(n); }

		r = "tr".createElement(trail.domWrapWithTag("td"));

		for(i = 1; i <= endTag; i++) {
			if(this.month == this.currMon && this.year == this.currYear && i == this.currDay) {
				c = "td".setAttr("class", "dateCell today").createElement(i);
			}
			else if(this.month == this.elemMon && this.year == this.elemYear && i == this.elemDay) {
				c = "td".setAttr("class", "dateCell marked").createElement(i);
			}
			else {
				c = "td".setAttr("class", "dateCell").createElement(i);
			}
			if(startCol++ % 7 == 0) {
				this.layer.firstChild.firstChild.appendChild(r);
				r = "tr".createElement();
			}
			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].createElement());
		}
		this.layer.firstChild.firstChild.appendChild(r);
	},

	insertDate: function() {
		if(this.htmlElem.disabled) { this.hide(); return; }

		var d = (""+this.day).lpad(2, "0");
		var m = (""+(this.month+1)).lpad(2, "0");
		var y = ""+this.year;

		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;
		}
	}
}
