/**
 * Copyright (c) 2009 Sergiy Kovalchuk (serg472@gmail.com)
 * 
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *  
 * Following code is based on Element.mask() implementation from ExtJS framework (http://extjs.com/)
 *
 */
;(function($) {

    /**
    * Displays loading mask over selected element.
    *
    * @param label Text message that will be displayed on the top of a mask besides a spinner (optional). 
    * 				If not provided only mask will be displayed without a label or a spinner.  	
    */
    $.fn.mask = function(label) {

        this.unmask();

        if (this.css("position") == "static") {
            this.addClass("masked-relative");
        }
        else if (this.css("position") == "fixed") {
            // Ryan's mod
            this.addClass("masked-fixed");
        }

        this.addClass("masked");

        var maskDiv = $('<div class="loadmask"></div>');

        //auto height fix for IE
        if (navigator.userAgent.toLowerCase().indexOf("msie") > -1) {
            maskDiv.height(this.height() + parseInt(this.css("padding-top")) + parseInt(this.css("padding-bottom")));
            maskDiv.width(this.width() + parseInt(this.css("padding-left")) + parseInt(this.css("padding-right")));
        }

        //fix for z-index bug with selects in IE6
        if (navigator.userAgent.toLowerCase().indexOf("msie 6") > -1) {
            $("body").find("select").addClass("masked-hidden"); // modified by Ryan
        }

        this.append(maskDiv);

        if (typeof label == "string") {
            var maskMsgDiv = $('<div class="loadmask-msg" style="display:none;" onclick="$(\'#loading\').unmask();"></div>'); // modifed by Ryan
            maskMsgDiv.append('<div>' + label + '</div>');
            this.append(maskMsgDiv);

            //calculate center position
            maskMsgDiv.css("top", Math.round(this.height() / 2 - (maskMsgDiv.height() - parseInt(maskMsgDiv.css("padding-top")) - parseInt(maskMsgDiv.css("padding-bottom"))) / 2) + "px");
            maskMsgDiv.css("left", Math.round(this.width() / 2 - (maskMsgDiv.width() - parseInt(maskMsgDiv.css("padding-left")) - parseInt(maskMsgDiv.css("padding-right"))) / 2) + "px");

            maskMsgDiv.show();

        }

    };

    /**
    * Removes mask from the element.
    */
    $.fn.unmask = function() {
        this.find(".loadmask-msg,.loadmask").remove();
        this.removeClass("masked");
        this.removeClass("masked-relative");
        this.removeClass("masked-fixed"); // Ryan's mod
        $("body").find("select").removeClass("masked-hidden"); // Ryan's mod
    };

})(jQuery);

// Ryan's mod
function myunmask(obj) {
    obj.className = "";
    if (obj.hasChildNodes()) {
        while (obj.childNodes.length >= 1) {
            obj.removeChild(obj.firstChild);
        }
    }
}
