var nsPrgsBar = {
        disableDiv : 0,
        prgs : "",
        options : {},
        iTimer : '',
        prgsTimerElm : '',
        disable : true,
        bShowedClose : 0,

        initialize: function(options) 
        {               
                if(this.prgs)
                        return;
                
                this.options = options?options:{};

                if(!this.options.deltaTime)
                        this.options.deltaTime = 100;//время обновления таймера(в мс)

                if(!this.options.timeToClose)
                        this.options.timeToClose = 15;// время через котрое появляется возможность закрыть прогресс бар (в сек)

                // стили используещиеся в баре
                if(!this.options.Style)
                        this.options.Style = {}; 

                //картинка
                if(!this.options.imgSrc)
                        this.options.imgSrc = '';



                if(!this.options.Style.prgs)
                        this.options.Style.prgs = {
                                'position' : 'absolute',
                                'display' : 'block',
                                //'top':  '-10px',
                                //'left': '-10px',
                                'background': '#FFFFFF',
                                'border' : '1px solid #D7D8DC',
                                'width' : '300px',
                                'height' : '150px',
                                'font' : 'bold 11px verdana',
                                'z-index' : '1000'
                        };

                //html прогресс бара
                if(!this.options.prgsHtml)
                        this.options.prgsHtml = '<br><br><br><div style="text-align:center"><img src="'+this.options.imgSrc+'"><br><br><span id="prgsMsg"></span> <span id="prgsTimer"></span><br/><br/><div id="prgsClose" align="center"><u>закрыть</u></div></div>';


                //html прогресс бара
                //if(!this.options.prgsHtml)
                //      this.options.prgsHtml = '<span id="prgsMsg"></span> <span id="prgsTimer"></span><br/><br/><div id="prgsClose" align="center"><u>закрыть</u></div>';


                this.disableDiv = this.create();

                this.prgs = $('prgsWin');
                this.prgsTimerElm = $('prgsTimer');
                this.prgsMsg = $('prgsMsg');
                this.prgsClose = $('prgsClose');
                this.prgsIfr = $('prgsIfr');


                this.prgsClose.addEvent('click', function(event)
                {
                        this.hide();
                }.bindWithEvent(this));         

        },

        enable : function(options)
        {
                if(options)
                        this.initialize(options);

                this.disable = false;
        },
        disable : function()
        {
                this.disable = true;
        },

        timer : function(time) {
                var dt = new Date();
                if (!time) {
                        this.iTimer = $clear(this.iTimer); // Снимаем таймаут
                        this.prgsTimerElm.innerHTML = '';
                        time = dt.getTime();
                }
                var numberCount = 1;
                var dtLen = this.options.deltaTime.toString().length;
                if(dtLen < 4)           
                        numberCount = 4 - dtLen;                



                var dtime = (dt.getTime() - time)/1000;
                this.prgsTimerElm.innerHTML = dtime.toDecimals(numberCount);
                if((dtime > this.options.timeToClose) && !this.bShowedClose)
                {
                        this.bShowedClose = 1;
                        this.showClose();
                }
                if (!this.iTimer)
                {
                        this.iTimer = this.timer.periodical(this.options.deltaTime, this, time);
                        this.bShowedClose = 0;
                }
        },

        show : function(msg) 
        {
                if (this.disable) 
                        return;

                if (!msg) 
                        msg = 'wait...';

                this.disableUserActions();
                this.hideClose();
                this.prgsMsg.setHTML(msg);
                //this.prgs.setStyle('display','block');

                //центруем
                var prgsPrams = this.prgs.getCoordinates();
                var top = (window.getScrollTop() + (window.getHeight()-prgsPrams.height)/2);
                var left = (window.getScrollLeft() + (window.getWidth()-prgsPrams.width)/2);


                this.prgs.setStyle('top', top+'px');
                this.prgs.setStyle('left', left+'px');

                this.prgsIfr.setStyle('top', top+'px');
                this.prgsIfr.setStyle('left', left+'px');

                this.timer();
        },
        
        hide : function() 
        {
                if (this.disable) 
                        return;

                this.iTimer = $clear(this.iTimer); // Снимаем таймаут
                //this.prgs.setStyle('display', 'none');
                this.enableUserActions();
                //this.hideClose();
//              this.progress.removeEvents('blur');
        },

        hideClose : function ()
        {
                this.prgsClose.setStyle('display', 'none');
        },
        showClose : function ()
        {
                this.prgsClose.setStyle('display', '');
        },

        disableUserActions : function() 
        {
                var bodyNode = $$("body")[0];
                var winSize = bodyNode.getSize();

                //this.disableDiv.setStyle('width',  (winSize.size.x - 5) + 'px');
                //alert("client_h=" + document.body.clientHeight + " offsetH=" + document.body.offsetHeight + " offsetTop=" + document.body.offsetTop + " scrollHeight=" + document.body.scrollHeight);
                this.disableDiv.setStyle('height', (window.getScrollHeight() - 5) + 'px');//winSize.size.y
                this.disableDiv.setStyle('width',  '100%');
                //this.disableDiv.setStyle('height', '100%');


                this.disableDiv.setStyle('display', '');
        
        },
        enableUserActions : function() 
        {
                this.disableDiv.setStyle('display', 'none');        
        },

        create : function() 
        {
                
                var disableDivNode = new Element("div");        

//'background' : 'url("/i/spacer.gif") no-repeat', Убрано Магометом 14-08-2008
                disableDivNode.setStyles({
                        'position' : 'absolute',
                        'top':  '0px',
                        'left': '0px',
                        'display' : 'none'
		        //'width' : '100%'
                        //'height' : '100%',
                        //'border' : '1px solid red'
                });
               
                disableDivNode.addEvent('click', function(){return false;});
                $$("body")[0].adopt(disableDivNode);
                
                //<iframe width="200" frameborder="0" src="" height="300" class="gg"></iframe>

                var ifrNode = new Element("iframe");
                ifrNode.setStyles({
                        'frameborder': '0',
                        'height': this.options.Style.prgs.height,
                        'width' : this.options.Style.prgs.width,
                        'position' : 'absolute',
                        'z-index' : '50'
                        });
                ifrNode.setProperty('id', 'prgsIfr');
                disableDivNode.adopt(ifrNode); // div.injectBefore($$("body")[0].getFirst());
        

                var winDivNode = new Element("div");
                winDivNode.setProperty('id', 'prgsWin');

                winDivNode.setStyles(this.options.Style.prgs);
                winDivNode.setHTML(this.options.prgsHtml);
                disableDivNode.adopt(winDivNode); // div.injectBefore($$("body")[0].getFirst());
                return disableDivNode;
        }
};
