
/*
 * Copyright (c) 2009 David Johansson
 * Dual licensed under the MIT and GPL licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */


/*

Example
$('#mydiv').loading(); //constructs an shows an overlay directly
$('#mydiv').loading({auto_show: false}).loading('show'); //constructs an shows an overlay with transition
...

//hide it
$('#mydiv').loading('hide');
//show it again
$('#mydiv').loading('show');




CSS example

.overlay { 
    background-color: white;
    opacity: .70;
    filter:Alpha(Opacity=70); 
}
.overlay { 
    position: absolute; 
    z-index: 10000;
}

*/


(function($){


$.fn.loading = function(arg){
    if (arg != undefined) { //do actions on an overlay, the same as trigger('overlay-<action>') but nicer api
        if (typeof(arg) == "string") {
            $(this).trigger('overlay-'+arg);
            return this;
        }
    }
    
    var options = $.extend($.fn.loading.defaults,arg);
    
    this.each(function(){
        var $this = $(this);
        var pos = $this.offset();
        var insertBefore = this;
        if ($this.is('body')) {
            insertBefore = $this.children().eq(0);
        }
        var overlay = $('<div class="'+options.overlay_class+'"></div>').insertBefore(insertBefore)
                        .width($this.width())
                        .height($this.height())
                        .css('left',pos.left).css('top',pos.top);
        if (!options.auto_show) {
            overlay.css('display','none');
        }
        
        $this.bind('overlay-hide',function(e){
            options.transition_out.apply(overlay,[options.transition_duration]);
        });
        $this.bind('overlay-show',function(e){
            var pos = $this.offset();
            overlay.width($this.width())
                   .height($this.height())
                   .css('left',pos.left).css('top',pos.top);
            options.transition_in.apply(overlay,[options.transition_duration]);
        });
    });
    return this;
};

$.fn.loading.defaults = {
    overlay_class: 'overlay',
    transition_in: $.fn.fadeIn,
    transition_out: $.fn.fadeOut,
    transition_duration: "fast",
    auto_show: true 
};



})(jQuery)

