MxCalendar = Class.create();
MxCalendar.prototype = {

	added: false,
	visible: false,

	monthTitles: [[
		"Январь",
		"Февраль",
		"Март",
		"Апрель",
		"Май",
		"Июнь",
		"Июль",
		"Август",
		"Сентябрь",
		"Октябрь",
		"Ноябрь",
		"Декабрь"
	],[
		"January",
		"February",
		"March",
		"April",
		"May",
		"June",
		"July",
		"August",
		"September",
		"Oktober",
		"Novermber",
		"December"
        ]][LC_Index],

	show: function() {
		if (this.anchor)
			this.setPosition();
		this.element.show();
		this.element.focus();
		this.visible=true;

		this.iframe = $(Builder.node('iframe'));
		this.element.setStyle({'z-index': 200});
		this.iframe.setStyle({'z-index': 1, position: 'absolute', border: 'none'});
		document.body.appendChild(this.iframe);
		Position.clone(this.element, this.iframe);

		this._window_click = this.window_click.bind (this);
		Event.observe (window, "click", this._window_click);
	},

	hide: function() {
		this.element.hide();
		this.visible=false;
		if (this._window_click) {
			Event.stopObserving (window, "click", this._window_click);
			this._window_click = undefined;
		}
		this.iframe.remove();
	},

	bindToAnchor: function(anchor) {
		this.anchor = anchor;
		this.element.hide();
		if (!this.added) {
			this.added=true;
			document.body.appendChild(this.element);
		}
	},

	setPosition: function() {
		var style=$H();
		style["left"] = Position.cumulativeOffset(this.anchor)[0]+"px";
		style["top"] = (Position.cumulativeOffset(this.anchor)[1]+this.anchor.getHeight())+"px";
		this.element.setStyle(style);
	},

	initialize: function() {
		this.initDefault();
		this.element = Element.extend(document.createElement("table"));
		this.element.addClassName("calendar2");

		this.build();
	},

	initDefault: function() {
		var date = new Date();
		this.year = date.getYear();
		if (this.year<1900) this.year+=1900;
		this.month = date.getMonth();
	},

	rebuild: function() {
		this.destroy();
		this.build();
	},

	destroy: function() {
		this.element.immediateDescendants().each (function(item) {item.remove();});
	},

	buildHeader: function() {
		var thead = Element.extend(document.createElement("thead"));
		this.element.appendChild(thead);


		var head_tr = thead.insertRow(thead.rows.length);

		var td = Element.extend(document.createElement("td"));
		var a = Element.extend(document.createElement("a"));
		a.update("‹‹");
		a.href="#";
		td.appendChild(a);
		td.addClassName("navbutton");
		td.setStyle({cursor: "pointer"});
		head_tr.appendChild(td);
		Event.observe(td,"click", function(e) {
			Event.stop(e);
			this.year--;
			this.rebuild();
		}.bind(this));

		var td = Element.extend(document.createElement("td"));
		var a = Element.extend(document.createElement("a"));
		a.update("‹");
		a.href="#";
		td.appendChild(a);
		td.addClassName("navbutton");
		td.setStyle({cursor: "pointer"});
		head_tr.appendChild(td);
		Event.observe(td,"click", function(e) {
			Event.stop(e);
			this.month--;
			if (this.month<0) {
				this.month=11;
				this.year--;
			}
			this.rebuild();
		}.bind(this));

		var td = Element.extend(document.createElement("td"));
		td.update(this.monthTitles[this.month]+" "+this.year);
		head_tr.appendChild(td);

		var td = Element.extend(document.createElement("td"));
		var a = Element.extend(document.createElement("a"));
		td.appendChild(a);
		td.addClassName("navbutton");
		a.href="#";
		a.update("›");
		td.setStyle({cursor: "pointer"});
		head_tr.appendChild(td);
		Event.observe(td,"click", function(e) {
			Event.stop(e);
			this.month++;
			if (this.month>11) {
				this.month=0;
				this.year++;
			}
			this.rebuild();
		}.bind(this));

		var td = Element.extend(document.createElement("td"));
		var a = Element.extend(document.createElement("a"));
		td.appendChild(a);
		td.addClassName("navbutton");
		a.href="#";
		a.update("››");
		td.setStyle({cursor: "pointer"});
		head_tr.appendChild(td);
		Event.observe(td,"click", function(e) {
			Event.stop(e);
			this.year++;
			this.rebuild();
		}.bind(this));
	},

	buildBody: function() {
		var tbody = Element.extend(document.createElement("tbody"));
		this.element.appendChild(tbody);
		var h_tr = Element.extend(document.createElement("tr"));
		tbody.appendChild(h_tr);
		var h_td = Element.extend(document.createElement("td"));
		h_td.colSpan=5;
		h_tr.appendChild(h_td);


		var table = Element.extend(document.createElement("table"));
		h_td.appendChild(table);

		var table_header = Element.extend(document.createElement("thead"));
		table.appendChild(table_header);
		var table_header_tr = table_header.insertRow(table_header.rows.length);

		[["пн","вт","ср","чт","пт","сб","вс"],["mo","tu","we","th","fr","sa","su"]][LC_Index].each (function(item) {
			var th = Element.extend (document.createElement("th"));
			th.update(item);
			this.appendChild(th);
		}.bind(table_header_tr));



		var table_data = Element.extend(document.createElement("tbody"));
		table.appendChild(table_data);
		tr = table_data.insertRow(table_data.rows.length);


		var builderDate = new Date();
		builderDate.setYear(this.year);
		builderDate.setMonth(this.month);
		builderDate.setDate(1);

		var prependDays = builderDate.getDay()-1;
		if (prependDays<0) prependDays+=7;

		for (var i=0;i<prependDays;i++) {
			var td = Element.extend(document.createElement("td"));
			tr.appendChild(td);
			td.update("&nbsp;");
		}
		while (builderDate.getMonth() == this.month) {

			if ((builderDate.getDay()==1) && (tr.cells.length>0)) {
				tr = table_data.insertRow(table_data.rows.length);
			}

			td = Element.extend(document.createElement("td"));
			var a = Element.extend(document.createElement("a"));
			a.href="#";
			td.appendChild(a);
			td.monthDay = builderDate.getDate();
			td.setStyle({cursor: "pointer"});
			a.update (builderDate.getDate());
			tr.appendChild(td);
			Event.observe(td,"click",function(item,e) {
				Event.stop(e);
				this.date=item.monthDay;
				if (this.onSelect)
					this.onSelect();
			}.bind(this,td));
			if ((builderDate.getDay()<6) & (builderDate.getDay()>0)) {
				Event.observe(td,"mouseover", function(e) {
					this.addClassName("hover");
				}.bind(td));
				Event.observe(td,"mouseout", function(e) {
					this.removeClassName("hover");
				}.bind(td));
			}
			else {
				td.addClassName("weekend");
			}

			var today = new Date();

			if (this.selected &&
				(builderDate.getDate() == this.selected.getDate()) &&
				(builderDate.getMonth() == this.selected.getMonth()) &&
				(builderDate.getYear() == this.selected.getYear())) {

					td.addClassName("selected");

				}
			else if ( (today.getYear() == builderDate.getYear()) &&
				 (today.getMonth() == builderDate.getMonth()) &&
				 (today.getDate() == builderDate.getDate()))

				 td.addClassName("today");

			builderDate.setDate(builderDate.getDate()+1);
		}

		while (builderDate.getDay()!=1) {
			td = Element.extend(document.createElement("td"));
			td.update ("&nbsp;");
			tr.appendChild(td);
			builderDate.setDate(builderDate.getDate()+1);
		}
	},

	buildFooter: function() {
		var tfoot = Element.extend(document.createElement("tfoot"));
		this.element.appendChild(tfoot);

		var tr = Element.extend (document.createElement("tr"));
		tfoot.appendChild(tr);

		var td = Element.extend (document.createElement("td"));
		tr.appendChild(td);
		td.colSpan=5;

		var a = Element.extend (document.createElement("a"));
		td.appendChild(a);
		a.href="#";
		a.update([["Сегодня"],["Today"]][LC_Index]);

		Event.observe(a,"click",function(e) {
			Event.stop(e);
			this.selected = new Date();
			this.year = this.selected.getYear();
			if (this.year<1900) this.year+=1900;
			this.month = this.selected.getMonth();
			this.rebuild();
		}.bind(this));
	},

	build: function () {

		this.buildHeader();
		this.buildBody();
		this.buildFooter();

	},

	window_click: function(e) {
		Event.stop(e);
		this.hide();
	}

};

MxCalendar.create = function (a, textfield,element) {
	var calendar = new MxCalendar();
	a.calendar = calendar;
	calendar.textfield=textfield;
	calendar.lElement=element;

	Event.observe(a,"click",function(calendar, e) {
	    Event.stop(e);

		if (calendar.visible) {
			calendar.hide();
		}
		else {
			calendar.bindToAnchor(this);

			if (calendar.textfield.disabled) return;
			var d = Finder.Date.stringDBToDate(calendar.lElement.getValue());
			calendar.year = d.getYear();
			if (calendar.year<1900) calendar.year+=1900;
			calendar.month = d.getMonth();
			calendar.selected=d;
			calendar.rebuild();
			calendar.show();
		}
	}.bind(a,calendar));

	calendar.onSelect = function () {
		var newDate = new Date(this.year, this.month, this.date);
		this.hide();
		this.lElement.setValue(Finder.Date.dateToString(newDate));
		this.lElement.dateValue = newDate;
		this.lElement.manualSelected = true;
		this.lElement.update();
		this.lElement.manualSelected = false;
	};
}
