/*
 * Interface Blocker - jQuery plugin to block interfaces while loading via ajax
 *
 * @author Igor Finchuk
 */

(function($) {

    var img_dir = '/images/portlets/'

    var isBlocked = function ( el )
    {
        return ( typeof el.mask != 'undefined' && el.mask != false );
    };

    var isLoaded = function ( el )
    {
        return ( typeof el.loader != 'undefined' && el.loader != false );
    };

    $.extend($.fn, {
        loadUI : function ( callback ) {
            return this.each(function() {
                if( ! isLoaded( this ) ){
                	this.loader = $('<div />')
                	    .text('Loading...')
                		.addClass('ajax-loader')
                		.css({display: 'none', position: 'absolute', zIndex: 9500 })
                		.prependTo($(this));

                	this.loader.fadeIn('fast', function(){
                		if(typeof( callback ) == 'function') {
                			callback();
                		}
                	});
                }
            });
        },

        unloadUI : function ( container ) {
        	return this.each(function(){
        	    if( isLoaded( this ) ){
        	   	    this.loader.fadeOut('fast', function(){ $(this).remove(); });
        	   	    this.loader = false;
        	    }
        	});
        },

        blockUI : function (){
            return this.each(function(){
                if( ! isBlocked( this ) ){
                    this.mask = $('<div class="x-mask" style="display: block" />');
                    if( ! $.browser.msie ){
                    	//this.mask.css('position', 'fixed');
                    }

                    if( $(this) != $('body') ){
                        this.mask
                            .width( $(this).width() + parseInt($(this).css('paddingLeft')) + parseInt($(this).css('paddingRight')) )
                            .height( $(this).height() + parseInt($(this).css('paddingTop')) + parseInt($(this).css('paddingBottom')) )
                            .css({
                                marginLeft: - parseInt( $(this).css('paddingLeft') ),
                                marginTop: - parseInt( $(this).css('paddingTop') )
                            })
                    }

                    $(this).addClass('x-masked');

                    this.mask.prependTo( $(this) );
                }
            });
        },

        unblockUI : function () {
            return this.each(function(){
                if( isBlocked( this ) ){
                    $(this).removeClass('x-masked');
                    this.mask.remove();
                    this.mask = false;
                }
            });
        }

    });

})(jQuery);
