/**
 * JQuery busy plugin
 *
 * Author: Artem Plyasunov, Zoral
 *
 * version 0.33
 *
 *      dobusy([message] [, options])       - function for make selected elements busy
 *      unbusy()                            - function for unbusy selected elements
 *      $.unbusy()                          - function for unbusy all busy elements
 *      toggleBusy([message] [, options])   - function fore on/off busy on selected elements
 *
 * @message - text message (Default: Loading...)
 *
 * @options             Defaults            Description
 *      opacity         0.25
 *      bgcolor         #ffffff
 *      maxfontsize     24                  maximum size of font for text message*
 *      minfontsize     12                  minimum size of font for text message*
 *      image           embedded image      It shows before text message
 *
 * -----------------------------------------------------------------------------------------------
 * (*) - font and image resizing to container size. If container very large or very small we should
 *       limit this values within reason
 */
(function($){
    $.extend({
        dobusy: new function() {
            this.options = {
                opacity: 0.75,
                bgcolor: "#000",
                maxfontsize: 24,
                minfontsize: 12,
                height: null,
                width: null,
                color: "#ffffff",
                zindex: null,
                imageURL: null
            }

            this.run = function(message, options) {
                return this.each(function() {
                    var internalMessage, $busyMask, $busyMessage, fontSize, $busy = $(this), opt;
                    opt = $.extend($.dobusy.options, options);
                    $busy.addClass("jquery-busy");
                    internalMessage = !!message ? message : "Loading...";
                    $busyMask = $busy.busy("mask");
                    if ($busyMask.length > 0) {
                        $busyMask.find(".jquery-busy-message-text").html(internalMessage);
                    } else {
                        // busyImgPath - global variable defined at alpMacro.includeJQueryBusyPlugin
                        $busyMask = $('<div class="jquery-busy-mask">' +
                                      '<div class="jquery-busy-message" style="top:50%;margin:auto;position:relative;text-align:center;">' +
                                      (opt.imageURL != null ? '<img src="' + opt.imageURL + '" style="margin-right:5px;"/>' : "") +
                                      '<span class="jquery-busy-message-text">' +
                                      internalMessage +
                                      '</span></div></div>');
                        fontSize = Math.max(opt.minfontsize, Math.min(opt.maxfontsize, Math.min($busy.height(), $busy.width()) / 10));
                        $busyMask.css({
                            position: "absolute",
                            zIndex: zindex($busy, opt.zindex),
                            width: opt.width || $busy.width(),
                            height: opt.height || $busy.height(),
                            backgroundColor: opt.bgcolor,
                            opacity: opt.opacity,
                            filter: "progid:DXImageTransform.Microsoft.Alpha(opacity=75)",
                            overflow: "hidden",
                            cursor: "progress",
                            fontSize: (fontSize) + "px"
                        });

                        $busy.busy("mask", $busyMask);
                        $busyMessage = $busyMask.find(".jquery-busy-message");
                        $busyMessage.css({
                            top: ((($busy.height() - $busyMessage.height()) / 2) * 100 / $busy.height()) + "%",
			    			color: opt.color || "#000"
                        });
                        $busyMask.find("img").css("width", Math.floor(fontSize / 1.5) + "px");
                    }

                    $(window).resize(function() {
                        $busyMask.css({
                            width: opt.width || $busy.width(),
                            height: opt.height || $busy.height()
                        });
                    });

                });
            }

            function zindex($busy, zindex) {//get parent z-index
                if (!!zindex) {
                    return zindex;
                }
                var zIndex = 0, currZIndex, parsedZIndex;
                currZIndex = $busy.css("z-index");
                parsedZIndex = parseInt(currZIndex);
                zIndex = isNaN(parsedZIndex) ? zIndex : Math.max(zIndex, parsedZIndex);
                return zIndex + 1;
            }
        },

        unbusychain: new function() {
            this.run = function() {
                return this.each(function(){
                    var $busy = $(this);
                    $busy.removeClass("jquery-busy");
                    $busy.busy("mask").remove();
                });
            }
        },

        unbusy: function() {
            $(".jquery-busy").unbusy();
        },

        toggleBusy: new function() {
            this.run = function(message, options) {
                if (this.length > 0) {
                    return this.each(function() {
                        var $this = $(this);
                        if ($this.hasClass("jquery-busy")) {
                            $this.unbusy();
                        } else {
                            $this.dobusy(message, options);
                        }
                    });
                } else {
                    return this;
                }
            }
        },

        busy: new function() {
            this.run = function(param, value) {
                if (!!param) {//return param value
                    switch (param.toLowerCase()) {
                        case "mask" : //internal mask layer
                                if (!!value) {
                                    if (this.get(0).tagName.toUpperCase() == "BODY") {
                                        //for bodie insert mask into parent container
                                        this.prepend(value);
                                    } else {
                                        //insert mask before parent container
                                        this.before(value);
                                    }
                                } else {
                                    return this.get(0).tagName.toUpperCase() == "BODY" ?
                                           this.find(".jquery-busy-mask:first") :
                                           this.prev(".jquery-busy-mask");
                                }
                            break;
                    }
                } else {//return busy state
                    if (this.length == 1) {//return for one element only
                        return this.hasClass("jquery-busy");
                    } else {//return array of value paars
                        if (this.length == 0) {
                            return null;
                        }
                        var result = new Array(this.length);
                        this.each(function() {
                            result.push({node: this, busy: $(this).hasClass("jquery-busy")});
                        })
                        return result;
                    }
                }
            }
        }
    });

    $.fn.extend({
        dobusy: $.dobusy.run,
        unbusy: $.unbusychain.run,
        toggleBusy: $.toggleBusy.run,
        busy: $.busy.run
    });

})(jQuery);
