﻿// fix for bug in IE where cleartype is lost after fadeIn
(function ($) {
	$.fn.applySpriteIcon = function (spriteIconClass) {
		return this.each(function () {

			var linkCss = 'spriteIconLink';
			var disabled = '';
			var isDisabled = $(this).attr('disabled');
			if (isDisabled) {
				disabled = ' gray';
				linkCss = 'spriteIconLinkDisabled';
			}

			var icon = '<span class="ui-silk floaty inline {0}{1}"></span>'.format(spriteIconClass, disabled);
			var titleText = $(this).attr('title');
			if (titleText.length == 0) {
				titleText = $(this).text();
			}
			$(this).addClass(linkCss).attr('title', titleText).prepend(icon);
		});
	}

	$.fn.fadeIn = function (speed, callback) {
		return this.animate({ opacity: 'show' }, speed, function () {
			if ($.browser.msie) {
				this.style.removeAttribute('filter');
			}
			if ($.isFunction(callback)) {
				callback.call(this);
			}
		});
	};

	$.fn.fadeOut = function (speed, callback) {
		return this.animate({ opacity: 'hide' }, speed, function () {
			if ($.browser.msie) {
				this.style.removeAttribute('filter');
			}
			if ($.isFunction(callback)) {
				callback.call(this);
			}
		});
	};

	$.fn.fadeTo = function (speed, to, callback) {
		return this.animate({ opacity: to }, speed, function () {
			if (to == 1 && $.browser.msie) {
				this.style.removeAttribute('filter');
			}
			if ($.isFunction(callback)) {
				callback.call(this);
			}
		});
	};

	// see http://stackoverflow.com/questions/483741/how-to-determine-which-html-page-element-has-focus
	$.fn.hasFocus = function () {
		if (document.activeElement)
			return ((this.length == 1) && (this[0] == document.activeElement));
	}

	function _formatThisString() {
		var txt = this;
		for (var i = 0; i < arguments.length; i++) {
			var exp = new RegExp('\\{' + (i) + '\\}', 'gm');
			txt = txt.replace(exp, arguments[i]);
		}
		return txt;
	}

	if (!String.prototype.format) {
		String.prototype.format = _formatThisString;
	}
})(jQuery);

function _dom_trackActiveElement(evt) {
	if (evt && evt.target) {
		document.activeElement = evt.target == document ? null : evt.target;
	}
}

function _dom_trackActiveElementLost(evt) {
	document.activeElement = null;
}

if (!document.activeElement && document.addEventListener) {
	document.addEventListener("focus", _dom_trackActiveElement, true);
	document.addEventListener("blur", _dom_trackActiveElementLost, true);
}

