
function jQ_CommonSetup() {

//|----------------------------------
//| Custom JQuery extensions
//|----------------------------------

	//| Case-insensitive $.Contains()
	jQuery.extend(
		jQuery.expr[':'].Contains = function(a,i,m){
		     return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
		}
	)

	jQuery.fn.htmlFromURL = function (sURL) {
		URLToElement(sURL, this, "");
		return this;
	}

	jQuery.fn.CenterOnScreen = function () {
		CenterOnScreen(oElement);
		return this;
	}

	jQuery.fn.CenterHoriz = function () {
	    this.css("position","absolute");
	    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	    return this;
	}

	jQuery.fn.CenterVert = function () {
	    this.css("position","absolute");
	    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	    return this;
	}

	jQuery.fn.Maximize = function () {
		MaximizeElement(this);
		return this;
	}

	jQuery.fn.CenterAtTop = function () {
		CenterAtTop(oElement);
		return this;
	}

//|----------------------------------
//| Attribute-based bindings
//|----------------------------------

	//|----------------------------------
	//| Display behaviors
	//|----------------------------------
	$("input[type=text],input[type=password],textarea").focus(
		function()
			{
			if ($(this).width() > 600)
				{ $(this).addClass('FormInputWideActive'); }
			else
				{ $(this).addClass('FormInputActive'); }
			}
		)
	$("input[type=text],input[type=password],textarea").blur(
		function()
			{
			if ($(this).width() > 600)
				{ $(this).addClass('FormInputWide'); }
			else
				{ $(this).addClass('FormInput'); }
			}
		)

	//|----------------------------------
	//| DHTML behaviors
	//|----------------------------------

	$("[data-TT]").qtip(
						{
						content: { text: false },
						position: { corner: { target: 'rightMiddle', tooltip: 'leftMiddle' } },
						hide: {
							fixed: true,
							delay: 500,
							effect: { length: 250 }
							},

						style: {
							name: "blue",
							tip: { corner: 'leftMiddle' },
							width: { min: 0, max: 600 }
							}
						}
					);

	$("[data-ElementsToggled]").click(
		function () { $($(this).attr("data-ElementsToggled")).toggle(); });

	$("[data-ElementsFiltered]").keyup(
		function ()
			{
			if ($(this).val().length > 1)
				{
				$($(this).attr("data-ElementsFiltered")).hide();
				$($(this).attr("data-ElementsFiltered") + ":Contains('" + $(this).val() + "')").show();
				$($(this).attr("data-ElementsFiltered")).removeHighlight().highlight($(this).val());
				}
			else if ($(this).val().length == 0)
				{
				$($(this).attr("data-ElementsFiltered") + ":Contains('" + $(this).val() + "')").show();
				$($(this).attr("data-ElementsFiltered")).removeHighlight();
				}
			});

	$(".AutoScroll").each(
		function () { $(this).scrollTop($(this).attr('scrollHeight')); });

	$("[data-Resizable]").each(
		function ()
				{
				$(this).resizable();
				}
		);


	$(".Clearable").each(
		function () {
			$(this).after("<DIV CLASS='PanelButton' STYLE='width:20px; display:inline;' onClick=\"$('#" + $(this).attr("ID") + "').val('').keyup()\"><IMG SRC='/Images/Icons/Silk/cross.png' CLASS='Icon'></DIV>")
			});

	$("[data-UnExpandedHeight]").each(
		function ()
			{
			var nUnExpandedHeight = GetInt($(this).attr("data-UnExpandedHeight"));
			var nFullHeight = GetInt($(this).height());
			$(this).attr("data-ExpandedHeight", nFullHeight)
			if (nFullHeight > nUnExpandedHeight)
				{
				$(this).height(nUnExpandedHeight);
				$(this).append("<DIV CLASS='BottomFade'></DIV>")

				AlignFaders();
				setTimeout(AlignFaders, 2000);
				setTimeout(AlignFaders, 5000);
				setTimeout(AlignFaders, 10000);

				//$(this).css("overflow", "hidden");
				if (GetStr($(this).attr("data-Scrollable")) != "")
					{
					$(this).css("overflow", "auto");
					}
				}
			});

function AlignFaders() {
	$("[data-UnExpandedHeight]").each(
		function () {
//			if (GetStr($(this).attr("data-Expanded")) == "")
//				{
				var oDivPos = $(this).position();
				$(this).children('.BottomFade')
					//.show()
					.css("left", (oDivPos.left) + "px")
					.width($(this).width() + 2)
					.css("top", (oDivPos.top + $(this).height() - $(this).children('.BottomFade').height() + 2) + "px");
//				}
		});
}


	$("[data-UnExpandedHeight]").mouseover(
		function ()
			{
			$(this).attr("data-Expanded", "1");
			$(this).stop(true);
			$(this).css("height","auto");

			AlignFaders();
			$(this).children('.BottomFade').hide();

			/*
			var nExpandedHeight = GetInt($(this).attr("data-ExpandedHeight"));
			$(this).animate(
					{
					height: nExpandedHeight + 'px'
					},
				   	{
				   	duration : 100,
					queue: false
					}
				);
			*/
			});

	$("[data-UnExpandedHeight]").mouseleave (
		function ()
			{
			/*
			$(this).stop(true);
			var nUnExpandedHeight = GetInt($(this).attr("data-UnExpandedHeight"));
			$(this).animate(
					{ height: nUnExpandedHeight + 'px' },
				   	{
				   	duration : 500,
					queue: false,
					step: AlignFaders
					}
				);
			*/
			});

	//|----------------------------------
	//| Ajax automation
	//|----------------------------------

	$("[data-ModalSourceURL]").click(
		function (e)
			{
			if (GetStr($(this).attr("data-ManualClick")) == "")
				{ HandleModalLink($(this)); }
			e.stopPropagation();
			e.preventDefault();
			});

	$("[data-TT_Content_Title]").each(
		function (e)
			{
			SetupTooltip($(this));
			});

	$("[data-content_url]").each(
		function ()
			{
//			if (GetStr($(this).attr("data-NoLoadingMessage")) == "")
//				{ $(this).html(AjaxLoadingMessage()); }
			var sContentURL = GetStr($(this).attr("data-content_url"));
			if (sContentURL.length > 2)
				{
				UpdateElementContent($(this).attr("ID"));
				if (GetStr($(this).attr("data-RefreshInterval")) != "")
					{
					var nRefreshInterval = GetInt($(this).attr("data-RefreshInterval"));
					if (nRefreshInterval >= 100)
						{ setInterval("UpdateElementContent('" + $(this).attr("ID") + "')", nRefreshInterval); }
					}
				}
			});

	$("[data-frameafter_url]").click(
		function ()
			{
			$(this).after("<BR><IFRAME SRC='" + $(this).attr("data-frameafter_url") +
						"' STYLE='height:" + $(this).attr("data-frameafter_height") + "px; width:" +
						$(this).attr("data-frameafter_width") + "px;'>");
			});

	$("[data-ajaxposturl]").click(
		function ()
			{
			if ($(this).attr("data-confirm"))
				{
				if ($(this).attr("data-confirm") != "")
					{
					if (!confirm($(this).attr("data-confirm")))
						{ return false; }
					}
				}

			oOutputBox = $(this).parent();
			if (GetStr($(this).attr("data-outputcontainer") != ""))
				{ oOutputBox = $("#" + $(this).attr("data-outputcontainer")); }


			oOutputBox.html(AjaxLoadingMessage()).slideDown(300);
			$.post($(this).attr("data-ajaxposturl"), $(this).attr("data-ajaxpostparams"),
				function(data, sStatus){
					oOutputBox.html(data).slideDown(300);
					} );
			});

	$("[data-outputcontainer]").click(
		function (e)
			{
			if (GetStr($(this).attr("data-ManualClick")) == "")
				{ OutputLinkToContainer($(this)); }
			e.stopPropagation();
			e.preventDefault();
			});

	$("FORM[data-ajaxoutput]").submit(
		function (e)
			{
			if (GetStr($(this).attr("data-ManualSubmit")) == "")
				{ HandleAjaxForm($(this)); }
			e.preventDefault();
			e.stopPropagation();
			});


	//|----------------------------------
	//| Form input behavior
	//|----------------------------------
	$("TEXTAREA.AutoResize").each(
		function ()
			{
			nMaxHeight = GetInt($("#" + $(this).attr("data-MaxHeight")));
			if (!(nMaxHeight > 5)) { nMaxHeight = 200; }
			$(this).autoResize({
				onResize : function() {
					$(this).css({opacity:0.8});
				},
				animateCallback : function() {
					$(this).css({opacity:1});
				},
				animateDuration : 300,
				animateDuration : 300,
				animateDuration : 300,
				animateDuration : 300,
				animateDuration : 300,
				extraSpace : 20,
				limit : nMaxHeight
				});
			}
		);


	$("INPUT.FormField_Money")
		.blur(
			function(event)
				{
				$(this).val(GetNumeric($(this).val()));
				nCurrentValue = GetNumeric($(this).val());
				nMinValue = GetNumeric($(this).attr("data-MinValue"));
				nMaxValue = GetNumeric($(this).attr("data-MaxValue"));

				if (nCurrentValue < nMinValue)
					{ $(this).val(nMinValue) }

				if ( (nCurrentValue > nMaxValue) && (nMaxValue > nMinValue) )
					{ $(this).val(nMaxValue) }

				$(this).val(FormatCurrency2($(this).val()));
				}
			);

	$("INPUT.FormField_Int")
		.blur(
			function(event)
				{
				$(this).val(GetInt($(this).val()));
				nCurrentValue = GetInt($(this).val());
				nMinValue = GetInt($(this).attr("data-MinValue"));
				nMaxValue = GetInt($(this).attr("data-MaxValue"));

				if (nCurrentValue < nMinValue)
					{ $(this).val(GetInt(nMinValue)) }

				if ( (nCurrentValue > nMaxValue) && (nMaxValue > nMinValue) )
					{ $(this).val(GetInt(nMaxValue)) }

				$(this).val(GetInt($(this).val()));
				}
			);

	// <TEXTAREA TITLE="Limit: 5000 Characters">
	$("TEXTAREA[TITLE*='Limit:']")
		.each(
			function()
				{ SetupTextAreaLimit(this) }
			);


	$("[data-BlankDefault]")
		.focus(
			function(event)
				{
				if (GetStr($(this).attr("data-AlreadyBlanked")) == "")
					{
					$(this).val('');
					$(this).attr("data-AlreadyBlanked","1");
					}
				}
			);
}

