﻿(function ($) {

    $.fn.customFadeIn = function (speed, callback) {
        $(this).fadeIn(speed, function () {
            if (!$.support.opacity)
                $(this).get(0).style.removeAttribute('filter');
            if (callback != undefined)
                callback();
        });
    };

    $.fn.MaskLoader = function (options) {
        this.defaults = {
            title: "ESSAI",
            container_id: 'container',
            background: { css: '#ffffff', opacity: 0.1 },
            border: "1px solid #ddd",
            fade_speed: 500,
            fade_speed_out:10
        }

        $.extend(this.defaults, options)
        $.extend(this, this.defaults);

        this.container_id += '_' + Math.ceil(this.offset().top) + '_' + Math.ceil(this.offset().left);

        $(this).css("opacity","0.5");

        if (!$('div#' + this.container_id).length) CreateContainer(this, $(this));
        else DeleteContainer(this, $(this));

        function CreateContainer(settings, currentElement) {

            var pos = currentElement.offset();

            //settings.container_id += '_' + pos.top;
            var container = $('<div id="' + settings.container_id + '"></div>');

            container.css({
                position: 'absolute',
                top: pos.top,
                left: pos.left,
                'z-index': 10,
                width: currentElement.width(),
                height: currentElement.height()
                //background: settings.background.css
                //opacity: settings.background.opacity
                //border: settings.border
            });

            //container.hide();

            var loadingBg = $('<div></div>');

            loadingBg.css({
                position: 'relative',
                'text-align': 'center',
                height: '98px',
                width: '130px',
                padding: '30px 0 0 0',
                margin: '' + (currentElement.height() / 2) - 48 + 'px 0 0 ' + ((currentElement.width() / 2) - 65) + 'px',
                behavior: 'url(/js/iepngfix.htc)',
                background: 'url("/medias/communs/bubble.png") top left no-repeat',
                'z-index': 20
            });

            var loadingImg = $('<div></div>');

            loadingImg.css({
                height: '32px',
                width: '100%',
                background: 'url("/medias/communs/loading.gif") top center no-repeat'
            });

            loadingBg.append(loadingImg);

            container.append(loadingBg);

            //currentElement.append(container);					
            //currentElement.parent().prepend(container);
            $("body").prepend(container);

            container.hide().fadeIn(settings.fade_speed);

        }
        function DeleteContainer(settings, currentElement) {
            currentElement.css({"opacity": 1});
            //$('#' + settings.container_id).fadeOut(settings.fade_speed_out, function () { $(this).remove(); });
            $('#' + settings.container_id).remove();
        }

    };
})(jQuery);
