Bw.Widgets.Calendar = 
{
	superclassName: "Bw.Widgets.Editable",
	selfclassName: "Bw.Widgets.Calendar",
	
	create: function (date)
	{
		var obj = document.createElement ("div");
	
		if (date) obj.setAttribute ("date", date);
		
		Bw.Core.bootstrap (obj, this.selfclassName);
		
		return obj;
	},
	
	initialize: function ()
	{
		Bw.Widgets.Widget.initialize.call (this);
		
		this.calendar = null;
			
		var d = this.getAttribute ("date");
		this.currDate = (d) ? Bw.Date.Helpers.read (d) : new Date ();
		
		this.draw();
		
		this.onaction = this.getAttribute ("onaction");
		this.onselect = this.getAttribute ("onselect");
		
		return false;
	},
	
	draw: function ()
	{
		var r = this.drawHeader();
		this.appendChild (r);
		
		r = this.drawDays();
		this.appendChild (r);
		
		r = this.drawMonth();
		this.appendChild (r);
		
		this.month = r;
	},
	
	redraw: function ()
	{
		this.monthLabel.setValue (Bw.Date.MONTH_LABELS[this.currDate.getMonth()]);
		this.yearLabel.setValue (this.currDate.getFullYear());
		
		var m = this.month;
		if (m) this.removeChild (m);
		
		m = this.drawMonth();
		this.appendChild (m);
		
		this.month = m;
	},
	
	drawHeader: function ()
	{
		var self = this;
		
		var r = document.createElement ("div");
		r.className = "controls";
		
		var b = document.createElement ("div");
		b.className = "control";
		r.appendChild (b);
		
		var e = document.createElement ("div");
		b.appendChild (e);
		
		var yl = Bw.Widgets.StockIcon.create ("leftleft");		
		e.appendChild (yl);
		
		var ml = Bw.Widgets.StockIcon.create ("left");
		e.appendChild (ml);
		
		var b = document.createElement ("div");
		b.className = "display";
		r.appendChild (b);

		var mm = Bw.Widgets.Label.create (Bw.Date.MONTH_LABELS[this.currDate.getMonth()]);
		var sp = Bw.Widgets.Label.create ("&nbsp;");
		var yy = Bw.Widgets.Label.create (this.currDate.getFullYear());
		b.appendChild (mm);
		b.appendChild (sp);
		b.appendChild (yy);
		
		this.monthLabel = mm;
		this.yearLabel = yy;
		
		var b = document.createElement ("div");
		b.className = "control";
		r.appendChild (b);
	
		var e = document.createElement ("div");
		b.appendChild (e);
		
		var mr = Bw.Widgets.StockIcon.create ("right");
		e.appendChild (mr);
		
		var yr = Bw.Widgets.StockIcon.create ("rightright");
		e.appendChild (yr);
		
		yl.onclick = function(){ self.changeYear (-1); };
		ml.onclick = function(){ self.changeMonth(-1); };
		mr.onclick = function(){ self.changeMonth(1); };
		yr.onclick = function(){ self.changeYear (1); };
		
		return r;
	},
	
	drawRow: function ()
	{
		var r = document.createElement ("div");
		r.className = "row";
		
		for (var i = 0; i < 7; i++)
		{
			var c = document.createElement ("div");
			c.className = "cell";
			r.appendChild (c);
			
			var v = document.createElement ("div");
			v.className = "cellValue";
			c.appendChild (v); 
		}
		
		return r;
	},
	
	drawDays: function ()
	{
		var r = this.drawRow ();
		r.className += " weekDays";
		
		for (var i = 0; i < 7; i++)
		{
			var c = r.childNodes[i].firstChild;
			var l = Bw.Widgets.Label.create (Bw.Date.SHORT_DAY_LABELS[i]);
			c.appendChild (l);
		}
		
		return r;
	},
	
	drawMonth: function ()
	{
		var self = this;
		
		var cal = new Date();
		cal.setDate (1);
		cal.setMonth (this.currDate.getMonth());
		cal.setFullYear (this.currDate.getFullYear());
		
		var startDate = new Date (cal);
		var startOffset = Bw.Date.Helpers.getDay (startDate);
		
		var m = document.createElement ("div");
		var r;
		
		while (cal.getMonth() == this.currDate.getMonth())
		{
			var d = cal.getDate();
			var j = Bw.Date.Helpers.getDay (cal);
			if (j == 0 || !r)
			{
				r = this.drawRow();
				m.appendChild (r);
			}
			
			var v = r.childNodes[j].firstChild;
			var l = Bw.Widgets.Label.create (d);
			v.onclick = function () { self.changeDay (this.firstChild.getValue()) };
			v.ondblclick = function () { if (self.onaction) return Bw.Util.call (self, self.onaction); };
			v.appendChild (l);
			if (d == this.currDate.getDate())
			{
				v.className += " selected";
			}
			
			Bw.Date.Helpers.addDays (cal, 1);
		}
		
		var stopDate = new Date (cal);
		var stopOffset = Bw.Date.Helpers.getDay (stopDate);
		
		r = m.firstChild;
		for (var i = startOffset - 1; i >= 0; i--)
		{
			Bw.Date.Helpers.addDays (startDate, -1);

			var c = r.childNodes[i];
			c.style.backgroundColor = "#eeeeee";
			c.style.color = "#bbbbbb";
			
			var l = Bw.Widgets.Label.create (startDate.getDate());
			c.firstChild.appendChild (l);
		}
		
		r = m.lastChild;
		for (var i = stopOffset; i != 0 && i < 7; i++)
		{
			var c = r.childNodes[i];
			c.style.backgroundColor = "#eeeeee";
			c.style.color = "#bbbbbb";
			
			var l = Bw.Widgets.Label.create (stopDate.getDate());
			c.firstChild.appendChild (l);
			
			Bw.Date.Helpers.addDays (stopDate, 1);
		}
		
		return m;
	},
	
	changeDay: function (d)
	{
		var date = new Date (this.currDate);
		date.setDate (d);
		
		var c = this.dateToCell (this.currDate);
		c.className = "cellValue";

		c = this.dateToCell (date);
		c.className += " selected";
		
		this.currDate = date;
		
		this.selectionChanged();
	},
	
	getValue: function ()
	{
		return this.currDate;
	},
	
	setValue: function (d)
	{
		var c = this.currDate;

		if (d.getMonth() != c.getMonth() || d.getFullYear() != c.getFullYear())
		{
			this.currDate = d;
			this.redraw ();
		}
		else
		{
			this.changeDay (d.getDate());
		}
	},
	
	dateToCell: function (t)
	{
		var d = new Date (t);
		var j = d.getDate() - 1;
		var s = Bw.Date.Helpers.monthStart (new Date (d));
		var o = Bw.Date.Helpers.getDay (s);
		
		var y = Math.floor (j / 7);
		var x = j - (y * 7) + o;
		if (x > 6)
		{
			x -= 7;
			y++;
		}
	
		return this.month.childNodes[y].childNodes[x].firstChild;
	},
	
	changeYear: function (o)
	{
		this.setValue (Bw.Date.Helpers.addYears (new Date(this.currDate), o));
 		this.selectionChanged();
	},
	
	changeMonth: function (o)
	{
		this.setValue (Bw.Date.Helpers.addMonths (new Date(this.currDate), o));
 		this.selectionChanged();
	},
	
	selectionChanged: function ()
	{
		if (this.onselect) return Bw.Util.call (this, this.onselect);
	}
};

