(function(D) {
	D.fn
			.extend( {
				renderCalendar : function(Q) {
					var Y = function(Z) {
						return document.createElement(Z)
					};
					Q = D.extend( {}, D.fn.datePicker.defaults, Q);
					if (Q.showHeader != D.dpConst.SHOW_HEADER_NONE) {
						var N = D(Y("tr"));
						for ( var T = Date.firstDayOfWeek; T < Date.firstDayOfWeek + 7; T++) {
							var H = T % 7;
							var S = Date.dayNames[H];
							N
									.append(jQuery(Y("th"))
											.attr(
													{
														scope : "col",
														abbr : S,
														title : S,
														"class" : (H == 0
																|| H == 6 ? "weekend"
																: "weekday")
													})
											.html(
													Q.showHeader == D.dpConst.SHOW_HEADER_SHORT ? S
															.substr(0, 1)
															: S))
						}
					}
					var E = D(Y("table")).attr( {
						cellspacing : 2
					}).addClass("jCalendar").append(
							(Q.showHeader != D.dpConst.SHOW_HEADER_NONE ? D(
									Y("thead")).append(N) : Y("thead")));
					var F = D(Y("tbody"));
					var V = (new Date()).zeroTime();
					var X = Q.month == undefined ? V.getMonth() : Q.month;
					var O = Q.year || V.getFullYear();
					var L = new Date(O, X, 1);
					var K = Date.firstDayOfWeek - L.getDay() + 1;
					if (K > 1) {
						K -= 7
					}
					var P = Math.ceil(((-1 * K + 1) + L.getDaysInMonth()) / 7);
					L.addDays(K - 1);
					var W = function(Z) {
						return function() {
							if (Q.hoverClass) {
								var a = D(this);
								if (!Q.selectWeek) {
									a.addClass(Q.hoverClass)
								} else {
									if (Z && !a.is(".disabled")) {
										a.parent().addClass("activeWeekHover")
									}
								}
							}
						}
					};
					var G = function() {
						if (Q.hoverClass) {
							var Z = D(this);
							Z.removeClass(Q.hoverClass);
							Z.parent().removeClass("activeWeekHover")
						}
					};
					var M = 0;
					while (M++ < P) {
						var R = jQuery(Y("tr"));
						var J = Q.dpController ? L > Q.dpController.startDate
								: false;
						for ( var T = 0; T < 7; T++) {
							var I = L.getMonth() == X;
							var U = D(Y("td"))
									.text(L.getDate() + "")
									.addClass(
											(I ? "current-month "
													: "other-month ")
													+ (L.isWeekend() ? "weekend "
															: "weekday ")
													+ (I
															&& L.getTime() == V
																	.getTime() ? "today "
															: "")).data(
											"datePickerDate", L.asString())
									.hover(W(J), G);
							R.append(U);
							if (Q.renderCallback) {
								Q.renderCallback(U, L, X, O)
							}
							L.addDays(1)
						}
						F.append(R)
					}
					E.append(F);
					return this.each(function() {
						D(this).empty().append(E)
					})
				},
				datePicker : function(E) {
					if (!D.event._dpCache) {
						D.event._dpCache = []
					}
					E = D.extend( {}, D.fn.datePicker.defaults, E);
					return this.each(function() {
						var G = D(this);
						var I = true;
						if (!this._dpId) {
							this._dpId = D.event.guid++;
							D.event._dpCache[this._dpId] = new A(this);
							I = false
						}
						if (E.inline) {
							E.createButton = false;
							E.displayClose = false;
							E.closeOnSelect = false;
							G.empty()
						}
						var F = D.event._dpCache[this._dpId];
						F.init(E);
						if (!I && E.createButton) {
							F.button = D(
									'<a href="#" class="dp-choose-date" title="'
											+ D.dpText.TEXT_CHOOSE_DATE + '">'
											+ D.dpText.TEXT_CHOOSE_DATE
											+ "</a>").bind("click", function() {
								G.dpDisplay(this);
								this.blur();
								return false
							});
							G.after(F.button)
						}
						if (!I && G.is(":text")) {
							G.bind("dateSelected", function(K, J, L) {
								this.value = J.asString()
							}).bind("change", function() {
								if (this.value == "") {
									F.clearSelected()
								} else {
									var J = Date.fromString(this.value);
									if (J) {
										F.setSelected(J, true, true)
									}
								}
							});
							if (E.clickInput) {
								G.bind("click", function() {
									G.trigger("change");
									G.dpDisplay()
								})
							}
							var H = Date.fromString(this.value);
							if (this.value != "" && H) {
								F.setSelected(H, true, true)
							}
						}
						G.addClass("dp-applied")
					})
				},
				dpSetDisabled : function(E) {
					return B.call(this, "setDisabled", E)
				},
				dpSetStartDate : function(E) {
					return B.call(this, "setStartDate", E)
				},
				dpSetEndDate : function(E) {
					return B.call(this, "setEndDate", E)
				},
				dpGetSelected : function() {
					var E = C(this[0]);
					if (E) {
						return E.getSelected()
					}
					return null
				},
				dpSetSelected : function(H, F, E, G) {
					if (F == undefined) {
						F = true
					}
					if (E == undefined) {
						E = true
					}
					if (G == undefined) {
						G = true
					}
					return B.call(this, "setSelected", Date.fromString(H), F,
							E, G)
				},
				dpSetDisplayedMonth : function(E, F) {
					return B.call(this, "setDisplayedMonth", Number(E),
							Number(F), true)
				},
				dpDisplay : function(E) {
					return B.call(this, "display", E)
				},
				dpSetRenderCallback : function(E) {
					return B.call(this, "setRenderCallback", E)
				},
				dpSetPosition : function(E, F) {
					return B.call(this, "setPosition", E, F)
				},
				dpSetOffset : function(E, F) {
					return B.call(this, "setOffset", E, F)
				},
				dpClose : function() {
					return B.call(this, "_closeCalendar", false, this[0])
				},
				_dpDestroy : function() {
				}
			});
	var B = function(G, F, E, I, H) {
		return this.each(function() {
			var J = C(this);
			if (J) {
				J[G](F, E, I, H)
			}
		})
	};
	function A(E) {
		this.ele = E;
		this.displayedMonth = null;
		this.displayedYear = null;
		this.startDate = null;
		this.endDate = null;
		this.showYearNavigation = null;
		this.closeOnSelect = null;
		this.displayClose = null;
		this.rememberViewedMonth = null;
		this.selectMultiple = null;
		this.numSelectable = null;
		this.numSelected = null;
		this.verticalPosition = null;
		this.horizontalPosition = null;
		this.verticalOffset = null;
		this.horizontalOffset = null;
		this.button = null;
		this.renderCallback = [];
		this.selectedDates = {};
		this.inline = null;
		this.context = "#dp-popup";
		this.settings = {}
	}
	D
			.extend(
					A.prototype,
					{
						init : function(E) {
							this.setStartDate(E.startDate);
							this.setEndDate(E.endDate);
							this.setDisplayedMonth(Number(E.month),
									Number(E.year));
							this.setRenderCallback(E.renderCallback);
							this.showYearNavigation = E.showYearNavigation;
							this.closeOnSelect = E.closeOnSelect;
							this.displayClose = E.displayClose;
							this.rememberViewedMonth = E.rememberViewedMonth;
							this.selectMultiple = E.selectMultiple;
							this.numSelectable = E.selectMultiple ? E.numSelectable
									: 1;
							this.numSelected = 0;
							this.verticalPosition = E.verticalPosition;
							this.horizontalPosition = E.horizontalPosition;
							this.hoverClass = E.hoverClass;
							this
									.setOffset(E.verticalOffset,
											E.horizontalOffset);
							this.inline = E.inline;
							this.settings = E;
							if (this.inline) {
								this.context = this.ele;
								this.display()
							}
						},
						setStartDate : function(E) {
							if (E) {
								this.startDate = Date.fromString(E)
							}
							if (!this.startDate) {
								this.startDate = (new Date()).zeroTime()
							}
							this.setDisplayedMonth(this.displayedMonth,
									this.displayedYear)
						},
						setEndDate : function(E) {
							if (E) {
								this.endDate = Date.fromString(E)
							}
							if (!this.endDate) {
								this.endDate = (new Date("12/31/2999"))
							}
							if (this.endDate.getTime() < this.startDate
									.getTime()) {
								this.endDate = this.startDate
							}
							this.setDisplayedMonth(this.displayedMonth,
									this.displayedYear)
						},
						setPosition : function(E, F) {
							this.verticalPosition = E;
							this.horizontalPosition = F
						},
						setOffset : function(E, F) {
							this.verticalOffset = parseInt(E) || 0;
							this.horizontalOffset = parseInt(F) || 0
						},
						setDisabled : function(E) {
							$e = D(this.ele);
							$e[E ? "addClass" : "removeClass"]("dp-disabled");
							if (this.button) {
								$but = D(this.button);
								$but[E ? "addClass" : "removeClass"]
										("dp-disabled");
								$but.attr("title", E ? ""
										: D.dpText.TEXT_CHOOSE_DATE)
							}
							if ($e.is(":text")) {
								$e.attr("disabled", E ? "disabled" : "")
							}
						},
						setDisplayedMonth : function(E, L, I) {
							if (this.startDate == undefined
									|| this.endDate == undefined) {
								return
							}
							var H = new Date(this.startDate.getTime());
							H.setDate(1);
							var K = new Date(this.endDate.getTime());
							K.setDate(1);
							var G;
							if ((!E && !L) || (isNaN(E) && isNaN(L))) {
								G = new Date().zeroTime();
								G.setDate(1)
							} else {
								if (isNaN(E)) {
									G = new Date(L, this.displayedMonth, 1)
								} else {
									if (isNaN(L)) {
										G = new Date(this.displayedYear, E, 1)
									} else {
										G = new Date(L, E, 1)
									}
								}
							}
							if (G.getTime() < H.getTime()) {
								G = H
							} else {
								if (G.getTime() > K.getTime()) {
									G = K
								}
							}
							var F = this.displayedMonth;
							var J = this.displayedYear;
							this.displayedMonth = G.getMonth();
							this.displayedYear = G.getFullYear();
							if (I
									&& (this.displayedMonth != F || this.displayedYear != J)) {
								this._rerenderCalendar();
								D(this.ele).trigger(
										"dpMonthChanged",
										[ this.displayedMonth,
												this.displayedYear ])
							}
						},
						setSelected : function(L, E, F, H) {
							if (L < this.startDate || L > this.endDate) {
								return
							}
							var G = this.settings;
							if (G.selectWeek) {
								L = L.addDays(-(L.getDay()
										- Date.firstDayOfWeek + 7) % 7);
								if (L < this.startDate) {
									return
								}
							}
							if (E == this.isSelected(L)) {
								return
							}
							if (this.selectMultiple == false) {
								this.clearSelected()
							} else {
								if (E && this.numSelected == this.numSelectable) {
									return
								}
							}
							if (F
									&& (this.displayedMonth != L.getMonth() || this.displayedYear != L
											.getFullYear())) {
								this.setDisplayedMonth(L.getMonth(), L
										.getFullYear(), true)
							}
							this.selectedDates[L.toString()] = E;
							this.numSelected += E ? 1 : -1;
							var J = "td."
									+ (L.getMonth() == this.displayedMonth ? "current-month"
											: "other-month");
							var K;
							D(J, this.context).each(
									function() {
										if (D(this).data("datePickerDate") == L
												.asString()) {
											K = D(this);
											if (G.selectWeek) {
												K.parent()[E ? "addClass"
														: "removeClass"]
														("selectedWeek")
											}
											K[E ? "addClass" : "removeClass"]
													("selected")
										}
									});
							D("td", this.context).not(".selected")[this.selectMultiple
									&& this.numSelected == this.numSelectable ? "addClass"
									: "removeClass"]("unselectable");
							if (H) {
								var G = this.isSelected(L);
								$e = D(this.ele);
								var I = Date.fromString(L.asString());
								$e.trigger("dateSelected", [ I, K, G ]);
								$e.trigger("change")
							}
						},
						isSelected : function(E) {
							return this.selectedDates[E.toString()]
						},
						getSelected : function() {
							var E = [];
							for (s in this.selectedDates) {
								if (this.selectedDates[s] == true) {
									E.push(Date.parse(s))
								}
							}
							return E
						},
						clearSelected : function() {
							this.selectedDates = {};
							this.numSelected = 0;
							D("td.selected", this.context).removeClass(
									"selected").parent().removeClass(
									"selectedWeek")
						},
						display : function(E) {
							if (D(this.ele).is(".dp-disabled")) {
								return
							}
							E = E || this.ele;
							var M = this;
							var I = D(E);
							var L = I.offset();
							var N;
							var O;
							var G;
							var J;
							if (M.inline) {
								N = D(this.ele);
								O = {
									id : "calendar-" + this.ele._dpId,
									"class" : "dp-popup dp-popup-inline"
								};
								D(".dp-popup", N).remove();
								J = {}
							} else {
								N = D("body");
								O = {
									id : "dp-popup",
									"class" : "dp-popup"
								};
								J = {
									top : L.top + M.verticalOffset,
									left : L.left + M.horizontalOffset
								};
								var K = function(R) {
									var P = R.target;
									var Q = D("#dp-popup")[0];
									while (true) {
										if (P == Q) {
											return true
										} else {
											if (P == document) {
												M._closeCalendar();
												return false
											} else {
												P = D(P).parent()[0]
											}
										}
									}
								};
								this._checkMouse = K;
								M._closeCalendar(true);
								D(document).bind("keydown.datepicker",
										function(P) {
											if (P.keyCode == 27) {
												M._closeCalendar()
											}
										})
							}
							if (!M.rememberViewedMonth) {
								var H = this.getSelected()[0];
								if (H) {
									H = new Date(H);
									this.setDisplayedMonth(H.getMonth(), H
											.getFullYear(), false)
								}
							}
							N
									.append(D("<div></div>")
											.attr(O)
											.css(J)
											.append(
													D("<h2></h2>"),
													D(
															'<div class="dp-nav-prev"></div>')
															.append(
																	D(
																			'<a class="dp-nav-prev-year" href="#" title="' + D.dpText.TEXT_PREV_YEAR + '">&lt;&lt;</a>')
																			.bind(
																					"click",
																					function() {
																						return M._displayNewMonth
																								.call(
																										M,
																										this,
																										0,
																										-1)
																					}),
																	D(
																			'<a class="dp-nav-prev-month" href="#" title="' + D.dpText.TEXT_PREV_MONTH + '">&lt;</a>')
																			.bind(
																					"click",
																					function() {
																						return M._displayNewMonth
																								.call(
																										M,
																										this,
																										-1,
																										0)
																					})),
													D(
															'<div class="dp-nav-next"></div>')
															.append(
																	D(
																			'<a class="dp-nav-next-year" href="#" title="' + D.dpText.TEXT_NEXT_YEAR + '">&gt;&gt;</a>')
																			.bind(
																					"click",
																					function() {
																						return M._displayNewMonth
																								.call(
																										M,
																										this,
																										0,
																										1)
																					}),
																	D(
																			'<a class="dp-nav-next-month" href="#" title="' + D.dpText.TEXT_NEXT_MONTH + '">&gt;</a>')
																			.bind(
																					"click",
																					function() {
																						return M._displayNewMonth
																								.call(
																										M,
																										this,
																										1,
																										0)
																					})),
													D('<div class="dp-calendar"></div>'))
											.bgIframe());
							var F = this.inline ? D(".dp-popup", this.context)
									: D("#dp-popup");
							if (this.showYearNavigation == false) {
								D(".dp-nav-prev-year, .dp-nav-next-year",
										M.context).css("display", "none")
							}
							if (this.displayClose) {
								F.append(D(
										'<a href="#" id="dp-close">'
												+ D.dpText.TEXT_CLOSE + "</a>")
										.bind("click", function() {
											M._closeCalendar();
											return false
										}))
							}
							M._renderCalendar();
							D(this.ele).trigger("dpDisplayed", F);
							if (!M.inline) {
								if (this.verticalPosition == D.dpConst.POS_BOTTOM) {
									F.css("top", L.top + I.height()
											- F.height() + M.verticalOffset)
								}
								if (this.horizontalPosition == D.dpConst.POS_RIGHT) {
									F.css("left", L.left + I.width()
											- F.width() + M.horizontalOffset)
								}
								D(document).bind("mousedown.datepicker",
										this._checkMouse)
							}
						},
						setRenderCallback : function(E) {
							if (E == null) {
								return
							}
							if (E && typeof (E) == "function") {
								E = [ E ]
							}
							this.renderCallback = this.renderCallback.concat(E)
						},
						cellRender : function(I, E, G, F) {
							var J = this.dpController;
							var H = new Date(E.getTime());
							I
									.bind(
											"click",
											function() {
												var K = D(this);
												if (!K.is(".disabled")) {
													J
															.setSelected(
																	H,
																	!K
																			.is(".selected")
																			|| !J.selectMultiple,
																	false, true);
													if (J.closeOnSelect) {
														J._closeCalendar()
													}
													if (!D.browser.msie) {
														D(J.ele)
																.trigger(
																		"focus",
																		[ D.dpConst.DP_INTERNAL_FOCUS ])
													}
												}
											});
							if (J.isSelected(H)) {
								I.addClass("selected");
								if (J.settings.selectWeek) {
									I.parent().addClass("selectedWeek")
								}
							} else {
								if (J.selectMultiple
										&& J.numSelected == J.numSelectable) {
									I.addClass("unselectable")
								}
							}
						},
						_applyRenderCallbacks : function() {
							var E = this;
							D("td", this.context)
									.each(
											function() {
												for ( var F = 0; F < E.renderCallback.length; F++) {
													$td = D(this);
													E.renderCallback[F]
															.apply(
																	this,
																	[
																			$td,
																			Date
																					.fromString($td
																							.data("datePickerDate")),
																			E.displayedMonth,
																			E.displayedYear ])
												}
											});
							return
						},
						_displayNewMonth : function(F, E, G) {
							if (!D(F).is(".disabled")) {
								this.setDisplayedMonth(this.displayedMonth + E,
										this.displayedYear + G, true)
							}
							F.blur();
							return false
						},
						_rerenderCalendar : function() {
							this._clearCalendar();
							this._renderCalendar()
						},
						_renderCalendar : function() {
							D("h2", this.context).html(
									(new Date(this.displayedYear,
											this.displayedMonth, 1))
											.asString(D.dpText.HEADER_FORMAT));
							D(".dp-calendar", this.context).renderCalendar(
									D.extend( {}, this.settings, {
										month : this.displayedMonth,
										year : this.displayedYear,
										renderCallback : this.cellRender,
										dpController : this,
										hoverClass : this.hoverClass
									}));
							if (this.displayedYear == this.startDate
									.getFullYear()
									&& this.displayedMonth == this.startDate
											.getMonth()) {
								D(".dp-nav-prev-year", this.context).addClass(
										"disabled");
								D(".dp-nav-prev-month", this.context).addClass(
										"disabled");
								D(".dp-calendar td.other-month", this.context)
										.each(function() {
											var I = D(this);
											if (Number(I.text()) > 20) {
												I.addClass("disabled")
											}
										});
								var H = this.startDate.getDate();
								D(".dp-calendar td.current-month", this.context)
										.each(function() {
											var I = D(this);
											if (Number(I.text()) < H) {
												I.addClass("disabled")
											}
										})
							} else {
								D(".dp-nav-prev-year", this.context)
										.removeClass("disabled");
								D(".dp-nav-prev-month", this.context)
										.removeClass("disabled");
								var H = this.startDate.getDate();
								if (H > 20) {
									var F = this.startDate.getTime();
									var G = new Date(F);
									G.addMonths(1);
									if (this.displayedYear == G.getFullYear()
											&& this.displayedMonth == G
													.getMonth()) {
										D(".dp-calendar td.other-month",
												this.context)
												.each(
														function() {
															var I = D(this);
															if (Date
																	.fromString(
																			I
																					.data("datePickerDate"))
																	.getTime() < F) {
																I
																		.addClass("disabled")
															}
														})
									}
								}
							}
							if (this.displayedYear == this.endDate
									.getFullYear()
									&& this.displayedMonth == this.endDate
											.getMonth()) {
								D(".dp-nav-next-year", this.context).addClass(
										"disabled");
								D(".dp-nav-next-month", this.context).addClass(
										"disabled");
								D(".dp-calendar td.other-month", this.context)
										.each(function() {
											var I = D(this);
											if (Number(I.text()) < 14) {
												I.addClass("disabled")
											}
										});
								var H = this.endDate.getDate();
								D(".dp-calendar td.current-month", this.context)
										.each(function() {
											var I = D(this);
											if (Number(I.text()) > H) {
												I.addClass("disabled")
											}
										})
							} else {
								D(".dp-nav-next-year", this.context)
										.removeClass("disabled");
								D(".dp-nav-next-month", this.context)
										.removeClass("disabled");
								var H = this.endDate.getDate();
								if (H < 13) {
									var E = new Date(this.endDate.getTime());
									E.addMonths(-1);
									if (this.displayedYear == E.getFullYear()
											&& this.displayedMonth == E
													.getMonth()) {
										D(".dp-calendar td.other-month",
												this.context).each(function() {
											var I = D(this);
											if (Number(I.text()) > H) {
												I.addClass("disabled")
											}
										})
									}
								}
							}
							this._applyRenderCallbacks()
						},
						_closeCalendar : function(E, F) {
							if (!F || F == this.ele) {
								D(document).unbind("mousedown.datepicker");
								D(document).unbind("keydown.datepicker");
								this._clearCalendar();
								D("#dp-popup a").unbind();
								D("#dp-popup").empty().remove();
								if (!E) {
									D(this.ele).trigger("dpClosed",
											[ this.getSelected() ])
								}
							}
						},
						_clearCalendar : function() {
							D(".dp-calendar td", this.context).unbind();
							D(".dp-calendar", this.context).empty()
						}
					});
	D.dpConst = {
		SHOW_HEADER_NONE : 0,
		SHOW_HEADER_SHORT : 1,
		SHOW_HEADER_LONG : 2,
		POS_TOP : 0,
		POS_BOTTOM : 1,
		POS_LEFT : 0,
		POS_RIGHT : 1,
		DP_INTERNAL_FOCUS : "dpInternalFocusTrigger"
	};
	D.dpText = {
		TEXT_PREV_YEAR : "Previous year",
		TEXT_PREV_MONTH : "Previous month",
		TEXT_NEXT_YEAR : "Next year",
		TEXT_NEXT_MONTH : "Next month",
		TEXT_CLOSE : "Close",
		TEXT_CHOOSE_DATE : "Choose date",
		HEADER_FORMAT : "mmmm yyyy"
	};
	D.dpVersion = "$Id: jquery.datePicker.js 61 2009-03-31 03:18:02Z kelvin.luck $";
	D.fn.datePicker.defaults = {
		month : undefined,
		year : undefined,
		showHeader : D.dpConst.SHOW_HEADER_SHORT,
		startDate : undefined,
		endDate : undefined,
		inline : false,
		renderCallback : null,
		createButton : true,
		showYearNavigation : true,
		closeOnSelect : true,
		displayClose : false,
		selectMultiple : false,
		numSelectable : Number.MAX_VALUE,
		clickInput : false,
		rememberViewedMonth : true,
		selectWeek : false,
		verticalPosition : D.dpConst.POS_TOP,
		horizontalPosition : D.dpConst.POS_LEFT,
		verticalOffset : 0,
		horizontalOffset : 0,
		hoverClass : "dp-hover"
	};
	function C(E) {
		if (E._dpId) {
			return D.event._dpCache[E._dpId]
		}
		return false
	}
	if (D.fn.bgIframe == undefined) {
		D.fn.bgIframe = function() {
			return this
		}
	}
	D(window).bind("unload", function() {
		var F = D.event._dpCache || [];
		for ( var E in F) {
			D(F[E].ele)._dpDestroy()
		}
	})
})(jQuery);
