(function ($) {

	var tlrk = $.telerik, c = /{0:?/, d = /{0:?(\S|\s)*}/;

	function a(selectedDateTime, minDateTime, maxDateTime) {
		var nowDateTime = new tlrk.datetime();
		if (selectedDateTime) {
			nowDateTime = new tlrk.datetime(selectedDateTime);
		}
		if (minDateTime > nowDateTime.value) {
			nowDateTime = new tlrk.datetime(minDateTime);
		}
		else {
			if (maxDateTime < nowDateTime.value) {
				nowDateTime = new tlrk.datetime(maxDateTime);
			}
		}
		return tlrk.datetime.firstDayOfMonth(nowDateTime);
	}

	$.extend(tlrk, { calendar: function (h, i) {
		this.element = h; $.extend(this, i);
		var f = new tlrk.datetime(this.minDate);
		var g = new tlrk.datetime(this.maxDate);
		this.currentView = tlrk.calendar.views[0];
		this.viewedMonth = a(this.selectedDate, this.minDate, this.maxDate);
		var j = new tlrk.stringBuilder().cat('<a href="#" class="t-link t-nav-prev')
			.catIf(" t-state-disabled", this.currentView.compare(this.viewedMonth, f, false) <= 0).cat('">')
			.cat('<span class="t-icon t-arrow-prev"></span></a><a href="#" class="t-link t-nav-fast">')
			.cat(this.currentView.title(this.viewedMonth)).cat("</a>")
			.cat('<a href="#" class="t-link t-nav-next')
			.catIf(" t-state-disabled", this.currentView.compare(this.viewedMonth, g, true) >= 0)
			.cat('"><span class="t-icon t-arrow-next"></span></a>');
		$(".t-header", this.element).html(j.string());
		$(".t-nav-next:not(.t-state-disabled)", h).live("click", $.proxy(this.navigateToFuture, this));
		$(".t-nav-prev:not(.t-state-disabled)", h).live("click", $.proxy(this.navigateToPast, this));
		$(".t-nav-fast:not(.t-state-disabled)", h).live("click", $.proxy(this.navigateUp, this));
		$(".t-link.t-state-disabled", h).live("click", tlrk.preventDefault);
		$("td:not(.t-state-disabled):has(.t-link)", h).live("mouseenter", tlrk.hover).live("mouseleave", tlrk.leave).live("click", $.proxy(this.navigateDown, this));
		tlrk.bind(this, { change: this.onChange, load: this.onLoad });
	}
	});

	tlrk.calendar.prototype = {
		stopAnimation: false,

		updateSelection: function () {
			var i = tlrk.datetime.firstDayOfMonth(this.viewedMonth).toDate();
			var h = new tlrk.datetime(i).date(32).date(0).toDate();
			if (this.selectedDate === null || !tlrk.calendar.isInRange(this.selectedDate, i, h)) {
				var g = a(this.selectedDate, this.minDate, this.maxDate);
				this.goToView(0, g)
			}
			var f = this;
			var j = $(".t-content td:not(.t-other-month)", this.element).removeClass("t-state-selected");
			if (this.selectedDate !== null) {
				j.filter(function () {
					return (parseInt($(this).text(), 10) == f.selectedDate.getDate())
				}).addClass("t-state-selected")
			}
		},

		value: function () {
			if (arguments.length == 0) {
				return this.selectedDate;
			}
			if (arguments.length == 1) {
				this.selectedDate = arguments[0] === null ? null : new Date(arguments[0].value ? arguments[0].value : arguments[0])
			}
			else {
				if (arguments.length > 1) {
					this.selectedDate = new Date(arguments[0], arguments[1], arguments[2])
				}
			}
			this.updateSelection();
			return this;
		},
		
		overlay: function (f) {
			if (!f) { return $(".t-overlay", this.element).remove() } $("<div/>").addClass("t-overlay").css({ opacity: 0, width: this.element.offsetWidth, height: this.element.offsetHeight, position: "absolute", top: 0, left: 0, zIndex: 3, backgroundColor: "#fff" }).appendTo(this.element)
		},
		
		goToView: function (h, g) { if (h < 0 || tlrk.calendar.views.length <= h) { return } var f = new tlrk.datetime(this.minDate); var i = new tlrk.datetime(this.maxDate); if (typeof g != "undefined") { g = g.value ? g : new tlrk.datetime(g); this.viewedMonth = tlrk.datetime.firstDayOfMonth(g) } this.currentView = tlrk.calendar.views[h]; $(".t-nav-prev", this.element).toggleClass("t-state-disabled", this.currentView.compare(g, f, false) <= 0); $(".t-nav-next", this.element).toggleClass("t-state-disabled", this.currentView.compare(g, i, true) >= 0); $(".t-nav-fast", this.element).html(this.currentView.title(g)).toggleClass("t-state-disabled", h == tlrk.calendar.views.length - 1); $(".t-content", this.element).html(this.currentView.body(g, f, i, this.selectedDate ? new tlrk.datetime(this.selectedDate) : null, this.urlFormat, this.dates)).toggleClass("t-meta-view", h == 1 || h == 2); return this }, navigateVertically: function (g, i, k, t) { i = new tlrk.datetime(i); this.viewedMonth = tlrk.datetime.firstDayOfMonth(i); this.currentView = tlrk.calendar.views[g]; this.overlay(true); var r = new tlrk.datetime(this.minDate); var q = new tlrk.datetime(this.maxDate); var s = $(".t-content", this.element); var h = s.outerWidth(); var v = s.outerHeight(); var l = s.css("font-size"); var u = s.css("line-height"); if (u === "normal") { u = parseInt(l) * 1.5 } s.find("td").removeClass("t-state-hover"); $(".t-nav-fast", this.element).html(this.currentView.title(i)).toggleClass("t-state-disabled", g == tlrk.calendar.views.length - 1); $(".t-nav-prev", this.element).toggleClass("t-state-disabled", this.currentView.compare(this.viewedMonth, r, false) <= 0); $(".t-nav-next", this.element).toggleClass("t-state-disabled", this.currentView.compare(this.viewedMonth, q, true) >= 0); var j = $('<table class="t-content" cellspacing="0"></table>').html(this.currentView.body(i, r, q, this.selectedDate ? new tlrk.datetime(this.selectedDate) : null, this.urlFormat, this.dates)).toggleClass("t-meta-view", g == 1 || g == 2); var f = this; var p = { fontSize: l, lineHeight: u, top: 0, left: 0, width: h, height: v, opacity: 1 }; var o; if (k) { o = tlrk.fx._wrap(s).css({ overflow: "hidden", position: "relative" }); j.wrap($("<div/>").addClass("t-animation-container").css($.extend({ position: "absolute", zIndex: 1, fontSize: 1, lineHeight: 1, width: t.outerWidth(), height: t.outerHeight(), opacity: 0 }, t.position()))).parent().insertAfter(s); if (!this.stopAnimation) { j.parent().animate({ fontSize: l, lineHeight: u, top: 0, left: 0, width: h, height: v, opacity: 1 }, "normal", function () { j.appendTo(f.element); o.remove(); f.overlay(false) }) } else { s.remove(); j.appendTo(f.element); o.remove(); f.overlay(false) } } else { j.insertBefore(s); o = tlrk.fx._wrap(j).css({ overflow: "hidden", position: "relative" }); var n; if (g != 0) { n = tlrk.calendar.views[g].verticalDate(this.viewedMonth) } var m = { top: (Math.floor(n / 4) * v) / 3, left: ((n % 4) * h) / 4 }; s.wrap($("<div/>").addClass("t-animation-container").css($.extend({ position: "absolute" }, p))).parent().insertAfter(j); if (!this.stopAnimation) { s.parent().animate($.extend({ fontSize: 1, lineHeight: 1, width: 48, height: 54, opacity: 0 }, m), "normal", function () { j.appendTo(f.element); o.remove(); f.overlay(false) }) } else { s.remove(); j.appendTo(f.element); o.remove(); f.overlay(false) } } tlrk.trigger(this.element, "navigate", { direction: k }) }, navigateHorizontally: function (f, j, l) { j = new tlrk.datetime(j); var g = new tlrk.datetime(this.minDate); var k = new tlrk.datetime(this.maxDate); this.viewedMonth = tlrk.datetime.firstDayOfMonth(tlrk.calendar.fitDateToRange(j, g, k)); this.currentView = tlrk.calendar.views[f]; $(".t-nav-fast", this.element).html(this.currentView.title(j)).toggleClass("t-state-disabled", f == tlrk.calendar.views.length - 1); $(".t-nav-prev", this.element).toggleClass("t-state-disabled", this.currentView.compare(this.viewedMonth, g, false) <= 0); $(".t-nav-next", this.element).toggleClass("t-state-disabled", this.currentView.compare(this.viewedMonth, k, true) >= 0); this.overlay(true); var i = $('<table class="t-content" cellspacing="0"></table>').html(this.currentView.body(j, g, k, this.selectedDate ? new tlrk.datetime(this.selectedDate) : null, this.urlFormat, this.dates)).toggleClass("t-meta-view", f == 1 || f == 2); var h = $(".t-content", this.element); var o = h.outerWidth(); h.add(i).css({ width: o, "float": "left" }); var n = tlrk.fx._wrap(h).css({ position: "relative", width: o * 2, "float": "left", left: (l ? 0 : -200) }); i[l ? "insertAfter" : "insertBefore"](h); var m = this; if (!this.stopAnimation) { n.animate({ left: (l ? -200 : 0) }, "normal", function () { i.appendTo(m.element); n.remove(); m.overlay(false) }) } else { h.remove(); i.appendTo(m.element); n.remove(); m.overlay(false) } tlrk.trigger(this.element, "navigate", { direction: l }) }, navigateUp: function (g) { if (g) { g.preventDefault() } var f = this.currentView.index; this.navigateVertically(f + 1, this.viewedMonth.toDate(), false) }, navigateDown: function (n, l, f) { var h = $($(n.target).hasClass("t-input") ? l : n.target); var o = h.text(); var m = f || this.currentView.index; var g = h.attr("href"); if (g && (g.charAt(g.length - 1) == "#")) { n.preventDefault() } if (m == 0) { var k = parseInt(o, 10); var j = this.viewedMonth.month(); if (h.parent().hasClass("t-other-month")) { j += (k < 15 ? 1 : -1) } var i = new Date(this.viewedMonth.year(), j, k); if (!this.selectedDate || (this.selectedDate > i || i > this.selectedDate)) { if (tlrk.trigger(this.element, "change", { previousDate: this.selectedDate, date: i })) { return this } this.selectedDate = i } this.updateSelection() } else { if (m != 0) { tlrk.calendar.views[m].verticalDate(this.viewedMonth, o) } this.viewedMonth = tlrk.calendar.fitDateToRange(this.viewedMonth, new tlrk.datetime(this.minDate), new tlrk.datetime(this.maxDate)); this.navigateVertically(m - 1, this.viewedMonth.toDate(), true, h.add(h.parent()).filter("td")) } }, navigateToPast: function (g) { if (g) { g.preventDefault() } var f = this.currentView.index; if (f == 0) { this.viewedMonth.date(1).date(-1) } else { this.viewedMonth.addYear(-Math.pow(10, f - 1)) } this.navigateHorizontally(f, this.viewedMonth.toDate(), false) }, navigateToFuture: function (g) { if (g) { g.preventDefault() } var f = this.currentView.index; if (f == 0) { this.viewedMonth.date(32).date(1) } else { this.viewedMonth.addYear(Math.pow(10, f - 1)) } this.navigateHorizontally(f, this.viewedMonth.toDate(), true) }
	}; $.fn.tCalendar = function (f) { return tlrk.create(this, { name: "tCalendar", init: function (g, h) { return new tlrk.calendar(g, h) }, options: f }) }; $.fn.tCalendar.defaults = { selectedDate: null, minDate: new Date(1899, 11, 31), maxDate: new Date(2100, 0, 1) }; $.extend(tlrk.calendar, { views: [{ index: 0, title: function (f) { return new tlrk.stringBuilder().cat(tlrk.cultureInfo.months[f.month()]).cat(" ").cat(f.year()).string() }, body: function (j, m, p, o, n, r) { var y = (new tlrk.stringBuilder()).cat("<thead><tr>"); var l = tlrk.cultureInfo.firstDayOfWeek, h = tlrk.cultureInfo.days, x = tlrk.cultureInfo.abbrDays, s = tlrk.cultureInfo.shortestDays; h = h.slice(l).concat(h.slice(0, l)); x = x.slice(l).concat(x.slice(0, l)); s = s.slice(l).concat(s.slice(0, l)); for (var k = 0; k < 7; k++) { y.cat('<th scope="col" abbr="').cat(x[k]).cat('" title="').cat(h[k]).cat('">').cat(s[k]).cat("</th>") } y.cat("</tr></thead><tbody>"); var t = tlrk.datetime.firstVisibleDay(j); var f = j.month(); var v = o === null ? false : j.year() == o.year(); var w; for (var u = 0; u < 6; u++) { y.cat("<tr>"); for (var g = 0; g < 7; g++) { w = t.month() != f ? "t-other-month" : (v && t.month() == o.month() && t.date() == o.date()) ? " t-state-selected" : ""; y.cat("<td").catIf(' class="' + w + '"', w).cat(">"); if (tlrk.calendar.isInRange(t.toDate(), m.toDate(), p.toDate())) { y.cat('<a href="'); var q = "#"; if (n) { q = tlrk.calendar.formatUrl(n, t); if (r && !tlrk.calendar.isInCollection(t, r)) { q = "#" } } y.cat(q).cat('" class="t-link').cat(q != "#" ? " t-action-link" : "").cat('" title="').cat(tlrk.datetime.format(t.toDate(), tlrk.cultureInfo.longDate)).cat('">').cat(t.date()).cat("</a>") } else { y.cat("&nbsp;") } y.cat("</td>"); tlrk.datetime.modify(t, tlrk.datetime.msPerDay) } y.cat("</tr>") } y.cat("</tbody>"); return y.string() }, compare: function (g, f) { var i; var l = g.month(); var h = g.year(); var k = f.month(); var j = f.year(); if (h > j) { i = 1 } else { if (h < j) { i = -1 } else { i = l == k ? 0 : l > k ? 1 : -1 } } return i }, firstLastDay: function (h, f, g) { return f ? tlrk.datetime.firstDayOfMonth(h) : new tlrk.datetime(h.year(), h.month() + 1, 0) }, navCheck: function (g, f, h) { if (h) { return new tlrk.datetime(f.year(), f.month() + 1, f.date()).value - g.value <= 0 } else { return this.compare(g, f) === -1 } } }, { index: 1, title: function (f) { return f.year() }, body: function (g, f, h) { return tlrk.calendar.metaView(true, g, function () { var j = []; var m = 0; var l = 11; if (f.year() == h.year()) { m = f.month(); l = h.month() } else { if (g.year() == f.year()) { m = f.month() } else { if (g.year() == h.year()) { l = h.month() } } } for (var k = 0; k < 12; k++) { if (k >= m && k <= l) { j.push(tlrk.cultureInfo.abbrMonths[k]) } else { j.push("&nbsp;") } } return j }) }, compare: function (f, g) { return f.year() > g.year() ? 1 : f.year() < g.year() ? -1 : 0 }, verticalDate: function (f, g) { if (!g) { return f.month() } f.month($.inArray(g, tlrk.cultureInfo.abbrMonths)) }, firstLastDay: function (f, g) { return new tlrk.datetime(f.year(), g ? 0 : 11, 1) }, navCheck: function (h, f, i) { var g = this.compare(h, f); return i ? g == 1 : g == -1 } }, { index: 2, title: function (g) { var f = g.year() - g.year() % 10; return f + "-" + (f + 9) }, body: function (g, f, h) { return tlrk.calendar.metaView(false, g, function () { var k = []; var j = f.year(); var m = h.year(); var l = g.year() - g.year() % 10 - 1; for (var n = 0; n < 12; n++) { k.push(l + n >= j && l + n <= m ? l + n : "&nbsp;") } return k }) }, compare: function (g, f, i) { var j = g.year(); var h = (j - j % 10); var k = (j - j % 10 + 9); return tlrk.calendar.check(h, k, f, i) }, verticalDate: function (f, g) { if (!g) { return f.year() % 10 + 1 } f.year(g) }, firstLastDay: function (f, g) { return new tlrk.datetime(f.year() - f.year() % 10 + (g ? 0 : 9), 0, 1) }, navCheck: function (h, f, i) { var g = this.compare(f, h, i); return i ? g == -1 : g == 1 } }, { index: 3, title: function (g) { var f = g.year() - g.year() % 100; return f + "-" + (f + 99) }, body: function (g, f, h) { return tlrk.calendar.metaView(false, g, function () { var l = g.year() - g.year() % 100; var j = []; for (var m = -1; m < 11; m++) { var k = l + m * 10; if ((k + 10) >= f.year() && k <= h.year()) { j.push(k + "-<br />" + (k + 9) + "&nbsp;") } else { j.push("&nbsp;<br />&nbsp;") } } return j }) }, compare: function (g, f, h) { var i = g.year(); var k = (i - i % 100); var j = (i - i % 100 + 99); return tlrk.calendar.check(k, j, f, h) }, verticalDate: function (f, g) { if (!g) { return Math.ceil(f.year() / 10) % 10 + 1 } f.year(g.substring(0, g.indexOf("-"))) }, firstLastDay: function (f, g) { return g ? new tlrk.datetime(f.year() - (f.year() % 100), 0, 1) : new tlrk.datetime(f.year() - (f.year() % 100) + 99, 0, 1) }, navCheck: function (h, f, i) { var g = this.compare(f, h, i); return i ? g == -1 : g == 1 } }], check: function (i, j, f, g) { var h = function (k) { return k < f.year() ? -1 : k > f.year() ? 1 : 0 }; return g ? h(j) : h(i) }, html: function (i, g, f, j, k, h) { i = i || new tlrk.datetime(); f = f || new tlrk.datetime($.fn.tCalendar.defaults.minDate); j = j || new tlrk.datetime($.fn.tCalendar.defaults.maxDate); return new tlrk.stringBuilder().cat('<div class="t-widget t-calendar">').cat('<div class="t-header">').cat('<a href="#" class="t-link t-nav-prev">').cat('<span class="t-icon t-arrow-prev"></span></a><a href="#" class="t-link t-nav-fast">').cat(tlrk.calendar.views[0].title(i)).cat("</a>").cat('<a href="#" class="t-link t-nav-next"><span class="t-icon t-arrow-next"></span></a>').cat("</div>").cat('<table class="t-content" cellspacing="0">').cat(tlrk.calendar.views[0].body(i, f, j, g, k, h)).cat("</table></div>").string() }, metaView: function (k, m, g) { var h = new tlrk.stringBuilder(); var l = g(); h.cat("<tr>"); for (var f = 0, j = l.length; f < j; f++) { h.catIf("</tr><tr>", f > 0 && f % 4 == 0).cat("<td").catIf(' class="t-other-month"', (f == 0 || f == j - 1) && k == false).cat(">"); if (l[f] !== "&nbsp;" && l[f] !== "&nbsp;<br />&nbsp;") { h.cat('<a href="#" class="t-link">').cat(l[f]).cat("</a>") } else { h.cat(l[f]) } h.cat("</td>") } h.cat("</tr>"); return h.string() }, isInRange: function (g, f, h) { if (!g) { return false } var i = new Date(g.getFullYear(), g.getMonth(), g.getDate()); var j = new Date(f.getFullYear(), f.getMonth(), f.getDate()); var k = new Date(h.getFullYear(), h.getMonth(), h.getDate()); return j - i <= 0 && k - i >= 0 }, fitDateToRange: function (h, f, g) { if (h.value < f.value) { h = new tlrk.datetime(f.value) } if (h.value > g.value) { h = new tlrk.datetime(g.value) } return h }, isInCollection: function (i, f) { var g = f[i.year()]; if (g) { var h = g[i.month()]; if (h && $.inArray(i.date(), h) != -1) { return true } } return false }, findTarget: function (k, n, m, i) { k = k.value ? k : new tlrk.datetime(k); var f = function (o, p) { return $.grep(o, function (q) { return $(q).children().eq(0).text().indexOf(p) > -1 })[0] }; var g = i ? "last" : "first"; var l = $(".t-content:" + g + " td:has(> .t-link)", m).removeClass("t-state-focus"); var h; if (n == 0) { h = $(f(l.filter(":not(.t-other-month)"), k.date())) } else { if (n == 1) { h = $(f(l, tlrk.cultureInfo.abbrMonths[k.month()])) } else { if (n == 2 || n == 3) { var j = k.year(); h = $(f(l, n == 2 ? j : j - (j % 10))); if (h.length == 0 && n == 3) { h = $(f(l, j - (j % 10) + 99)) } } } } return h }, focusDate: function (i, f, h, g) { tlrk.calendar.findTarget(i, f, h, g).addClass("t-state-focus") }, formatUrl: function (f, i) { var h = f.match(d); if (h) { h = h[0]; var g = h === "{0}" ? tlrk.cultureInfo.generalDateTime : h.replace(c, "").replace("}", ""); return f.replace(h, tlrk.datetime.format(i.toDate(), g)) } return f } })
})(jQuery);

if (typeof (Sys) !== 'undefined')
	Sys.Application.notifyScriptLoaded();
