﻿/******************************************************************************
**	Desc: Extensions to jQuery.
**	Auth: Aleksey Fomichenko
******************************************************************************/

(function() 
{
	$.fn.linkCmd = function (fn, context)
	{
		/// <summary>Binds a function to the command (click or enter key) event of each matched element.</summary>
		/// <param name="fn" type="Function">The function to execute.</param>
		/// <param name="context" type="Object">The instance of an object to set the 'this' keyword to when executing the function.</param>

		if (!fn) return this;

		return this.
			click(function (evt) { evt.preventDefault(); $(evt.target).blur(); if ($(evt.target).hasClass("disabled")) return; fn.apply(context || this, arguments); }).
			keydown(function (evt) { if (!$util.isSubmitKey(evt, true) || $(evt.target).hasClass("disabled")) return; fn.apply(context || this, arguments); });
	};

	$.fn.txtCmd = function (fn, selectOnFocus, context)
	{
		/// <summary>Binds a function to the enter key event of each matched textbox element.</summary>
		/// <param name="fn" type="Function">The function to execute.</param>
		/// <param name="selectOnFocus" type="Boolean">Whether to select the text in a textbox on focus.</param>
		/// <param name="context" type="Object">The instance of an object to set the 'this' keyword to when executing the function.</param>

		if (!fn) return this;
		
		if (selectOnFocus)
			this.focus(function (evt) { window.setTimeout(function () { $(evt.target).select() }, 10); });

		return this.keydown(function (evt) { if ($util.isSubmitKey(evt)) fn.apply(context || this, arguments); });
	};
})();
