//---------Затемнение экрана------------

var shade = 0;
var sto = false;

function scr()                 
{
    if (again || !shade) return;
    again = true;
    
    /*var w = document.getElementById('shadeInner').offsetWidth;
    if (!w) w = 0;
    var h = document.getElementById('shadeInner').offsetHeight;
    if (!h) h = 0;*/
    
    $('#shadeInner').css(
                            {
                            'left': '0px'/*document.documentElement.scrollLeft + (window.innerWidth - w)/2*/,
                            'top': document.documentElement.scrollTop/* + (window.innerHeight - h)/2*/
                            }
                         );
                         
   setTimeout('again = false;', 33);                           
}

function res()
{
    if (!shade) return;
    $('#shadeDocument').css(
                            {
                                'width': (document.width ? document.width : document.body.scrollWidth),
                                'height': (document.height ? document.height : document.body.scrollHeight)
                             }
                            );
}
               
function shadeDocument(htm, max_time)
{
    if (shade > 0)
    {
        shade++;
        res();
        return;
    }
    
    shade = 1;
    $('body').append(
                $('<div></div>')
                .attr({'id': 'shadeDocument'})
                .css({'opacity': '0.0', 'position':'absolute', 'left': '0px', 'top': '0px',
                      'background': 'black', 'z-index': '1003'})
                .fadeTo(5000, 0.3)
                )       
             .append(   
                $('<div></div>')
                .attr({'id': 'shadeInner'})
                .css({'background':'white', 'position':'absolute', 'left': '0px', 'top': '0px', 'z-index': '1004'})
                .html( (htm ? htm : '<img src="./img/loading.gif" style="padding: 4px 34px 4px 34px">') )
                .fadeIn(2000)
                );
    
    again = false;            
    scr();
    res();
        
    document.body.onscroll = scr;
    $('body').attr({'onscroll': 'scr();'});
    
    document.body.onResize = res;
    $('body').attr({'onresize': 'res();'});
    
    if (max_time === 0) return;
    if (!max_time) max_time = 20000;
    
            
    if (!sto) setTimeout('timeRemove();', max_time);
    sto = true;
}

function timeRemove()
{    
    if ((shade == null) || (shade < 1)) return;
    
    if (document.getElementById('shadeInner') == null) return;
    
    if (confirm('Действие продолжается длительный период времени. Продолжить ожидание?'))
        setTimeout('timeRemove();', 20000);
    else
    {
        sto = false;
        shade = 0;
        $('#shadeInner').remove();
        $('#shadeDocument').remove();
    }
}

function removeShade()
{
    if (shade > 0) shade --;
    
    if (shade) return;
    $('#shadeInner').remove();
    $('#shadeDocument').remove();
    shade = 0;
}
