﻿/// <reference path="../Lib.js" />

var CILA_Calendar = new Class();
CILA_Calendar.prototype =
{
	language:
	{
		en:
		{
			Months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
			Days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thurday', 'Friday', 'Saturday'],
			SortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
		},

		vn:
		{
			Months: ['Tháng Giêng', 'Tháng Hai', 'Tháng Ba', 'Tháng Tư', 'Tháng Năm', 'Tháng Sáu', 'Tháng Bảy', 'Tháng Tám', 'Tháng Chín', 'Tháng Mười', 'Tháng Mười Một', 'Tháng Mười Hai'],
			Days: ['Chủ Nhật', 'Thứ Hai', 'Thứ Ba', 'Thứ Tư', 'Thứ Năm', 'Thứ Sáu', 'Thứ Bảy'],
			SortDays: ['CN', 'Hai', 'Ba', 'Tư', 'Năm', 'Sáu', 'Bảy']
		}
	},

	button: null,
	textbox: null,
	date: null,
	lang: null,
	useTime: null,

	init: function()
	{
		this.button = get(this.button);
		this.textbox = get(this.textbox);
		if (!this.date) this.date = new Date();
		if (!this.lang) this.lang = "en";
		if (this.useTime == null) this.useTime = true;

		this.create();
	},

	create: function()
	{
		var span = document.createElement("SPAN");
		var p = this.button.parentNode;
		p.insertBefore(span, this.button);
		p.removeChild(this.button);
		span.appendChild(this.button);
		Element.hide(span);
		var top = this.button.offsetHeight;
		var cons =
		[
			["DIV", { ID: "WIN", className: "CILA_Calendar_Container", style: { left: "0px", top: top + "px", visibility: "hidden"} }, [
		//["DIV", { className: "CILA_Calendar_Shadow"}],
				["DIV", { className: "CILA_Calendar_Border1" }, [
					["DIV", { ID: "MOVE", className: "CILA_Calendar_Border2" }, [
						["DIV", { className: "CILA_Calendar_Border3" }, [
							["DIV", { className: "CILA_Calendar_DayZone" }, [
								["DIV", { style: { textAlign: "center", paddingBottom: "5px"} }, [
									["INPUT", { type: "button", className: "CILA_Calendar_Button2", value: "«", onclick: this._incYear.handle(this, -1)}],
									["INPUT", { type: "button", className: "CILA_Calendar_Button1", value: "◄", onclick: this._incMonth.handle(this, -1)}],
									["INPUT", { ID: "MONTH", type: "text", className: "CILA_Calendar_TextBox1", value: "Tháng 10", readOnly: true}],
									["SPAN", { text: ", "}],
									["INPUT", { ID: "YEAR", type: "text", className: "CILA_Calendar_TextBox2", value: "2008", readOnly: true}],
									["INPUT", { type: "button", className: "CILA_Calendar_Button1", value: "►", onclick: this._incMonth.handle(this, 1)}],
									["INPUT", { type: "button", className: "CILA_Calendar_Button2", value: "»", onclick: this._incYear.handle(this, 1)}]
								]],
								["DIV", { ID: "DAYS"}]
							]],
							["DIV", { className: "CILA_Calendar_CloseButton", onclick: this.hide.handle(this)}],
							["DIV", { ID: "TIMEZONE", className: "CILA_Calendar_SubmitZone1" }, [
								["DIV", { className: "CILA_Calendar_SubmitZone2" }, [
									["TABLE", { width: "250px", cellSpacing: 0, cellPadding: 0 }, [
										["TR", {}, [
											["TD", {}, [
												["UC", "CILA_Calendar.SpinButton", { ID: "HOUR", value: 0, min: 1, max: 12}]
											]],
											["TD", { text: ":"}],
											["TD", {}, [
												["UC", "CILA_Calendar.SpinButton", { ID: "MINUTE", value: 0, min: 0, max: 59}]
											]],
											["TD", { text: ":"}],
											["TD", {}, [
												["UC", "CILA_Calendar.SpinButton", { ID: "SECOND", value: 0, min: 0, max: 59}]
											]],
											["TD", { innerHTML: "&nbsp;"}],
											["TD", {}, [
												["SELECT", { ID: "AMPM", className: "CILA_Calendar_AMPM" }, [
													["OPTION", { value: "1", text: "AM"}],
													["OPTION", { value: "0", text: "PM"}]
												]]
											]],
											["TD", { align: "right", width: "100%" }, [
												["INPUT", { type: "button", className: "CILA_Calendar_OKButton", value: "OK", onclick: this.saveDate.handle(this)}]
											]]
										]]
									]]
								]]
							]]
						]]
					]]
				]]
			]]
		];

		this.ctrl = create(cons, span);
		Element.show(span);
		this._createDays();

		this.popupmenu = new PopupMenu
		({
			menu: this.ctrl.WIN,
			moveObj: this.ctrl.MOVE,
			moved: true,
			resetPos: true,
			useMask: true,
			autoClose: true,
			opacity: 0.1
		});

		this.button.onclick = this.show.handle(this);
		this.textbox.onchange = this._checkDate.handle(this, this.textbox.onchange);
		if (this.useTime == false) Element.hide(this.ctrl.TIMEZONE);
	},

	show: function()
	{
		this._checkDate();
		var pos = Element.getRealOffsetPosition(this.button);
		var size = Element.getSize(this.button);
		this._applyDate();
		this._bound();
		this.popupmenu.show
		(
			pos[0] - (Browser.IE6 ? 0 : (document.documentElement.scrollLeft || document.body.scrollLeft)),
			pos[1] + size[1] - (Browser.IE6 ? 0 :(document.documentElement.scrollTop || document.body.scrollTop))
		);
		return false;
	},

	hide: function()
	{
		this.popupmenu.hide();
	},

	saveDate: function()
	{
		this.textbox.value = (this.Month + 1) + "/" + this.Date + "/" + this.Year;
		if (this.useTime)
			this.textbox.value += " " + this.ctrl.HOUR.value + ":" + this.ctrl.MINUTE.value + ":" +
			this.ctrl.SECOND.value + " " + ((this.ctrl.AMPM.value == "1") ? "AM" : "PM");
		this.hide();
		this.textbox.onchange();
	},

	_checkDate: function(onchange)
	{
		var d = null;
		try
		{
			d = Date.parse(this.textbox.value);
		} catch (err) { }
		if (!d) this.textbox.value = "";
		if (onchange) onchange();
	},

	_incMonth: function(value)
	{
		this.Month += value;
		if (this.Month < 0)
		{
			this.Month = 11;
			this._incYear(-1);
			return;
		}
		if (this.Month > 11)
		{
			this.Month = 0;
			this._incYear(1);
			return;
		}
		this._bound();
	},

	_incYear: function(value)
	{
		this.Year += value;
		if (this.Year < 0) this.Year = 0;
		this._bound();
	},

	_createDays: function()
	{
		var lang = this.language[this.lang];

		var cons =
		[
			["TABLE", { className: "CILA_Calendar_Table" }, [
				["TR", {}, []],
				["TR", {}, [["TD", { className: "CILA_Calendar_TD_Seperator", colSpan: 7}]]],
				["TR", {}, []],
				["TR", {}, []],
				["TR", {}, []],
				["TR", {}, []],
				["TR", {}, []],
				["TR", {}, []]
			]]
		];

		for (var i = 0; i < 7; i++)
		{
			for (var j = 0; j < 7; j++)
			{
				var className = "CILA_Calendar_TD_DAY";
				if (j == 0) className = "CILA_Calendar_TD_CN";
				if (j == 6) className = "CILA_Calendar_TD_SAT";
				if (i == 0)
				{
					cons[0][2][0][2].push
					(
						["TH", { text: lang.SortDays[j], className: className}]
					);
				}
				else
				{
					cons[0][2][i + 1][2].push
					(
						["TD", { ID: "TD_" + (i - 1) + "_" + j, className: className, saveClass: className}]
					);
				}
			}
		}
		this.ctrlDays = create(cons, this.ctrl.DAYS);
	},

	_applyDate: function()
	{
		var d = new Date();
		try
		{
			d = Date.parse(this.textbox.value);
			if (d) d = new Date(d);
			this.curDate = d;
		} catch (err) { }
		if (!d)
		{
			d = new Date();
			this.curDate = null;
		}

		this.Year = d.getFullYear();
		this.Month = d.getMonth();
		this.Date = d.getDate();
		this.Hour = d.getHours();

		if (this.Hour < 12)
		{
			if (this.Hour == 0) this.Hour = 12;
			this.AM = 1;
		}
		else
		{
			this.Hour = this.Hour - 12;
			this.AM = 0;
		}
		this.Minute = d.getMinutes();
		this.Second = d.getSeconds();
	},

	_bound: function()
	{
		var lang = this.language[this.lang];

		this.ctrl.MONTH.value = lang.Months[this.Month];
		this.ctrl.YEAR.value = this.Year;
		this.ctrl.HOUR.setValue(this.Hour);
		this.ctrl.AMPM.value = this.AM;
		this.ctrl.MINUTE.setValue(this.Minute);
		this.ctrl.SECOND.setValue(this.Second);

		var curDate = new Date();
		var startDate = new Date(this.Year, this.Month, 1);
		var Day = startDate.getDay();
		var numDaysInMonth = this.getDaysInMonth(this.Month);
		var numDaysBackMonth = this.getDaysInMonth((this.Month == 0) ? 11 : (this.Month - 1)) - Day + 1;
		var count = 1;
		for (var i = 0; i < 6; i++)
		{
			for (var j = 0; j < 7; j++)
			{
				var num = 0;
				var td = this.ctrlDays["TD_" + i + "_" + j];
				td.onclick = null;
				if (i == 0 && j < Day)
				{
					num = numDaysBackMonth++;
					td.className = "CILA_Calendar_TD_DeActive";
				}
				else
				{
					if (count <= numDaysInMonth)
					{
						num = count++;
						td.className = td.saveClass;
						if (num == curDate.getDate() && this.Month == curDate.getMonth() && this.Year == curDate.getFullYear())
							td.className = "CILA_Calendar_TD_CurDate";
						if (this.curDate && num == this.curDate.getDate() && this.Month == this.curDate.getMonth() && this.Year == this.curDate.getFullYear())
							td.className = "CILA_Calendar_TD_SelDate";
						td.onclick = this._selectDate.handle(this, num);
					}
					else
					{
						num = (count++) - numDaysInMonth;
						td.className = "CILA_Calendar_TD_DeActive";
					}
				}
				Element.setText(td, num);
			}
		}
	},

	_selectDate: function(date)
	{
		this.Date = date;
		this.saveDate();
	},

	getDaysInMonth: function(month)
	{
		var aNumDays = [31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
		if (month == 1)
		{
			var endDate = new Date(this.Year, 2, 1);
			endDate = new Date(endDate - (24 * 60 * 60 * 1000));
			return endDate.getDate();
		}
		else
			return aNumDays[month];
	}
};

CILA_Calendar.SpinButton = function(){};
CILA_Calendar.SpinButton.prototype = 
{
	value		: null,
	max			: null,
	min			: null,
	_endTime	: false,

	create : function(parent)
	{
		var cons = 
		[
			["DIV", {className : "CILA_Calendar_Spin"}, [
				["INPUT", {ID : "VALUE", type : "text", className : "", value : this.value, onchange : this._spin.handle(this, 0)} ],
				["INPUT", {type : "button", className : "CILA_Calendar_SpinUp", 
							onmousedown : this._startSpin.handle(this, 1),
							onmouseup : this._endSpin.handle(this),
							onmouseout : this._endSpin.handle(this)
							}],
				["INPUT", {type : "button", className : "CILA_Calendar_SpinDown", 
							onmousedown : this._startSpin.handle(this, -1),
							onmouseup : this._endSpin.handle(this),
							onmouseout : this._endSpin.handle(this)
							}]
			]]
		];
		this.ctrl = create(cons, parent);
		this._spin(0);
	},
	
	setValue : function(value)
	{
		this.ctrl.VALUE.value = this.value = value;
		this._spin(0);
	},
	
	_spin : function(value, setTime)
	{
		if (setTime && this._endTime)
			return;
		var v = this.ctrl.VALUE.value * 1 + value;
		if (this.min != null && v < this.min) v = this.max;
		if (this.max != null && v > this.max) v = this.min;
		this.ctrl.VALUE.value = this.value = (v < 10) ? ("0" + v) : v;
		if (setTime)
			this.timer = setTimeout(this._spin.handle(this, value, true), 100);
	},
	
	_startSpin : function(value)
	{
		if (this.timer) clearTimeout(this.timer);
		this._endTime = false;
		this._spin(value);
		this.timer = setTimeout(this._spin.handle(this, value, true), 1000);
	},
	
	_endSpin : function()
	{
		this._endTime = true;
	}
};
