﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="jquery-1.3.2.min-vsdoc.js" />


function $d(obj, method, delay) {
    if (delay && typeof (delay) == 'number' && delay > 0)
        return function() {
            setTimeout(function() { method.apply(obj, arguments); }, delay);
        }
    else
        return function() {
            method.apply(obj, arguments);
        }
};
(function($) {
	$.fn.CheckValueLength = function(maxLen) {
		this.each(function() {
			var inpt = null, ta = null, msg = $('<span class="avail-chars-title">&#160;Осталось: <span></span></span>')[0], label = $(msg).children("span")[0],
			setLen = function(e) {
				var l = maxLen - this.value.length;
				$(e.data).text(l >= 0 ? l : 0);
			};
			if (this.tagName.toLowerCase() == "textarea" || this.tagName.toLowerCase() == "input") {
				if (this.tagName.toLowerCase() == "input") {
					inpt = this;
					$(inpt).attr("maxlength", maxLen);
				}
				else
					ta = this;
				var l = $(this).prev("label[for='" + this.id + "']")[0];
				if (l != null)
					$(msg).appendTo(l);
				else
					$(msg).insertBefore(this);


			} else if ($(this).hasClass("field")) {
				ta = $(this).find("textarea")[0];
				if (ta == null)
					inpt = $(this).find(":text")[0];
				var l = $(this).find("label")[0];
				if (l != null)
					$(msg).appendTo(l);
				else
					$(msg).insertBefore(ta || inpt);
			}
			if (ta == null && inpt == null)
				return this;
			setLen.call(ta || inpt, { data: label });

			if (ta != null) {
				$(ta).bind("change", function(e) {
					if (this.value.length > maxLen)
						this.value = this.value.substr(0, maxLen);
				}).bind("keyup", function(e) {
					if (e.ctrlKey || e.altKey)
						return;
					if (this.value.length <= maxLen)
						return;
					this.value = this.value.substr(0, maxLen);
				}).bind("keydown", function(e) {
					if (e.ctrlKey || e.altKey)
						return;
					if ((e.keyCode < 48 && e.keyCode != 32) || (e.keyCode > 57 && e.keyCode < 61))
					{ return; }
					if (this.value.length >= maxLen)
						e.preventDefault();
				});
			}


			$(ta || inpt).bind("change", label, setLen)
				.bind("keyup", label, setLen)
				.bind("keydown", label, setLen);

			return this;
		});
		return this;
	}
})(jQuery);
(function($) {
	$.fn.SelectAllCb = function() {
		this.each(function() {
			//alert(this.id);
			var t = this, n = this.value, f = this.form;
			$(this).bind("click", function() {
				var c = this.checked;
				$(f).find("input[type='checkbox'][name='" + n + "']").each(function() {
					this.checked = c;
				});
			});
			$(f).find("input[type='checkbox'][name='" + n + "']").bind("click", function() {
				if (t.checked && !this.checked)
					t.checked = false;
			});
		});
	}
})(jQuery);
(function($) {
	$.fn.Toggleable = function(options) {
		oP = options || {};
		this.each(function() {
			var t = $(this);
			if (t.children().length != 2)
				return;
			t.children(":first").addClass("ui-toggle-header").prepend('<span class="ui-icon ui-icon-triangle-1-e ui-icon-triangle-1-s"/>').click(function() {
				$(this).children("span.ui-icon").toggleClass("ui-icon-triangle-1-s").end().next().toggle(oP.speed);
			});

		});
	}
})(jQuery);


(function($) {
	$.fn.LinkedFields = function(options) {
		this.each(function() {
			var t = $(this), state = {};
			function onchange() {
				var q;
				if (this.value != "") {
					if ($(this).parent().next("div.field").length == 0) {
						q = $(state.tpl).clone().appendTo(state.rootEl);
						q.find("select").attr("selectedIndex", 0).bind("change", onchange);
						q.find("input").attr("value", "").bind("change", onchange).bind("blur", onchange);
						;
					}
				} else {
					if ($(this).parent().next("div.field").length > 0 && $(state.rootEl).children("div.field").length > 1)
						$(this).parent().remove();
				}
			}
			state.rootEl = this;
			state.tpl = $(this).children("div.field")[0];
			if (!state.tpl)
				return this;
			t.find("select").bind("change", onchange);
			t.find("input").bind("change", onchange);
			return this;
		});
		return this;

	};
})(jQuery);

(function($) {
	$.fn.listMenu = function(options) {
		$(this).find(".section").each(function() {
			new menuItem(this, null, options);
		});
		return this;
	};
	function menuItem(el, parent, options) {
		var t = this; //, off = t.offset();
		t.options = options || {};
		t.parent = null;
		t.o = false;
		t.over = itemOver;
		t.out = itemOut;
		t.toggleWiew = toggleWiew;

		t.el = $(el)
			.bind("mouseover.plugin-listMenu", $d(t, t.over))
			.bind("mouseout.plugin-listMenu", $d(t, t.out));
		;
		t.dx1 = Math.ceil((t.el.outerWidth(true) - t.el.outerWidth(false)) / 2);
		t.dx2 = t.el.outerWidth(false);
		t.dy = t.el.outerHeight(false)
		if (parent == null && t.options.horizontal) {
			t.setPosition = function() {
				var p = this.el.position();
				this.sub.css("left", p.left + this.dx1).css("top", p.top + this.dy);
				if (this.ifrBg)
					this.ifrBg.css("left", p.left + this.dx1).css("top", p.top + this.dy);
			}
		} else {
			t.setPosition = function() {
				var p = this.el.position();
				this.sub.css("left", p.left + this.dx2).css("top", p.top);
				if (this.ifrBg)
					this.ifrBg.css("left", p.left + this.dx1).css("top", p.top + this.dy);
			}
		}
		//		if (parent == null && t.options.horizontal) {
		//			pos = t.el.offset();
		//			x = pos.left;
		//			y = pos.top + t.el.outerHeight(false);
		//		} else {
		//			pos = t.el.position();
		//			x = pos.left + t.el.outerWidth(false);
		//			y = pos.top;
		//		}
		var off = t.el.offset();
		t.sub = t.el.children("ul").eq(0)
			.css("position", "absolute")//.css("left", x).css("top", y)
			.bind("mouseover.plugin-listMenu", $d(t, t.over))
			.bind("mouseout.plugin-listMenu", $d(t, t.out));
		if ($.browser.msie) {
			t.ifrBg = $('<iframe frameborder="0" scrolling="no" style="display:none;position:absolute;"/>').insertBefore(t.sub).hide();
		}
		t.setPosition();
		if (t.options.deep) {
			t.o = true;
			t.toggleWiew();
			t.sub.children("li").each(function() {
				new menuItem(this, t);
			});
			t.o = false;
			t.toggleWiew();
		}
	};
	function toggleWiew() {
		var t = this;
		if (t.o) {
			t.el.addClass("ngn-menu-over");
			t.sub.addClass("ngn-submenu-over").show();
			if (t.ifrBg)
				t.ifrBg.css({ width: t.sub.outerWidth(), height: t.sub.outerHeight() }).show();
		} else {
			t.el.removeClass("ngn-menu-over");
			t.sub.removeClass("ngn-submenu-over").hide();
			if (t.ifrBg)
				t.ifrBg.hide();
		}
	}
	function itemOver() {
		this.o = true;
		this.setPosition();
		setTimeout($d(this, toggleWiew), 100);
	};
	function itemOut() {
		this.o = false;
		setTimeout($d(this, toggleWiew), 100);
	};
})(jQuery);

(function($) {
    $.fn.listAdvancedMenu = function(options) {
        $(this).find(".section").each(function() {
            new menuItem(this, null, options);
        });
        return this;
    };
    function menuItem(el, parent, options) {
        var t = this, x, y, pos; //, off = t.offset();
        t.options = options || {};
        t.parent = null;
        t.o = false;
        t.over = itemOver;
        t.out = itemOut;
        t.toggleWiew = toggleWiew;

        t.el = $(el)
			.bind("mouseover.plugin-listMenu", $d(t, t.over))
			.bind("mouseout.plugin-listMenu", $d(t, t.out));
        ;
        if (parent == null && t.options.horizontal) {
            pos = t.el.offset();
            x = pos.left;
            y = pos.top + t.el.outerHeight(false);
        } else {
            pos = t.el.position();
            x = pos.left + t.el.outerWidth(false);
            y = pos.top;
        }
        var off = t.el.offset();
        t.sub = t.el.children("ul").eq(0)
			.css("position", "absolute").css("left", x).css("top", y)
			.bind("mouseover.plugin-listMenu", $d(t, t.over))
			.bind("mouseout.plugin-listMenu", $d(t, t.out));
        if (t.options.deep) {
            t.o = true;
            t.toggleWiew();
            t.sub.children("li").each(function() {
                new menuItem(this, t);
            });
            t.o = false;
            t.toggleWiew();
        }
    };
    function toggleWiew() {
        var t = this;
        if (t.o) {
            t.el.addClass("ngn-menu-over");
            t.sub.addClass("ngn-submenu-over").show();
        } else {
            t.el.removeClass("ngn-menu-over");
            t.sub.removeClass("ngn-submenu-over").hide();
        }
    }
    function itemOver() {
        this.o = true;
        setTimeout($d(this, toggleWiew), 100);
    };
    function itemOut() {
        this.o = false;
        setTimeout($d(this, toggleWiew), 100);
    };
})(jQuery);
