(function( $ ) {
	
	$.fn.addLoadingMask = function(_text, _dom, _alpha, _contextPath, _top) {
		
		var loadingText = (_text == null) ? 'Updating list of courses...' : _text;
		var bgAlpha = (_alpha == null) ? .5 : _alpha;
		var contextPath = (_contextPath == null) ? '' : _contextPath;
		
		//if appending dom, will have to remove the original mask entirely
		if($(this).find('.contentMask').get(0)) {
			$(this).find('.contentMask').remove();
		}
		
		if(!$(this).find('.contentMask').get(0)) {
			var mask = document.createElement('div');
			$(mask).css({
				'display'	: 'none',
				'position'	: 'absolute',
				'clear'		: 'both',
				'overflow'	: 'hidden',
				'z-index'	: 10000,
				'top'		: $(this).position().top, //changed these from offset() to position() to resolve negative margin issues
				'left'		: $(this).position().left,
				'width'		: $(this).width(),
				'height'	: $(this).height()
			});
			if(_dom) {
				mask.appendChild(_dom);
			} else {
				tbox = document.createElement('div');
				$(tbox).css({
					'position'	: 'absolute',
					'top'		: $(this).position().top, //changed these from offset() to position() to resolve negative margin issues
					'left'		: $(this).position().left,
					'width': $(this).width(),
					'height': $(this).height(),
					'margin': '0 auto',
					'opacity': bgAlpha,
					'filter': 'alpha(opacity=50)',  
					'-moz-opacity': bgAlpha,					
					'background-color': '#000000'
				});
				mask.appendChild(tbox);

				//add spinner image
				var spinner = document.createElement('img');
				spinner.src = contextPath+'static/images/activity_indicator.gif';
				$(spinner).css({
					'margin': '0 10px -6px 0'
				})
				
				text = document.createElement('div');
				text.appendChild(spinner);
				text.appendChild(document.createTextNode(loadingText));
				$(text).css({
					'width': '500px',
					'margin': '0 auto',
					'margin-top': ((_top != undefined)? (_top + 'px'): '85px'),
					'padding': '8px 0 8px 0',
					'text-align': 'center',
					'font-size': '24px',
					'font-weight': 'bold',
					'color': '#FFFFFF',
					'background-color': '#000000',
					'border': 'solid 0.5px #eee', 
					'-webkit-box-shadow': '0 0 1px #fff',
					'border-radius': '5px',
					'-moz-border-radius': '5px',
					'-webkit-border-radius': '5px', 
					'opacity': 0.8,
					'filter': 'alpha(opacity=80)',  
					'-moz-opacity': 0.8				
				});
				mask.appendChild(text);
			}
			
			$(this).prepend(mask);
			$(mask).css({
				//'opacity'	: bgAlpha,
				//'background-color': '#000000',
				'display'	: 'block'
			});
			$(mask).addClass('contentMask');
		} else {
			$(this).find('.contentMask').css({
				'display': 'block',
				'top'		: $(this).position().top,
				'left'		: $(this).position().left,
				'width'		: $(this).width(),
				'height'	: $(this).height()
			});
		}
		
		return this;
	}
	
	$.fn.removeLoadingMask = function(_forceremove) {
		
		if(_forceremove) {
			if($(this).find('.contentMask').get(0)) {
				$(this).find('.contentMask').remove();
			}
		} else {
			if($(this).find('.contentMask').get(0)) {
				$(this).find('.contentMask').css({ 'display': 'none' });
			}
		}
		
	}
	
})( jQuery );
