(function(){    
 
    jQuery.slLoading = function(options){
        
        options = jQuery.extend({    
            boxSize:115,
            animate: 'check',
            position: 'right',
            time_sleep: 60,
            time: 100,
            color: 'black'  
        },options);
        
        var all_box = 0;
        var t       = 0;
        
        
        w_width  = jQuery(window).width();
        w_height = jQuery(window).height();
            
        count_w_box = Math.ceil(w_width/options.boxSize);
        count_h_box = Math.ceil(w_height/options.boxSize);
        
        all_box = count_w_box * count_h_box;
        
        options.boxSize = (options.boxSize < 100) ? 100: options.boxSize;
        options.boxSize = (options.boxSize > 200) ? 200: options.boxSize;
        
        set_color = (options.color == 'white') ? ' class="slLoading_white"' : '';
        
        create = function(){
            var er = [
                '<div id="slLoading"',set_color,'>',
                    '<div id="slLoading_box"></div>',
                    '<div id="slLoading_info" onclick="hide_slLoading()">',
                        '<div class="slLoading_relative">',
              
                            '<div class="slLoading_info_left"><div id="slPage"></div></div>',
              
                            '<div class="slLoading_info_right">',
                                '<div id="slLogo"></div>',
                                '<div class="slshadow"></div>',
                            '</div>',
              
                        '</div>',
                    '</div>',
                '</div>'
	        ].join('');
        
            jQuery(er).appendTo('body');
            
            var count_box = 0;
            
            var col = 1;
            
            for(i=0;i<count_w_box;i++){
                for(r=0;r<count_h_box;r++){
                    
                    if(options.animate == 'line'){
                        jQuery('#slLoading_box').append('<b class="slLw_'+(count_box++)+'" style="right:'+(i*options.boxSize)+'px;top:'+(r*options.boxSize)+'px;width:'+options.boxSize+'px;height:'+options.boxSize+'px"></b>');   
                    }
                    else if(options.animate == 'check'){
                        col++;
                        col = (col > 2) ? 1: col;
                        
                        jQuery('#slLoading_box').append('<b class="slLw_'+i+'_'+col+'" style="right:'+(i*options.boxSize)+'px;top:'+(r*options.boxSize)+'px;width:'+options.boxSize+'px;height:'+options.boxSize+'px"></b>');
                        
                    }
                    
                }
            }
        }
        
        time = function(id,time){
            setTimeout(function(){
                jQuery(id).fadeOut(options.time);
            },time);
        }
        
        close_load = function(){
            jQuery('#slLoading').remove();
        }
        
        destroy = function(){
            jQuery('#slLoading_box').show();
            
            jQuery('#slLoading').css({background:'transparent'});
            
            if(options.animate == 'line'){
                
                if(options.position == 'right'){
                    for(i=0;i<all_box;i++){
                        t += options.time_sleep;
                        time(jQuery('.slLw_'+i),t);
                    }
                }
                else{
                    for(i=all_box;i>=0;i--){
                        t += options.time_sleep;
                        time(jQuery('.slLw_'+i),t);
                    }
                }
                
                setTimeout(function(){
                    close_load();
                },all_box * options.time_sleep + options.time);

            }
            else if(options.animate == 'check'){
                
                if(options.position == 'right'){
                    for(i=0;i<count_w_box;i++){
                        t += options.time_sleep;
                        time(jQuery('.slLw_'+i+'_1'),t);
                        time(jQuery('.slLw_'+i+'_2'),t+options.time_sleep);
                    }
                }
                else{
                    for(i=count_w_box;i>=0;i--){     
                        t += options.time_sleep;
                        time(jQuery('.slLw_'+i+'_1'),t);
                        time(jQuery('.slLw_'+i+'_2'),t+options.time_sleep);
                   } 
                }
                
                setTimeout(function(){
                    close_load();
                },count_w_box * options.time_sleep + options.time);
                
               
			   
            }
            else{
                jQuery('#slLoading').fadeOut(300,function(){
                    jQuery(this).remove();
                });
            }
            

        }
        
        hide_info = function(){
            
            jQuery('#slLoading_info').fadeOut(300,function(){
                destroy();
            });
        }
        
        create();
        
        jQuery('#slLogo').animate({left:'0px',opacity:1},300);
        jQuery('#slPage').animate({right:'0px',opacity:1},300);
        
        var stop = false;
        
        hide_slLoading = function(){
            
            stop = true;
            
            jQuery('#slLoading').css({backgroundImage:'none'});
            
            setTimeout(function(){
                jQuery('#slLogo').animate({left:'-180px',opacity:0},300);
                jQuery('#slPage').animate({right:'-299px',opacity:0},300,hide_info());
            },700);
        }
        
        jQuery(window).load(function() {
            if(!stop) hide_slLoading();
            
        });

 }
})(jQuery);
