function SelfPreloader( TextHtml , ElementId , options )
{

    var settings = $.extend({
        scale: '33',
        html: false,
        url: false,
        NewContent: true,
        PreloadingIco: '<i class="fa fa-circle-o-notch fa-spin"></i>',
        WindowClass: 'JsWindow',
        WindowPreloaderClass: 'JsWindowNoBg',
        WindowHandlerClass: 'JsWindowHandler',
        CloseBtnClass: 'JsWindowCloseBtn'
    }, options );
            
    var JsWindowFrame = $("<div></div>");
    $( JsWindowFrame ).addClass( 'JsWindowFrame' );
    $(JsWindowFrame).attr('id',ElementId.replace('#',''));
    
    var JsWindow = $("<div></div>");
    $( JsWindowFrame ).html( JsWindow );
    
    
    $(JsWindow).addClass( settings.WindowClass );
    
    var JsHandler = $("<div></div>");
    $(JsHandler).addClass( settings.WindowHandlerClass );
    $(JsWindow).html( JsHandler );
        
    $(JsWindow).addClass( settings.WindowPreloaderClass );
    $(JsHandler).html('<div class="JsWindowPreloader"><div class="JsWindowPreloaderIco">' + settings.PreloadingIco + '</div>' + TextHtml + '</div>');
        
        
    $('body').append( JsWindowFrame );

    if( settings.scale.toString() != 'auto' ){
    
        var LeftDistance = ( 100 - settings.scale ) / 2;
        $(JsWindow).css({
            width: settings.scale + '%',
            left: LeftDistance + '%' 
        });
    
    } else {
        
        var WindowWidth = $(window).outerWidth(true);
        var HandlerWidth = $(JsWindow).outerWidth(true);
        
        var LeftDistance = ( WindowWidth - HandlerWidth ) / 2;
        $(JsWindow).css({
            left: LeftDistance + 'px' 
        });
        
    }
            
    $( ElementId ).fadeIn();
}

function SelfPreloaderClose( PreloaderId )
{
   $( PreloaderId ).fadeOut(function(){
        $( PreloaderId ).remove();
   });
}

function JsWindowPreloader( ShowHide , PreloaderIco , JsWindow , JsHandler , settings ){
    
    // Show preloader
    if( ShowHide !== false ){
        
        $(JsWindow).addClass( settings.WindowPreloaderClass );
        $(JsHandler).html('<div class="JsWindowPreloader"><div class="JsWindowPreloaderIco">' + PreloaderIco + '</div>' + ShowHide + '</div>');
        
    // Hide preloader
    } else {
        
        $(JsWindow).removeClass( settings.WindowPreloaderClass );
        $(JsHandler).html('');
        
    }
    
    
}
function JsWindowExecute( JsWindowUrl , JsWindowFrame , JsWindow , JsHandler , NewContent , settings ){
    
    if( JsWindowUrl ){
        
        if( NewContent ) JsWindowPreloader( 'Wczytywanie' , settings.ico_preloader , JsWindow , JsHandler , settings );
        else {
            // Button preload animation
            $('form .FormRowSubmit',JsHandler).attr( 'data-tmp-value' , $('form .FormRowSubmit',JsHandler).html() );
            $('form .FormRowSubmit',JsHandler).html( settings.ico_preloader );
        }
        
        $.getJSON( JsWindowUrl ,function( response ){

            // Preloading
            if( NewContent ) JsWindowPreloader( false , false , JsWindow , JsHandler , settings );
            else {
                // Reset button
                $('form .FormRowSubmit',JsHandler).html( $('form .FormRowSubmit',JsHandler).attr('data-tmp-value') );
            }
            
            // Response status
            if( response.status === true ){
                
                // Refresh window
                if( response.refresh === true ){
                    
                    JsWindowPreloader( '' , settings.ico_refresh , JsWindow , JsHandler , settings );
                    window.location.reload();
                    return;
                    
                }
                
                // Button lock
                if( response.BtnLock ){
                    if( !$('.FormRowSubmit',JsHandler).hasClass('FormRowSubmitLock') ){
                        $('.FormRowSubmit',JsHandler).addClass('FormRowSubmitLock');
                    }
                } else {
                     if( $('.FormRowSubmit',JsHandler).hasClass('FormRowSubmitLock') ){
                        $('.FormRowSubmit',JsHandler).removeClass('FormRowSubmitLock')
                     }
                }

                // Set HTML content
                if( response.html ){
                    
                    console.log('Response HTML');
                    
                    $( JsHandler ).html( response.html );
                    
                    // Form Controler
                    if( response.form === true ){
                        
                        var NewForm = $( 'form .FormRowSubmit' , JsHandler );

                        
                        $( NewForm ).click(function(){
                            
                            if( !$('.FormRowSubmit',JsHandler).hasClass('FormRowSubmitLock') ){
                                
                                var ClosestForm = $(this).closest('form');
                                JsWindowExecute( $( ClosestForm ).attr('action') + '?' + $( ClosestForm ).serialize() , JsWindowFrame , JsWindow , JsHandler , false , settings );
                            
                            }
                        });

                        // Enter
                        $(NewForm).closest('form').keypress(function(e){
                            if(e.which == 13){
                                $( NewForm ).click();
                            }
                        });
                            
                    } else {
                        console.log('Initialize ERROR');
                    }
                    
                } else {
                    
                    console.log('Response MSG');
                    
                    if( response.msg ){
                        
                        // Remove old msg
                        if( $('.JsWindowMsgHandler',JsHandler).length ){
                            $('.JsWindowMsgHandler',JsHandler).remove();
                        }

                        // Set MSG
                        if( $('.FormRowHeader',JsHandler).length ){
                            
                            $('.FormRowHeader',JsHandler).after( '<div class="JsWindowMsgHandler">' + response.msg + '</div>' );
                        
                        } else {
                            
                            $('form',JsHandler).prepend( '<div class="JsWindowMsgHandler">' + response.msg + '</div>' );
                        
                        }
                    }
                    
                }
              
            // Other error  
            } else {
                
                if( response.html ) $( JsHandler ).html( reponse.html );
                else $( JsHandler ).html( response.msg );
                
            } 
        });
        
    } else {
        
        $( JsHandler ).html( settings.html );
        
    }

                

}

$(function( $ ) {
    
    $.fn.JWindow = function( options ) {
        
        var settings = $.extend({
            scale: '33',
            html: false,
            url: false,
            NewContent: true,
            ico_preloader: '<i class="fa fa-circle-o-notch fa-spin"></i>',
            ico_refresh: '<i class="fa fa-refresh fa-spin"></i>',
            closeBtn: true,
            id: getRandID( 'window' ),
            WindowClass: 'JsWindow',
            WindowPreloaderClass: 'JsWindowNoBg',
            WindowHandlerClass: 'JsWindowHandler',
            CloseBtnClass: 'JsWindowCloseBtn'
        }, options );
        
        $(this).click(function(){
    
            var JsWindowFrame = $("<div></div>");
            $( JsWindowFrame ).addClass( 'JsWindowFrame' );
            
            var JsWindow = $("<div></div>");
            $( JsWindowFrame ).html( JsWindow );
            
            $(JsWindow).attr('id',settings.id);
            $(JsWindow).addClass( settings.WindowClass );
            
            var JsHandler = $("<div></div>");
            $(JsHandler).addClass( settings.WindowHandlerClass );
            $(JsWindow).html( JsHandler );
            
            // Execute JsWindowExecute
            JsWindowExecute( settings.url , JsWindowFrame , JsWindow , JsHandler , settings.NewContent , settings );

            //
            $('body').append( JsWindowFrame );
            
            // Scale calculation
            
            if( settings.scale.toString() != 'auto' ){
            
                var LeftDistance = ( 100 - settings.scale ) / 2;
                $(JsWindow).css({
                    width: settings.scale + '%',
                    left: LeftDistance + '%' 
                });
            
            } else {
                
                var WindowWidth = $(window).outerWidth(true);
                var HandlerWidth = $(JsWindow).outerWidth(true);
                
                var LeftDistance = ( WindowWidth - HandlerWidth ) / 2;
                $(JsWindow).css({
                    left: LeftDistance + 'px' 
                });
                
            }

            
            // Close Button
            if( settings.closeBtn === true ){
                
                var CloseBtn = $('<a></a>');
                $(CloseBtn).attr({
                    'href': 'javascript:;',
                    'class': settings.CloseBtnClass 
                });
                $(CloseBtn).html( '<i class="fa fa-close"></i>' );
                
                $(JsWindow).prepend( CloseBtn );
                
                $(CloseBtn,Window).click(function(){
                    $(JsWindowFrame).fadeOut(function(){
                       $(JsWindowFrame).remove(); 
                    });
                })
            }
                        
            $(JsWindowFrame).fadeIn();
            
        });

        
    }
    
});

$(function(){
    $('.sign-in').JWindow({
        url: 'ajax.php?mode=login&function=form',
        scale: '30',
        NewContent: true
    });

});