$.ns('AEC');

/*
 * AEC.Mask
 *
 * Masks an element. Caller must keep a reference to the mask to be able to destroy it by calling destroy();
 *
 * Example usage:  var mask = AEC.Mask({
 *                   // The element to mask
 *                   el : $('myDiv'),
 *                   
 *                   // Optional, the message to display. If empty, only a loading gif-image will be shown
 *                   message : 'Please wait',      
 *
 *                   // Optional (default 'center'), horizontal alignment of the message accepted values: 'left', 'center' or 'right'
 *                   msgAlign : 'center',          
 *
 *                   // Optional (default false)
 *                   border : true                 
 *                 });
 *                 
 *                 // Call with true to fade out the mask
 *                 mask.destroy(true);
 *
 *  TODO: Reposition mask after window resize
 */
AEC.Mask = function(config) {
	var body = $('body'),
	    horizontalOffset,
	    el = config.el, 
	    message = config.message, 
	    msgAlign = config.msgAlign,
	    border = config.border,
	    offsets = el.offset(),
	    maskWidth = el.width(),
	    maskHeight = el.height(),
	    maskEl = $('<div class="loadmask ' + (border ? 'border' : '') + '"></div>');
	
	maskEl.height(maskHeight);
	maskEl.width(maskWidth);
	
	maskEl.css('left', offsets.left);
	maskEl.css('top', offsets.top);
	maskEl.css('filter', "alpha(opacity=70)");
	
	maskEl.maskLoadIndicator = $('<div class="loadmask-msg">' + (message ? '<span>'+ message+ '</span>' : '') + '</div>');
	
	maskEl.destroy = function(useFade) {
	    if (useFade) {
	      var that = this;
	      that.maskLoadIndicator.remove();
	      this.fadeOut("normal", function(){ that.remove();});
	    } else {
	      this.remove();
	      this.maskLoadIndicator.remove();
	    }
	};
	
	body.append(maskEl);
	body.append(maskEl.maskLoadIndicator);
	
	if (!msgAlign || msgAlign === "center") {
	  horizontalOffset = (maskWidth - 20 - maskEl.maskLoadIndicator.width())/2;
	} else if (msgAlign === "left") {
	  horizontalOffset = 20;
	} else {
	  horizontalOffset = (maskWidth - 40 - maskEl.maskLoadIndicator.width());
	}
	
	//Apply center vert & / right horizontal position to the mask
	maskEl.maskLoadIndicator.css("top", offsets.top + (maskHeight / 2) - 10 + "px");
	maskEl.maskLoadIndicator.css("left", offsets.left + horizontalOffset + "px");
	
	return maskEl;
};