﻿/// <reference path="jquery-vsdoc.js" />
/// <reference name="MicrosoftAjax.js" />

(function () {
	var _getValue = function (data, expr, format) {
		var value = null;

		if (expr == ".") value = data;
		else value = data[expr];

		if (value == null) return "";
		try {
			if (format != null) return value.localeFormat(format);
		} catch (ex) {
			throw String.format("Cannot specify format {0} for value {1}. Inner error: {2}", format, value, ex);
		}
		return value.toString();
	};

	var _setValue = function (data, expr, value) {
		if (expr == ".") throw "Unable to use self expression in two-way binding.";
		data[expr] = value;
	};

	var _twoWay = function (element, data, expr, format, attr, event) {
		element.attr(attr, _getValue(data, expr, format));
		element.bind(event, function () {
			var value = element.attr(attr);
			_setValue(data, expr, value);
		});
	};

	$ = jQuery;

	$.prototype.visible = function (value) {
		if (value)
			this.show();
		else
			this.hide();
	};

	$.prototype.dataRefresh = function () {
		var data = this.data("cx-data");
		var callback = this.data("cx-onitembound");
		this.dataBind(data, callback);
	};

	$.prototype.dataAppend = function (newData) {
		var data = this.data("cx-data");
		data.push(newData);
		var callback = this.data("cx-onitembound");
		this.dataBind(data, callback);
	};

	$.prototype.dataBind = function (data, callback, parentData, childIndex, rootElement) {
		if (this.hasClass("cx-repeater")) {
			this.data("cx-data", data);
			this.data("cx-onitembound", callback);
			this.children(".cx-item").remove();

			var template = this.children(".cx-template");
			for (var i = 0; i < data.length; i++) {
				var newNode = $(template.clone(false));
				this.append(newNode);
				newNode.dataBind(data[i], callback, data, i, this);
				newNode.addClass("cx-item");
			}
		} else {
			this.data("cx-data", data);
			this.data("cx-onitembound", callback);
			$("*", this).andSelf().each(function () {
				var bound = $(this);
				for (var i = 0; i < this.attributes.length; i++) {
					var attr = this.attributes[i].name + "";
					if (attr.indexOf("cx:") == 0) {
						attr = attr.substring(3);
						var fullExpr = this.attributes[i].value;
						var dot = fullExpr.indexOf(':');
						var expr = dot != -1 ? fullExpr.substring(0, dot) : fullExpr;
						var format = dot != -1 ? fullExpr.substring(dot + 1) : null;

						if (attr == "value") {
							bound.attr(attr, value);
							if (expr == ".")
								_twoWay(bound, parentData, childIndex, format, attr, "change");
							else
								_twoWay(bound, data, expr, format, attr, "change");
						} else {
							var value = _getValue(data, expr, format);
							if (attr == "text")
								bound.text(value);
							else if (attr == "html")
								bound.html(value);
							else if (attr == "visible")
								bound.visible(value == "true");
							else
								bound.attr(attr, value);
						}
					}
				}
			});

			this.children(".cx-remove").click(function () {
				parentData.splice(childIndex, 1);
				rootElement.dataRefresh();
			});

			this.removeClass("cx-template");

			if (callback != null)
				callback({ index: childIndex, data: data, element: this });
		}
	};

	$.prototype.setActiveView = function () {
		var multiview = this.closest(".cx-multiview");
		multiview.children(".cx-view").hide();
		this.show();
	};

	$.prototype.watermark = function (watermark) {
		var field = this;

		field.data("cx-watermark", watermark);

		var focus = function () {
			if (field.hasClass("cx-watermark")) {
				field.removeClass("cx-watermark");
				field.val("");
			}
		};

		var blur = function () {
			window.setTimeout(function () {
				if (field.val() == "") {
					field.addClass("cx-watermark");
					field.val(watermark);
				}
			}, 100);
		};

		field.focus(focus);
		field.blur(blur);
		blur();
	};

	$.hideWatermarks = function () {
		$(".cx-watermark").val("");
	};

	$.showWatermarks = function () {
		$(".cx-watermark").each(function () {
			$(this).val($(this).data("cx-watermark"));
		});
	};

	if (typeof (Page_ClientValidate) != "undefined") {
		var _oldValidateFunction = Page_ClientValidate;

		Page_ClientValidate = function (validationGroup) {
			$.hideWatermarks();

			if (_oldValidateFunction(validationGroup))
				return true;

			$.showWatermarks();
		};
	}

	// Fixes weird problem on the ShowReport page, without this fix it's not possible to click the [get me a new report] button, because
	// validation chockes up and button event is not triggered. Validation chokes up because Page_ValidationSummaries array contains an element
	// which is equal to null, this code removes that redundant element so that validation does not throw exception
	$(function () {
		if (typeof(Page_ValidationSummaries) != "undefined") {
			var x = Page_ValidationSummaries; if (x) for (var i = x.length - 1; i >= 0; i--) if (!x[i]) x.splice(i, 1);
		}
	});
})();

MAX_show = function () { };
